找回密码
 中文实名注册
搜索
查看: 57|回复: 0

汽车

[复制链接]

29

主题

97

回帖

1132

积分

金牌会员

积分
1132
发表于 2025-10-2 10:31:27 | 显示全部楼层 |阅读模式
import pygame
import sys
import random

# 初始化 Pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("极速赛车")

# 颜色定义
SKY_BLUE = (135, 206, 235)
ROAD_GRAY = (100, 100, 100)
ROAD_LINE = (255, 255, 255)  # 白色虚线
RED = (255, 0, 0)
GREEN = (0, 180, 0)
BLUE = (0, 120, 255)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 游戏参数
clock = pygame.time.Clock()
FPS = 60
score = 0
game_over = False
scroll_speed = 5
road_scroll = 0
font = pygame.font.SysFont(None, 36)

class PlayerCar:
    def __init__(self):
        self.width = 50
        self.height = 90
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - self.height - 20
        self.speed = 8
        self.color = RED

    def draw(self):
        # 绘制车身
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height), border_radius=10)
        pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height), 2, border_radius=10)

        # 绘制车窗
        pygame.draw.rect(screen, (200, 230, 255), (self.x + 5, self.y + 10, self.width - 10, 25), border_radius=5)
        pygame.draw.rect(screen, (200, 230, 255), (self.x + 5, self.y + 45, self.width - 10, 20), border_radius=5)

        # 绘制车轮
        pygame.draw.circle(screen, BLACK, (self.x + 10, self.y + self.height - 15), 10)
        pygame.draw.circle(screen, BLACK, (self.x + self.width - 10, self.y + self.height - 15), 10)

    def move(self, keys):
        if keys[pygame.K_LEFT] and self.x > 150:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width - 150:
            self.x += self.speed
        if keys[pygame.K_UP] and self.y > HEIGHT // 2:
            self.y -= self.speed
        if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height - 10:
            self.y += self.speed

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

class Obstacle:
    def __init__(self):
        self.width = random.randint(40, 80)
        self.height = random.randint(40, 80)
        self.x = random.randint(150, WIDTH - 150 - self.width)
        self.y = -self.height
        self.speed = random.randint(3, 7)
        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))

    def update(self):
        self.y += self.speed
        return self.y > HEIGHT

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height), border_radius=8)
        pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height), 2, border_radius=8)

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

class Coin:
    def __init__(self):
        self.radius = 15
        self.x = random.randint(150, WIDTH - 150)
        self.y = -self.radius
        self.speed = random.randint(3, 5)

    def update(self):
        self.y += self.speed
        return self.y > HEIGHT

    def draw(self):
        pygame.draw.circle(screen, YELLOW, (self.x, self.y), self.radius)
        pygame.draw.circle(screen, BLACK, (self.x, self.y), self.radius, 2)
        pygame.draw.circle(screen, (200, 150, 0), (self.x, self.y), self.radius - 5)

    def get_rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius,
                          self.radius * 2, self.radius * 2)

# 创建玩家和障碍物列表
player = PlayerCar()
obstacles = []
coins = []
obstacle_timer = 0
coin_timer = 0

# 绘制道路
def draw_road():
    # 道路
    pygame.draw.rect(screen, ROAD_GRAY, (100, 0, WIDTH - 200, HEIGHT))

    # 道路边界
    pygame.draw.rect(screen, BLACK, (95, 0, 5, HEIGHT))
    pygame.draw.rect(screen, BLACK, (WIDTH - 100, 0, 5, HEIGHT))

    # 道路中间的虚线
    for i in range(0, HEIGHT, 40):
        pygame.draw.rect(screen, ROAD_LINE, (WIDTH // 2 - 5, (i + road_scroll) % 600, 10, 20))

# 绘制云朵
def draw_clouds():
    for i in range(5):
        x = (i * 200 + road_scroll // 2) % (WIDTH + 200) - 100
        pygame.draw.circle(screen, WHITE, (x, 80), 30)
        pygame.draw.circle(screen, WHITE, (x + 20, 70), 30)
        pygame.draw.circle(screen, WHITE, (x + 40, 80), 30)
        pygame.draw.circle(screen, WHITE, (x + 20, 90), 30)

# 绘制背景
def draw_background():
    # 天空
    screen.fill(SKY_BLUE)

    # 云朵
    draw_clouds()

    # 草地
    pygame.draw.rect(screen, GREEN, (0, HEIGHT - 20, WIDTH, 20))
    pygame.draw.rect(screen, (0, 160, 0), (0, HEIGHT - 20, WIDTH, 5))

    # 道路
    draw_road()

# 显示分数
def show_score():
    score_text = font.render(f"分数: {score}", True, BLACK)
    screen.blit(score_text, (20, 20))

    speed_text = font.render(f"速度: {scroll_speed} km/h", True, BLACK)
    screen.blit(speed_text, (20, 60))

# 游戏结束画面
def game_over_screen():
    overlay = pygame.Surface((WIDTH, HEIGHT))
    overlay.set_alpha(180)
    overlay.fill(BLACK)
    screen.blit(overlay, (0, 0))

    game_over_text = font.render("游戏结束!", True, RED)
    final_score = font.render(f"最终分数: {score}", True, WHITE)
    restart_text = font.render("按 R 键重新开始", True, WHITE)

    screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 60))
    screen.blit(final_score, (WIDTH // 2 - final_score.get_width() // 2, HEIGHT // 2))
    screen.blit(restart_text, (WIDTH // 2 - restart_text.get_width() // 2, HEIGHT // 2 + 60))

# 游戏主循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r and game_over:
                # 重置游戏
                game_over = False
                score = 0
                scroll_speed = 5
                player = PlayerCar()
                obstacles = []
                coins = []

    if not game_over:
        # 获取按键状态
        keys = pygame.key.get_pressed()

        # 移动玩家
        player.move(keys)

        # 更新道路滚动
        road_scroll = (road_scroll + scroll_speed) % 40

        # 生成障碍物
        obstacle_timer += 1
        if obstacle_timer > 60 - min(40, score // 10):
            obstacles.append(Obstacle())
            obstacle_timer = 0

        # 生成金币
        coin_timer += 1
        if coin_timer > 90 - min(50, score // 5):
            coins.append(Coin())
            coin_timer = 0

        # 更新障碍物
        for obstacle in obstacles[:]:
            if obstacle.update():
                obstacles.remove(obstacle)
                score += 1
                scroll_speed = min(20, 5 + score // 15)

            # 碰撞检测
            if player.get_rect().colliderect(obstacle.get_rect()):
                game_over = True

        # 更新金币
        for coin in coins[:]:
            if coin.update():
                coins.remove(coin)

            # 收集金币
            if player.get_rect().colliderect(coin.get_rect()):
                coins.remove(coin)
                score += 5

    # 绘制背景
    draw_background()

    # 绘制金币
    for coin in coins:
        coin.draw()

    # 绘制障碍物
    for obstacle in obstacles:
        obstacle.draw()

    # 绘制玩家
    player.draw()

    # 显示分数
    show_score()

    # 游戏结束画面
    if game_over:
        game_over_screen()

    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    clock.tick(FPS)


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表