我正在制作蛇游戏,而我的问题是要理解为什么在使用lead_x_change增量并导致仅将lead_x增加+10或事件的情况下,使用波纹管代码会不断向lead_x值添加-10或+10的原因。 -10一次1个活动?谢谢。
import pygame
pygame.init()
#Colors
white= (255,255,255)
black=(0,0,0)
red= (255,0,0)
gameDisplay= pygame.display.set_mode((800,600))
pygame.display.set_caption('ALIEN')
gameExit= False
lead_x=300
lead_y=300
lead_x_change=0
while not gameExit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
if event.type==pygame.KEYDOWN:
if event.key== pygame.K_LEFT:
lead_x_change =-1
if event.key==pygame.K_RIGHT:
lead_x_change = 1
lead_x += lead_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()
答案 0 :(得分:-1)
尝试这样的事情
import pygame
pygame.init()
#Colors
white= (255,255,255)
black=(0,0,0)
red= (255,0,0)
gameDisplay= pygame.display.set_mode((800,600))
pygame.display.set_caption('ALIEN')
gameExit= False
lead_x=300
lead_y=300
lead_x_change=0
while not gameExit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
if event.type==pygame.KEYDOWN:
if event.key== pygame.K_LEFT:
lead_x_change =-1
if event.key==pygame.K_RIGHT:
lead_x_change = 1
if event.type==pygame.KEYUP:
if event.key== pygame.K_LEFT:
lead_x_change = 0
if event.key== pygame.K_RIGHT:
lead_x_change = 0
lead_x += lead_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()