我一直在学习如何使用pythonprogramming.net中的PyGame。我目前正在窗口上移动图像:
这是我正在运行的代码,以防您在检查时网站关闭。出于某种原因,我不知道为什么,汽车图像不动。当我关注视频时,我已经输入了所有内容,当我发现它不起作用时,我复制并粘贴了仍然无法获得预测的性能。我打印出要调试的事件(这是我添加的唯一一行,而不是从网站复制的代码)。
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
car_speed = 0
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
######################
##
x += x_change
##
gameDisplay.fill(white)
car(x,y)
print(event) # added for debugging
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
我使用了Spyder,但是我的窗户突然冻结了,我不得不在我的Mac上退出Spyder。我切换到PyCharm和终端,发现两者都没有冻结,但仍然没有移动。
终端上打印出来的是:
libpng warning: iCCP: known incorrect sRGB profile
<Event(4-MouseMotion {'pos': (364, 569), 'rel': (364, 569), 'buttons': (0, 0, 0)})>
<Event(5-MouseButtonDown {'pos': (364, 569), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (364, 569), 'button': 1})>
<Event(4-MouseMotion {'pos': (491, 27), 'rel': (127, -542), 'buttons': (0, 0, 0)})>
^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D^[[C^[[C<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(12-Quit {})>
我理解代码的第一行是来自How do I disable the libpng warning? (python, pygame)的警告,不应以这种方式影响代码。
答案 0 :(得分:0)
抱歉已经有一段时间了,我猜他们现在已经改变了一些东西,因为我正在阅读文档。它改变了很多我彻底的倾诉。我想指出的一件事是在比赛开始前填写背景是一个好习惯。我会更多地了解这一点,我将自己进行测试。
编辑:我刚测试过,对我来说它完全正常。它可能是你的键盘布局,但我唯一想到的是,你选择了窗口吗?