帮我写一段射击小游戏的代码
以下是一个简单的射击小游戏的代码,使用Python语言编写:
import random
# 初始化游戏界面
print("Welcome to Shooting Game!")
print("You have 10 bullets to shoot 5 targets.")
# 初始化游戏数据
bullets_left = 10
targets_hit = 0
# 开始游戏循环
while bullets_left > 0 and targets_hit < 5:
# 随机生成目标位置
target_position = random.randint(1, 10)
# 提示玩家射击
print("You have", bullets_left, "bullets left.")
print("Where do you want to shoot? (1-10)")
player_input = input()
# 判断玩家是否命中目标
if int(player_input) == target_position:
print("You hit the target!")
targets_hit += 1
else:
print("You missed the target.")
# 减少子弹数量
bullets_left -= 1
# 游戏结束,输出结果
if targets_hit == 5:
print("Congratulations! You hit all the targets!")
else:
print("Game over. You hit", targets_hit, "targets.")
这个小游戏的规则很简单,玩家有10颗子弹,需要在10个位置中击中5个目标。每次玩家射击时,程序会随机生成一个目标位置,玩家需要输入一个数字表示自己的射击位置。如果玩家命中目标,就会得到一分,否则就没有得分。当玩家的子弹用完或者击中了5个目标时,游戏结束,程序会输出玩家的得分情况
原文地址: https://www.cveoy.top/t/topic/cOhD 著作权归作者所有。请勿转载和采集!