以下是实现上述需求的Python代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('开箱实验')

# 加载图片
box_image = pygame.image.load('D:\1开箱实验\开箱实验_8平衡\正式实验\zsPic\box.png')
gold_image = pygame.image.load('D:\1开箱实验\开箱实验_8平衡\正式实验\zsPic\gold.png')
bomb_image = pygame.image.load('D:\1开箱实验\开箱实验_8平衡\正式实验\zsPic\bomb3.png')

# 缩小图片
box_image = pygame.transform.scale(box_image, (int(box_image.get_width() / 12), int(box_image.get_height() / 12)))
gold_image = pygame.transform.scale(gold_image, (int(gold_image.get_width() / 12), int(gold_image.get_height() / 12)))
bomb_image = pygame.transform.scale(bomb_image, (int(bomb_image.get_width() / 12), int(bomb_image.get_height() / 12)))

# 箱子相关参数
box_width = box_image.get_width()
box_height = box_image.get_height()
box_spacing = 20
box_initial_x = (screen_width - (box_width * 8 + box_spacing * 7)) // 2
box_y = (screen_height - box_height) // 2

# 游戏相关参数
rounds = 5
gold_count = 0
game_over = False
bomb_positions = []

# 生成炸弹位置
for _ in range(rounds):
    bomb_positions.append(random.randint(0, 7))

# 游戏循环
running = True
current_round = 0

while running:
    # 设置背景颜色
    screen.fill((255, 255, 255))

    # 绘制箱子
    for i in range(8):
        x = box_initial_x + (box_width + box_spacing) * i
        if i == current_round and not game_over:
            screen.blit(box_image, (x, box_y))
        else:
            if i in bomb_positions[:current_round]:
                screen.blit(bomb_image, (x, box_y))
            else:
                screen.blit(gold_image, (x, box_y))

    # 更新屏幕
    pygame.display.flip()
    
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w and not game_over:
                if current_round in bomb_positions:
                    game_over = True
                else:
                    gold_count += 1
                current_round += 1
            elif event.key == pygame.K_p and not game_over:
                game_over = True
                current_round += 1

    # 判断游戏是否结束
    if current_round == rounds:
        running = False

# 打印结果
print('游戏结束!')
print('获得的金币数量:', gold_count)

# 退出pygame
pygame.quit()

注意事项:

  1. 请确保指定的图片路径是正确的。
  2. 需要安装pygame库。可以使用命令pip install pygame进行安装。
Python开箱游戏:随机炸弹,金币奖励

原文地址: https://www.cveoy.top/t/topic/o9bq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录