软件价值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)

截图:

运行:

 

射箭游戏

改进:

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

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HTML5射箭游戏可以使用JavaScript和Canvas来实现。下面是一个简单的示例代码,实现了射箭游戏的基本功能: HTML代码: ``` <!DOCTYPE html> <html> <head> <title>射箭游戏</title> <style> canvas {border: 1px solid black;} </style> </head> <body> <h1>射箭游戏</h1> <canvas id="canvas" width="800" height="600"></canvas> <p>按下空格键开始游戏,按下左右箭头键调整角度,按下上下箭头键调整力度,松开空格键射箭。</p> <script src="game.js"></script> </body> </html> ``` JavaScript代码(game.js): ``` var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); // 游戏状态 var gameState = "ready"; // ready / playing / over // 箭 var arrow = { x: 0, y: 0, angle: 0, speed: 0, alive: false }; // 目标 var target = { x: 700, y: 300, radius: 50 }; // 绘制箭 function drawArrow() { context.save(); context.translate(arrow.x, arrow.y); context.rotate(arrow.angle); context.beginPath(); context.moveTo(0, -10); context.lineTo(0, 10); context.lineTo(50, 0); context.closePath(); context.fillStyle = "black"; context.fill(); context.restore(); } // 绘制目标 function drawTarget() { context.beginPath(); context.arc(target.x, target.y, target.radius, 0, Math.PI * 2); context.fillStyle = "red"; context.fill(); } // 游戏循环 function gameLoop() { // 清空画布 context.clearRect(0, 0, canvas.width, canvas.height); // 绘制箭和目标 drawArrow(); drawTarget(); if (gameState == "playing") { // 移动箭 arrow.x += arrow.speed * Math.cos(arrow.angle); arrow.y += arrow.speed * Math.sin(arrow.angle); // 判断箭是否碰到目标 if (Math.sqrt(Math.pow(arrow.x - target.x, 2) + Math.pow(arrow.y - target.y, 2)) < target.radius) { gameState = "over"; alert("你击中了目标!"); } // 判断箭是否飞出屏幕 if (arrow.x < 0 || arrow.x > canvas.width || arrow.y < 0 || arrow.y > canvas.height) { gameState = "over"; alert("你没有击中目标!"); } } requestAnimationFrame(gameLoop); } // 按下空格键开始游戏 document.addEventListener("keydown", function(event) { if (event.keyCode == 32 && gameState == "ready") { gameState = "playing"; arrow.x = 100; arrow.y = canvas.height / 2; arrow.angle = Math.PI / 4; arrow.speed = 0; arrow.alive = true; } }); // 按下左右箭头键调整角度,按下上下箭头键调整力度,松开空格键射箭 document.addEventListener("keydown", function(event) { if (event.keyCode == 37 && gameState == "playing") { // left arrow key arrow.angle -= Math.PI / 180; } if (event.keyCode == 39 && gameState == "playing") { // right arrow key arrow.angle += Math.PI / 180; } if (event.keyCode == 38 && gameState == "playing") { // up arrow key if (arrow.speed < 10) { arrow.speed++; } } if (event.keyCode == 40 && gameState == "playing") { // down arrow key if (arrow.speed > 0) { arrow.speed--; } } if (event.keyCode == 32 && gameState == "playing" && arrow.alive) { // space key arrow.alive = false; } }); // 开始游戏循环 gameLoop(); // 相关问题: // 1. 如何使用Canvas绘制图形? // 2. 如何处理用户的键盘输入? // 3. 如何计算箭的运动轨迹?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dracularking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值