Welcome to part 15 of the intermediate Python programming tutorial series. In this tutorial, we're going to explore some of the properties of using our object.
Code from the previous tutorial:
import pygame
import random
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
game_display = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Blob World')
clock = pygame.time.Clock()
class Blob:
def __init__(self, color):
self.x = random.randrange(0, WIDTH)
self.y = random.randrange(0, HEIGHT)
self.size = random.randrange(4,8)
self.color = color
def move(self):
self.move_x = random.randrange(-1,2)
self.move_y = random.randrange(-1,2)
self.x += self.move_x
self.y += self.move_y
if self.x < 0: self.x = 0
elif self.x > WIDTH: self.x = WIDTH
if self.y < 0: self.y = 0
elif self.y > HEIGHT: self.y = HEIGHT
def draw_environment(blob):
game_display.fill(WHITE)
pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size)
pygame.display.update()
blob.move()
def main():
red_blob = Blob(RED)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment(red_blob)
clock.tick(60)
if __name__ == '__main__':
main()
To begin, we're going to take our simple class, and illustrate how we can quickly generate many objects from our class. To begin, let's add some new constants:
STARTING_BLUE_BLOBS = 10 STARTING_RED_BLOBS = 3
Next, let's consider a scenario where we want to add a bunch of blue blobs from our Blob class. We might go into our main function and do something like:
def main():
blue_blobs = [Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]
Then we could modify the draw_environment function:
def draw_environment(blobs):
game_display.fill(WHITE)
for blob in blobs:
pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size)
blob.move()
pygame.display.update()
Now we have many blue blobs wiggling about! That's fine, but we should probably should have some way of tracking each blob, otherwise we really don't know which blob is which. In the future, we might want to actually know blobs by some sort of id or name. If only we knew of a great way to assign a sort of id via a counter and then maybe make a dictionary out of it.... oh yeah! We just learned about
Thus, back in our main function:
def main():
blue_blobs = dict(enumerate([Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]))
Now we have a dictionary, where the key is an ID, and the value is the blob object. While we're at it, let's add some red_blobs:
def main():
blue_blobs = dict(enumerate([Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]))
red_blobs = dict(enumerate([Blob(RED) for i in range(STARTING_RED_BLOBS)]))
Now, what we can do is instead pass a list of dictionaries to our draw_environments function, doing this in the main function:
def main():
blue_blobs = dict(enumerate([Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]))
red_blobs = dict(enumerate([Blob(RED) for i in range(STARTING_RED_BLOBS)]))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment([blue_blobs,red_blobs])
clock.tick(60)
Now ourdraw_environments function:
def draw_environment(blob_list):
game_display.fill(WHITE)
for blob_dict in blob_list:
for blob_id in blob_dict:
blob = blob_dict[blob_id]
pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size)
blob.move()
pygame.display.update()
Now we're taking this list of blob_dicts, where the dictionaries are key: id, value: object. We iterate through blob_dicts by id, and then draw and move the blobs as usual.
Full code up to this point:
import pygame
import random
STARTING_BLUE_BLOBS = 10
STARTING_RED_BLOBS = 3
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
game_display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Blob World")
clock = pygame.time.Clock()
class Blob:
def __init__(self, color):
self.x = random.randrange(0, WIDTH)
self.y = random.randrange(0, HEIGHT)
self.size = random.randrange(4,8)
self.color = color
def move(self):
self.move_x = random.randrange(-1,2)
self.move_y = random.randrange(-1,2)
self.x += self.move_x
self.y += self.move_y
if self.x < 0: self.x = 0
elif self.x > WIDTH: self.x = WIDTH
if self.y < 0: self.y = 0
elif self.y > HEIGHT: self.y = HEIGHT
def draw_environment(blob_list):
game_display.fill(WHITE)
for blob_dict in blob_list:
for blob_id in blob_dict:
blob = blob_dict[blob_id]
pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size)
blob.move()
pygame.display.update()
def main():
blue_blobs = dict(enumerate([Blob(BLUE) for i in range(STARTING_BLUE_BLOBS)]))
red_blobs = dict(enumerate([Blob(RED) for i in range(STARTING_RED_BLOBS)]))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment([blue_blobs,red_blobs])
clock.tick(60)
if __name__ == '__main__':
main()
We're off to a good start, but we already have some fundamental issues with this program, which I will discuss in the next tutorial!