我写了这个简单的代码来绘制然后让你移动一个矩形,但即使我通过调用 move_player_x_ 函数更改了矩形的坐标,它也根本不动。我不明白为什么。我来这里是为了寻求澄清和对我的问题的详细解决方案。

这是代码:

import pygame

white = (255, 255, 255)
black = (0, 0, 0)

class Game():
    width = 800
    height = 600
    screen = pygame.display.set_mode((width, height))

    def __init__(self):
        pass
    def fill_screen(self, color):
        self.color = color
        self.screen.fill(self.color)

    def update_method(self):
        pygame.display.update()

game = Game()


class Player(pygame.sprite.Sprite):
    lead_x = game.width/2
    lead_y = game.height/2
    lead_x_change = 0
    lead_y_change = 0
    velocity = 0.2
    block_size = 10
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

    def move_player_x_left(self):
        self.lead_x_change += -self.velocity

    def move_player_x_right(self):
        self.lead_x_change += self.velocity

    def move_player_y_up(self):
        self.lead_y_change += -self.velocity

    def move_player_y_down(self):
        self.lead_y_change += self.velocity

    def draw_player(self):
        pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])

    def key_up_x_stop(self):
        self.lead_x = 0

    def key_up_y_stop(self):
        self.lead_y = 0

    def constant_x_movement(self):
        self.lead_x += self.lead_x_change

    def constant_y_movement(self):
        self.lead_y += self.lead_y_change


player = Player()

exitGame = False
while not exitGame:
    game.fill_screen(white)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitGame = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    player.move_player_y_up()
                if event.key == pygame.K_s:
                    player.move_player_y_down()
                if event.key == pygame.K_a:
                    player.move_player_x_left()
                if event.key == pygame.K_d:
                    player.move_player_x_right()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_w or event.key == pygame.K_s:
                    player.key_up_y_stop()
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    player.key_up_x_stop()
    player.constant_x_movement()
    player.constant_y_movement()

    player.draw_player()
    game.update_method()
pygame.quit()
quit()

最佳答案

事件循环中的代码没有正确缩进。这是一个更正的版本:

while not exitGame:
    game.fill_screen(white)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitGame = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                player.move_player_y_up()
            if event.key == pygame.K_s:
                player.move_player_y_down()
            if event.key == pygame.K_a:
                player.move_player_x_left()
            if event.key == pygame.K_d:
                player.move_player_x_right()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                player.key_up_y_stop()
            if event.key == pygame.K_a or event.key == pygame.K_d:
                player.key_up_x_stop()

此外,在 ...stop 方法中,您必须将 lead_x_changelead_y_change 设置为 0 而不是 lead_xlead_y
def key_up_x_stop(self):
    self.lead_x_change = 0

def key_up_y_stop(self):
    self.lead_y_change = 0

关于python - 球员运动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52105750/

10-12 22:24