gertrud.leijonhufvud
gertrud.leijonhufvud

Reputation: 19

"Class object has no attribute"

:) Hello!

I'm a student and somewhat new to coding and I've searched quite some time for an answer to my problem but I haven't been able to solve it. It should be super easy but for some reason my code doesn't work. We're supposed to code a "cannon game" using a library from our course literature. I need to define a getter for my graphicgame class's window so that i can retrive it when i need to draw my graphics player but the getWindow(self) function doesn't work. The most common problem when you get this kind of error message seems to be mixing tabs and spaces but my editor is supposed to replace all tabs with spaces automatically. I hope that there's an easy fix.

NOTE: the code below isn't indented in that way in my editor, something happened when I pasted it into stack overflow. All defintions of attributes are indented in my original code.

Relevant code:

class GraphicGame: 
def __init__(self, game):
    self.model = game
    self.model.players[0] = GraphicPlayer(self, self.model.players[0])
    self.model.players[1] = GraphicPlayer(self, self.model.players[1])

    self.model.currentplayer = self.model.players[0]

    self.window = GraphWin("Cannon game" , 640, 480, autoflush=False)
    self.window.setCoords(-110, -10, 110, 155)
    ground = Line(Point(-110,0), Point(110,0))
    ground.draw(self.window)

def getWindow(self):
    return self.window

class GraphicPlayer:
def __init__(self, ggame, player):
    self.player = player
    self.ggame = ggame

    self.width = self.ggame.getCannonSize()
    self.color = self.player.getColor()
    self.pos = self.player.getX()
    self.window = ggame.getWindow()

    self.gplayer = Rectangle(Point(self.pos-(self.width/2), 0), Point(self.pos + (self.width/2), 0))
    self.gplayer.draw(self.window)

The error occurs when i try to construct a graphic player. Error message:

  File "c:\Users\User\Python Programmering\Programmeringteknik\Grafik\lab2\gamegraphics.py", line 79, in __init__
self.window = ggame.getWindow()
  File "c:\Users\User\Python Programmering\Programmeringteknik\Grafik\lab2\gamegraphics.py", line 66, in getWindow
    return self.window
AttributeError: 'GraphicGame' object has no attribute 'window

'

Upvotes: 0

Views: 593

Answers (1)

gertrud.leijonhufvud
gertrud.leijonhufvud

Reputation: 19

Solved: Graphicgame calls on graphicplayer before a the window of graphic game is created. Hence the error.

Upvotes: 1

Related Questions