【发布时间】:2022-01-22 01:59:11
【问题描述】:
我正在使用 pygame 模块来制作一个简单的游戏。目前,我正在尝试让一个矩形在我的 500x500 屏幕上移动,但它只在 x 值(左右)中移动。
这是我的代码:
import pygame
pygame.init()
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
# Game Screen Dimensions
game_layout_length = 500
game_layout_width = 500
# Character Attributes
character_length = 10
character_width = 10
character_x = 0
character_y = 0
game_screen = pygame.display.set_mode((game_layout_width, game_layout_length))
game_close = False
game_lost = False
while not game_close:
while not game_lost:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_close = True
game_lost = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
character_x -= 10
elif event.key == pygame.K_RIGHT:
character_x += 10
elif event.key == pygame.K_DOWN:
character_y -= 10
elif event.key == pygame.K_UP:
character_y += 10
pygame.draw.rect(game_screen, blue, [character_x, character_y, character_length, character_width])
pygame.display.update()
print(f'{character_x, character_y}')
pygame.quit()
quit()
print(f'{character_x, character_y}') 行是调试行,它显示“character_y”的值正在改变,所以我一直在试图找出问题所在。
【问题讨论】:
-
1) 无需更新不变的值。例如那些“ += 0 ”行。 2)你把打印线放在哪里?就在抽奖电话之前?如果您发布包括调试行在内的完整代码,其他人可能更容易提供帮助。
-
谢谢,我将删除不变的值。并且打印线放在绘图调用之前。包括在内,这就是整个代码。解决此问题后,我将添加更多代码。