o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
写一个python程序,贪吃蛇游戏
- User
写一个python程序,贪吃蛇游戏
- Assistant
写一个python程序,贪吃蛇游戏
当然!以下是一个用 Python 和 Pygame 库编写的贪吃蛇游戏的完整示例。这个简单的游戏允许玩家控制蛇在屏幕上移动,吃食物,增长长度,同时避免撞墙或自己身体。
在开始之前,请确保你已经安装了 Pygame 库。如果还没有安装,可以使用以下命令通过 pip 进行安装:
pip install pygame
import pygame
import time
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# 设置显示窗口大小
dis_width = 600
dis_height = 400
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇游戏')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
# 设置字体
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def your_score(score):
value = score_font.render("得分: " + str(score), True, black)
dis.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
def gameLoop():
game_over = False
game_close = False
# 起始位置
x1 = dis_width / 2
y1 = dis_height / 2
# 起始移动距离
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# 生成食物
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close == True:
dis.fill(blue)
message("你失败了! 按 Q 退出 或 C 重新开始", red)
your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
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 or event.key == pygame.K_a:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_block
x1_change = 0
# 检查边界
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, green, [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_close = True
our_snake(snake_block, snake_List)
your_score(Length_of_snake - 1)
pygame.display.update()
# 检查是否吃到食物
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
import pygame
import time
import random
pygame:用于游戏开发的库。time:用于时间相关的功能。random:用于生成随机数(如食物的位置)。pygame.init()
# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
初始化 Pygame 并定义了几种颜色的 RGB 值,便于后续使用。
dis_width = 600
dis_height = 400
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇游戏')
clock = pygame.time.Clock()
设置游戏窗口的宽度和高度,并初始化显示窗口和时钟。
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
snake_block:蛇每一节的大小。snake_speed:控制游戏速度。def your_score(score):
value = score_font.render("得分: " + str(score), True, black)
dis.blit(value, [0, 0])
渲染并显示当前分数。
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
根据 snake_list 绘制蛇的每一节。
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
在屏幕上显示消息,如游戏结束时的提示。
def gameLoop():
...
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
初始化游戏状态、蛇的位置、变化方向、蛇的身体列表、蛇的长度和食物的位置。
while not game_over:
...
持续运行游戏,直到 game_over 为 True。
while game_close == True:
dis.fill(blue)
message("你失败了! 按 Q 退出 或 C 重新开始", red)
your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
当游戏结束时,显示失败消息并等待玩家选择退出(Q)或重新开始(C)。
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 or event.key == pygame.K_a:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_block
x1_change = 0
检测玩家的按键输入(方向键或 WASD 键)来改变蛇的移动方向。
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
更新蛇的位置,并检查是否撞墙。如果撞墙,设置 game_close 为 True 以结束游戏。
dis.fill(blue)
pygame.draw.rect(dis, green, [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_close = True
如果蛇头的位置与身体的任何一节相同,游戏结束。
our_snake(snake_block, snake_List)
your_score(Length_of_snake - 1)
pygame.display.update()
绘制蛇和显示当前分数,然后更新显示。
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
如果蛇头的位置与食物位置相同,生成新的食物位置并增加蛇的长度。
clock.tick(snake_speed)
控制游戏的帧率,使游戏以设定的速度运行。
pygame.quit()
quit()
当游戏结束时,退出 Pygame 并关闭程序。
.py 文件,例如 snake_game.py。python snake_game.py
你可以根据自己的需求进一步改进这个游戏,例如:
希望这个示例能帮助你开始制作自己的贪吃蛇游戏!如果有任何问题或需要进一步的帮助,请随时提问。
Not yet included in a published release.