足球游戏代码
时间: 2023-07-03 12:04:29 浏览: 265
Python足球游戏源代码,热血足球小游戏,可双人玩
5星 · 资源好评率100%
这是一个简单的Python足球游戏代码,可以供你参考:
```
import random
# 初始化得分和回合数
player_score = 0
computer_score = 0
rounds = 1
# 定义足球游戏函数
def football_game():
global player_score, computer_score, rounds
print(f"\nRound {rounds}\n")
# 让玩家和电脑选择进攻方向
player_direction = input("Choose your attack direction (left or right): ").lower()
computer_direction = random.choice(["left", "right"])
print(f"The computer chooses {computer_direction}.\n")
# 根据选择的方向计算得分
if player_direction == "left" and computer_direction == "right":
player_score += 1
print("You scored a goal!")
elif player_direction == "right" and computer_direction == "left":
computer_score += 1
print("The computer scored a goal.")
else:
print("The ball is stolen by the opponent.")
# 显示当前得分
print(f"\nPlayer Score: {player_score} Computer Score: {computer_score}")
# 增加回合数
rounds += 1
# 进行五轮比赛
while rounds <= 5:
football_game()
# 判断最终胜负
print("\nFinal Score:")
print(f"Player Score: {player_score} Computer Score: {computer_score}")
if player_score > computer_score:
print("Congratulations! You win!")
elif player_score < computer_score:
print("Sorry, you lose!")
else:
print("It's a tie!")
```
这个游戏会让玩家和电脑选择进攻方向,根据选择的方向计算得分。最终比赛结束后,会根据得分判断最终胜负。
阅读全文