创建一个简单的贪吃蛇游戏可以通过使用Pygame库来实现。以下是一个完整的示例代码,它包含了基本的游戏功能,如蛇的移动、食物的生成以及碰撞检测。
首先,确保你已经安装了Pygame库。如果你还没有安装,可以使用以下命令进行安装:
pip install pygame
然后,你可以使用以下代码来创建一个简单的贪吃蛇游戏:
import pygame
import time
import random
# 初始化Pygame
pygame.init()
# 定义屏幕大小
screen_width = 800
screen_height = 600
# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# 定义蛇的大小和速度
snake_block = 10
snake_speed = 15
# 创建屏幕对象
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇游戏')
# 定义时钟对象
clock = pygame.time.Clock()
# 定义字体
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, yellow)
screen.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, green, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [screen_width / 6, screen_height / 3])
def gameLoop():
game_over = False
game_close = False
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close == True:
screen.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:
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:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(black)
pygame.draw.rect(screen, blue, [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, screen_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
代码说明:
- 初始化:Pygame库初始化,定义了一些常量(如屏幕大小、颜色、蛇的大小和速度)。
- 函数定义:
your_score(score)
:显示当前分数。our_snake(snake_block, snake_list)
:绘制蛇的形状。message(msg, color)
:显示游戏结束的消息。
- 游戏主循环:在
gameLoop()
函数中,包含了处理键盘输入、蛇的移动、食物的生成以及碰撞检测的逻辑。
游戏玩法:
- 使用方向键控制蛇的移动方向。
- 吃到蓝色的食物后,蛇会变长并增加分数。
- 如果撞到墙壁或蛇自身,游戏结束。
运行上述代码,你将看到一个简单的贪吃蛇游戏界面,并可以通过键盘控制蛇的移动。