python贪吃蛇小游戏
时间: 2023-06-21 12:13:01 浏览: 32
好的,我可以为你提供一个简单的Python贪吃蛇小游戏。你需要先安装Python,并安装Pygame库。
以下是代码:
```python
import pygame
import random
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置屏幕大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# 贪吃蛇方块大小
BLOCK_SIZE = 20
# 初始化pygame
pygame.init()
# 创建屏幕
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置窗口标题
pygame.display.set_caption('贪吃蛇游戏')
# 设置时钟
clock = pygame.time.Clock()
# 贪吃蛇类
class Snake:
def __init__(self):
self.body = [(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)]
self.direction = 'right'
# 移动
def move(self):
head_x, head_y = self.body[0]
if self.direction == 'right':
new_head = (head_x + BLOCK_SIZE, head_y)
elif self.direction == 'left':
new_head = (head_x - BLOCK_SIZE, head_y)
elif self.direction == 'up':
new_head = (head_x, head_y - BLOCK_SIZE)
elif self.direction == 'down':
new_head = (head_x, head_y + BLOCK_SIZE)
self.body.insert(0, new_head)
# 改变方向
def change_direction(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 hit_wall(self):
head_x, head_y = self.body[0]
if head_x < 0 or head_x >= SCREEN_WIDTH or head_y < 0 or head_y >= SCREEN_HEIGHT:
return True
return False
# 判断是否撞到自己
def hit_self(self):
head = self.body[0]
for i in range(1, len(self.body)):
if head == self.body[i]:
return True
return False
# 吃食物
def eat_food(self, food):
if self.body[0] == food:
return True
return False
# 绘制贪吃蛇
def draw(self):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, [x, y, BLOCK_SIZE, BLOCK_SIZE])
# 获取贪吃蛇头部坐标
def get_head(self):
return self.body[0]
# 食物类
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 regenerate(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
# 创建贪吃蛇和食物
snake = Snake()
food = Food()
# 游戏循环
done = False
while not done:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.change_direction('right')
elif event.key == pygame.K_LEFT:
snake.change_direction('left')
elif event.key == pygame.K_UP:
snake.change_direction('up')
elif event.key == pygame.K_DOWN:
snake.change_direction('down')
# 绘制背景
screen.fill(BLACK)
# 移动贪吃蛇
snake.move()
# 判断是否撞墙或撞到自己
if snake.hit_wall() or snake.hit_self():
done = True
# 判断是否吃到食物
if snake.eat_food(food.get_head()):
food.regenerate()
# 绘制贪吃蛇和食物
snake.draw()
food.draw()
# 更新屏幕
pygame.display.update()
# 控制游戏速度
clock.tick(10)
# 退出游戏
pygame.quit()
```
运行代码后,你就可以玩贪吃蛇小游戏啦!