用python写一个贪吃蛇游戏代码
时间: 2023-12-17 18:50:11 浏览: 96
好的,以下是一个简单的贪吃蛇游戏的 Python 代码:
```python
import pygame
import random
# 设置游戏窗口尺寸
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 定义方块大小和蛇的初始长度
BLOCK_SIZE = 20
INITIAL_LENGTH = 3
# 初始化 pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置游戏窗口标题
pygame.display.set_caption("Snake Game")
# 创建时钟对象
clock = pygame.time.Clock()
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义方向常量
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.length = INITIAL_LENGTH
self.direction = RIGHT
self.body = []
for i in range(self.length):
self.body.append([i*BLOCK_SIZE, 0])
def move(self):
# 计算贪吃蛇头部的下一个位置
if self.direction == UP:
new_head = [self.body[0][0], self.body[0][1] - BLOCK_SIZE]
elif self.direction == DOWN:
new_head = [self.body[0][0], self.body[0][1] + BLOCK_SIZE]
elif self.direction == LEFT:
new_head = [self.body[0][0] - BLOCK_SIZE, self.body[0][1]]
elif self.direction == RIGHT:
new_head = [self.body[0][0] + BLOCK_SIZE, self.body[0][1]]
# 把新位置插入到贪吃蛇头部
self.body.insert(0, new_head)
# 如果贪吃蛇头部和食物位置重合,则贪吃蛇长度加1
if self.body[0] == food.position:
food.generate()
self.length += 1
# 如果贪吃蛇长度大于1,则删除贪吃蛇尾部
if self.length > 1:
self.body.pop()
def draw(self):
for block in self.body:
pygame.draw.rect(window, WHITE, [block[0], block[1], BLOCK_SIZE, BLOCK_SIZE])
# 定义食物类
class Food:
def __init__(self):
self.position = [0, 0]
self.generate()
def generate(self):
# 在随机位置生成食物
x = random.randint(0, (WINDOW_WIDTH-BLOCK_SIZE)//BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (WINDOW_HEIGHT-BLOCK_SIZE)//BLOCK_SIZE) * BLOCK_SIZE
self.position = [x, y]
def draw(self):
pygame.draw.rect(window, RED, [self.position[0], self.position[1], BLOCK_SIZE, BLOCK_SIZE])
# 创建贪吃蛇和食物对象
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 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.move()
# 绘制游戏界面
window.fill(BLACK)
snake.draw()
food.draw()
# 检测贪吃蛇是否撞到墙壁或自己身体
if snake.body[0][0] < 0 or snake.body[0][0] >= WINDOW_WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= WINDOW_HEIGHT:
text = font.render("Game Over!", True, WHITE)
window.blit(text, [WINDOW_WIDTH//2-text.get_width()//2, WINDOW_HEIGHT//2-text.get_height()//2])
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
quit()
for block in snake.body[1:]:
if block == snake.body[0]:
text = font.render("Game Over!", True, WHITE)
window.blit(text, [WINDOW_WIDTH//2-text.get_width()//2, WINDOW_HEIGHT//2-text.get_height()//2])
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
quit()
# 更新游戏窗口
pygame.display.update()
# 设置游戏帧率
clock.tick(10)
```
运行以上代码,即可开始玩贪吃蛇游戏。
阅读全文