寶可夢小隊代碼
以下是一個簡單的寶可夢小隊代碼示例,使用Python編寫。這個代碼模擬了一個包含寶可夢的小隊,並展示了如何添加、刪除和查看小隊中的寶可夢。
class Pokemon:
def __init__(self, name, type, level):
self.name = name
self.type = type
self.level = level
def __str__(self):
return f"{self.name} (Type: {self.type}, Level: {self.level})"
class PokemonSquad:
def __init__(self):
self.squad = []
def add_pokemon(self, pokemon):
if len(self.squad) < 6:
self.squad.append(pokemon)
print(f"{pokemon.name} has been added to the squad.")
else:
print("Squad is full! Cannot add more Pokemon.")
def remove_pokemon(self, name):
for pokemon in self.squad:
if pokemon.name == name:
self.squad.remove(pokemon)
print(f"{name} has been removed from the squad.")
return
print(f"{name} is not in the squad.")
def show_squad(self):
if not self.squad:
print("The squad is empty.")
else:
print("Current Squad:")
for pokemon in self.squad:
print(pokemon)
# 示例使用
squad = PokemonSquad()
pikachu = Pokemon("Pikachu", "Electric", 25)
charmander = Pokemon("Charmander", "Fire", 15)
bulbasaur = Pokemon("Bulbasaur", "Grass", 12)
squad.add_pokemon(pikachu)
squad.add_pokemon(charmander)
squad.add_pokemon(bulbasaur)
squad.show_squad()
squad.remove_pokemon("Charmander")
squad.show_squad()
代碼說明:
- Pokemon類:表示一個寶可夢,包含名稱、屬性和等級。
- PokemonSquad類:表示一個寶可夢小隊,最多可以包含6隻寶可夢。
- add_pokemon方法:將寶可夢添加到小隊中,如果小隊已滿則無法添加。
- remove_pokemon方法:從小隊中移除指定名稱的寶可夢。
- show_squad方法:顯示當前小隊中的所有寶可夢。
輸出示例:
Pikachu has been added to the squad.
Charmander has been added to the squad.
Bulbasaur has been added to the squad.
Current Squad:
Pikachu (Type: Electric, Level: 25)
Charmander (Type: Fire, Level: 15)
Bulbasaur (Type: Grass, Level: 12)
Charmander has been removed from the squad.
Current Squad:
Pikachu (Type: Electric, Level: 25)
Bulbasaur (Type: Grass, Level: 12)
這個代碼可以作為一個基礎框架,進一步擴展以支持更多功能,例如寶可夢對戰、屬性相剋等。