4.3 Tanks, Targets, and Bullets
You’ll notice that the game object class that we programmed had a lot of methods but wasn’t very specific. This is because tanks, targets, and bullets all have the defined methods and attributes in common (they all need to be moved, to be drawn, to check collisions, etc.). So, we can use inheritance, which you’ll remember from Python section 12.5, to pass these attributes to separate Tank, Target, and Bullet classes.
As you can see, the
Target
class inherits from the game object, but always sets the size to (40, 40) because targets should only be one size.class Target(Game_obj): def __init__(self, **kwargs) -> None: kwargs["size"] = 40, 40 super().__init__(TARGET_IMG_PATH, **kwargs) self.name = "Target"
As you can see, the
Bullet
class inherits from the game object too.class Bullet(Game_obj): def __init__(self, **kwargs) -> None: super().__init__(BULLET_IMG_PATH, **kwargs) self.name = "Bullet"
This is why we use inheritance; it makes these classes much easier to define. After all, those classes only took a few lines of code.
While
Tank
does inherit from the game object, the Tank
class is more complicated than the other two because it has to deal with player input. Since players can press and release the movement buttons, in this case, WASD, we have a set_path
and an unset_path
method to deal with those occurrences, respectively. set_speed
is the method that we call after dealing with players pressing and/or releasing WASD to get the resulting speed to use when we call Tank
’s move()
method.class Tank(Game_obj): def __init__(self, **kwargs) -> None: super().__init__(TANK_IMG_PATH, **kwargs) self.direction = [0, 0] self.SPEED = kwargs["speed"] if "speed" in kwargs else [2, 2] self.speed["x"], self.speed["y"] = 0, 0 def set_speed(self) -> None: # use math stuff to calculate the speed given that the # max speed is self.SPEED self.speed["x"] = ( self.direction[0] / math.sqrt(sum(abs(num) for num in self.direction)) * self.SPEED[0] if (sum(abs(num) for num in self.direction)) != 0 else self.direction[0] * self.SPEED[0] ) self.speed["y"] = ( self.direction[1] / math.sqrt(sum(abs(num) for num in self.direction)) * self.SPEED[1] if (sum(abs(num) for num in self.direction)) != 0 else self.direction[1] * self.SPEED[1] ) def set_path(self, direction: str) -> None: if direction == "up": self.direction[1] -= 1 if direction == "down": self.direction[1] += 1 if direction == "left": self.direction[0] -= 1 if direction == "right": self.direction[0] += 1 def unset_path(self, direction: str) -> None: if direction == "up": self.direction[1] += 1 if direction == "down": self.direction[1] -= 1 if direction == "left": self.direction[0] += 1 if direction == "right": self.direction[0] -= 1
Similar to what we did, whenever you create a game of your own with more than one object, it is very useful to create a base class and then have your objects inherit from the base class (if the objects are similar).
Previous Section
4.2 - Object ClassNext Section
4.4 - App ClassCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.