软件价值12-射箭游戏

射箭游戏,按空格键发射,打击移动靶,左上角显示成绩状态。

代码:

import pygame
import sys
import random

# 初始化Pygame
pygame.init()

# 设置窗口大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("射箭游戏")

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

# 设置靶子的参数
target_width = 50
target_height = 100
target_x = SCREEN_WIDTH - target_width - 20
target_y = SCREEN_HEIGHT // 2 - target_height // 2
target_speed = 0.1
target_hit = False
hit_count = 0
arrow_count = 0

# 设置箭的参数
arrow_width = 50
arrow_height = 10
arrow_x = 50
arrow_y = SCREEN_HEIGHT // 2 - arrow_height // 2
arrow_speed = 0.5
arrow_direction = "right"
arrow_fired = False

# 设置计时器
clock = pygame.time.Clock()
FPS = 60

# 初始化内置声音
hit_sound = pygame.mixer.Sound("sound/beep.mp3")
shoot_sound = pygame.mixer.Sound("sound/shoot.mp3")


# 绘制靶子
def draw_target():
    pygame.draw.rect(screen, RED, (target_x, target_y, target_width, target_height))


# 绘制箭
def draw_arrow():
    pygame.draw.rect(screen, BLUE, (arrow_x, arrow_y, arrow_width, arrow_height))


# 检查箭是否击中靶子
def check_hit():
    global target_hit, hit_count, arrow_count
    if not target_hit and arrow_x + arrow_width >= target_x and arrow_y + arrow_height >= target_y and arrow_y <= target_y + target_height:
        target_hit = True
        hit_count += 1
        # 播放音效
        hit_sound.play()


# 显示箭支数和成功率
def show_hit_info():
    global arrow_count, hit_count
    font = pygame.font.SysFont(None, 25)
    text = font.render("Hits: " + str(hit_count), True, BLACK)
    screen.blit(text, (10, 50))
    if arrow_count > 0:
        hit_rate = hit_count / arrow_count * 100
    else:
        hit_rate = 0
    text = font.render("Hit Rate: {:.2f}%".format(hit_rate), True, BLACK)
    screen.blit(text, (10, 90))


# 显示击中次数
def show_arrow_count():
    font = pygame.font.SysFont(None, 25)
    text = font.render("Arrows: " + str(arrow_count), True, BLACK)
    screen.blit(text, (10, 10))


# 主循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not arrow_fired:
                arrow_fired = True
                arrow_count += 1
                shoot_sound.play()

    # 清屏
    screen.fill(WHITE)

    # 绘制靶子
    draw_target()

    # 绘制箭
    draw_arrow()

    # 移动靶子
    target_y += target_speed * clock.get_time()
    if target_y <= 0:
        target_speed = abs(target_speed)
    elif target_y >= SCREEN_HEIGHT - target_height:
        target_speed = -abs(target_speed)

    # 移动箭
    if arrow_fired:
        if arrow_direction == "right":
            arrow_x += arrow_speed * clock.get_time()
            if arrow_x >= SCREEN_WIDTH:
                arrow_fired = False
                target_hit = False
                arrow_x = 50
        elif arrow_direction == "left":
            arrow_x -= arrow_speed * clock.get_time()
            if arrow_x <= 0:
                arrow_fired = False
                target_hit = False
                arrow_x = 50

    # 检查箭是否击中靶子
    check_hit()

    # 如果箭击中靶子,显示成功,并计数
    if target_hit:
        font = pygame.font.SysFont(None, 36)
        text = font.render("Hit!", True, GREEN)
        screen.blit(text, (SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT // 2))
        target_y = 0
        target_speed = random.uniform(0.1, 0.9)

    # 显示击中次数
    show_hit_info()

    # 显示箭支数和成功率
    show_arrow_count()

    # 更新画面
    pygame.display.flip()

    # 控制帧率
    clock.tick(FPS)

截图:

运行:

 

射箭游戏

改进:

可以增加射箭的力度输入等来增加乐趣与难度。

相关推荐

  1. 软件价值2-贪吃蛇游戏

    2024-02-12 23:48:02       40 阅读
  2. 软件价值14-碰撞弹球

    2024-02-12 23:48:02       32 阅读
  3. 游戏心理学Day12

    2024-02-12 23:48:02       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-12 23:48:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-12 23:48:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-12 23:48:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-12 23:48:02       20 阅读

热门阅读

  1. STM32 7-8

    STM32 7-8

    2024-02-12 23:48:02      27 阅读
  2. C++ 同构数,的问题。

    2024-02-12 23:48:02       30 阅读
  3. H5/CSS 笔试面试考题(41-50)

    2024-02-12 23:48:02       25 阅读
  4. H5/CSS 笔试面试考题(51-60)

    2024-02-12 23:48:02       27 阅读
  5. ZooKeeper分布式锁

    2024-02-12 23:48:02       29 阅读
  6. C语言:表达式求值

    2024-02-12 23:48:02       34 阅读
  7. 【嵌入式开发】72

    2024-02-12 23:48:02       27 阅读
  8. Pandas to_csv() - 将 DataFrame 转换为 CSV

    2024-02-12 23:48:02       27 阅读
  9. leetcode81 搜索旋转排序数组 II

    2024-02-12 23:48:02       40 阅读
  10. 数据结构基础

    2024-02-12 23:48:02       28 阅读
  11. jQuery---判断元素是否是显示状态

    2024-02-12 23:48:02       34 阅读
  12. 深入解析torch.load中的【map_location】参数

    2024-02-12 23:48:02       44 阅读