这是我到目前为止使用random
来选择移动的代码:
import time
import random
PHealth = 10
CHealth = 10
PShots = [
"Great Body Shot To Your Opponent!",
"Nice Take Down!",
"Nice Punch!",
"Strong Kick!",
"You Have Him Pinned Against The Cage!",
"Excellent Counter-Shot!"
]
CShots = [
"You Took a Shot to the Body!",
"You Got Taken Down!",
"Strong Kick Hit You!",
"You Took A Big Punch!",
"You Are Pinned Against The Cage",
"Counter-Shot Got Ya!"
]
for i in range(20):
i = random.randint(0, 100)
if i >= 51:
print(random.choice(PShots))
CHealth = CHealth -1
if CHealth >= 1:
print("Player Health", PHealth)
print("Computer Health", CHealth)
time.sleep(5)
if i <= 50:
print(random.choice(CShots))
PHealth = PHealth -1
if PHealth >= 1:
print("Player Health", PHealth)
print("Computer Health", CHealth)
time.sleep(5)
if CHealth < 1:
print("What A Shot!")
time.sleep(1)
print("Down He Goes!")
time.sleep(1)
print("The Referee Has Stopped The Fight!!")
time.sleep(1)
print("Player Wins!!!")
break
if PHealth < 1:
print("What A Shot!")
time.sleep(1)
print("Down You Go!")
time.sleep(1)
print("The Referee Has Stopped The Fight!!")
time.sleep(1)
print("Computer Wins!!!")
break
基本上,我想了解玩家如何输入一个动作。因此,如果玩家输入body shot
它就会击败take down
。如果玩家输入kick
则击败punch
。如果玩家输入take down
则击败pinned against the cage
,等等。思考 6-7 种变化和计数器。
这里有一个想法,说明如何使用类和函数的组合来实现类似于你似乎正在寻找的东西。
以下代码应适用于 Python3.9+
,并且没有其他依赖项。
首先,我们定义一个Move
类,其实例需要具有name
、text_used
(当玩家成功使用移动时)和一个text_affected
(当移动用于对付玩家时)。每个实例还存储一组它胜过的其他Move
对象,以及一组它胜过的对象。我们有一个帮助程序方法should_beat
可以轻松定义两个移动之间的这种关系。
class Move:
def __init__(self, name: str, text_used: str, text_affected: str, damage: int = 1) -> None:
self.name: str = name
self.text_used: str = text_used
self.text_affected: str = text_affected
self.damage: int = damage
self.trumps: set['Move'] = set()
self.trumped_by: set['Move'] = set()
def __str__(self) -> str:
return self.name
def should_beat(self, other_move: 'Move') -> None:
self.trumps.add(other_move)
other_move.trumped_by.add(self)
接下来,我们定义一个Player
类。默认情况下,其实例具有设置为先前定义的常量name
和可选starting_health
。
Player
还有一个use_move
方法,该方法采用一个Move
对象、另一个Player
对象(对手)和第二个Move
对象(对手使用的移动)。该方法检查哪个移动跳动哪个,并相应地计算健康减法。
最后,Player
对象具有win
和lose
方法,必要时可以调用这些方法来打印出赢/输语句。
class Player:
def __init__(self, name: str, starting_health: int = DEFAULT_STARTING_HEALTH) -> None:
self.name: str = name
self.health: int = starting_health
def __str__(self) -> str:
return self.name
def use_move(self, move: Move, vs_player: 'Player', vs_move: Move) -> None:
if vs_move in move.trumped_by:
self.health -= vs_move.damage
print(vs_move.text_affected)
elif move in vs_move.trumped_by:
vs_player.health -= move.damage
print(move.text_used)
else:
print(TEXT_NO_EFFECT)
def win(self, vs: 'Player') -> None:
print("What A Shot!")
sleep(1)
print(f"{vs} Goes Down!")
sleep(1)
print("The Referee Has Stopped The Fight!!")
sleep(1)
print(f"{self} Wins!!!")
def lose(self, vs: 'Player') -> None:
print("What A Shot!")
sleep(1)
print(f"Down You Go, {self}!")
sleep(1)
print("The Referee Has Stopped The Fight!!")
sleep(1)
print(f"{vs} Wins!!!")
接下来,我们需要一个函数来定义我们的移动,并且一个方便地将移动列表打印到终端的函数。显然,您将需要扩展define_moves
功能以合并所有您想要的动作及其关系。它应该返回所有Move
对象的列表。
def define_moves() -> list[Move]:
kick = Move("kick", "Strong Kick!", "Strong Kick Hit You!")
punch = Move("punch", "Nice Punch!", "You Took A Big Punch!")
...
kick.should_beat(punch)
...
return [
kick,
punch,
]
def print_moves(moves_list: list[Move]) -> None:
print("Available moves:")
for i, move in enumerate(moves_list):
print(i, "-", move.name)
现在对于有趣的部分,我们需要我们的主要战斗循环。在每次迭代中,我们都会提示玩家输入一个数字,该数字对应于我们之前定义的移动列表中的索引。(玩家也可以键入h
以再次查看移动。我们执行一些检查以确保我们收到一个有效的数字来获取我们的Move
对象。
然后我们从招式列表中随机为电脑对手选择一个招式,并调用我们的use_move
方法。它为我们进行健康计算。因此,在那之后,我们只需检查是否有人完成了调用适当的win
或lose
方法并脱离循环。否则,我们将打印当前运行状况统计信息并继续。
def fight_computer(player: Player, moves_list: list[Move]) -> None:
computer = Player(name="Computer")
while True:
string = input('Choose your move! (or type "h" to see available moves again)n').strip().lower()
if string == 'h':
print_moves(moves_list)
continue
try:
i = int(string)
except ValueError:
print("You need to pick a number!")
continue
try:
move = moves_list[i]
except IndexError:
print("No move available with number", i)
continue
computer_move = choice(moves_list)
print(computer, "chose", computer_move)
player.use_move(move, vs_player=computer, vs_move=computer_move)
if player.health < 1:
player.lose(vs=computer)
break
if computer.health < 1:
player.win(vs=computer)
break
print(player, "health:", player.health)
print(computer, "health:", computer.health)
sleep(1)
最后,我们需要一个main
函数来将它们组合在一起,提示玩家输入他的名字,等等。
def main() -> None:
player_name = input("Enter your name: ").strip()
player = Player(player_name)
moves_list = define_moves()
print_moves(moves_list)
fight_computer(player, moves_list)
if __name__ == '__main__':
main()
不要忘记将导入和常量定义添加到模块的开头:
from random import choice
from time import sleep
DEFAULT_STARTING_HEALTH = 10
TEXT_NO_EFFECT = "Your move had no effect!"
总而言之,这应该为您提供您所描述的游戏的粗略版本。试试吧。希望这有帮助。