Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – defining a class that represents a tank


To start out, we will define a class that models a tank (meaning a tracked, armored fighting vehicle). A tank typically runs on two continuous tracks, and it has a rotating turret that houses a powerful cannon. The relationship between the various components of a tank is shown in the following diagram:

Based on this conceptual representation, we will define classes to represent the cannon, the tracks, and turret. We will then define another class that utilizes these classes to model a tank. To keep the example simple, we'll use point values to represent the relative strength of the tank's armor and the damage inflicted by its main gun. Although this is a simple "toy" example, a class like this could be used as the foundation of a video game or a simulator for military training:

class Cannon():

    """Model of a large cannon."""

    def __init__(self, damage):
        """Create a Cannon instance
            Arguments:
            ...