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

跑酷游戏(豪华版)

[复制链接]

17

主题

21

回帖

1622

积分

金牌会员

积分
1622
发表于 2025-7-19 15:17:53 | 显示全部楼层 |阅读模式
购买主题 已有 3 人购买  本主题需向作者支付 3 金钱 才能浏览
回复

使用道具 举报

8

主题

19

回帖

1613

积分

金牌会员

积分
1613
发表于 2025-7-26 15:17:40 | 显示全部楼层
import pygame

import random

import sys



# 初始化 Pygame

pygame.init()



# 游戏常量

SCREEN_WIDTH = 800

SCREEN_HEIGHT = 400

GROUND_HEIGHT = 50

GRAVITY = 1

JUMP_STRENGTH = 20

GAME_SPEED = 5

OBSTACLE_FREQUENCY = 1500  # 毫秒



# 颜色定义

SKY_BLUE = (135, 206, 235)

GROUND_COLOR = (101, 67, 33)

WHITE = (255, 255, 255)

RED = (255, 0, 0)

BLACK = (0, 0, 0)



# 创建游戏窗口

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("跑酷游戏")



# 游戏时钟

clock = pygame.time.Clock()



# 字体

font = pygame.font.SysFont('Arial', 30)

big_font = pygame.font.SysFont('Arial', 50, bold=True)





class Player:

    def __init__(self):

        self.width = 40

        self.height = 60

        self.x = 100

        self.y = SCREEN_HEIGHT - GROUND_HEIGHT - self.height

        self.velocity_y = 0

        self.is_jumping = False

        self.color = (70, 130, 180)  # 钢蓝色



    def jump(self):

        if not self.is_jumping:

            self.velocity_y = -JUMP_STRENGTH

            self.is_jumping = True



    def update(self):

        # 应用重力

        self.velocity_y += GRAVITY

        self.y += self.velocity_y



        # 检查地面碰撞

        ground_level = SCREEN_HEIGHT - GROUND_HEIGHT - self.height

        if self.y > ground_level:

            self.y = ground_level

            self.velocity_y = 0

            self.is_jumping = False



    def draw(self, surface):

        pygame.draw.rect(surface, self.color,

                         (self.x, self.y, self.width, self.height))





class Obstacle:

    def __init__(self, x):

        self.width = random.randint(20, 40)

        self.height = random.randint(30, 70)

        self.x = x

        self.y = SCREEN_HEIGHT - GROUND_HEIGHT - self.height

        self.color = RED



    def update(self):

        self.x -= GAME_SPEED



    def draw(self, surface):

        pygame.draw.rect(surface, self.color,

                         (self.x, self.y, self.width, self.height))



    def is_off_screen(self):

        return self.x + self.width < 0





def check_collision(player, obstacle):

    player_rect = pygame.Rect(player.x, player.y, player.width, player.height)

    obstacle_rect = pygame.Rect(

        obstacle.x, obstacle.y, obstacle.width, obstacle.height)

    return player_rect.colliderect(obstacle_rect)





def draw_ground(surface):

    pygame.draw.rect(surface, GROUND_COLOR, (0, SCREEN_HEIGHT -

                     GROUND_HEIGHT, SCREEN_WIDTH, GROUND_HEIGHT))





def game_over_screen(surface, score):

    surface.fill(SKY_BLUE)



    game_over_text = big_font.render("游戏结束!", True, RED)

    score_text = font.render(f"得分: {score}", True, BLACK)

    restart_text = font.render("按R键重新开始", True, BLACK)

    quit_text = font.render("按Q键退出", True, BLACK)



    surface.blit(game_over_text, (SCREEN_WIDTH // 2 -

                 game_over_text.get_width() // 2, 80))

    surface.blit(score_text, (SCREEN_WIDTH // 2 -

                 score_text.get_width() // 2, 150))

    surface.blit(restart_text, (SCREEN_WIDTH // 2 -

                 restart_text.get_width() // 2, 220))

    surface.blit(quit_text, (SCREEN_WIDTH // 2 -

                 quit_text.get_width() // 2, 270))



    pygame.display.update()



    waiting = True

    while waiting:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                return False

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_r:

                    return True

                elif event.key == pygame.K_q:

                    return False

    return True





def main():

    player = Player()

    obstacles = []

    obstacle_timer = pygame.time.get_ticks()

    score = 0

    game_over = False



    running = True

    while running:

        # 处理事件

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                running = False

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:

                    player.jump()

                if event.key == pygame.K_ESCAPE:

                    running = False



        if not game_over:

            # 更新游戏状态

            player.update()



            # 生成新障碍物

            current_time = pygame.time.get_ticks()

            if current_time - obstacle_timer > OBSTACLE_FREQUENCY:

                obstacles.append(Obstacle(SCREEN_WIDTH))

                obstacle_timer = current_time



            # 更新障碍物

            for obstacle in obstacles[:]:

                obstacle.update()

                if obstacle.is_off_screen():

                    obstacles.remove(obstacle)

                    score += 1



            # 检测碰撞

            for obstacle in obstacles:

                if check_collision(player, obstacle):

                    game_over = True



            # 绘制背景

            screen.fill(SKY_BLUE)



            # 绘制地面

            draw_ground(screen)



            # 绘制障碍物

            for obstacle in obstacles:

                obstacle.draw(screen)



            # 绘制玩家

            player.draw(screen)



            # 绘制分数

            score_text = font.render(f"得分: {score}", True, BLACK)

            screen.blit(score_text, (20, 20))



        if game_over:

            if game_over_screen(screen, score):

                # 重置游戏

                player = Player()

                obstacles = []

                obstacle_timer = pygame.time.get_ticks()

                score = 0

                game_over = False

            else:

                running = False



        pygame.display.update()

        clock.tick(60)



    pygame.quit()

    sys.exit()
回复

使用道具 举报

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

本版积分规则

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