Python Forum
Creating a “Player” class, and then importing it into game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a “Player” class, and then importing it into game
#1
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
Reply
#2
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?
Reply
#3
How to do it right? I haven't done such things yet.
Reply
#4
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?
Reply
#5
ok i found a bug. self.image = "gfx\120.jpg" > self.image = pygame.image.load("gfx\\120.jpg")
it's working now
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Bringing My Game Code Into Class Format ZQ12 1 2,197 Dec-22-2019, 05:32 AM
Last Post: michael1789
  [PyGame] Creating "saves" for the game subject71 2 2,176 Oct-29-2019, 11:31 AM
Last Post: Zman350x
  Adding a single player mode to my wxOthello game keames 29 11,973 May-01-2019, 02:56 AM
Last Post: SheeppOSU
  Creating Snake game in Turtle Shadower 1 8,643 Feb-11-2019, 07:00 PM
Last Post: woooee
  Can a player play game created with Python without installing Python? hsunteik 3 5,289 Feb-23-2017, 10:44 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020