我是这个编程领域的新手,因此我在制作第一款游戏时遇到了一些麻烦。角色只是在鼠标在游戏屏幕中移动时才移动,我看到我需要更新函数中的代码,但实际上我不知道该怎么做,因此有时我会重新组织代码以查看结果发生了,但是我仍然有问题。游戏尚未准备就绪。
import pygame
from random import randrange
def eat(c1, c2):
return c1[0] == c2[0] and c1[1] == c2[1]
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
pygame.init()
SCREEN = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Jogo da Cobrinha')
icon = pygame.image.load('snake_icon_game.png')
pygame.display.set_icon(icon)
snake = [(230, 250), (240, 250), (250, 250)]
snake_skin = pygame.Surface((10, 10))
snake_skin.fill((255, 255, 255))
current_direction = RIGHT
apple_position = (randrange(0, 500, 10), randrange(0, 500, 10))
apple_skin = pygame.image.load('apple_skin_game.png').convert_alpha()
score = 0
clock = pygame.time.Clock()
game_over = False
while not game_over:
clock.tick(10)
SCREEN.fill((0, 0, 0))
for x in range(0, 500, 10):
pygame.draw.line(SCREEN, (40, 40, 40), (x, 0), (x, 600))
for y in range(0, 500, 10):
pygame.draw.line(SCREEN, (40, 40, 40), (0, y), (600, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and current_direction != DOWN:
current_direction = UP
if event.key == pygame.K_RIGHT and current_direction != LEFT:
current_direction = RIGHT
if event.key == pygame.K_DOWN and current_direction != UP:
current_direction = DOWN
if event.key == pygame.K_LEFT and current_direction != RIGHT:
current_direction = LEFT
if current_direction == UP:
snake[0] = (snake[0][0], snake[0][1] - 10)
if current_direction == RIGHT:
snake[0] = (snake[0][0] + 10, snake[0][1])
if current_direction == DOWN:
snake[0] = (snake[0][0], snake[0][1] + 10)
if current_direction == LEFT:
snake[0] = (snake[0][0] - 10, snake[0][1])
for i in range(len(snake) - 1, 0, -1):
snake[i] = (snake[i - 1][0], snake[i - 1][1])
if eat(snake[0], apple_position):
apple_position = (randrange(0, 500, 10), randrange(0, 500, 10))
snake.append((0, 0))
score += 10
if snake[0][0] == 500 or snake[0][1] == 500 or snake[0][0] < 0 or snake[0][1] < 0:
game_over = True
SCREEN.blit(apple_skin, apple_position)
for position in snake:
SCREEN.blit(snake_skin, position)
pygame.display.update()
答案 0 :(得分:2)
也许这只是一个粘贴错误,但是您在主循环中的处理逻辑是缩进的,以便仅在事件发生时才进行处理。这与当鼠标不在窗口内移动(鼠标移动会生成事件)时停止的事情相关。
请注意区别:
while not game_over:
clock.tick(10)
SCREEN.fill((0, 0, 0))
for x in range(0, 500, 10):
pygame.draw.line(SCREEN, (40, 40, 40), (x, 0), (x, 600))
for y in range(0, 500, 10):
pygame.draw.line(SCREEN, (40, 40, 40), (0, y), (600, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and current_direction != DOWN:
current_direction = UP
if event.key == pygame.K_RIGHT and current_direction != LEFT:
current_direction = RIGHT
if event.key == pygame.K_DOWN and current_direction != UP:
current_direction = DOWN
if event.key == pygame.K_LEFT and current_direction != RIGHT:
current_direction = LEFT
# STARTING HERE -- INDENTATION FIX
if current_direction == UP:
snake[0] = (snake[0][0], snake[0][1] - 10)
if current_direction == RIGHT:
snake[0] = (snake[0][0] + 10, snake[0][1])
if current_direction == DOWN:
snake[0] = (snake[0][0], snake[0][1] + 10)
if current_direction == LEFT:
snake[0] = (snake[0][0] - 10, snake[0][1])
for i in range(len(snake) - 1, 0, -1):
snake[i] = (snake[i - 1][0], snake[i - 1][1])
if eat(snake[0], apple_position):
apple_position = (randrange(0, 500, 10), randrange(0, 500, 10))
snake.append((0, 0))
score += 10
if snake[0][0] == 500 or snake[0][1] == 500 or snake[0][0] < 0 or snake[0][1] < 0:
game_over = True
SCREEN.blit(apple_skin, apple_position)
for position in snake:
SCREEN.blit(snake_skin, position)
pygame.display.update()