python-3.x Pygame在启动时冻结

bq3bfh9z  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(151)

我用pygame来尝试和python更好的配合,但是它就是没有React。我不知道为什么,因为我有类似的代码,工作得很好。

import pygame
import random
import time
width = 500
height = 500
snake = [[width / 2,height / 2]]
direction = "right"
pygame.init()
move_increment = 0.1
screen = pygame.display.set_mode((width,height))
running = True
pygame.display.set_caption(("Snake for Python"))
icon = pygame.image.load(("download.png"))
pygame.display.set_icon(icon)
def Keys():
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
       direction = "up"
       print("w pressed")
    if keys[pygame.K_s]:
        direction = "down"
        print("s pressed")
    if keys[pygame.K_d]:
       direction = "right"
       print("d pressed")
    if keys[pygame.K_a]:
      direction = "left"
      print("a pressed")
  
while running:
 for x in snake:
    pygame.draw.rect(screen, (255,255,255), [x[0], x[1], 15, 15])
    if direction == "up":
     x[1] -= move_increment
    if direction == "down":
     x[1] += move_increment
    if direction == "left":
      x[0] -= move_increment
    if direction == "right":
      x[0] += move_increment
pygame.draw.rect(screen,(0,0,0),[0,0,width,height])
pygame.display.flip()
Keys()
  
for event in pygame.event.get():
   if event.type == pygame.QUIT:
    running = False

没有错误,没有提示停止执行,这只是没有意义。

xcitsw88

xcitsw881#

pygame.draw.rect(screen,(0,0,0),[0,0,width,height])
pygame.display.flip()
Keys()
  
for event in pygame.event.get():
   if event.type == pygame.QUIT:
    running = False

这段代码需要缩进更多。它当前在while循环之外,因此永远不会运行

相关问题