Last section :
<> In the previous section, we learned the basic use of classes , To consolidate learning , Continue to use today class To program . # study hard , Clock in every day #
What we are going to study today is “ Altman beat the little monster ”
Game planning Don't feel useless
# subject #
Write one “ Ultraman ” and “ Little monster ” class , The basic attribute has a name , Health and Mana . To achieve “ Ultraman ” hit “ Little monster ”.
The basic way to play is , Altman attacks first , The little monster can only fight back ; Altman can use normal attacks , There are three attack modes: magic attack and must kill , Small monsters can only use normal attacks . Altman uses magic attack , buckle 40 Mana , Restore mana after each normal attack .
After the basic playing method is satisfied , Test it “ Ultraman ” The odds of winning
Thinking analysis How to fight
# analysis #
First, we design two classes , Then they attack each other in a dead loop , Until one of the bars is 0, Announce game results .
“ Ultraman ” class , Normal attack required , Magic attack , unique skills , Magic recovery , Display these class methods . What can be done with random functions , generate 1-10, Use attack skills randomly according to probability .“ Little monster ” Class is a little simpler , As long as the normal attack , Show two class methods .
Think about the basic ideas , As usual , Make a list of ideas .
# thinking #
Ultraman class
Common attack :
The damage range is 15-25 Between points .
Each attack restores mana range 1-10 spot .
Magic attack :
The damage range is 20-30 Between points .
Use a magic attack button 40 Point Mana .
unique skills :
Damage value : Take the current health value of the little monster 3/4,
Every time you attack yourself 50 spot .
recovery :
Recover after each normal attack 1-10 Point Mana
Little monsters
Common attack :
The damage range is 10-20 Between points .
Game main program
In a simple way while Statement control loop ,If Basic flow of statement control .
Start coding Step by step
# Write notes #
According to the previous analysis , Write notes . The purpose of the note , On the one hand, it is to make your thinking clear , Know what to do next , Another is to keep the original intention , Because the code is written , You forget how it started .
So in order to keep the original intention , It's important to write notes
After writing notes , We started writing classes based on annotations
# Ultraman class #
1, Define an Ultraman class , It contains the basic name , Health and Mana :
In the game is generally used HP Represents health ,MP Represents mana
2, Write according to the notes attack() Common attack .
attack function , The object to be attacked is a parameter , So here's in parentheses ms It's a little monster . Attack once , It will ms Of hp Deduct a random number . It's used here randint function , It's a random function , So you need to add a line of code at the beginning of the code ,
there print() Function uses %s Represents a string ,%d Representative number , Their values are behind %() It corresponds to the inside , This is print New usage of .
The attack is over , call resume function , Restore mana , This function will be written later .
3, Write according to the notes magic() Magic attack ,
You need to determine if mana is enough , One time deduction 40 So it's necessary to judge whether the magic value is greater than 40, No more than 40 It's a normal attack .
4, Write according to the notes super() unique skills
You need to determine if health is enough , One time deduction 50 So we have to judge whether the health value is greater than 50, No more than 40 It's a normal attack . greater than 50 In my words , Buckle yourself first 50( Kill the enemy ), Then deduct three-quarters of the monster's current health .
5, Write according to the notes resume() Restore magic
Need to determine if the magic value has exceeded 90, Avoid mana recovery greater than 100.
6, Write according to the notes display() Display basic information
Here we simulate a blood bar , See the data clearly and intuitively , Not much .
# Ultraman class #
1, Define an Ultraman class , It contains the basic name , Health and Mana :
2, Write according to the notes attack() Common attack .
3, Write according to the notes display() Display basic information
# main program #
1, instantiation Altman and monster
Three values in brackets of Altman object , Name respectively , Health and Mana , Monsters have names and health values in brackets . Because Altman has too many skills , The damage is slightly higher , It's too cool , So it's pathetic to be beaten for the sake of the little monster , Give the little monster more blood here , After all, we have to fight back .
2, The game begins , Salute your opponent
Define a variable times Used to save rounds , And then they started to get beaten . What are the conditions for entering the cycle , Altman's HP or monster's HP is greater than 0, It means that neither of them is dead , Just keep fighting .
3, Altman starts to show off his skills
It's used here 1-10 To control the skill probability by using random numbers , Normal attack probability 60%, Magic attack probability 30%, Must kill probability 10%. Poor little monster is about to be beaten .
4, It's the monster's turn to fight back
As long as it has more blood than 0, It means it's still alive , To live is to fight bravely ! Take it , Ultraman !
After the little monster counterattacks , Display their two basic information , Then the game rounds are added 1.
5, Judge whether you win or lose
# Open it, open it, open it ! #
Poor little monster was arrested by Altman in the first place 112 Point blood volume , Three rounds down .
The war is fierce ,
Said the little monster :“ As long as you don't have to kill , I am the master !”
Altman said :“ Abuse vegetables , I can beat you without killing !”
To see who they are .
I changed the code
Let them fight 300 round .
Code modification above . You can start automatic testing 300 second , Count the number of wins and losses .
The test results are as follows :
It seems that the little monster is more powerful ,
After all, the skin is thicker .
python Complete code
↓ ↓ ↓ Join us , Let's study together ↓ ↓ ↓
I knew you “ I'm looking ”
Technology