用python编写一个斗兽棋的小游戏
时间: 2023-09-07 08:13:43 浏览: 113
好的,我可以帮您提供一个斗兽棋的小游戏的Python代码。请注意,这只是一个简单的版本,可以根据您的需求进行修改和改进。
```python
# 斗兽棋小游戏
import random
# 定义动物类
class Animal:
def __init__(self, name, strength):
self.name = name
self.strength = strength
self.alive = True
def __str__(self):
return self.name
def fight(self, other):
if self.strength == other.strength:
self.alive = False
other.alive = False
print("两只动物同归于尽!")
elif self.strength > other.strength:
other.alive = False
print(f"{self.name}胜利!")
else:
self.alive = False
print(f"{other.name}胜利!")
# 定义动物种类
class Lion(Animal):
def __init__(self):
super().__init__("狮", 7)
class Tiger(Animal):
def __init__(self):
super().__init__("虎", 6)
class Leopard(Animal):
def __init__(self):
super().__init__("豹", 5)
class Wolf(Animal):
def __init__(self):
super().__init__("狼", 4)
class Dog(Animal):
def __init__(self):
super().__init__("狗", 3)
class Cat(Animal):
def __init__(self):
super().__init__("猫", 2)
class Mouse(Animal):
def __init__(self):
super().__init__("鼠", 1)
# 定义棋盘类
class ChessBoard:
def __init__(self):
self.board = [[None for _ in range(3)] for _ in range(7)]
self.animals = []
def __str__(self):
rows = []
for i in range(7):
row = []
for j in range(3):
if self.board[i][j] is None:
row.append(" ")
else:
row.append(str(self.board[i][j]))
rows.append(" ".join(row))
return "\n".join(rows)
def add_animal(self, animal, row, col):
if self.board[row][col] is not None:
print("该位置已经有动物了!")
else:
self.animals.append(animal)
self.board[row][col] = animal
def remove_animal(self, animal):
self.animals.remove(animal)
for i in range(7):
for j in range(3):
if self.board[i][j] == animal:
self.board[i][j] = None
return
def get_animal(self, row, col):
return self.board[row][col]
# 定义游戏类
class Game:
def __init__(self):
self.board = ChessBoard()
self.current_player = 0
self.players = [[Lion(), Tiger(), Leopard(), Wolf(), Dog(), Cat(), Mouse()],
[Lion(), Tiger(), Leopard(), Wolf(), Dog(), Cat(), Mouse()]]
def setup(self):
for i in range(7):
self.board.add_animal(self.players[0][i], i, 0)
self.board.add_animal(self.players[1][i], i, 2)
def play(self):
while True:
print("当前棋盘状态:")
print(self.board)
player_animals = self.players[self.current_player]
print(f"当前玩家:玩家{self.current_player + 1}")
while True:
animal_name = input("请选择要移动的动物(输入动物名字):")
animal = None
for a in player_animals:
if a.name == animal_name:
animal = a
break
if animal is None:
print("该动物不存在或者不属于当前玩家!")
elif not animal.alive:
print("该动物已经死亡!")
else:
break
while True:
direction = input("请选择方向(w:上,s:下,a:左,d:右):")
row, col = self.get_next_position(animal, direction)
if row < 0 or row >= 7 or col < 0 or col >= 3:
print("不能移动到棋盘外!")
else:
break
target_animal = self.board.get_animal(row, col)
if target_animal is None:
self.board.board[row][col] = animal
else:
if target_animal in player_animals:
print("不能攻击自己的动物!")
else:
animal.fight(target_animal)
if animal.alive:
self.board.board[row][col] = animal
else:
self.board.remove_animal(animal)
player_animals.remove(animal)
if self.is_game_over():
print("游戏结束!")
print(f"玩家{1 if self.players[0][0].alive else 2}获胜!")
break
self.current_player = 1 - self.current_player
def get_next_position(self, animal, direction):
row, col = -1, -1
for i in range(7):
for j in range(3):
if self.board.board[i][j] == animal:
row, col = i, j
break
if direction == "w":
row -= 1
elif direction == "s":
row += 1
elif direction == "a":
col -= 1
elif direction == "d":
col += 1
return row, col
def is_game_over(self):
lions_alive = False
for animal in self.players[0]:
if isinstance(animal, Lion) and animal.alive:
lions_alive = True
break
if not lions_alive:
return True
lions_alive = False
for animal in self.players[1]:
if isinstance(animal, Lion) and animal.alive:
lions_alive = True
break
if not lions_alive:
return True
return False
# 开始游戏
game = Game()
game.setup()
game.play()
```
希望这个代码能够帮到您,如果您有任何问题或者需要进一步的帮助,请随时联系我。