写一个贪吃蛇的代码
时间: 2023-08-28 15:08:46 浏览: 34
以下是一个简单的贪吃蛇游戏的代码,使用 Python 和 Pygame 实现:
```python
import pygame
import random
# 游戏窗口大小
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500
# 贪吃蛇头部大小和速度
SNAKE_SIZE = 10
SNAKE_SPEED = 10
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 初始化 Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 创建时钟对象
clock = pygame.time.Clock()
# 定义贪吃蛇的类
class Snake:
def __init__(self):
self.body = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)]
self.direction = random.choice(["up", "down", "left", "right"])
self.score = 0
def move(self):
x, y = self.body[0]
if self.direction == "up":
self.body.insert(0, (x, y - SNAKE_SIZE))
elif self.direction == "down":
self.body.insert(0, (x, y + SNAKE_SIZE))
elif self.direction == "left":
self.body.insert(0, (x - SNAKE_SIZE, y))
elif self.direction == "right":
self.body.insert(0, (x + SNAKE_SIZE, y))
self.body.pop()
def draw(self):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, (x, y, SNAKE_SIZE, SNAKE_SIZE))
def eat(self):
x, y = self.body[0]
if x == food.x and y == food.y:
self.score += 1
self.body.append((x, y))
return True
return False
def collide(self):
x, y = self.body[0]
if x < 0 or x > SCREEN_WIDTH - SNAKE_SIZE or y < 0 or y > SCREEN_HEIGHT - SNAKE_SIZE:
return True
for i in range(1, len(self.body)):
if x == self.body[i][0] and y == self.body[i][1]:
return True
return False
# 定义食物的类
class Food:
def __init__(self):
self.x = random.randrange(0, SCREEN_WIDTH - SNAKE_SIZE, SNAKE_SIZE)
self.y = random.randrange(0, SCREEN_HEIGHT - SNAKE_SIZE, SNAKE_SIZE)
def draw(self):
pygame.draw.rect(screen, RED, (self.x, self.y, SNAKE_SIZE, SNAKE_SIZE))
# 创建贪吃蛇和食物对象
snake = Snake()
food = Food()
# 游戏主循环
running = True
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 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()
# 判断是否吃到食物
if snake.eat():
food = Food()
# 绘制游戏界面
screen.fill(BLACK)
snake.draw()
food.draw()
# 判断贪吃蛇是否撞墙或自己
if snake.collide():
running = False
# 显示分数
font = pygame.font.SysFont(None, 30)
text = font.render("Score: {}".format(snake.score), True, WHITE)
screen.blit(text, (10, 10))
# 更新屏幕
pygame.display.update()
# 控制游戏速度
clock.tick(SNAKE_SPEED)
# 退出 Pygame
pygame.quit()
```
此代码实现了一个简单的贪吃蛇游戏,你可以在这个基础上进行修改和扩展,添加更多的功能和玩法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)