DEV Community

JHSletten
JHSletten

Posted on

My first Python mini project

As a part of me trying to learn Python I embarked on a small coding project, where I programmed a simple text-based computer game you can run in the command line. The game is called The Tower of Py, where the aim of the game is to fight your way through the tower and defeat the evil bosses at the top.

How the game works:

In the game you can choose between three classes (Fighter, Wizard, and Paladin) which have different stats and spells available to use in combat. The combat loop is pretty simple; you can either attack or cast a spell (if you have sufficient Mana Points) and the enemy will have a chance to hit you with an attack. Spells always hit, while regular attacks have chance of missing which increases if your opponent is stronger than you. Enemies and spells have different ‘damage types’ (like nature, fire, physical, etc.), and these types affect how much damage is dealt. Fire damage, for example is less effective against a water Enemy. For every three enemies defeated the player levels up which further improves their stats.

Stat list:

  • Strength: dictates damage of regular attacks and how likely these attacks are to hit your opponent.
  • Hit Points (HP): decreases when you take damage, and when all these are depleted ... you die.
  • Mana Points (MP): are expended to cast spells. Higher level spells cost more MP.
  • Grit: determines the rate of regeneration of HP.
  • Magic: determines the rate of regenerating MP.
  • Score: increases for each enemy you defeat and for each time you level up.

Image description

You need to download the classes.py, functions.py, materials.py, and run.py files to run the game. To start playing run the run.py script.

https://github.com/JHSletten/Tower_of_Py

The code itself:

The core of the program is three classes: Hero, Spell, and Enemy. The Hero class determines the ‘player class’ (i.e. Wizard), all the related stats, and has functions to: increase or decrease HP and MP, increase stats (level up), return base damage, etc. The Spell class allows Spell objects to have different types, mana costs, damage values, etc.. The Enemy class is similar to the Hero class, in that it defines object with HP and damage values with the associated functions. The Enemy class also has a type and level attribute.

There are functions that creates the hero, enemy, and spell objects based on input, lists, or dictionaries. The combat loop is one big function that also uses a calculate damage function and an attack function (determines if opponent is hit). Lastly there are three stage functions which initiates combats, provides the flavor text of the game, and levels up the hero.

Feel free to give the game a try! I recommend starting out with a Paladin because it combines strong attacks with a limited spell selection.

Do be warned though... it is challenging to climb the tower of Py...

Top comments (1)

Collapse
 
johanneshsletten profile image
JHSletten

You can also play the game in your browser here:
replit.com/@JohannesSletten/Towero...