用Python编写简单的贪吃蛇小游戏
时间: 2023-12-07 16:05:00 浏览: 33
好的,下面是一个简单的贪吃蛇小游戏的Python代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置游戏窗口大小和标题
size = (400, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇小游戏")
# 初始化游戏参数
block_size = 10
snake_speed = 20
# 定义字体
font = pygame.font.SysFont('arial', 25)
# 定义蛇的类
class Snake:
def __init__(self):
self.length = 1
self.positions = [(random.randint(0, 39) * 10, random.randint(0, 39) * 10)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
self.color = GREEN
def get_head_position(self):
return self.positions[0]
def turn(self, point):
if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction:
return
else:
self.direction = point
def move(self):
cur = self.get_head_position()
x, y = self.direction
new = ((cur[0] + (x * block_size)), (cur[1] + (y * block_size)))
if new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def reset(self):
self.length = 1
self.positions = [(random.randint(0, 39) * 10, random.randint(0, 39) * 10)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
def draw(self, surface):
for p in self.positions:
r = pygame.Rect((p[0], p[1]), (block_size, block_size))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, BLACK, r, 1)
# 定义食物的类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = RED
self.randomize_position()
def randomize_position(self):
self.position = (random.randint(0, 39) * 10, random.randint(0, 39) * 10)
def draw(self, surface):
r = pygame.Rect((self.position[0], self.position[1]), (block_size, block_size))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, BLACK, r, 1)
# 定义游戏结束的函数
def game_over():
screen.fill(WHITE)
text = font.render("Game Over!", True, BLACK)
text_rect = text.get_rect()
text_x = screen.get_width() / 2 - text_rect.width / 2
text_y = screen.get_height() / 2 - text_rect.height / 2
screen.blit(text, [text_x, text_y])
pygame.display.flip()
pygame.time.wait(2000)
# 定义方向常量
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
# 创建蛇和食物对象
snake = Snake()
food = Food()
# 设置游戏循环
clock = pygame.time.Clock()
while True:
clock.tick(snake_speed)
# 处理事件
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.turn(UP)
elif event.key == pygame.K_DOWN:
snake.turn(DOWN)
elif event.key == pygame.K_LEFT:
snake.turn(LEFT)
elif event.key == pygame.K_RIGHT:
snake.turn(RIGHT)
# 移动蛇
snake.move()
# 判断是否吃到了食物
if snake.get_head_position() == food.position:
snake.length += 1
food.randomize_position()
# 绘制背景和物体
screen.fill(WHITE)
snake.draw(screen)
food.draw(screen)
# 判断是否游戏结束
if snake.get_head_position()[0] < 0 or snake.get_head_position()[0] > 390 or snake.get_head_position()[1] < 0 or snake.get_head_position()[1] > 390:
game_over()
snake.reset()
# 更新屏幕
pygame.display.update()
```
这个小游戏使用了pygame库来实现游戏窗口、贪吃蛇和食物的绘制,以及处理用户输入事件。游戏中用到了Snake和Food两个类来表示贪吃蛇和食物,使用了常量来表示蛇的运动方向。在游戏循环中,每次先处理事件,再移动蛇,判断是否吃到了食物,绘制背景和物体,最后判断是否游戏结束并更新屏幕。
相关推荐
![](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)
![](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)