📜  dodge (1)

📅  最后修改于: 2023-12-03 14:40:51.333000             🧑  作者: Mango

Dodge游戏

简介

Dodge是一款程序员小游戏,玩家需要控制一个圆点在屏幕上躲避随机生成的障碍物,以获得更高的分数。

游戏规则
  1. 玩家控制一个圆点在屏幕上移动,避开障碍物,获得尽可能高的分数。
  2. 游戏难度会逐渐升级,障碍物数量和速度会增加。
  3. 如果圆点碰到了障碍物,则游戏结束。
  4. 游戏结束后,玩家可以选择重新开始游戏或退出游戏。
实现思路
  1. 使用 Python 编程语言实现游戏逻辑。
  2. 使用 Pygame 库实现游戏画面和交互。
  3. 随机生成障碍物和移动速度,实现游戏难度逐渐升级的功能。
  4. 实现碰撞检测,当圆点与障碍物重合时,游戏结束。
  5. 实现得分计算和保存最高分数,提供给玩家参考。
游戏截图

Dodge screenshot

代码片段
import pygame
import random

# 初始化 Pygame 库
pygame.init()

# 游戏窗口尺寸
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# 障碍物尺寸和速度范围
MIN_OBSTACLE_SIZE = 10
MAX_OBSTACLE_SIZE = 50
MIN_OBSTACLE_SPEED = 1
MAX_OBSTACLE_SPEED = 5

# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Dodge Game")

# 圆点初始位置和半径
player_x = WINDOW_WIDTH / 2
player_y = WINDOW_HEIGHT / 2
player_radius = 10

# 障碍物位置和速度
obstacles = []
for i in range(5):
    obstacle_size = random.randint(MIN_OBSTACLE_SIZE, MAX_OBSTACLE_SIZE)
    obstacle_x = random.randint(0, WINDOW_WIDTH - obstacle_size)
    obstacle_y = random.randint(0, WINDOW_HEIGHT - obstacle_size)
    obstacle_speed = random.randint(MIN_OBSTACLE_SPEED, MAX_OBSTACLE_SPEED)
    obstacles.append([obstacle_x, obstacle_y, obstacle_size, obstacle_speed])

# 得分和最高分数
score = 0
high_score = 0

# 游戏循环
running = True
while running:
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 移动圆点
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > player_radius:
        player_x -= 5
    elif keys[pygame.K_RIGHT] and player_x < WINDOW_WIDTH - player_radius:
        player_x += 5
    elif keys[pygame.K_UP] and player_y > player_radius:
        player_y -= 5
    elif keys[pygame.K_DOWN] and player_y < WINDOW_HEIGHT - player_radius:
        player_y += 5

    # 移动障碍物
    for obstacle in obstacles:
        obstacle[1] += obstacle[3]
        if obstacle[1] > WINDOW_HEIGHT:
            obstacle[1] = 0 - obstacle[2]
            obstacle[0] = random.randint(0, WINDOW_WIDTH - obstacle[2])
            obstacle[3] = random.randint(MIN_OBSTACLE_SPEED, MAX_OBSTACLE_SPEED)

        # 碰撞检测
        if (obstacle[0] + obstacle[2] > player_x - player_radius and obstacle[0] < player_x + player_radius and
                obstacle[1] + obstacle[2] > player_y - player_radius and obstacle[1] < player_y + player_radius):
            if score > high_score:
                high_score = score
            score = 0
            player_x = WINDOW_WIDTH / 2
            player_y = WINDOW_HEIGHT / 2
            for obstacle in obstacles:
                obstacle[1] = 0 - obstacle[2]
                obstacle[0] = random.randint(0, WINDOW_WIDTH - obstacle[2])
                obstacle[3] = random.randint(MIN_OBSTACLE_SPEED, MAX_OBSTACLE_SPEED)

        # 得分计算
        if obstacle[1] > player_y:
            score += 1

    # 绘制画面
    screen.fill(BLACK)
    pygame.draw.circle(screen, RED, (int(player_x), int(player_y)), player_radius)
    for obstacle in obstacles:
        pygame.draw.rect(screen, WHITE, (obstacle[0], obstacle[1], obstacle[2], obstacle[2]))

    # 绘制得分和最高分数
    font = pygame.font.Font(None, 36)
    score_text = font.render("Score: " + str(score), True, WHITE)
    high_score_text = font.render("High Score: " + str(high_score), True, WHITE)
    screen.blit(score_text, (10, 10))
    screen.blit(high_score_text, (WINDOW_WIDTH - high_score_text.get_width() - 10, 10))

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

# 退出 Pygame 库
pygame.quit()

以上是 Dodge 游戏的实现代码。游戏中使用 Pygame 库实现了游戏画面和交互,使用 Python 编程语言实现了游戏逻辑。玩家需要控制一个圆点在屏幕上躲避随机生成的障碍物,以获得更高的分数。实现碰撞检测和得分计算,玩家不断躲避障碍物并获得得分。游戏难度逐渐升级,障碍物数量和速度会增加,让玩家感受到挑战和快乐。