Python基础程序代写,python程序代写,python程序辅导,python编程辅导
Classes
In this assignment, you'll practice working with classes and object-oriented programming techniques by building a simiplified Pokémon-like game. You'll have lots of freedom to determine the game mechanics. Because most of us are new to Object-Oriented Programming, this assignment is structures a little more like a lab to help you get started.
Challenge 1: Creating a class definition
Recall from lecture that classes function as blueprints for building objects in Python. They bundle together data (inside attributes) and actions (inside methods) in ways that are both logical and easy to maintain/extend. In this first challenge, we'll create a generic Pokemon class.

Create a Pokemon class. The class definition should include:
- a constructor that takes in a
nameparameter - the constructor should initialize the following
attributes(don't forget that allattributes/methodsshould be members of thePokemonclass -- use the wordself!):name= the value contained in thenameparameter passed to the constructorpokemon_type="NORMAL"max_hp= a random integer (you decide the range)current_hp=max_hpattack_power= a random integer (greater than 0 but less thanhp)defensive_power= a random integer (greater than 0 but less thanhp)fainted=False
- a method called
printStats()that prints the Pokémon's stats to the screen - a method called
defend()that decreases the value ofcurrent_hpby some amount (you determine by how much, but it should probably be related todefensein some way) and settingfainted = Trueifcurrent_hpfalls below 0 - a method called
attack(opponent)that takes in an opponent (anotherPokemoninstance) and decreases itscurrent_hpby callingopponent.defend() - a method called
revive()that iffainted = Truesetscurrent_hp = max_hp/2andfainted = Falseand returnsTrue, otherwise returnsFalse
Test out your class by creating an instance of the Pokemon class in your main() function, calling .printStats() once to see the starting stats, calling .defend() until the Pokemon faints, and then calling .revive() and .printStats() again.
Challenge 2: Creating a simple battle() function
Woohoo, you've got your Pokémon class working! Unfortunately, the game isn't very much fun by yourself.
Create a battle() function that takes two Pokemon instances (one belonging to each player), and alternates between them.
On each player's turn, their Pokemon will attack the other player's Pokemon, and the defending Pokemon will then print its stats. You may want to print additional information to the screen to make gameplay more interesting.
Test your battle() function by creating a second Pokemon instance within your main() function, and calling battle() on the two instances.
Challenge 3: Extending the class definition
In the actual game, there is more than one kind of Pokémon, each with its own special kinds of attack.
Add three or more subclasses that extend the Pokemon class, modifying the pokemon_type if necessary and overriding the attack() method (which may do different kinds of damage depending on the opponent). For example:
class Pikachu(Pokemon):
self.type = "ELECTRIC"
def attack(opponent):
if (opponent.type == "GROUND"):
# super-effective
# do twice as much damage
elif (opponent.type == "ELECTRIC" or opponent.type = "FLYING"):
# not effective
# do half as much damage
Test out your subclasses by creating instances of them in your main() function, and then calling battle() repeatedly to see how they fare against one another.
Final Challenge: Coder's Choice!
Add some additional functionality to make the game more interesting: you might implement a more complex formula for .attack()/.defend(), add multiple kinds of .attack() and allow the player to choose between them, use ASCII art to make the game more visually appealing... how far you go is entirely up to you!
Recommendation: if you find yourself adding the same functionality to every subclass (e.g. if you want your Pokémon to have a nickname, etc.), consider changing the parent class instead!

