使用python编写贪吃蛇小游戏
时间: 2023-07-20 21:43:22 浏览: 55
好的,下面是一个简单的贪吃蛇小游戏的代码实现:
```python
import pygame
import random
# 定义常量
WIDTH = 400
HEIGHT = 400
BLOCK_SIZE = 20
# 初始化pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义字体
FONT = pygame.font.SysFont(None, 30)
# 定义Snake类
class Snake:
def __init__(self):
self.body = [(WIDTH // 2, HEIGHT // 2)]
self.direction = random.choice(["left", "right", "up", "down"])
self.score = 0
def move(self):
if 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])
elif 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)
self.body.insert(0, new_head)
self.body.pop()
def change_direction(self, new_direction):
if new_direction == "left" and self.direction != "right":
self.direction = new_direction
elif new_direction == "right" and self.direction != "left":
self.direction = new_direction
elif new_direction == "up" and self.direction != "down":
self.direction = new_direction
elif new_direction == "down" and self.direction != "up":
self.direction = new_direction
def draw(self):
for block in self.body:
pygame.draw.rect(window, GREEN, (block[0], block[1], BLOCK_SIZE, BLOCK_SIZE))
def eat(self, food):
if self.body[0] == food:
self.body.append(self.body[-1])
self.score += 1
return True
return False
# 定义Food类
class Food:
def __init__(self):
self.position = (random.randint(0, WIDTH // BLOCK_SIZE - 1) * BLOCK_SIZE,
random.randint(0, HEIGHT // BLOCK_SIZE - 1) * BLOCK_SIZE)
def draw(self):
pygame.draw.rect(window, RED, (self.position[0], self.position[1], BLOCK_SIZE, BLOCK_SIZE))
# 创建Snake和Food对象
snake = Snake()
food = Food()
# 定义游戏主循环
clock = pygame.time.Clock()
game_over = False
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake.change_direction("left")
elif event.key == pygame.K_RIGHT:
snake.change_direction("right")
elif event.key == pygame.K_UP:
snake.change_direction("up")
elif event.key == pygame.K_DOWN:
snake.change_direction("down")
# 移动Snake
snake.move()
# 判断是否吃到Food
if snake.eat(food.position):
food = Food()
# 绘制游戏界面
window.fill(BLACK)
snake.draw()
food.draw()
score_text = FONT.render("Score: {}".format(snake.score), True, WHITE)
window.blit(score_text, (10, 10))
pygame.display.update()
# 判断是否碰到边界或自身
if snake.body[0][0] < 0 or snake.body[0][0] >= WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= HEIGHT:
game_over = True
for block in snake.body[1:]:
if block == snake.body[0]:
game_over = True
# 控制游戏速度
clock.tick(10)
# 退出游戏
pygame.quit()
```
运行以上代码,即可开始游戏。玩家可以使用键盘上的方向键来控制贪吃蛇的移动方向,吃到食物后,贪吃蛇的长度会增加,得分也会增加。如果贪吃蛇碰到边界或自身,则游戏结束。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)