求大佬帮忙弄贪吃蛇游戏结束的统计得分!
import pygame, sys, randomfrom pygame.locals import *
red_color = pygame.Color(255, 0, 0)
blue_color = pygame.Color(0, 0, 255)
green_color = pygame.Color(0, 255, 0)
def game_over():
pygame.quit()
sys.exit()
def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
# 初始化蛇的位置和身体
snake_position =
snake_body = [, , ]
food_position =
food_flag = 1
direction = 'RIGHT'
key_direction = direction
while True:
for event in pygame.event.get():
if event.type == QUIT:
game_over()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
key_direction = 'RIGHT'
if event.key == K_LEFT:
key_direction = 'LEFT'
if event.key == K_UP:
key_direction = 'UP'
if event.key == K_DOWN:
key_direction = 'DOWN'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if key_direction == 'LEFT' and direction != 'RIGHT':
direction = key_direction
if key_direction == 'RIGHT' and direction != 'LEFT':
direction = key_direction
if key_direction == 'UP' and direction != 'DOWN':
direction = key_direction
if key_direction == 'DOWN' and direction != 'UP':
direction = key_direction
if direction == 'RIGHT':
snake_position += 20
if direction == 'LEFT':
snake_position -= 20
if direction == 'UP':
snake_position -= 20
if direction == 'DOWN':
snake_position += 20
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
food_flag = 0
else:
snake_body.pop()
if food_flag == 0:
x = random.randrange(1, 32) * 20
y = random.randrange(1, 24) * 20
food_position =
food_flag = 1
screen.fill(blue_color)
for position in snake_body:
pygame.draw.rect(screen, red_color, Rect(position, position, 20, 20))
pygame.draw.rect(screen, green_color, Rect(food_position, food_position, 20, 20))
pygame.display.flip()
if snake_position >= 640 or snake_position < 0 or snake_position >= 480 or snake_position < 0:
game_over()
clock.tick(10)
if __name__ == '__main__':
main()
好的,我来看一下你的代码,并帮你添加统计得分的功能。
我们需要在游戏结束时输出蛇的长度来表示得分,并修复一些代码错误。以下是改进后的版本:
import pygame, sys, random
from pygame.locals import *
red_color = pygame.Color(255, 0, 0)
blue_color = pygame.Color(0, 0, 255)
green_color = pygame.Color(0, 255, 0)
def game_over(score):
font = pygame.font.SysFont('arial', 36)
game_over_surface = font.render(f'Game Over! Score: {score}', True, red_color)
screen.blit(game_over_surface, (120, 200))
pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()
sys.exit()
def main():
pygame.init()
global screen
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
# 初始化蛇的位置和身体
snake_position =
snake_body = [, , ]
food_position =
food_flag = 1
direction = 'RIGHT'
key_direction = direction
while True:
for event in pygame.event.get():
if event.type == QUIT:
game_over(len(snake_body) - 3)
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
key_direction = 'RIGHT'
if event.key == K_LEFT:
key_direction = 'LEFT'
if event.key == K_UP:
key_direction = 'UP'
if event.key == K_DOWN:
key_direction = 'DOWN'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if key_direction == 'LEFT' and direction != 'RIGHT':
direction = key_direction
if key_direction == 'RIGHT' and direction != 'LEFT':
direction = key_direction
if key_direction == 'UP' and direction != 'DOWN':
direction = key_direction
if key_direction == 'DOWN' and direction != 'UP':
direction = key_direction
if direction == 'RIGHT':
snake_position += 20
if direction == 'LEFT':
snake_position -= 20
if direction == 'UP':
snake_position -= 20
if direction == 'DOWN':
snake_position += 20
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
food_flag = 0
else:
snake_body.pop()
if food_flag == 0:
x = random.randrange(1, 32) * 20
y = random.randrange(1, 24) * 20
food_position =
food_flag = 1
screen.fill(blue_color)
for position in snake_body:
pygame.draw.rect(screen, red_color, Rect(position, position, 20, 20))
pygame.draw.rect(screen, green_color, Rect(food_position, food_position, 20, 20))
pygame.display.flip()
if snake_position >= 640 or snake_position < 0 or snake_position >= 480 or snake_position < 0:
game_over(len(snake_body) - 3)
for block in snake_body:
if snake_position == block:
game_over(len(snake_body) - 3)
clock.tick(10)
if __name__ == '__main__':
main()
主要的修改点包括:
1. 修复了`snake_position`和`food_position`的初始化问题。
2. 添加了判断蛇是否撞墙或者撞到自己的逻辑。
3. 在`game_over`函数中添加了得分显示。
4. 调整了绘制蛇和食物的代码。
这样修改之后,游戏在结束时会显示玩家的得分。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-8-30 19:00
好的,我来看一下你的代码,并帮你添加统计得分的功能。
我们需要在游戏结束时输出蛇的长度来表示得分, ...
我试过了,蛇吃不到食物!
小甲鱼303 发表于 2024-08-31 12:46
我试过了,蛇吃不到食物!
好的,我们来修复代码,让蛇能正确地移动和吃到食物,并统计得分。请看以下改进后的版本:
import pygame, sys, random
from pygame.locals import *
# 颜色定义
red_color = pygame.Color(255, 0, 0)
blue_color = pygame.Color(0, 0, 255)
green_color = pygame.Color(0, 255, 0)
def game_over(score):
print(f"游戏结束! 你的得分是: {score}")
pygame.quit()
sys.exit()
def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
# 初始化蛇的位置和身体
snake_position =
snake_body = [, , ]
food_position =
food_flag = 1
direction = 'RIGHT'
key_direction = direction
score = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
game_over(score)
elif event.type == KEYDOWN:
if event.key == K_RIGHT and direction != 'LEFT':
direction = 'RIGHT'
if event.key == K_LEFT and direction != 'RIGHT':
direction = 'LEFT'
if event.key == K_UP and direction != 'DOWN':
direction = 'UP'
if event.key == K_DOWN and direction != 'UP':
direction = 'DOWN'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if direction == 'RIGHT':
snake_position += 20
if direction == 'LEFT':
snake_position -= 20
if direction == 'UP':
snake_position -= 20
if direction == 'DOWN':
snake_position += 20
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
score += 10
food_flag = 0
else:
snake_body.pop()
if food_flag == 0:
food_position =
food_flag = 1
screen.fill(blue_color)
for position in snake_body:
pygame.draw.rect(screen, red_color, Rect(position, position, 20, 20))
pygame.draw.rect(screen, green_color, Rect(food_position, food_position, 20, 20))
pygame.display.flip()
# 检查是否碰到边界
if snake_position >= 640 or snake_position < 0 or snake_position >= 480 or snake_position < 0:
game_over(score)
# 检查是否碰到自己
for block in snake_body:
if snake_position == block:
game_over(score)
clock.tick(10)
if __name__ == '__main__':
main()
这个改进版的代码修复了以下问题:
1. 蛇的位置初始化错误。
2. 食物位置初始化和更新。
3. 蛇的移动方向处理逻辑。
4. 增加了得分统计和游戏结束时的得分显示。
5. 检查蛇是否碰到自己的逻辑。
希望这对你有帮助!如果有任何其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]