我正在创建一个简单的游戏,但是在所有代码之后,我得到了两个错误消息:文件"C:/Users/victo/PycharmProjects/SnakeGame/Snake_Game.py",第96行,在snakegame()中。
和
文件"C:/Users/victo/PycharmProjects/SnakeGame/Snake_Game.py",第60行,在snakegame中elif event.key == pygame.K_RIGHT: AttributeError:'Event‘对象没有属性'key’
我不知道故障是什么?代码如下:
import pygame
import time
import random
pygame.init()
clock = pygame.time.Clock()
FPS = 60
redcolor = (213, 50, 80)
greencolor = (0, 255, 0)
blackcolor = (0, 0, 0)
whitecolor = (255, 255, 255)
width, height = 640, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game')
snake_block = 18
snake_speed = 15
snake_list = [1]
def snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, greencolor, [x[50], x[50], snake_block, snake_block])
def snakegame():
game_over = False
game_end = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_list[0]
Length_of_snake = 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_end == True:
score = Length_of_snake - 1
score_font = pygame.font.SysFont("comicsanms", 35)
value = score_font.render("Your Score: " + str(score), True, whitecolor)
screen.blit(value, [width / 3, height / 5])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_end = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
x1_change = 0
y1_change = -snake_block
elif event.key == pygame.K_DOWN:
x1_change = 0
y1_change = snake_block
if x1 >= width or x1 < 0 or y1 >= height or y1 <0:
game_end = True
x1 += x1_change
y1 += y1_change
screen.fill(blackcolor)
pygame.draw.rect(screen, redcolor, [foodx, foody, snake_block, 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_end = True
snake(snake_block, snake_list)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
snakegame()
发布于 2020-08-11 06:58:45
尝尝这个
您还应该尝试使用正方形网格,因为您的网格系统令人困惑。
import pygame
import time
import random
pygame.init()
clock = pygame.time.Clock()
FPS = 60
redcolor = (213, 50, 80)
greencolor = (0, 255, 0)
blackcolor = (0, 0, 0)
whitecolor = (255, 255, 255)
width, height = 640, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game')
snake_block = 18
snake_speed = 15
snake_list = [1]
def snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, greencolor, [x[0], x[1], snake_block, snake_block])
def snakegame():
game_over = False
game_end = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_list[0]
Length_of_snake = 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_end == True:
score = Length_of_snake - 1
score_font = pygame.font.SysFont("comicsanms", 35)
value = score_font.render("Your Score: " + str(score), True, whitecolor)
screen.blit(value, [width / 3, height / 5])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_end = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
x1_change = 0
y1_change = -snake_block
elif event.key == pygame.K_DOWN:
x1_change = 0
y1_change = snake_block
if x1 >= width or x1 < 0 or y1 >= height or y1 <0:
game_end = True
x1 += x1_change
y1 += y1_change
screen.fill(blackcolor)
pygame.draw.rect(screen, redcolor, [foodx, foody, snake_block, 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_end = True
snake(snake_block, snake_list)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
snakegame()
https://stackoverflow.com/questions/63337123
复制相似问题