Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created December 19, 2020 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/ed8babac73cfe5eec17439b82d62a252 to your computer and use it in GitHub Desktop.
Save horstjens/ed8babac73cfe5eec17439b82d62a252 to your computer and use it in GitHub Desktop.
cardgame peter emile
#import PySimpleGUI as sg
import pysimplegui as sg
import random
class Monster:
def __init__(self, name, hp):
self.name = name
self.hp = hp
def __repr__(self):
return f"{self.name} {self.hp} hp"
class Game:
army_of_good = []
army_of_evil = []
def battle():
text = []
# good side starts
for army in [Game.army_of_good, Game.army_of_evil]:
opponent_army = Game.army_of_evil if army == Game.army_of_good else Game.army_of_good
for monster in army:
if monster.hp <= 0:
continue
try:
victim = random.choice([monster for monster in opponent_army if monster.hp > 0])
except IndexError:
continue
damage =random.randint(1,6) + random.randint(1,6)
victim.hp -= damage
text.append(f"{monster} inflicts {damage} hp damage on {victim}")
return text
def main():
Game.army_of_evil.append(Monster("evil Wizard", 100))
Game.army_of_evil.append(Monster("Hellhound", 25))
Game.army_of_evil.append(Monster("Demon Lord", 200))
Game.army_of_evil.append(Monster("Goblin Archer", 33))
Game.army_of_evil.append(Monster("Goblin Thief", 22))
Game.army_of_good.append(Monster("good Wizard", 100))
# Define the window's contents
layout = [
[sg.Text("Welcome to the magic card game")],
# hier 4 zeilen, und zwar:
# 1 (verdeckt) Karten vom Gegner
# 2 (offen) army vom Gegner
# 3 (offen) army von mir
# 4 (offen) Karten von mir
# jeder Spieler hat immer 5 Karten
[sg.Text("enemy cards:"), sg.Text("-???-", key="enemycard0"), sg.Text("-???-", key="enemycard1"),
sg.Text("-???-", key="enemycard2"), sg.Text("-???-", key="enemycard3"), sg.Text("-???-", key="enemycard4"), ],
[sg.Text("enemy army:"), sg.Text("nobody", size=(50,15), key="army_of_evil"), ],
[sg.Text("your army:"), sg.Text("nobody", size=(50,15), key="army_of_good"), ],
[sg.Text("your cards:"), sg.Button("Fireball", key="card0"), sg.Button("Ligthning", key="card1") ,
sg.Button("Poison Cloud", key="card2"), sg.Button("Dwarf", key="card3"), sg.Button("Wasp", key="card4"), ],
# [sg.Text("What's your name?")],
# [sg.Input(key='-INPUT-')],
[sg.Text("your eney:"), sg.Text("brutus, the brutal Brutalo")],
[sg.Text("enemy hp:"), sg.Text("80", key="hp_enemy")],
[sg.Text("hp:"), sg.Text("o" * 80, key="hp_enemy_o")],
[sg.Text("-=-=-=-=-")],
[sg.Text("you are:"), sg.Text("Peter the heroic Hero")],
[sg.Text("your hp:"), sg.Text("80", key="your_hp")],
[sg.Text("hp:"), sg.Text("o" * 80, key="your_hp_o")],
[sg.Text("-=-=-=-=-")],
# [sg.Text("Your hitpoints:"), sg.Text("100", key="hp")],
# [sg.Text(size=(40,1), key='-OUTPUT-')],
[sg.Button('Fight!'), sg.Button('Ok'), sg.Button('Quit')],
]
# Create the window
window = sg.Window('Window Title', layout)
window.finalize()
window["army_of_evil"].update("\n".join([str(monster) for monster in Game.army_of_evil]))
window["army_of_good"].update("\n".join([str(monster) for monster in Game.army_of_good]))
# Display and interact with the Window using an Event Loop
hitpoints1 = 80
hp1full = 80
hitpoints2 = 80
hp2full = 80
while hitpoints1 > 0 and hitpoints2 > 0:
event, values = window.read()
# See if user wants to quit or window was closed
if event == sg.WINDOW_CLOSED or event == 'Quit':
break
if "card" in event:
# card0, card1, card2 ... etc
print("you clicked:", window[event].ButtonText)
btext = window[event].ButtonText
# summoning
if btext == "Dwarf":
Game.army_of_good.append(Monster("Dwarf", 150))
sg.popup_ok("A dwarf joins your army!")
elif btext == "Wasp":
Game.army_of_good.append(Monster("Wasp", 15))
sg.popup_ok("A wasp joins your army!")
# direct damage
if btext == "Fireball":
victim = random.choice(Game.army_of_evil)
damage = random.randint(6,12)
victim.hp -= damage
sg.popup_ok(f"You cast a Fireball and inflict {damage} hp damage to the enemy {victim.name}")
# finally
print(battle())
window["army_of_evil"].update("\n".join([str(monster) for monster in Game.army_of_evil]))
window["army_of_good"].update("\n".join([str(monster) for monster in Game.army_of_good]))
if event == "Fight!":
damage1 = random.randint(1, 8) + random.randint(1, 8) + random.randint(1, 8) + random.randint(1, 8) - 4
damage2 = random.randint(1, 20)
sg.popup_ok(f"Bum! Zack! Pang! You make {damage1} hp damage!\n"
f"Autsch, Aua, Auwehh.....you lose {damage2} hp")
hitpoints2 -= damage1
hitpoints1 -= damage2
window["your_hp"].update(str(hitpoints1))
window["your_hp_o"].update("\u2665" * hitpoints1 + "\u2661" * (hp1full - hitpoints1))
window["hp_enemy"].update(str(hitpoints2))
window["hp_enemy_o"].update("\u2661" * hitpoints2 + "\u2665" * (hp1full - hitpoints2))
# Output a message to the window
# window['-OUTPUT-'].update('Hello ' + values['-INPUT-'] + "! Thanks for trying PySimpleGUI")
if hitpoints1 > 0:
sg.popup_ok("You have won")
elif hitpoints2 > 0:
sg.popup_ok("You have lost")
else:
sg.popup_ok("draw...nobody has won")
# Finish up by removing from the screen
window.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment