Don't forget to create documentation for classes and methods!
You joined the startup FIRESTORM™ which aims to develop the next MMO. Your task now is to help develop the character models.
-
Make a file
review.py
which will contain all the code for the classes we are about to make. -
In
review.py
create a class called BaseCharacter which takes one argumentname
.- In the constructor, use reflection to instantiate the
name
attribute and add the following fields:- intel = 3 (intellect)
- stam = 3 (stamina)
- strn = 3 (strength)
- agi = 3 (agility)
- level = 1 (starting level)
- In the constructor, use reflection to instantiate the
-
Add a method in the BaseCharacter class called
level_up
which will level up a character by a factor ofincrement
which will represent an integer greather than or equal to 1. Keep the default value ofincrement
to 1.-
Within the method, add a clause which will raise an
Exception
error message stating'Parameter "increment" must be an integer greater than or equal to 1.'
-
Create a variable called
stat_increase
which will be equal to3 * increment
. This will be how much each stat (besides level) will increase each time the character levels up. -
Create a for loop that will iterate through the class attributes and increment the main stats
intel, stam, strn
andagi
bystat_increase
and thelevel
by the value ofincrement
. This can be accomplished using a built-in Python function, and a dunder (__) method in the class object but, if this is too challenging, feel free to code out each attribute and increment them as mentioned in the previous statement. -
Once that is done, print (don't return) a block of text (or multiple print statements) stating the following:
-
The name of the character and the level that they reached
-
Each stat has increased by
stat_increase
to whatever it is after leveling up -
Be creative with this part!
-
-
-
Create another Python class representing a character type (like: Mage, Warrior, Thief) which will inherit from the BaseCharacter class.
-
In the constructor, instantiate the inherited attributes using the
super()
method or by referencing the parent class andname
parameter. -
In addition, increment other primary stats that would fit to the background of the character type (i.e. Mages would probably receive more intellect and stamina at creation than Warriors).
-
Lastly, create two new fields/attributes
health
and another of your choice which serves as that character's ability resource (i.e. Mages would usemana
).- In some MMOs, health - and sometimes the ability resource - have a base amount that is modified by a primary stat like stamina. Reflect this in your code!
-
-
Finally, create a method inside the new class called
display_stats
which only takes itself as the arguement.-
This method should print out three statements displaying the character's:
-
Name
-
Total amount of health
-
Total amount of character ability resource
-
-
-
First thing is first! We need to install the
pytest
package.- While in your environment, run
pip install pytest
to accomplish this.
- While in your environment, run
-
Now, create a directory called
tests
which will reside on the same level asreview.py
.-
In the
tests
directory, create a file calledconftest.py
which will house ourpytest
fixtures. -
To read more about
pytest
fixtures, check out this link.
-
-
In
conftest.py
import both thepytest
package as well as yourBaseCharacter
and second character class - this can be done with a relative import-
Instantiate a fixture for
BaseCharacter
with the name'Template'
-
Instantiate a second fixture for your other character class using any name you would like!
-
Now that we have made our pytest fixtures, let's make some tests!
-
-
Create a new file called
test_<something>.py
replacing the carats and content with something relevant to what you are testing.- Create at least four different tests total which will test both class attributes and methods you made in your source code.
-
When you are done, test it out!
-
Open a command line terminal and make sure you are at the base level of your repository.
-
Run the command
pytest
and your four tests should run. -
If you receive any errors, change the code around to fix it up and try again!
-
That's all here - good luck on your Sprint Challenge!