用python制作一个贪吃蛇小游戏
时间: 2024-05-13 13:17:53 浏览: 9
好的,制作贪吃蛇小游戏需要用到 Pygame 这个库,首先需要安装它。
安装 Pygame:
```
pip install pygame
```
接下来就可以开始编写代码了,下面是贪吃蛇小游戏的代码,注释中有详细的说明:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义窗口大小和标题
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
WINDOW_TITLE = 'Snake Game'
WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption(WINDOW_TITLE)
# 定义颜色
WHITE_COLOR = (255, 255, 255)
BLACK_COLOR = (0, 0, 0)
RED_COLOR = (255, 0, 0)
GREEN_COLOR = (0, 255, 0)
# 定义贪吃蛇的属性
SNAKE_SIZE = 10
SNAKE_SPEED = 15
# 定义食物的属性
FOOD_SIZE = 10
FOOD_COLOR = RED_COLOR
# 定义字体
FONT_SIZE = 30
FONT = pygame.font.SysFont('Arial', FONT_SIZE)
# 定义方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
# 定义游戏结束标志
GAME_OVER = False
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.length = 1
self.positions = [(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
self.color = GREEN_COLOR
self.score = 0
# 贪吃蛇移动
def move(self):
x, y = self.positions[0]
if self.direction == UP:
y -= SNAKE_SPEED
elif self.direction == DOWN:
y += SNAKE_SPEED
elif self.direction == LEFT:
x -= SNAKE_SPEED
elif self.direction == RIGHT:
x += SNAKE_SPEED
self.positions = [(x, y)] + self.positions[:-1]
# 贪吃蛇改变方向
def change_direction(self, direction):
if self.direction == UP and direction == DOWN:
return
if self.direction == DOWN and direction == UP:
return
if self.direction == LEFT and direction == RIGHT:
return
if self.direction == RIGHT and direction == LEFT:
return
self.direction = direction
# 贪吃蛇增加长度
def grow(self):
x, y = self.positions[0]
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.length += 1
self.score += 1
self.positions = [(x, y)] + self.positions
# 贪吃蛇绘制
def draw(self):
for position in self.positions:
x, y = position
pygame.draw.rect(WINDOW, self.color, (x, y, SNAKE_SIZE, SNAKE_SIZE))
# 定义食物类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = FOOD_COLOR
self.randomize_position()
# 食物随机位置
def randomize_position(self):
x = random.randint(0, WINDOW_WIDTH - FOOD_SIZE)
y = random.randint(0, WINDOW_HEIGHT - FOOD_SIZE)
self.position = (x, y)
# 食物绘制
def draw(self):
x, y = self.position
pygame.draw.rect(WINDOW, self.color, (x, y, FOOD_SIZE, FOOD_SIZE))
# 定义游戏主循环
def main():
global GAME_OVER
# 创建贪吃蛇和食物对象
snake = Snake()
food = Food()
# 定义游戏时钟
clock = pygame.time.Clock()
# 游戏循环
while not GAME_OVER:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
GAME_OVER = True
break
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.positions[0] == food.position:
snake.grow()
food.randomize_position()
# 判断是否游戏结束
for position in snake.positions[1:]:
if snake.positions[0] == position:
GAME_OVER = True
break
if snake.positions[0][0] < 0 or snake.positions[0][0] > WINDOW_WIDTH - SNAKE_SIZE:
GAME_OVER = True
break
if snake.positions[0][1] < 0 or snake.positions[0][1] > WINDOW_HEIGHT - SNAKE_SIZE:
GAME_OVER = True
break
# 绘制游戏界面
WINDOW.fill(BLACK_COLOR)
snake.draw()
food.draw()
score_text = FONT.render('Score: {}'.format(snake.score), True, WHITE_COLOR)
WINDOW.blit(score_text, (10, 10))
pygame.display.update()
# 控制游戏帧率
clock.tick(30)
# 游戏结束
game_over_text = FONT.render('Game Over', True, WHITE_COLOR)
game_over_rect = game_over_text.get_rect()
game_over_rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
WINDOW.fill(BLACK_COLOR)
WINDOW.blit(game_over_text, game_over_rect)
pygame.display.update()
pygame.time.wait(2000)
# 退出 Pygame
pygame.quit()
# 启动游戏
if __name__ == '__main__':
main()
```
运行程序后即可开始游戏,按上下左右键控制贪吃蛇的方向,吃到食物后贪吃蛇会变长,碰到边界或自己身体时游戏结束。