Back

Creating main () and getting the monsters to fight

Introduction
We are going to reorganise our code slightly. All we are going to do is put the instructions inside of a function called main() and then we will call that function. We won't put any functions in it or any classes, only the actually code that calls them, or prints something, for example. You can see the changes we have made below and you should alter your code to match it.

main

You can see the function header on line 34. The body of the function is after the header. We actually call the function from line 44. There are a number of reasons why we have used a function called main(). It does make our code look more organised but it also allows us to do some checking for errors really easily later on. For now, we are just going to accept that we will put our instructions inside this function.

Fighting monsters
We want our monsters to be able to have a fight. We'll keep it nice and simple to start with. When two monsters fight, we will compare their strengths. Whoever is stronger, has their strength increased by two and the loser has their strength decreased by two. If it is a draw, then both monsters lose one strength point so there is always an incentive to win! The first thing we need to do is to add some extra methods to our monster class. We need to be able to increase and decrease strengths. You can see below that we have added two extra methods under the __str__ method. As always, the first parameter is always self, so that the method knows it has to work with the object i.e. itself. We may want to use this to increase or decrease the strengths in other situations so we need to make it as flexible as possible. We will pass it the number to increase or decrease rather than putting in a fixed number that we can't change.

i strength

Now we need to write a function we can call anytime we want any two monsters to fight. We'll add this new function underneath the monster class but above main(). We will pass the function two monster objects, and then compare the strengths of them. When we have a result, we will adjust the strengths of the monsters and print out the result. To test this, we have to add some code to call the function, and then print out the monsters again so we can check that the strengths have indeed been adjusted. Examine the code below.

fight

Line 73 calls the fight function. The object monster1 gets 'mapped' to mon1 and monster2 gets 'mapped' to mon2. If we write mon1.strength, that is now the same as monster1.strength and if we change the value of mon.strength, that is the same as changing monster1.strength and this is a really great system! It means that we can pass any two monster objects to our function for a fight (not just monster1 and monster2). It gives us total flexibility.

Total flexibility
Lets add an extra monster called Baaad and have a fight between Smog and Baaad. We will also add a few extra print statements, to improve the layout of the display. Here are the code changes in main().

fights

Try these tasks:
1) Add a few extra monsters and have a few fights between them. Before the fight happens what results did you expect? Predict the outcome of a fight and how the strengths will change. Then run the code and check the result with your prediction. Remember! Just because the code runs and gives you some numbers, does not mean the code is running correctly and giving you the right results. You have to carefully check the results.
2) Delete self on line 34 (the method header for i_strength). What happened when you ran the code?
3) Delete self on line 35 (in the body of the i_increase method). What happened when you ran the code?

Back