write a code to a python file that is a text based Pokémon game. which works as follows: 1. player get chance of catching any random Pokémon where they are given 3 poke balls and 5 chances where they catch the wild pokemons only on the basis of chance where also they have choice of catching or fleeing .2 . ones they have used all poke ball or all 5 chances they fight 5 ai players. 3. where generate random Pokémons, their attacks, player, and storyline for the game to make it engaging and also make it feel professional.
Assistant
Creating a text-based Pokémon game in Python involves several components, including managing Pokémon data, handling user interactions, implementing battle mechanics, and maintaining the game flow. Below is a comprehensive Python script that fulfills your requirements:
python
import random
import time
# Define Pokémon species with their possible attacks
POKEMON_SPECIES = {
"Pikachu": {
"hp": 35,
"attacks": {"Thunder Shock": 10, "Quick Attack": 8, "Tail Whip": 5}
},
"Charmander": {
"hp": 39,
"attacks": {"Ember": 10, "Scratch": 8, "Growl": 5}
},
"Squirtle": {
"hp": 44,
"attacks": {"Water Gun": 10, "Tackle": 8, "Tail Whip": 5}
},
"Bulbasaur": {
"hp": 45,
"attacks": {"Vine Whip": 10, "Tackle": 8, "Growl": 5}
},
"Eevee": {
"hp": 55,
"attacks": {"Quick Attack": 10, "Bite": 8, "Tail Whip": 5}
}
}
# Define AI Trainers
AI_TRAINERS = [
{"name": "Red", "pokemon": "Charmander"},
{"name": "Blue", "pokemon": "Squirtle"},
{"name": "Green", "pokemon": "Bulbasaur"},
{"name": "Yellow", "pokemon": "Pikachu"},
{"name": "Black", "pokemon": "Eevee"}
]
class Pokemon:
def __init__(self, name):
self.name = name
self.max_hp = POKEMON_SPECIES[name]["hp"]
self.current_hp = self.max_hp
self.attacks = POKEMON_SPECIES[name]["attacks"]
def is_alive(self):
return self.current_hp > 0
def attack(self, other):
attack_name, damage = random.choice(list(self.attacks.items()))
print(f"{self.name} uses {attack_name}! It deals {damage} damage.")
other.current_hp -= damage
if other.current_hp < 0:
other.current_hp = 0
print(f"{other.name}'s HP is now {other.current_hp}/{other.max_hp}\n")
class Trainer:
def __init__(self, name, pokemon_name):
self.name = name
self.pokemon = Pokemon(pokemon_name)
def is_defeated(self):
return not self.pokemon.is_alive()
class Player:
def __init__(self):
self.pokeballs = 3
self.chances = 5
self.team = []
self.active_pokemon = None
def catch_pokemon(self, wild_pokemon):
if self.pokeballs <= 0:
print("No Poké Balls left!")
return False
self.pokeballs -= 1
catch_chance = random.randint(1, 100)
required_chance = 50 # 50% base chance
if catch_chance <= required_chance:
print(f"Congratulations! You caught a wild {wild_pokemon.name}!")
self.team.append(wild_pokemon)
if not self.active_pokemon:
self.active_pokemon = wild_pokemon
return True
else:
print(f"{wild_pokemon.name} escaped!")
return False
def has_pokeballs_or_chances(self):
return self.pokeballs > 0 and self.chances > 0
def choose_action(self):
while True:
action = input("Do you want to (C)atch or (F)lee? ").strip().lower()
if action in ['c', 'f']:
return action
else:
print("Invalid input. Please enter 'C' to catch or 'F' to flee.")
class Game:
def __init__(self):
self.player = Player()
self.ai_trainers = [Trainer(trainer["name"], trainer["pokemon"]) for trainer in AI_TRAINERS]
def intro(self):
print("Welcome to the Pokémon World!")
print("Your journey begins now.\n")
time.sleep(2)
def generate_wild_pokemon(self):
name = random.choice(list(POKEMON_SPECIES.keys()))
return Pokemon(name)
def wild_encounter(self):
wild_pokemon = self.generate_wild_pokemon()
print(f"A wild {wild_pokemon.name} appeared!")
while self.player.has_pokeballs_or_chances():
action = self.player.choose_action()
if action == 'c':
caught = self.player.catch_pokemon(wild_pokemon)
if caught:
break
elif action == 'f':
print("You fled from the wild Pokémon!\n")
break
self.player.chances -= 1
print(f"Chances left: {self.player.chances} | Poké Balls left: {self.player.pokeballs}\n")
if self.player.chances <= 0:
print("No chances left!")
break
def battle_trainer(self, trainer):
print(f"\nYou are challenged by Trainer {trainer.name}!")
print(f"{trainer.name} sends out {trainer.pokemon.name}!\n")
while self.player.active_pokemon.is_alive() and trainer.pokemon.is_alive():
# Player's turn
print("Your turn:")
player_move = self.player.active_pokemon.attack(trainer.pokemon)
if trainer.pokemon.is_alive():
# AI's turn
print(f"{trainer.name}'s turn:")
trainer.pokemon.attack(self.player.active_pokemon)
if self.player.active_pokemon.is_alive():
print(f"You defeated Trainer {trainer.name}!\n")
time.sleep(1)
else:
print("Your Pokémon fainted! You lost the battle...\n")
time.sleep(1)
def start_battles(self):
print("Time for battles against AI Trainers!\n")
for trainer in self.ai_trainers:
self.battle_trainer(trainer)
if not self.player.active_pokemon.is_alive():
print("Game Over. You were defeated in battle.")
return
print("Congratulations! You have defeated all the AI Trainers!")
def start_game(self):
self.intro()
# Wild Pokémon encounters
while self.player.has_pokeballs_or_chances():
self.wild_encounter()
print(f"Team size: {len(self.player.team)}\n")
time.sleep(1)
# Start battles
self.start_battles()
if __name__ == "__main__":
game = Game()
game.start_game()
How to Run the Game
Save the Script:
Create a new file named pokemon_game.py.
Copy and paste the above Python code into this file.
Run the Script:
Open your terminal or command prompt.
Navigate to the directory where pokemon_game.py is saved.
Execute the script by running:
code
python pokemon_game.py
Ensure you have Python installed on your system. If not, download it from python.org.
Game Flow
Introduction:
The game greets the player and begins the journey.
Wild Pokémon Encounters:
The player encounters random wild Pokémon.
For each encounter, the player can choose to Catch (C) or Flee (F).
Catching consumes a Poké Ball and has a 50% success rate.
Each encounter also consumes one chance regardless of the action.
The player has 3 Poké Balls and 5 chances.
Battles with AI Trainers:
After using all Poké Balls or chances, the player battles 5 AI Trainers.
Each trainer has a predefined Pokémon.
Battles are turn-based: the player's Pokémon and the trainer's Pokémon take turns attacking.
The battle continues until one Pokémon faints.
If the player's Pokémon faints, the game ends.
Winning the Game:
Defeat all AI Trainers to win the game.
Features
Randomized Encounters:
Wild Pokémon are chosen randomly from a predefined list.
Catching Mechanism:
Players must strategically decide when to catch or flee.
Limited resources with Poké Balls and chances add to the challenge.
Battle System:
Turn-based battles with simple attack mechanics.
Each Pokémon has unique attacks with varying damage.
AI Trainers:
Five AI Trainers each with their own Pokémon to provide sequential challenges.
Enhancements You Can Make
Expand Pokémon Data:
Add more Pokémon species with unique abilities and stats.
Implement Multiple Team Members:
Allow the player to have multiple Pokémon and switch between them during battles.
Enhance Battle Mechanics:
Introduce types, strengths, and weaknesses (e.g., Water beats Fire).
Add Items and Power-ups:
Include items like potions to heal Pokémon during battles.
Improve Storyline:
Add more narrative elements to engage the player further.
This script provides a solid foundation for a text-based Pokémon game. You can expand and customize it to include more features and complexity as per your creativity and requirements.