In this part of my Learn to Program series we learn about Classes, Objects, Self, __init__, Getters, Setters, Properties, and then create 2 warriors that fight to the death. It is the beginning knowledge we will eventually use to make awesome games.
All of the code along with a transcript of the video follows the video below. If you missed previous tutorials they start here.
If you like videos like this consider donating on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# ---------- LEARN TO PROGRAM 9 ---------- # Real world objects have attributes and capabilities # A dog for example has the attributes of height, weight # favorite food, etc. # It has the capability to run, bark, scratch, etc. # In object oriented programming we model real world objects # be defining the attributes (fields) and capabilities (methods) # that they have. # A class is the template used to model these objects # Here we will model a Dog object class Dog: # The init method is called to create an object # We give default values for the fields if none # are provided def __init__(self, name="", height=0, weight=0): # self allows an object to refer to itself # It is like how you refer to yourself with my # We will take the values passed in and assign # them to the new Dog objects fields (attributes) self.name = name self.height = height self.weight = weight # Define what happens when the Dog is asked to # demonstrate its capabilities def run(self): print("{} the dog runs".format(self.name)) def eat(self): print("{} the dog eats".format(self.name)) def bark(self): print("{} the dog barks".format(self.name)) def main(): # Create a new Dog object spot = Dog("Spot", 66, 26) spot.bark() bowser = Dog() main() # ---------- GETTERS & SETTERS ---------- # Getters and Setters are used to protect our objects # from assigning bad fields or for providing improved # output class Square: def __init__(self, height="0", width="0"): self.height = height self.width = width # This is the getter @property def height(self): print("Retrieving the height") # Put a __ before this private field return self.__height # This is the setter @height.setter def height(self, value): # We protect the height from receiving a bad value if value.isdigit(): # Put a __ before this private field self.__height = value else: print("Please only enter numbers for height") # This is the getter @property def width(self): print("Retrieving the width") return self.__width # This is the setter @width.setter def width(self, value): if value.isdigit(): self.__width = value else: print("Please only enter numbers for width") def getArea(self): return int(self.__width) * int(self.__height) def main(): aSquare = Square() height = input("Enter height : ") width = input("Enter width : ") aSquare.height = height aSquare.width = width print("Height :", aSquare.height) print("Width :", aSquare.width) print("The Area is :", aSquare.getArea()) main() # ---------- WARRIORS BATTLE ---------- # We will create a game with this sample output ''' Sam attacks Paul and deals 9 damage Paul is down to 10 health Paul attacks Sam and deals 7 damage Sam is down to 7 health Sam attacks Paul and deals 19 damage Paul is down to -9 health Paul has Died and Sam is Victorious Game Over ''' # We will create a Warrior & Battle class import random import math # Warriors will have names, health, and attack and block maximums # They will have the capabilities to attack and block random amounts class Warrior: def __init__(self, name="warrior", health=0, attkMax=0, blockMax=0): self.name = name self.health = health self.attkMax = attkMax self.blockMax = blockMax def attack(self): # Randomly calculate the attack amount # random() returns a value from 0.0 to 1.0 attkAmt = self.attkMax * (random.random() + .5) return attkAmt def block(self): # Randomly calculate how much of the attack was blocked blockAmt = self.blockMax * (random.random() + .5) return blockAmt # The Battle class will have the capability to loop until 1 Warrior dies # The Warriors will each get a turn to attack each turn class Battle: def startFight(self, warrior1, warrior2): # Continue looping until a Warrior dies switching back and # forth as the Warriors attack each other while True: if self.getAttackResult(warrior1, warrior2) == "Game Over": print("Game Over") break if self.getAttackResult(warrior2, warrior1) == "Game Over": print("Game Over") break # A function will receive each Warrior that will attack the other # Have the attack and block amounts be integers to make the results clean # Output the results of the fight as it goes # If a Warrior dies return that result to end the looping in the # above function # Make this method static because we don't need to use self @staticmethod def getAttackResult(warriorA, warriorB): warriorAAttkAmt = warriorA.attack() warriorBBlockAmt = warriorB.block() damage2WarriorB = math.ceil(warriorAAttkAmt - warriorBBlockAmt) warriorB.health = warriorB.health - damage2WarriorB print("{} attacks {} and deals {} damage".format(warriorA.name, warriorB.name, damage2WarriorB)) print("{} is down to {} health".format(warriorB.name, warriorB.health)) if warriorB.health <= 0: print("{} has Died and {} is Victorious".format(warriorB.name, warriorA.name)) return "Game Over" else: return "Fight Again" def main(): # Create 2 Warriors paul = Warrior("Paul", 50, 20, 10) sam = Warrior("Sam", 50, 20, 10) # Create Battle object battle = Battle() # Initiate Battle battle.startFight(paul, sam) main() |
Leave a Reply