Programming: Objects & Attributes


With the general menu structure created, we can start filling up with game windows with actual content. And the first thing any game needs are characters!

Python is an object-oriented programming language. This means that objects are created from classes, which contain the attributes used in functions. These attributes are variables like a character's position, healthpoints, stats, and anything else used for the game. Each object has the same types of attributes within the class, but with independent values. This means each object is

Syntax

 The class is first created by defining its type. "Objects" are the simplest class used to define a set of attributes. The “def__init__” string is used to initialize the class. This allows attributes to be created from other variables using “self” references. These are flexible, implicit references which makes them very dynamic. 


I’ll define a basic "Character" class: 
 class Characters(object):
     def__init__(self, x, y, color):
         self.x = x          self.y = y          self.color = color

To create an object from this "Character" class, it requires inputs for x, y, and color. The class function assigns each variable specific to the object as "self.x" or "self.y".  I can use the "Character" class to create an object called “Red_Guy”, starting as position (100, 100):
 Red_Guy = Characters(100, 100, (255, 0, 0)) 
When an object is created from the class, the individual attributes can be referenced from the object:
print(Red_Guy.color)
returns (255, 0, 0) 
 
Pygame has functions for drawing shapes to the game window. The draw function has the following inputs for drawing a rectangle at a certain coordinate onto a surface;
pygame.draw.rect(surface, color, rect) 
The "rect" input is a tuple to define: x, y, width, and height values

I'll define the surface as the game window "win", with size of 400 by 300 pixels. Then I'll plug in in the "Red_Guy" attributes to draw the object as a rectangle. I'll set the pixel width and height as 5 x 10. 
win = pygame.display.set_mode
pygame.draw.rect(win, Red_Guy.color, (Red_Guy.x, Red_Guy.y, 5, 10))
I'll compile this all together with the other initializing commands and the main while loop:
class Characters(object):
def __init__(self, x, y, color): self.x = x self.y = y self.color = color def main(): win = pygame.display.set_mode((400, 300)) #Creates window surface pygame.init() #Initializes Pygame module Red_Guy = Characters(100, 100, (255, 0, 0)) #Creates object

	while True: #Main Game Loop
            for event in pygame.event.get(): #Exit game on window close
                if event.type == pygame.QUIT:
                    return
    	    pygame.draw.rect(win, Red_Guy.color, (Red_Guy.x, Red_Guy.y, 5, 10))
	    pygame.display.flip()

main()
Running this script in Python (with Pygame module installed) creates the game window, defines the object, and draws it at the pixel coordinate (100, 100).
I can create additional objects to give "Red_Guy" some company. For each object, I'll adjust the x-position and the RGB color used in the "Character" class. 
Blue_Guy = Characters(110, 100, (0, 0, 255))
Green_Guy = Characters(120, 100, (0, 255, 0))
Yellow_Guy = Characters(130, 100, (255, 255, 0))
Rather than typing out the "pygame.draw.rect" for each object, I can save some time and add them into a list. I can use a simple "For" loop to draw each object:
guy_list = [Red_Guy, Blue_Guy, Green_Guy, Yellow_Guy]
for guy in guy_list:
    pygame.draw.rect(win, guy.color, (guy.x, guy.y, 5, 10))
    pygame.display.flip()
Inserting that into the script and running generates the window below.

Wow, look at those guys! 

Each of those four rectangles represent individual object variables derived from the same class. This is the most basic use of objects in Python, but is the backbone for games. Since a game has many different characters running around the screen, the game needs a system for keeping track of each one individually. 


Comments