一、玩法描述

贪吃蛇游戏是一款经典的电子游戏,其核心玩法简单但富有挑战性。玩家控制一条不断成长的蛇,在一个封闭的空间内移动。游戏的目标是尽可能长时间地生存下去,同时吃掉出现在屏幕上的食物来增加得分和蛇的长度。以下是贪吃蛇游戏的主要玩法和逻辑:

玩家通过键盘上的方向键或WASD键来改变蛇的移动方向,蛇在游戏区域内自动前进,玩家需要及时调整方向以避免碰撞,游戏区域内随机生成食物。

当蛇的头部与食物的位置重合时,蛇会吃掉食物,蛇的长度随之增加,玩家的得分也会增加,每吃掉一个食物,蛇的长度就会增加一个单位,玩家的得分也相应增加。游戏的难度随着蛇的长度增加而增加,玩家需要避免蛇头碰到自己的身体或游戏边界,一旦发生碰撞,游戏就会结束。

游戏开始时,蛇有一个初始长度,并在游戏区域内的一个固定位置出现。游戏区域内同时会随机生成一个食物。蛇的身体由一系列相连的单位组成。蛇在移动时,头部向前移动一个单位距离,而每个身体单位都移动到前一个单位之前的位置,这样就形成了蛇的连续移动效果。当蛇吃掉一个食物后,游戏会在随机位置生成一个新的食物,以便蛇继续吃食并增长。如果蛇头碰到自己的身体或游戏边界,游戏结束。游戏结束时,可以显示玩家的最终得分,并提供重新开始游戏的选项。玩家的得分基于吃掉食物的数量。有些变种游戏可能会引入不同类型的食物,每种食物的得分可能不同。

二、游戏源代码

需要通过pip安装pygame ,如:pip install pygame

"""

Snake Eater

Made with PyGame

"""

import pygame, sys, time, random

# Difficulty settings

# Easy -> 10

# Medium -> 25

# Hard -> 40

# Harder -> 60

# Impossible-> 120

difficulty = 25

# Window size

frame_size_x = 720

frame_size_y = 480

# Checks for errors encountered

check_errors = pygame.init()

# pygame.init() example output -> (6, 0)

# second number in tuple gives number of errors

if check_errors[1] > 0:

print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')

sys.exit(-1)

else:

print('[+] Game successfully initialised')

# Initialise game window

pygame.display.set_caption('Snake Eater')

game_window = pygame.display.set_mode((frame_size_x, frame_size_y))

# Colors (R, G, B)

black = pygame.Color(0, 0, 0)

white = pygame.Color(255, 255, 255)

red = pygame.Color(255, 0, 0)

green = pygame.Color(0, 255, 0)

blue = pygame.Color(0, 0, 255)

# FPS (frames per second) controller

fps_controller = pygame.time.Clock()

# Game variables

snake_pos = [100, 50]

snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]

food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]

food_spawn = True

direction = 'RIGHT'

change_to = direction

score = 0

# Game Over

def game_over():

my_font = pygame.font.SysFont('times new roman', 90)

game_over_surface = my_font.render('YOU DIED', True, red)

game_over_rect = game_over_surface.get_rect()

game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)

game_window.fill(black)

game_window.blit(game_over_surface, game_over_rect)

show_score(0, red, 'times', 20)

pygame.display.flip()

time.sleep(3)

pygame.quit()

sys.exit()

# Score

def show_score(choice, color, font, size):

score_font = pygame.font.SysFont(font, size)

score_surface = score_font.render('Score : ' + str(score), True, color)

score_rect = score_surface.get_rect()

if choice == 1:

score_rect.midtop = (frame_size_x/10, 15)

else:

score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)

game_window.blit(score_surface, score_rect)

# pygame.display.flip()

# Main logic

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

# Whenever a key is pressed down

elif event.type == pygame.KEYDOWN:

# W -> Up; S -> Down; A -> Left; D -> Right

if event.key == pygame.K_UP or event.key == ord('w'):

change_to = 'UP'

if event.key == pygame.K_DOWN or event.key == ord('s'):

change_to = 'DOWN'

if event.key == pygame.K_LEFT or event.key == ord('a'):

change_to = 'LEFT'

if event.key == pygame.K_RIGHT or event.key == ord('d'):

change_to = 'RIGHT'

# Esc -> Create event to quit the game

if event.key == pygame.K_ESCAPE:

pygame.event.post(pygame.event.Event(pygame.QUIT))

# Making sure the snake cannot move in the opposite direction instantaneously

if change_to == 'UP' and direction != 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and direction != 'UP':

direction = 'DOWN'

if change_to == 'LEFT' and direction != 'RIGHT':

direction = 'LEFT'

if change_to == 'RIGHT' and direction != 'LEFT':

direction = 'RIGHT'

# Moving the snake

if direction == 'UP':

snake_pos[1] -= 10

if direction == 'DOWN':

snake_pos[1] += 10

if direction == 'LEFT':

snake_pos[0] -= 10

if direction == 'RIGHT':

snake_pos[0] += 10

# Snake body growing mechanism

snake_body.insert(0, list(snake_pos))

if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:

score += 1

food_spawn = False

else:

snake_body.pop()

# Spawning food on the screen

if not food_spawn:

food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]

food_spawn = True

# GFX

game_window.fill(black)

for pos in snake_body:

# Snake body

# .draw.rect(play_surface, color, xy-coordinate)

# xy-coordinate -> .Rect(x, y, size_x, size_y)

pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))

# Snake food

pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))

# Game Over conditions

# Getting out of bounds

if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10:

game_over()

if snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:

game_over()

# Touching the snake body

for block in snake_body[1:]:

if snake_pos[0] == block[0] and snake_pos[1] == block[1]:

game_over()

show_score(1, white, 'consolas', 20)

# Refresh game screen

pygame.display.update()

# Refresh rate

fps_controller.tick(difficulty)

三、游戏源代码详解

下面是对贪吃蛇游戏代码的详细分析,包括每个部分的功能和逻辑。

这部分代码主要负责游戏的初始化设置,包括导入库、设置难度、窗口大小、初始化Pygame、创建游戏窗口、定义颜色和创建FPS控制器。

# 导入必要的库

import pygame, sys, time, random

# 设置游戏难度,影响蛇的移动速度

difficulty = 25

# 设置游戏窗口的大小

frame_size_x = 720

frame_size_y = 480

# 初始化pygame,并检查是否有错误发生

check_errors = pygame.init()

if check_errors[1] > 0:

print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')

sys.exit(-1)

else:

print('[+] Game successfully initialised')

# 设置游戏窗口的标题,并创建游戏窗口

pygame.display.set_caption('Snake Eater')

game_window = pygame.display.set_mode((frame_size_x, frame_size_y))

# 定义游戏中使用的颜色

black = pygame.Color(0, 0, 0)

white = pygame.Color(255, 255, 255)

red = pygame.Color(255, 0, 0)

green = pygame.Color(0, 255, 0)

blue = pygame.Color(0, 0, 255)

# 创建一个FPS控制器,用于控制游戏的帧率

fps_controller = pygame.time.Clock()

这部分代码初始化了游戏中的关键变量,包括蛇的位置和身体、食物的位置、蛇的移动方向和得分。

# 初始化蛇的位置和身体

snake_pos = [100, 50]

snake_body = [[100, 50], [90, 50], [80, 50]]

# 随机生成食物的位置,并设置食物生成标志为True

food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]

food_spawn = True

# 设置初始移动方向为右,以及方向改变变量

direction = 'RIGHT'

change_to = direction

# 初始化得分

score = 0

当游戏结束(蛇撞到自己或边界)时,这个函数会被调用。它会显示“YOU DIED”信息,展示最终得分,然后退出游戏。

def game_over():

# 设置游戏结束时的字体和大小

my_font = pygame.font.SysFont('times new roman', 90)

# 渲染游戏结束的文字

game_over_surface = my_font.render('YOU DIED', True, red)

game_over_rect = game_over_surface.get_rect()

game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)

# 填充背景色,显示游戏结束的文字

game_window.fill(black)

game_window.blit(game_over_surface, game_over_rect)

# 显示最终得分

show_score(0, red, 'times', 20)

pygame.display.flip()

# 等待3秒后退出游戏

time.sleep(3)

pygame.quit()

sys.exit()

当游戏结束(蛇撞到自己或边界)时,这个函数会被调用。它会显示“YOU DIED”信息,展示最终得分,然后退出游戏。

def show_score(choice, color, font, size):

# 设置得分的字体和大小

score_font = pygame.font.SysFont(font, size)

# 渲染得分的文字

score_surface = score_font.render('Score : ' + str(score), True, color)

score_rect = score_surface.get_rect()

# 根据choice参数决定得分显示的位置

if choice == 1:

score_rect.midtop =(frame_size_x/10, 15)

else:

score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)

# 将得分的文字显示在游戏窗口上

game_window.blit(score_surface, score_rect)

这个循环是游戏的核心,负责处理玩家的输入、更新游戏状态、绘制游戏画面和检查游戏结束条件。通过不断循环,游戏能够实时响应玩家的操作,同时保持流畅的游戏体验。

while True:

# 处理游戏中的事件,例如按键

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.KEYDOWN:

# 根据按键改变蛇的移动方向

if event.key == pygame.K_UP or event.key == ord('w'):

change_to = 'UP'

if event.key == pygame.K_DOWN or event.key == ord('s'):

change_to = 'DOWN'

if event.key == pygame.K_LEFT or event.key == ord('a'):

change_to = 'LEFT'

if event.key == pygame.K_RIGHT or event.key == ord('d'):

change_to = 'RIGHT'

# 按下ESC键退出游戏

if event.key == pygame.K_ESCAPE:

pygame.event.post(pygame.event.Event(pygame.QUIT))

# 确保蛇不能立即反向移动

if change_to == 'UP' and direction != 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and direction != 'UP':

direction = 'DOWN'

if change_to == 'LEFT' and direction != 'RIGHT':

direction = 'LEFT'

if change_to == 'RIGHT' and direction != 'LEFT':

direction = 'RIGHT'

# 根据蛇的移动方向更新蛇头的位置

if direction == 'UP':

snake_pos[1] -= 10

if direction == 'DOWN':

snake_pos[1] += 10

if direction == 'LEFT':

snake_pos[0] -= 10

if direction == 'RIGHT':

snake_pos[0] += 10

# 蛇吃到食物后增长身体,并重新生成食物

snake_body.insert(0, list(snake_pos))

if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:

score += 1

food_spawn = False

else:

snake_body.pop()

if not food_spawn:

food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]

food_spawn = True

# 绘制游戏的背景、蛇和食物

game_window.fill(black)

for pos in snake_body:

pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))

pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))

# 检查游戏结束的条件

if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10 or snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:

game_over()

for block in snake_body[1:]:

if snake_pos[0] == block[0] and snake_pos[1] == block[1]:

game_over()

# 显示得分并更新游戏窗口

show_score(1, white, 'consolas', 20)

pygame.display.update()

# 控制游戏的帧率

fps_controller.tick(difficulty)

以上代码就能开发一个简单的贪吃蛇游戏了

本文由 曲速引擎(Warp Drive)个人博客https://www.exp-9.com/曲速引擎(Warp Drive)CSDN技术博客https://blog.csdn.net/siberiaWarpDrive曲速引擎 Warp Drive 微信公众号联合创作,更多有趣的技术文章请关注个人博客,转载请说明出处谢谢。

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: