大鱼吃小鱼游戏代码
时间: 2023-07-03 19:11:17 浏览: 192
以下是一个简单的大鱼吃小鱼游戏的 Python 代码:
```python
import random
class Fish:
def __init__(self):
self.size = random.randint(1, 5)
class Piranha(Fish):
def __init__(self):
super().__init__()
self.name = "Piranha"
self.dangerous = True
class Goldfish(Fish):
def __init__(self):
super().__init__()
self.name = "Goldfish"
self.dangerous = False
class Player:
def __init__(self):
self.size = random.randint(1, 5)
self.gold = 0
def catch(self, fish):
if self.size >= fish.size and not fish.dangerous:
self.gold += 1
print(f"You caught a {fish.name} and earned 1 gold!")
elif self.size < fish.size and fish.dangerous:
print(f"You were eaten by a {fish.name}!")
return False
else:
print(f"You couldn't catch the {fish.name}.")
return True
player = Player()
fishes = [Piranha(), Goldfish()]
while True:
fish = random.choice(fishes)
print(f"A {fish.name} appeared!")
if not player.catch(fish):
break
print(f"Game over! You earned {player.gold} gold.")
```
在这个游戏中,你作为玩家扮演一个大鱼,通过尝试吃掉小鱼来赚取金币。你可以控制你的大鱼的大小,但是有些鱼是危险的,如果你的大小小于它们的大小,你就会被吃掉。游戏将一直进行,直到你被吃掉或者选择退出。
阅读全文