Skip to content

Instantly share code, notes, and snippets.

@Gertkeno
Last active June 19, 2023 19:39
Show Gist options
  • Save Gertkeno/176fa5cef45f6bc927478d86c2762808 to your computer and use it in GitHub Desktop.
Save Gertkeno/176fa5cef45f6bc927478d86c2762808 to your computer and use it in GitHub Desktop.
import sys
import time
import random
# this function will prompt the user every choice in array `choices`
# they must select with a number. returns the index they selected
def multiple_choice(choices):
highest_choice = len(choices)
for i, choice in enumerate(choices):
print(i + 1, choice)
while True:
try:
selection = int(input(">")) - 1
if selection >= 0 and selection < highest_choice:
return selection
else:
print("Not in range 1-" + str(highest_choice))
except ValueError:
print("Bad input!")
class Pokemon:
def __init__(self, name, Type):
self.name = name
self.level = 5
self.xp = 0
self.health = 20
self.Type = Type
self.attack = 8
def add_xp(self, xp):
self.xp += xp
if self.xp >= 100:
self.xp -= 100
self.level += 1
self.health += 4
self.attack += random.randint(2, 5)
print(self.name + " leveled up! now lv." + self.level)
def max_health(self):
return self.level * 4
# create a health bar string.
def health_bar(self, length):
full = int(self.health * length / self.max_health())
empty = length - full
return '[' + (full * '#') + (empty * ' ') + ']'
# another magic function like __init__, this is used to print classes
def __str__(self):
return f"'{self.name}'\tlv.{self.level} type: {self.Type} \t" + self.health_bar(12)
class Trainer:
def __init__(self):
self.pokemon = []
self.money = 0
def add_pokemon(self, new_pokemon):
new_name = input("What would you like to name your " + new_pokemon.name + "?\n>")
if len(new_name) > 1:
new_pokemon.name = new_name
self.pokemon.append(new_pokemon)
def count_pokemon(self):
return len(self.pokemon)
def count_alive_pokemon(self):
total = 0
for pokemon in self.pokemon:
if pokemon.health > 0:
total += 1
return total
def select_pokemon(self):
print("select a pokemon!")
selection = multiple_choice(self.pokemon)
selected_pokemon = self.pokemon[selection]
while selected_pokemon.health <= 0 and count_alive_pokemon() > 0:
print(selected_pokemon.name + " cannot fight!")
selection = multiple_choice(self.pokemon)
selected_pokemon = self.pokemon[selection]
return selected_pokemon
# global player
player = Trainer()
# fight-loop code
def start_wild_fight(opponent):
print("A " + opponent.name + " is here to fight!")
out_pokemon = player.select_pokemon()
while opponent.health > 0 and player.count_alive_pokemon() > 0:
# player's turn
print("What will " + out_pokemon.name + " do?")
selection = multiple_choice([
"Attack",
"Item",
"Swap",
"Run",
])
if selection == 0: #Attack
# TODO: add code to damage the opponent's health
pass
elif selection == 1: #Item
# TODO: add code to catch the opponent
pass
elif selection == 2: #Swap
out_pokemon = player.select_pokemon()
elif selection == 3: #Run
print("You flee!")
return
# opponent's turn
# TODO: make the opponent do a random thing
if out_pokemon.health <= 0 and player.count_alive_pokemon() > 0:
print(out_pokemon.name + " fainted!")
out_pokemon = player.select_pokemon()
if opponent.health <= 0:
# TODO: player wins! give xp
pass
else:
# player loses
print("You rush back to town.")
# gameplay code
while True:
print("\nWelcome to pokemon town, from the hit game Pokemon (gen 1)")
print("Where would you like to go?")
selection = multiple_choice([
"Forest",
"Pokecenter",
"Minimart",
"Gym",
"Lab",
"Quit",
])
if selection == 0: #Forest
if player.count_pokemon() > 0:
print("you are in the forest")
# TODO: bonus! fight a random pokemon
start_wild_fight(Pokemon("rattata", "normal"))
else:
print("You cannot enter the forest without a pokemon, meet with the professor at the Lab")
elif selection == 1: #Pokecenter
print("Welcome to the pokecenter, we heal all your pokemon")
for pokemon in player.pokemon:
print("Healing:", pokemon)
pokemon.health = pokemon.max_health()
elif selection == 2: #Minimart
print("Buy stuff? if only you had money")
elif selection == 3: #Gym
print("Welcome to the gym, everyone here is much better than you are so you leave")
elif selection == 4: #Lab
if player.count_pokemon() == 0:
print("Welcome to the lab please select a starter pokemon!")
available_pokemon = [
Pokemon("bulbasaur", "grass"),
Pokemon("charmander", "fire"),
Pokemon("squirtle", "water")
]
selected_starter = multiple_choice(available_pokemon)
player.add_pokemon(available_pokemon[selected_starter])
else:
print("You already have a starter! get out there slugger!")
elif selection == 5: #Quit
print("Ok bye!")
sys.exit()
time.sleep(0.75)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment