Python实现贪吃蛇小游戏是一个非常好的练手项目,通过这个项目,可以加深对Python编程基础的理解和掌握,同时也可以提升编程能力和逻辑思维能力。下面是完整攻略:
游戏规则
贪吃蛇是一款非常经典的小游戏,游戏规则如下:
- 蛇的身体由一个个方块组成,蛇头在最前面,蛇的初始长度为3个方块
- 当蛇头碰到了边界或者碰到了自己的身体时,游戏结束
- 蛇头碰到食物后,蛇的长度加1,并且在一个随机位置生成一个新的食物
- 蛇头每前进一步,身体向前移动一格,蛇头方向由玩家控制
实现步骤
- 安装Pygame库
首先需要安装Pygame库,可以使用pip命令安装:
pip install pygame
- 初始化Pygame库与游戏窗口
首先需要导入pygame库,并且初始化相关变量,例如游戏窗口的大小、颜色、标题等信息:
import pygame
# 初始化Pygame库
pygame.init()
# 定义窗口尺寸
WIDTH = 600
HEIGHT = 400
SIZE = 20
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇小游戏')
- 定义贪吃蛇的类
定义贪吃蛇的类,包含贪吃蛇的初始化、移动、吃食物等操作。
class Snake:
def __init__(self):
pass
def update(self):
pass
def draw(self):
pass
def eat(self, food):
pass
- 实现贪吃蛇的移动
在贪吃蛇的类中,实现贪吃蛇的移动,主要是更新贪吃蛇的坐标,通过键盘事件控制。
class Snake:
def __init__(self):
self.body = [(WIDTH/2, HEIGHT/2), (WIDTH/2-SIZE, HEIGHT/2), (WIDTH/2-2*SIZE, HEIGHT/2)]
self.direction = "RIGHT"
def update(self):
if self.direction == "UP":
self.body.insert(0, (self.body[0][0], self.body[0][1]-SIZE))
elif self.direction == "DOWN":
self.body.insert(0, (self.body[0][0], self.body[0][1]+SIZE))
elif self.direction == "LEFT":
self.body.insert(0, (self.body[0][0]-SIZE, self.body[0][1]))
elif self.direction == "RIGHT":
self.body.insert(0, (self.body[0][0]+SIZE, self.body[0][1]))
self.body.pop()
def draw(self):
for block in self.body:
pygame.draw.rect(screen, GREEN, (block[0], block[1], SIZE, SIZE))
def eat(self, food):
pass
# 处理键盘事件
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake.direction != "DOWN":
snake.direction = "UP"
elif event.key == pygame.K_DOWN and snake.direction != "UP":
snake.direction = "DOWN"
elif event.key == pygame.K_LEFT and snake.direction != "RIGHT":
snake.direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake.direction != "LEFT":
snake.direction = "RIGHT"
- 实现贪吃蛇吃食物
在贪吃蛇的类中,实现贪吃蛇吃食物的逻辑,并且在画面上生成新的食物。
class Snake:
def __init__(self):
self.body = [(WIDTH/2, HEIGHT/2), (WIDTH/2-SIZE, HEIGHT/2), (WIDTH/2-2*SIZE, HEIGHT/2)]
self.direction = "RIGHT"
def update(self):
if self.direction == "UP":
self.body.insert(0, (self.body[0][0], self.body[0][1]-SIZE))
elif self.direction == "DOWN":
self.body.insert(0, (self.body[0][0], self.body[0][1]+SIZE))
elif self.direction == "LEFT":
self.body.insert(0, (self.body[0][0]-SIZE, self.body[0][1]))
elif self.direction == "RIGHT":
self.body.insert(0, (self.body[0][0]+SIZE, self.body[0][1]))
self.body.pop()
def draw(self):
for block in self.body:
pygame.draw.rect(screen, GREEN, (block[0], block[1], SIZE, SIZE))
def eat(self, food):
if self.body[0] == food.pos:
self.body.append(food.pos)
food.generate()
- 实现食物的类
定义食物的类,包含食物的初始化、生成等操作。
class Food:
def __init__(self):
self.pos = (0, 0)
self.generate()
def generate(self):
self.pos = (random.randint(0, WIDTH//SIZE-1)*SIZE, random.randint(0, HEIGHT//SIZE-1)*SIZE)
def draw(self):
pygame.draw.rect(screen, RED, (self.pos[0], self.pos[1], SIZE, SIZE))
- 实现游戏主逻辑
在游戏主逻辑中,需要不断更新贪吃蛇的状态,判断游戏是否结束,并且在画面上显示当前的分数等信息。
snake = Snake()
food = Food()
score = 0
clock = pygame.time.Clock()
while True:
# 处理退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 更新状态
snake.update()
snake.draw()
food.draw()
# 判断是否碰到墙壁或蛇身
if snake.body[0][0] < 0 or snake.body[0][0] >= WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= HEIGHT:
pygame.quit()
sys.exit()
for block in snake.body[1:]:
if snake.body[0] == block:
pygame.quit()
sys.exit()
# 判断是否吃到食物
snake.eat(food)
# 显示分数等信息
score += 10
font = pygame.font.SysFont(None, 24)
score_text = font.render("Score: " + str(score), True, BLACK)
screen.blit(score_text, (10, 10))
# 刷新画面
pygame.display.update()
# 控制帧率
clock.tick(10)
示例
下面是两个示例:
示例1:实现贪吃蛇的移动
实现贪吃蛇的移动,通过键盘事件控制贪吃蛇的运动。
import pygame
# 初始化Pygame库
pygame.init()
# 定义窗口尺寸
SIZE = 20
WIDTH = 640
HEIGHT = 480
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇小游戏')
# 定义贪吃蛇的类
class Snake:
def __init__(self):
self.body = [(100, 100), (80, 100), (60, 100)]
self.direction = "RIGHT"
def update(self):
if self.direction == "UP":
self.body.insert(0, (self.body[0][0], self.body[0][1]-SIZE))
elif self.direction == "DOWN":
self.body.insert(0, (self.body[0][0], self.body[0][1]+SIZE))
elif self.direction == "LEFT":
self.body.insert(0, (self.body[0][0]-SIZE, self.body[0][1]))
elif self.direction == "RIGHT":
self.body.insert(0, (self.body[0][0]+SIZE, self.body[0][1]))
self.body.pop()
def draw(self):
for block in self.body:
pygame.draw.rect(screen, (0, 255, 0), (block[0], block[1], SIZE, SIZE))
# 实例化贪吃蛇对象
snake = Snake()
# 游戏主循环
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 and snake.direction != "DOWN":
snake.direction = "UP"
elif event.key == pygame.K_DOWN and snake.direction != "UP":
snake.direction = "DOWN"
elif event.key == pygame.K_LEFT and snake.direction != "RIGHT":
snake.direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake.direction != "LEFT":
snake.direction = "RIGHT"
# 更新状态
snake.update()
# 填充背景色
screen.fill((255, 255, 255))
# 画出贪吃蛇
snake.draw()
# 刷新画面
pygame.display.update()
示例2:实现贪吃蛇吃食物
实现贪吃蛇吃食物的逻辑,并且在画面上生成新的食物。
import pygame
import random
# 初始化Pygame库
pygame.init()
# 定义窗口尺寸
SIZE = 20
WIDTH = 640
HEIGHT = 480
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇小游戏')
# 定义贪吃蛇的类
class Snake:
def __init__(self):
self.body = [(100, 100), (80, 100), (60, 100)]
self.direction = "RIGHT"
def update(self):
if self.direction == "UP":
self.body.insert(0, (self.body[0][0], self.body[0][1]-SIZE))
elif self.direction == "DOWN":
self.body.insert(0, (self.body[0][0], self.body[0][1]+SIZE))
elif self.direction == "LEFT":
self.body.insert(0, (self.body[0][0]-SIZE, self.body[0][1]))
elif self.direction == "RIGHT":
self.body.insert(0, (self.body[0][0]+SIZE, self.body[0][1]))
self.body.pop()
def draw(self):
for block in self.body:
pygame.draw.rect(screen, (0, 255, 0), (block[0], block[1], SIZE, SIZE))
def eat(self, food):
if self.body[0] == food.pos:
self.body.append(food.pos)
food.generate()
# 定义食物的类
class Food:
def __init__(self):
self.pos = (0, 0)
self.generate()
def generate(self):
self.pos = (random.randint(0, WIDTH//SIZE-1)*SIZE, random.randint(0, HEIGHT//SIZE-1)*SIZE)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), (self.pos[0], self.pos[1], SIZE, SIZE))
# 实例化贪吃蛇对象与食物对象
snake = Snake()
food = Food()
# 游戏主循环
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 and snake.direction != "DOWN":
snake.direction = "UP"
elif event.key == pygame.K_DOWN and snake.direction != "UP":
snake.direction = "DOWN"
elif event.key == pygame.K_LEFT and snake.direction != "RIGHT":
snake.direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake.direction != "LEFT":
snake.direction = "RIGHT"
# 更新状态
snake.update()
# 填充背景色
screen.fill((255, 255, 255))
# 画出贪吃蛇
snake.draw()
# 画出食物
food.draw()
# 判断是否吃到食物
snake.eat(food)
# 刷新画面
pygame.display.update()
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python实现贪吃蛇小游戏 - Python技术站