Racey – Creating a Class

STEP ONE: LOAD A DOG IMAGE

You will need to find an image and save it into the code folder

You should do this near the top of your code where you are already loading images

dogImg = pygame.image.load('dogsm.png')

STEP TWO: CREATE A BASIC CLASS

You should do this near the top of the page, under where you are loading images and before you declaration of functions

define a class
class dog:

define an initialisation function which receives an x and y value
def __init__(self, x, y):

save the x and y values
self.x = x
self.y = y

define a draw function
def draw(self):

load the image to the display
gameDisplay.blit(dogImg, (self.x, self.y))

class dog:
    def __init__(self, x, y):
        self.x = x
        self.y = y
  
    def draw(self):
        gameDisplay.blit(dogImg, (self.x, self.y))

STEP THREE: CREATE AN ARRAY AND ADD AN INSTANCE OF DOG

You need to do this out side your Game Loop, but usually close to it so it is easy to find

create the array
dogArray = []

create an instance of dog and append it to the array
dogArray.append(dog(200,200))

dogArray = []
dogArray.append(dog(200,200))

STEP FOUR: DISPLAY ALL THE DOGS IN YOUR ARRAY

Inside your game loop, after your event code and after creating the background of your screen.

loop through your dogArray
for dog in dogArray:

and inside that loop, draw each dog
    dog.draw()

for dog in dogArray:
    dog.draw()