Programming Basics

  Home arrow Programming Basics arrow Exploring Object-Oriented Programming ...
PROGRAMMING BASICS

Exploring Object-Oriented Programming with Python
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-07-27

    Table of Contents:
  • Exploring Object-Oriented Programming with Python
  • Using Classes

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Exploring Object-Oriented Programming with Python


    (Page 1 of 2 )

    In this fourth part of six-part series on the Python programming language, we'll explain what object-oriented programming is, and take a look at classes in Python. This article is excerpted from chapter two of the book Beginning Game Development with Python and Pygame: From Novice to Professional, written by Will McGugan (Apress; ISBN: 9781590598726).

    Introducing Object-Oriented Programming

    You may have heard the term object-oriented programming (OOP) before. But don’t worry if you are unfamiliar with it, because the concept is remarkably simple.

    So what exactly is an object in OOP terms? Well, it can literally be anything. In a game we may have an object for a particle—say, a burning ember emitted from an explosion, or the hover tank that caused the explosion. In fact, the entire game world could be an object. The purpose of an object is to contain information and to give the programmer the ability to do things with that information.

    When constructing an object, it is usually best to start by working out what information, or properties, it contains. Let’s think about what would be found in an object designed to represent a futuristic hover tank. It should contain a bare minimum of the following properties:

    1. Position: Where is the tank?
    2. Direction: In what direction is it facing?
    3. Speed: How fast is it going?
    4. Armor: How much armor does it have?
    5. Ammo: How many shells does it have?

    Now that we have the information to describe a tank and what it is doing, we need to give it the ability to perform all the actions that a tank needs to do in a game. In OOP-speak, these actions are called methods. I can think of the following methods that a tank will definitely require, but there will likely be far more:

    1. Move: Move the tank forward.
    2. Turn: Rotate the tank left/right.
    3. Fire: Launch a shell.
    4. Hit: This is the action when an enemy shell hits the tank.
    5. Explode: Replace the tank with an explosion animation.

    You can see that the methods typically change the properties of an object. When theMovemethod is used, it will update the tank’sPosition. Similarly, when theFiremethod is used, the value ofAmmo will be updated (unless of course there is noAmmo left; thenFirewould not do anything!).

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap