简体   繁体   中英

Why won't my player character move?

I want my character to move up/left/down/right when I press w/a/s/d. What I think should be happening is that, for example, when I press d:

  1. main() will "see" that there is an event happening

  2. see that I pressed a key down

  3. go to player1.move()

  4. see that the key was d

  5. go to Character.move_right(self)

  6. update x_pos and x_vel

  7. go back to main()

  8. go to player1.display()

  9. draw a rectangle wherever the updated x_pos and x_vel are

  10. update the screen

  11. "tick the clock"

This obviously isn't happening, though. Where am I misinterpreting what's going on?

import sys
import pygame


class Character(object):
    def __init__(self, surface, x_vel, y_vel, x_pos, y_pos):
        self.surface = surface
        self.x_vel = x_vel
        self.y_vel = y_vel
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.size = (5, 20)

    def move_right(self):
        self.x_vel += 5
        self.x_pos += self.x_vel

    def move_left(self):
        self.x_vel -= 5
        self.x_pos += self.x_vel

    def move_up(self):
        self.y_vel -= 5
        self.y_pos += self.y_vel

    def move_down(self):
        self.y_vel += 5
        self.y_pos += self.y_vel

    def move(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    Character.move_up(self)
                elif event.key == pygame.K_a:
                    Character.move_left(self)
                elif event.key == pygame.K_s:
                    Character.move_down(self)
                elif event.key == pygame.K_d:
                    Character.move_right(self)
        self.character = pygame.Rect((self.x_pos, self.y_pos), self.size)

    def display(self):
        pygame.draw.rect(self.surface, (255, 255, 255), self.character)

pygame.init()

screen = pygame.display.set_mode((640, 480))
screen_rect = screen.get_rect()

clock = pygame.time.Clock()

fps = 30


def main():
    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                else:
                    player1 = Character(screen, 0, 0, 200, 200)
                    player1.move()

        player1.display()

        pygame.display.update(screen_rect)
        clock.tick(fps)

if __name__ == "__main__":
    main()

Summarizing the possible errors:

As @tobias_k pointed out your events are already consumed. This can be fixed like this

def move(self, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_w:
            ....

def main():
    player1 = Character(screen, 0, 0, 200, 200)
    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                else:
                    player1.move(event)

Second, move the creation of the Character out of the main loop

def main():
    player1 = Character(screen, 0, 0, 200, 200)
    while True:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM