我正在尝试制作snake game,但我被困住了。我希望蛇不断运动。
然而,只有当我按下箭头时,蛇才会移动。如果我释放箭头,蛇就会停止移动。我必须说代码不是我的。它取自互联网。
以下是代码:
import sys, pygame
window_size = ( 400, 400 )
white = ( 255, 255, 255 )
class Player():
image = pygame.image.load( 'snikebodydraw.png')
rect = image.get_rect()
player = Player()
screen = pygame.display.set_mode( window_size )
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move = (-10, 0 )
player.rect = player.rect.move(move)
if event.key == pygame.K_RIGHT:
move = ( 10, 0 )
player.rect = player.rect.move(move)
if event.key == pygame.K_UP:
move = ( 0,-10 )
player.rect = player.rect.move(move)
if event.key == pygame.K_DOWN:
move = ( 0, 10 )
player.rect = player.rect.move(move)
screen.fill( white )
screen.blit( player.image, player.rect )
pygame.display.flip()
答案 0 :(得分:-1)
import sys, pygame
window_size = ( 400, 400 )
white = ( 255, 255, 255 )
class Player():
image = pygame.image.load( 'snikebodydraw.png')
rect = image.get_rect()
player = Player()
screen = pygame.display.set_mode( window_size )
move = (0,0) # init movement
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move = (-10, 0 )
if event.key == pygame.K_RIGHT:
move = ( 10, 0 )
if event.key == pygame.K_UP:
move = ( 0,-10 )
if event.key == pygame.K_DOWN:
move = ( 0, 10 )
player.rect = player.rect.move(move)
screen.fill( white )
screen.blit( player.image, player.rect )
pygame.display.flip()