Linux Format

OBJECT ORIENTATED PROGRAMMIN­G

-

Object Orientated Programmin­g (OOP) techniques have been used throughout the Star Fighter project. The two main Object Orientated tools that have been used are classes, creating instances of classes (commonly known as an object) and inheritanc­e.

A class can be looked as a template for the object to be created that contains operations known as methods and variables that are known as properties in the world of OOP, which all can help determine how the object will behave once it’s created. Let’s look at an example that can be found in the source code: class PlayerBull­et(pygame.sprite.Sprite):

def __init__(self, image, position, velocity):

super().__init__() self.image = image self.rect = self.image.get_rect() self.rect.centerx = position.x self.rect.bottom = position.y self. position = Vec2(self.rect.centerx, self. rect.bottom)

self.velocity = Vec2(velocity.x, velocity.y) self.radius = PLAYER_BULLET_

RADIUS

A class here in the above example is declared as PlayerBull­et, which is the name of the class itself. Within the definition of the class there exists what’s called the class constructo­r. Here’s an example of this: def __init__(self, image, position, velocity): super().__init__()

The class constructo­r is mainly used to initialise property values and perform initial operations when an instance of that class is created. In the class shown in this example, the constructo­r is used to store an image, set position and velocity values. An instance of this class is declared later on in the source code.

def _attack(self):

As can be seen from the above code segment, an instance of the class is created by first calling the class by name and then passing in values to form an object of that class.

b = PlayerBull­et(self.bullet_image, …..)

Newspapers in English

Newspapers from Australia