我正在用Python编写基本游戏“蛇”,使用“pygame”。当我在开始时四处走动并且还活着时,我的箭头键工作正常。当我死亡时,我不能使用任何键,我不能通过按下窗口右上角的X按钮来关闭窗口。终止它的唯一方法是在控制台中按Ctrl-x
,这样它就不会关闭
当我调试它时,我的控制台说我的q
和c
值是59
,248
相应地,但是pygameK_q
和{113
,99.
有人知道原因吗?我死后的代码如下:
while game_close == True:
self.dis.fill(colors("blue"))
self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
如果有人知道为什么会发生这种情况,这将是有用的。 这是我第一次写任何任务,很抱歉没有最好的描述,或者有任何重复(我搜索了,没有找到适合我的)
编辑:这是整个脚本:
game_over = False
game_close = False
x1 = self.dis_width / 2
y1 = self.dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
flagex=True
flagey=True
while not game_over:
while game_close :
self.dis.fill(colors.blue())
self.message("You Lost! Press C-Play Again or Q-Quit", colors.red())
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_close = True
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and flagey==True:
flagey = False
flagex = True
x1_change = -self.snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT and flagey==True:
flagey = False
flagex = True
x1_change = self.snake_block
y1_change = 0
elif event.key == pygame.K_UP and flagex==True:
flagey = True
flagex = False
y1_change = -self.snake_block
x1_change = 0
elif event.key == pygame.K_DOWN and flagex==True:
flagey = True
flagex = False
y1_change = self.snake_block
x1_change = 0
if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
self.dis.fill(colors.blue())
pygame.draw.rect(self.dis, colors.green(), [foodx, foody, self.snake_block, self.snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
self.our_snake(self.snake_block, snake_List)
self.Your_score(Length_of_snake - 1,)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
Length_of_snake += 1
self.clock.tick(self.snake_speed)
pygame.quit()
quit()
我无法完全回答您的问题,因为看到完整的代码会很好,但我会将其更改为:
试着换成这个
未包含整个代码,缺少一个类,并且未定义某些方法。但首先,我建议将所有按键改为:
不要把它放在循环事件中
相关问题 更多 >
编程相关推荐