Python:游戏:贪吃蛇原理及代码实现
时间: 2024-02-03 10:03:06 浏览: 190
Python贪吃蛇游戏编写代码
游戏原理:
贪吃蛇是一款经典的游戏,游戏中的玩家需要操控一条蛇去吃食物,同时避免撞到游戏界面的边缘或者自己的身体。每吃到一块食物,蛇的身体就会变长一截,游戏难度也会逐渐加大。当蛇头和蛇身相撞或者撞到边缘时,游戏结束。
代码实现:
下面是一个简单的贪吃蛇游戏实现,使用 Python 的 Pygame 库实现,具体代码如下:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义常量
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 480
CELL_SIZE = 20
CELL_WIDTH = SCREEN_WIDTH // CELL_SIZE
CELL_HEIGHT = SCREEN_HEIGHT // CELL_SIZE
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
FPS = 10
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.body = [(CELL_WIDTH // 2, CELL_HEIGHT // 2)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
# 移动
def move(self):
head = self.body[0]
x, y = self.direction
new_head = (head[0] + x, head[1] + y)
self.body.insert(0, new_head)
self.body.pop()
# 改变方向
def change_direction(self, direction):
if direction[0] + self.direction[0] != 0 and direction[1] + self.direction[1] != 0:
self.direction = direction
# 吃食物
def eat_food(self):
head = self.body[0]
x, y = self.direction
new_head = (head[0] + x, head[1] + y)
self.body.insert(0, new_head)
# 判断是否撞到自己或者墙壁
def is_dead(self):
head = self.body[0]
if head[0] < 0 or head[0] >= CELL_WIDTH or head[1] < 0 or head[1] >= CELL_HEIGHT:
return True
for i in range(1, len(self.body)):
if head == self.body[i]:
return True
return False
# 定义食物类
class Food:
def __init__(self):
self.position = self.generate_position()
# 生成位置
def generate_position(self):
x = random.randint(0, CELL_WIDTH - 1)
y = random.randint(0, CELL_HEIGHT - 1)
return (x, y)
# 重新生成位置
def regenerate_position(self):
self.position = self.generate_position()
# 绘制网格
def draw_grid():
for x in range(0, SCREEN_WIDTH, CELL_SIZE):
pygame.draw.line(screen, BLACK, (x, 0), (x, SCREEN_HEIGHT))
for y in range(0, SCREEN_HEIGHT, CELL_SIZE):
pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y))
# 绘制贪吃蛇
def draw_snake(snake):
for cell in snake.body:
x, y = cell
pygame.draw.rect(screen, WHITE, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# 绘制食物
def draw_food(food):
x, y = food.position
pygame.draw.rect(screen, RED, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# 主函数
def main():
# 创建贪吃蛇和食物
snake = Snake()
food = Food()
# 创建时钟
clock = pygame.time.Clock()
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
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.position:
snake.eat_food()
food.regenerate_position()
# 绘制游戏界面
screen.fill(BLACK)
draw_grid()
draw_snake(snake)
draw_food(food)
pygame.display.update()
# 判断游戏是否结束
if snake.is_dead():
pygame.quit()
exit()
# 控制游戏帧率
clock.tick(FPS)
if __name__ == '__main__':
main()
```
运行代码后,即可开始游戏。通过方向键控制贪吃蛇的移动,吃到食物后贪吃蛇的身体会变长,撞到自己或者墙壁就会游戏结束。
阅读全文