gpt4 book ai didi

python - 球员运动

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:13 24 4
gpt4 key购买 nike

我编写了这个简单的代码来绘制然后让您移动一个矩形,但即使在我通过调用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com