下面是一个简单的打地鼠游戏的Python代码示例:
import random
import time
# 初始化屏幕
os.system('cls' if os.name == 'nt' else 'clear')
# 游戏设置
width, height = 10, 10 # 地鼠出现的范围
moles = [] # 地鼠列表
holes = set() # 孔洞位置集合
score = 0 # 玩家得分
game_over = False # 游戏是否结束
# 随机生成地鼠
def generate_moles():
global moles
moles = []
for _ in range(random.randint(5, 10)):
mole = [random.randint(0, width-1), random.randint(0, height-1)]
moles.append(mole)
# 显示地鼠和孔洞
def show_moles_and_holes():
for x, y in moles:
print("M", end=" ")
print()
for x, y in holes:
print("O", end=" ")
print()
# 检查是否击中地鼠
def check_mole_hit(x, y):
return [x, y] in moles
# 主游戏循环
while not game_over:
generate_moles() # 重新生成地鼠
show_moles_and_holes() # 显示地鼠和孔洞
print("当前得分:", score)
# 获取玩家输入
player_i