写一个贪吃蛇游戏100行左右Python
时间: 2024-03-10 16:48:52 浏览: 14
好的,以下是一个简单的贪吃蛇游戏 Python 代码,仅供参考:
```python
import pygame
import random
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# 定义游戏区域大小和格子大小
WIDTH = 500
HEIGHT = 500
GRID_SIZE = 20
# 初始化 Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 定义字体
font = pygame.font.SysFont(None, 30)
# 定义函数,用于绘制文字
def draw_text(text, color, x, y):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
screen.blit(text_surface, text_rect)
# 定义函数,用于绘制网格
def draw_grid():
for x in range(0, WIDTH, GRID_SIZE):
pygame.draw.line(screen, WHITE, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, GRID_SIZE):
pygame.draw.line(screen, WHITE, (0, y), (WIDTH, y))
# 定义贪吃蛇类
class Snake():
def __init__(self):
self.body = [(WIDTH // 2, HEIGHT // 2)]
self.direction = random.choice(["up", "down", "left", "right"])
self.add_block = False
def move(self):
x, y = self.body[0]
if self.direction == "up":
y -= GRID_SIZE
elif self.direction == "down":
y += GRID_SIZE
elif self.direction == "left":
x -= GRID_SIZE
elif self.direction == "right":
x += GRID_SIZE
self.body.insert(0, (x, y))
if not self.add_block:
self.body.pop()
else:
self.add_block = False
def change_direction(self, direction):
if direction == "up" and self.direction != "down":
self.direction = "up"
elif direction == "down" and self.direction != "up":
self.direction = "down"
elif direction == "left" and self.direction != "right":
self.direction = "left"
elif direction == "right" and self.direction != "left":
self.direction = "right"
def draw(self):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, (x, y, GRID_SIZE, GRID_SIZE))
# 定义食物类
class Food():
def __init__(self):
self.x = random.randrange(0, WIDTH, GRID_SIZE)
self.y = random.randrange(0, HEIGHT, GRID_SIZE)
def draw(self):
pygame.draw.rect(screen, RED, (self.x, self.y, GRID_SIZE, GRID_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:
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.body[0] == (food.x, food.y):
snake.add_block = True
food = Food()
# 判断是否游戏结束
if snake.body[0][0] < 0 or snake.body[0][0] >= WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= HEIGHT:
running = False
for x, y in snake.body[1:]:
if snake.body[0] == (x, y):
running = False
# 绘制游戏界面
screen.fill(BLACK)
draw_grid()
snake.draw()
food.draw()
draw_text("Score: {}".format(len(snake.body)-1), YELLOW, WIDTH // 2, 20)
pygame.display.flip()
# 退出 Pygame
pygame.quit()
```
这个游戏还有很多可以改进的地方,比如增加难度、添加音效等等,不过这个代码已经足够实现一个简单的贪吃蛇游戏了。