Python 贪吃蛇游戏代码:使用 Pygame 库
以下是一个使用 Python 和 Pygame 库实现的简单贪吃蛇游戏代码:
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义常量
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLOCK_SIZE = 10
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 定义蛇类
class Snake:
def __init__(self):
self.body = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)]
self.direction = 'right'
def move(self):
x, y = self.body[0]
if self.direction == 'up':
y -= BLOCK_SIZE
elif self.direction == 'down':
y += BLOCK_SIZE
elif self.direction == 'left':
x -= BLOCK_SIZE
elif self.direction == 'right':
x += BLOCK_SIZE
self.body.insert(0, (x, y))
self.body.pop()
def turn(self, direction):
self.direction = direction
def draw(self):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, (x, y, BLOCK_SIZE, BLOCK_SIZE))
def grow(self):
x, y = self.body[0]
if self.direction == 'up':
y -= BLOCK_SIZE
elif self.direction == 'down':
y += BLOCK_SIZE
elif self.direction == 'left':
x -= BLOCK_SIZE
elif self.direction == 'right':
x += BLOCK_SIZE
self.body.insert(0, (x, y))
def check_collisions(self):
x, y = self.body[0]
if x < 0 or x >= SCREEN_WIDTH or y < 0 or y >= SCREEN_HEIGHT:
return True
for i in range(1, len(self.body)):
if x == self.body[i][0] and y == self.body[i][1]:
return True
return False
# 定义食物类
class Food:
def __init__(self):
self.x = random.randint(0, SCREEN_WIDTH / BLOCK_SIZE - 1) * BLOCK_SIZE
self.y = random.randint(0, SCREEN_HEIGHT / BLOCK_SIZE - 1) * BLOCK_SIZE
def draw(self):
pygame.draw.rect(screen, RED, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE))
def check_collision(self, snake):
x, y = snake.body[0]
if self.x == x and self.y == y:
return True
return False
# 创建蛇和食物
snake = Snake()
food = Food()
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.turn('up')
elif event.key == pygame.K_DOWN:
snake.turn('down')
elif event.key == pygame.K_LEFT:
snake.turn('left')
elif event.key == pygame.K_RIGHT:
snake.turn('right')
# 移动蛇
snake.move()
# 检查碰撞
if snake.check_collisions():
pygame.quit()
quit()
# 检查是否吃到食物
if food.check_collision(snake):
snake.grow()
food = Food()
# 绘制屏幕
screen.fill(BLACK)
snake.draw()
food.draw()
pygame.display.update()
pygame.time.Clock().tick(20)
这个代码实现了基本的贪吃蛇游戏逻辑,包括蛇的移动、碰撞检测和食物机制。它使用 Pygame 库来处理图形和用户输入。
代码解释:
- 初始化 Pygame:使用
pygame.init()初始化 Pygame 库。 - 定义常量:定义游戏窗口的宽度、高度、方块大小和颜色。
- 创建游戏窗口:使用
pygame.display.set_mode()创建一个游戏窗口,并使用pygame.display.set_caption()设置窗口标题。 - 定义蛇类:
Snake类包含蛇的身体、移动方向、绘制、生长和碰撞检测方法。 - 定义食物类:
Food类包含食物的位置、绘制和碰撞检测方法。 - 游戏循环:游戏循环持续运行,处理用户输入、移动蛇、检查碰撞和绘制游戏画面。
功能说明:
- 蛇的移动:通过键盘控制蛇的移动方向,使用
Snake.move()方法更新蛇的身体位置。 - 碰撞检测:使用
Snake.check_collisions()方法检测蛇是否撞到边界或自身。 - 食物机制:使用
Food.check_collision()方法检测蛇是否吃到食物,吃到食物后使用Snake.grow()方法增加蛇的长度。
运行代码:
- 确保已经安装了 Pygame 库。
- 将代码保存为
.py文件。 - 在终端中运行该文件。
扩展功能:
- 增加游戏难度等级。
- 添加游戏分数系统。
- 增加不同的游戏背景和音乐。
- 添加关卡设计和游戏结束界面。
这个代码提供了一个简单的贪吃蛇游戏示例,你可以根据自己的需求进行修改和扩展。
原文地址: https://www.cveoy.top/t/topic/l0j2 著作权归作者所有。请勿转载和采集!