Luca Glaentzer
Luca Glaentzer

Reputation: 79

Missing positional arguments in pygame

My player should move after I put in the movements and pressed 0. In case he hits the wall, he moves out based on velocity. But after I put in every move and I press 0, it tells me that the move function is missing 1 required positional argument "dy":

I put in the moves via the arrow keys into the list "moveList". If I press 0 it should move. You can see the main loop here:

def main_loop_state_config():
    global level, end_rect, place_y, curr_moves, max_moves
    screen.fill(WOODY)
    drawRect()
    drawGrid()
    drawLevel(level)
    players = drawLevel(level)
    walls = drawLevel(level)
    ends = drawLevel(level)
    for event in pygame.event.get():
            if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
                running = False
                pygame.quit()
                print("Spiel wird beendet!")
            elif event.type == KEYDOWN:
                if event.key == K_UP:               
                        move_list.append(1)
                elif event.key == K_DOWN:
                        move_list.append(2)
                elif event.key == K_RIGHT:
                        move_list.append(3)
                elif event.key == K_LEFT:
                        move_list.append(4)
                elif event.key == K_0:
                        player.movePlayer()

That is my Player class with all the neccessary functions:

class Player(object):
    def __init__(self, pos):
        
        self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))

    def move(self, dx, dy):
        
        # Den Spieler bewegen
        self.rect.x += dx
        self.rect.y += dy

        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0:  
                    self.rect.right = wall.rect.left
                if dx < 0:  
                    self.rect.left = wall.rect.right
                if dy > 0:  
                    self.rect.bottom = wall.rect.top
                if dy < 0:  
                    self.rect.top = wall.rect.bottom

    def movePlayer(self):
        for i in range(len(move_list)):
            if move_list[i] == 1:
               self.move((pxl(0), pxl(-1)))
            elif move_list[i] == 2:
                self.move((pxl(0), pxl(1)))
            elif move_list[i] == 3:
                self.move((pxl(1), pxl(0)))
            elif move_list[i] == 4:
                self.move((pxl(-1), pxl(0)))

In order to get the players list, I wrote this into drawLevel():

def drawLevel(level):
    x = y = 0

    walls = []

    ends = []

    players = []

    for row in levels[level]:
        for col in row:
            if col == "W":
                wall = Wall((x, y))
                walls.append(wall)
            if col == "E":
                end = End((x, y))
                ends.append(end)
            if col == "P":
                player = Player((x,y))
                players.append(player)
            x += 80
        y += 80
        x = 0
    for wall in walls:
        pygame.draw.rect(screen, BLACK, wall.rect)
    for end in ends:
        pygame.draw.rect(screen, RED, end.rect)
    for player in players:
        pygame.draw.rect(screen, BLUE, player.rect)
    
    return players
    return walls
    return ends

Now it tells me, as said above, that move is missing the positional argument "dy" and that player in the main_loop_state_config() is not defined. What can I do to change that?

Upvotes: 1

Views: 301

Answers (1)

hauslex
hauslex

Reputation: 36

Found the bug. In these calls:

   self.move((pxl(0), pxl(-1)))
elif move_list[i] == 2:
    self.move((pxl(0), pxl(1)))
elif move_list[i] == 3:
    self.move((pxl(1), pxl(0)))
elif move_list[i] == 4:
    self.move((pxl(-1), pxl(0)))

You have one set too many parentheses on each one of the self.move lines.

You are calling self.move((a, b)), which is calling self.move with a tuple containing a and b as the dx argument, and no dy argument. To do it correctly, you need to call self.move(a, b) -- that is, self.move(pxl(0), pxl(-1)), etc.

In the future, you can diagnose this by putting a print(dx) or a pdb.set_trace() in the Player.move function and seeing what's inside of dx.

Upvotes: 1

Related Questions