Python贪吃蛇游戏代码:10个关卡,限时挑战
以下是一个简单的贪吃蛇游戏代码示例,根据您的需求进行了相应的修改:
import pygame
import random
# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 定义每个方块的大小和地图的大小
BLOCK_SIZE = 20
MAP_SIZE = (30, 20)
# 定义关卡和速度的相关参数
LEVELS = 10
TIME_LIMIT = 60
SNAKE_SPEED = 5
SNAKE_SPEED_INCREMENT = 0.5
# 初始化游戏
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((MAP_SIZE[0] * BLOCK_SIZE, MAP_SIZE[1] * BLOCK_SIZE))
pygame.display.set_caption('贪吃蛇游戏')
# 定义贪吃蛇类
class Snake:
    def __init__(self):
        self.head_pos = [MAP_SIZE[0] // 2, MAP_SIZE[1] // 2]
        self.body = [self.head_pos, [self.head_pos[0] - 1, self.head_pos[1]], [self.head_pos[0] - 2, self.head_pos[1]]]
        self.direction = 'RIGHT'
        
    def move(self):
        if self.direction == 'UP':
            self.head_pos[1] -= 1
        elif self.direction == 'DOWN':
            self.head_pos[1] += 1
        elif self.direction == 'LEFT':
            self.head_pos[0] -= 1
        elif self.direction == 'RIGHT':
            self.head_pos[0] += 1
        
        self.body.insert(0, list(self.head_pos))
        
    def change_direction(self, new_direction):
        if new_direction == 'UP' and self.direction != 'DOWN':
            self.direction = 'UP'
        elif new_direction == 'DOWN' and self.direction != 'UP':
            self.direction = 'DOWN'
        elif new_direction == 'LEFT' and self.direction != 'RIGHT':
            self.direction = 'LEFT'
        elif new_direction == 'RIGHT' and self.direction != 'LEFT':
            self.direction = 'RIGHT'
            
    def check_collision(self):
        if self.head_pos[0] < 0 or self.head_pos[0] >= MAP_SIZE[0] or self.head_pos[1] < 0 or self.head_pos[1] >= MAP_SIZE[1]:
            return True
        if self.head_pos in self.body[1:]:
            return True
        return False
    
    def check_eat(self, egg_pos):
        if self.head_pos == egg_pos:
            return True
        return False
    
    def grow(self):
        self.body.append(list(self.head_pos))
        
    def draw(self):
        for segment in self.body:
            pygame.draw.rect(screen, GREEN, (segment[0] * BLOCK_SIZE, segment[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
            pygame.draw.rect(screen, BLACK, (segment[0] * BLOCK_SIZE, segment[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 定义蛋类
class Egg:
    def __init__(self):
        self.pos = self.generate_pos()
        
    def generate_pos(self):
        return [random.randint(0, MAP_SIZE[0] - 1), random.randint(0, MAP_SIZE[1] - 1)]
    
    def draw(self):
        pygame.draw.rect(screen, RED, (self.pos[0] * BLOCK_SIZE, self.pos[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
        pygame.draw.rect(screen, BLACK, (self.pos[0] * BLOCK_SIZE, self.pos[1] * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 定义计时器类
class Timer:
    def __init__(self, time_limit):
        self.time_limit = time_limit
        self.start_time = pygame.time.get_ticks()
        
    def get_time_left(self):
        elapsed_time = pygame.time.get_ticks() - self.start_time
        time_left = self.time_limit - (elapsed_time // 1000)
        return time_left
# 初始化贪吃蛇、蛋和计时器
snake = Snake()
egg = Egg()
timer = Timer(TIME_LIMIT)
# 游戏主循环
running = True
game_over = False
current_level = 1
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')
    
    if not game_over:
        screen.fill(WHITE)
        
        snake.move()
        if snake.check_collision():
            game_over = True
        
        if snake.check_eat(egg.pos):
            snake.grow()
            egg.pos = egg.generate_pos()
        
        if timer.get_time_left() <= 0:
            current_level += 1
            if current_level > LEVELS:
                game_over = True
            else:
                snake = Snake()
                egg.pos = egg.generate_pos()
                timer = Timer(TIME_LIMIT)
        
        snake.draw()
        egg.draw()
        
        pygame.display.update()
        
        # 控制贪吃蛇的速度
        pygame.time.delay(int(1000 / (SNAKE_SPEED + current_level * SNAKE_SPEED_INCREMENT)))
pygame.quit()
请确保您已安装Pygame库。您可以将以上代码保存为Python文件并运行,将会启动一个简单的贪吃蛇游戏。游戏的关卡数量为10,每个关卡限时1分钟,贪吃蛇的速度会随着倒计时的减少而逐渐加快。贪吃蛇会在地图内随机位置生成蛋,吃到蛋后会增长身体长度。
游戏玩法:
- 使用方向键控制贪吃蛇移动
- 吃到蛋后贪吃蛇会增长
- 避免撞到地图边界和自身
- 每过一分钟进入下一关,难度增加
游戏目标:
- 尽可能吃到更多的蛋
- 通关所有关卡
希望您能享受这个游戏!
 
原文地址: https://www.cveoy.top/t/topic/hXO 著作权归作者所有。请勿转载和采集!