用python编写一个贪吃蛇游戏
时间: 2023-09-15 08:23:06 浏览: 80
Python编写一个贪吃蛇游戏
好的,下面是Python的一个简单贪吃蛇游戏的代码:
```python
import pygame
import random
# 初始化 pygame
pygame.init()
# 定义一些常量
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
SNAKE_SIZE = 20
FOOD_SIZE = 20
SNAKE_COLOR = (0, 255, 0)
FOOD_COLOR = (255, 0, 0)
# 创建窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 创建时钟
clock = pygame.time.Clock()
# 创建贪吃蛇的类
class Snake:
def __init__(self):
self.rects = [pygame.Rect(SNAKE_SIZE * 3, SNAKE_SIZE * 3, SNAKE_SIZE, SNAKE_SIZE),
pygame.Rect(SNAKE_SIZE * 2, SNAKE_SIZE * 3, SNAKE_SIZE, SNAKE_SIZE),
pygame.Rect(SNAKE_SIZE, SNAKE_SIZE * 3, SNAKE_SIZE, SNAKE_SIZE)]
self.direction = "right"
def move(self):
# 计算蛇头位置
x = self.rects[0].x
y = self.rects[0].y
if self.direction == "up":
y -= SNAKE_SIZE
elif self.direction == "down":
y += SNAKE_SIZE
elif self.direction == "left":
x -= SNAKE_SIZE
elif self.direction == "right":
x += SNAKE_SIZE
# 添加蛇头
self.rects.insert(0, pygame.Rect(x, y, SNAKE_SIZE, SNAKE_SIZE))
# 删除蛇尾
self.rects.pop()
def draw(self):
for rect in self.rects:
pygame.draw.rect(window, SNAKE_COLOR, rect)
def change_direction(self, direction):
if (self.direction == "up" and direction == "down") or \
(self.direction == "down" and direction == "up") or \
(self.direction == "left" and direction == "right") or \
(self.direction == "right" and direction == "left"):
return
self.direction = direction
def collide_with_food(self, food):
return self.rects[0].colliderect(food.rect)
def grow(self):
# 添加一个蛇尾
x = self.rects[-1].x
y = self.rects[-1].y
if self.direction == "up":
y += SNAKE_SIZE
elif self.direction == "down":
y -= SNAKE_SIZE
elif self.direction == "left":
x += SNAKE_SIZE
elif self.direction == "right":
x -= SNAKE_SIZE
self.rects.append(pygame.Rect(x, y, SNAKE_SIZE, SNAKE_SIZE))
def collide_with_self(self):
for i in range(1, len(self.rects)):
if self.rects[0].colliderect(self.rects[i]):
return True
return False
# 创建食物的类
class Food:
def __init__(self):
self.rect = pygame.Rect(random.randint(0, WINDOW_WIDTH - FOOD_SIZE),
random.randint(0, WINDOW_HEIGHT - FOOD_SIZE),
FOOD_SIZE, FOOD_SIZE)
def draw(self):
pygame.draw.rect(window, FOOD_COLOR, self.rect)
def move(self):
self.rect.x = random.randint(0, WINDOW_WIDTH - FOOD_SIZE)
self.rect.y = random.randint(0, WINDOW_HEIGHT - FOOD_SIZE)
# 创建游戏
def game():
snake = Snake()
food = Food()
score = 0
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.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.collide_with_food(food):
snake.grow()
food.move()
score += 1
# 绘制游戏界面
window.fill(0)
snake.draw()
food.draw()
pygame.display.update()
# 检查是否游戏结束
if snake.collide_with_self() or \
snake.rects[0].x < 0 or \
snake.rects[0].x >= WINDOW_WIDTH or \
snake.rects[0].y < 0 or \
snake.rects[0].y >= WINDOW_HEIGHT:
print("游戏结束,得分:%d" % score)
return
# 控制帧数
clock.tick(10)
# 启动游戏
game()
# 退出 pygame
pygame.quit()
```
阅读全文