1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| ''' Description: 狼吃羊的游戏
假设游戏场景范围为(x,y),0≤x≤9,0≤y≤9。
游戏生成1只狼和10只羊; 它们的移动方向均随机; 狼的最大移动能力是2(可以随机移动1或2),羊的最大移动能力是1,移动随机沿x轴或y轴进行; 当移动到场景边界,则向反方向移动; 狼初始化体力为100; 狼每移动一次,体力消耗1; 当狼和羊坐标重叠,狼吃掉羊,狼体力增加20; 羊暂不计算体力; 当狼体力值为0或者羊的数量为0,游戏结束。
注意:无需图形界面展示,但要有中间过程输出。 ''' import random
class GameScene: def __init__(self, x, y): self.round = 1 self.x = x self.y = y self.Wolves = [] self.Sheep = []
def generate_animals(self, num_wolves, num_sheep): for _ in range(num_wolves): wolf = Wolf(self) self.Wolves.append(wolf) for _ in range(num_sheep): sheep = Sheep(self) self.Sheep.append(sheep)
def game_logic(self): while self.Wolves and self.Sheep: print(f"==== Round {self.round} ====") print(f"Wolves: {len(self.Wolves)} Sheep: {len(self.Sheep)}") for wolf in self.Wolves: wolf.move() for sheep in self.Sheep: sheep.move() self.display_map() self.check_collisions() self.round += 1 print("")
def check_collisions(self): for wolf in self.Wolves: print(f">> Wolf at {wolf.get_coordinates()}") for sheep in self.Sheep: print(f">> Sheep at {sheep.get_coordinates()}") if wolf.x == sheep.x and wolf.y == sheep.y: wolf.eat() sheep.die()
def game_over(self): check = len(self.Wolves) == 0 or len(self.Sheep) == 0 return check
def display_map(self): print("+++" * (self.x + 2) + "+") map = [[0 for _ in range(self.x + 1)] for _ in range(self.y + 1)] for wolf in self.Wolves: x, y = wolf.get_coordinates() map[x][y] += 1 for sheep in self.Sheep: x, y = sheep.get_coordinates() map[x][y] += 2 for i in range(self.y + 1): print("|", end=" ") for j in range(self.x + 1): if map[i][j] == 0: print("·", end=" ") elif map[i][j] == 1: print("W", end=" ") elif map[i][j] == 2: print("S", end=" ") elif map[i][j] == 3: print("X", end=" ") elif map[i][j] % 2 == 0 and map[i][j] > 3: cnt = map[i][j] // 2 print("S" + str(cnt), end=" ") print("|") print("+++" * (self.x + 2) + "+") map.clear()
class Animal: def __init__(self, game_scene): self.game_scene = game_scene self.x = random.randint(0, game_scene.x) self.y = random.randint(0, game_scene.y) self.energy = 100
def random_move(self): directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] direction = random.choice(directions) max_move = self.max_move_ability() dx, dy = direction new_x = self.x + dx * random.randint(1, max_move) new_y = self.y + dy * random.randint(1, max_move) if 0 <= new_x <= self.game_scene.x: self.x = new_x if 0 <= new_y <= self.game_scene.y: self.y = new_y
def is_at_edge(self): return self.x == 0 or self.x == self.game_scene.x or self.y == 0 or self.y == self.game_scene.y
def max_move_ability(self): pass
def get_coordinates(self): return (self.x, self.y)
def die(self): pass
class Wolf(Animal): def __init__(self, game_scene): super().__init__(game_scene) self.max_energy = 100 self.energy = self.max_energy self.max_move = 2
def max_move_ability(self): return 2
def move(self): self.energy -= 1 self.random_move() if self.is_at_edge(): self.random_move()
def eat(self): self.energy += 20 if self.energy > self.max_energy: self.energy = self.max_energy
def die(self): if self.energy == 0: self.game_scene.Wolves.remove(self)
class Sheep(Animal): def max_move_ability(self): return 1
def move(self): self.random_move() if self.is_at_edge(): self.random_move()
def die(self): print(f"Sheep at {self.get_coordinates()} died!") self.game_scene.Sheep.remove(self)
if __name__ == "__main__": scene = GameScene(4, 4) scene.generate_animals(1, 5) while not scene.game_over(): scene.game_logic() print("Game Over!")
|