python贪吃蛇小游戏代码
时间: 2023-07-10 14:40:04 浏览: 114
python小游戏-贪吃蛇实例代码.rar
好的,以下是一个简单的贪吃蛇小游戏的Python代码,使用了Pygame库:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义常量
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLOCK_SIZE = 20
SPEED = 5
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.body = [(4, 3), (3, 3), (2, 3)]
self.direction = "right"
def move(self):
# 根据方向移动贪吃蛇
head = self.body[0]
if self.direction == "right":
new_head = (head[0] + 1, head[1])
elif self.direction == "left":
new_head = (head[0] - 1, head[1])
elif self.direction == "up":
new_head = (head[0], head[1] - 1)
elif self.direction == "down":
new_head = (head[0], head[1] + 1)
# 将新的头部加入贪吃蛇身体
self.body.insert(0, new_head)
def turn(self, direction):
# 改变贪吃蛇的方向
if direction == "right" and self.direction != "left":
self.direction = "right"
elif direction == "left" and self.direction != "right":
self.direction = "left"
elif direction == "up" and self.direction != "down":
self.direction = "up"
elif direction == "down" and self.direction != "up":
self.direction = "down"
def draw(self):
# 绘制贪吃蛇
for block in self.body:
rect = pygame.Rect(block[0] * BLOCK_SIZE, block[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, GREEN, rect)
def check_collide(self):
# 检查是否碰到墙壁或自己的身体
head = self.body[0]
if head[0] < 0 or head[0] >= SCREEN_WIDTH // BLOCK_SIZE:
return True
if head[1] < 0 or head[1] >= SCREEN_HEIGHT // BLOCK_SIZE:
return True
for block in self.body[1:]:
if head == block:
return True
return False
def check_eat(self, food):
# 检查是否吃到食物
head = self.body[0]
if head == food:
return True
else:
return False
# 定义食物类
class Food:
def __init__(self):
self.pos = (0, 0)
self.randomize()
def randomize(self):
# 随机生成食物位置
x = random.randint(0, SCREEN_WIDTH // BLOCK_SIZE - 1)
y = random.randint(0, SCREEN_HEIGHT // BLOCK_SIZE - 1)
self.pos = (x, y)
def draw(self):
# 绘制食物
rect = pygame.Rect(self.pos[0] * BLOCK_SIZE, self.pos[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, RED, rect)
# 初始化游戏对象
snake = Snake()
food = Food()
# 创建游戏时钟
clock = pygame.time.Clock()
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.turn("right")
elif event.key == pygame.K_LEFT:
snake.turn("left")
elif event.key == pygame.K_UP:
snake.turn("up")
elif event.key == pygame.K_DOWN:
snake.turn("down")
# 移动贪吃蛇
snake.move()
# 检查是否吃到食物
if snake.check_eat(food.pos):
food.randomize()
else:
snake.body.pop()
# 检查是否碰到墙壁或自己的身体
if snake.check_collide():
pygame.quit()
exit()
# 绘制游戏场景
screen.fill(BLACK)
snake.draw()
food.draw()
pygame.display.update()
# 控制游戏速度
clock.tick(SPEED)
```
这个贪吃蛇小游戏使用Pygame库实现。游戏窗口的大小为640x480,每个方块的大小为20x20。贪吃蛇的初始长度为3个方块,每次吃到食物会增加一个方块。贪吃蛇碰到墙壁或自己的身体时游戏结束。在游戏中,可以使用方向键控制贪吃蛇的移动方向。
阅读全文