给出导弹追踪小游戏的python代码
时间: 2023-11-10 07:05:54 浏览: 121
Python小游戏代码
以下是一个简单的导弹追踪小游戏的Python代码示例:
```python
import random
# 初始化游戏
missile_x = random.randint(1, 10)
missile_y = random.randint(1, 10)
target_x = random.randint(1, 10)
target_y = random.randint(1, 10)
num_guesses = 0
# 开始游戏
print("Welcome to Missile Tracking Game!")
print("The missile is at ({}, {})".format(missile_x, missile_y))
while True:
# 获取用户猜测的坐标
guess_x = int(input("Enter x coordinate: "))
guess_y = int(input("Enter y coordinate: "))
num_guesses += 1
# 判断猜测是否正确
if guess_x == missile_x and guess_y == missile_y:
print("You hit the missile! It took you {} guesses.".format(num_guesses))
break
else:
# 根据猜测与目标的距离给出提示
distance_to_missile = ((guess_x - missile_x) ** 2 + (guess_y - missile_y) ** 2) ** 0.5
distance_to_target = ((guess_x - target_x) ** 2 + (guess_y - target_y) ** 2) ** 0.5
if distance_to_missile < distance_to_target:
print("You're getting closer to the missile.")
else:
print("You're getting further away from the missile.")
```
这个游戏中,玩家需要猜测导弹的坐标,直到猜中为止。每次猜测后,程序会给出提示,告诉玩家猜测是否靠近导弹。玩家最终需要在最少的猜测次数内猜中导弹的位置。
阅读全文