帮我写一个贪吃蛇游戏代码
时间: 2023-08-20 22:08:43 AIGC 浏览: 56
好的,以下是一个简单的贪吃蛇游戏代码,使用 Python 编写:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义游戏区域大小和格子大小
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
GRID_SIZE = 20
# 定义颜色
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 = [(4, 3), (3, 3), (2, 3)]
self.direction = "right"
# 移动蛇
def move(self):
head = self.body[0]
if self.direction == "up":
new_head = (head[0], head[1] - 1)
elif self.direction == "down":
new_head = (head[0], head[1] + 1)
elif self.direction == "left":
new_head = (head[0] - 1, head[1])
elif self.direction == "right":
new_head = (head[0] + 1, head[1])
self.body.insert(0, new_head)
self.body.pop()
# 改变蛇的方向
def change_direction(self, direction):
if direction == "up" and self.direction != "down":
self.direction = "up"
elif direction == "down" and self.direction != "up":
self.direction = "down"
elif direction == "left" and self.direction != "right":
self.direction = "left"
elif direction == "right" and self.direction != "left":
self.direction = "right"
# 判断蛇是否撞墙或撞到自己
def is_dead(self):
head = self.body[0]
if head[0] < 0 or head[0] >= SCREEN_WIDTH / GRID_SIZE:
return True
elif head[1] < 0 or head[1] >= SCREEN_HEIGHT / GRID_SIZE:
return True
for i in range(1, len(self.body)):
if head == self.body[i]:
return True
return False
# 判断蛇是否吃到食物
def eat_food(self, food_position):
if self.body[0] == food_position:
self.body.insert(0, food_position)
return True
return False
# 定义食物类
class Food:
def __init__(self):
self.position = (0, 0)
self.randomize_position()
# 随机生成食物位置
def randomize_position(self):
x = random.randint(0, SCREEN_WIDTH / GRID_SIZE - 1)
y = random.randint(0, SCREEN_HEIGHT / GRID_SIZE - 1)
self.position = (x, y)
# 绘制食物
def draw(self):
x = self.position[0] * GRID_SIZE
y = self.position[1] * GRID_SIZE
pygame.draw.rect(screen, RED, (x, y, GRID_SIZE, GRID_SIZE))
# 创建蛇和食物对象
snake = Snake()
food = Food()
# 游戏循环
running = True
clock = pygame.time.Clock()
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction("up")
elif event.key == pygame.K_DOWN:
snake.change_direction("down")
elif event.key == pygame.K_LEFT:
snake.change_direction("left")
elif event.key == pygame.K_RIGHT:
snake.change_direction("right")
# 移动蛇
snake.move()
# 判断蛇是否死亡
if snake.is_dead():
running = False
# 判断蛇是否吃到食物
if snake.eat_food(food.position):
food.randomize_position()
# 绘制游戏界面
screen.fill(BLACK)
snake_color = WHITE
for body_part in snake.body:
x = body_part[0] * GRID_SIZE
y = body_part[1] * GRID_SIZE
pygame.draw.rect(screen, snake_color, (x, y, GRID_SIZE, GRID_SIZE))
snake_color = GREEN if snake_color == WHITE else WHITE
food.draw()
pygame.display.flip()
# 控制游戏速度
clock.tick(10)
# 退出 Pygame
pygame.quit()
```
这个代码实现了一个基本的贪吃蛇游戏,使用 Pygame 库进行图形界面的绘制和事件处理。你可以根据需要进行修改和优化。
阅读全文
相关推荐













