Python Forum

Full Version: Creating a “Player” class, and then importing it into game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I moved the "player" functions to the class.I have a problem with drawing.I get an error "TypeError: draw() missing 1 required positional argument: 'screen'"

game.py
class GAME:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((HEIGHT,WIDTH))
        pg.display.set_caption(TILE)
        self.clock = pg.time.Clock()
        self.load_data()
        self.player = Player()
...
...
def draw(self):
...
self.player.draw()
...
player.py
class Player(object):
    def __init__(self):
        self.x = 10
        self.y = 10
        self.image = "gfx\120.jpg"
        self.K =30
    def draw(self,screen):
        screen.blit(self.image,(self.x*self.K,self.y*self.K))
    def move_left(self):
        self.x =self.x -1
    def move_right(self):
        self.x = self.x +1
    def move_up(self):
        self.y =self.y -1
    def move_down(self):
        self.y = self.y+1
Of course you do. On line 13 of game.p, you're calling the method with no arguments, but on line 7 of player.py, you've declared draw to take an argument called screen.

What's unclear about that?
How to do it right? I haven't done such things yet.
Pass the right number of arguments to the function. How have you gotten this far without understanding functions and how to call them with arguments?
ok i found a bug. self.image = "gfx\120.jpg" > self.image = pygame.image.load("gfx\\120.jpg")
it's working now