Racey – Adding Instances

Now you might want to add multiple instances of your class

STEP ONE: Randomly placing instances on the screen

Python has a function called randrange and can be used to generate a number between a top and bottom value
i.e. random.randrange(1,10) will randomly generate a number between 1 and 10

There for to randomly place our class on the screen we can randomly generate a x and y value
x = random.randrange(10,800)

While this works, it may be better to use the variable we have for our screen width, and also allow for the width of the image so that it is not half out of the screen.
x = random.randrange(10, display_width-50)

We can do the same with the y value
random.randrange(10, display_height-100)

Using this we can change the code for generating a new instance of our class frok

dogArray.append(dog(200,200))

to

dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))

dogArray = []
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))

STEP TWO: Adding Multiple instances

Instead of adding just on instance we can add lots of instances.

dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))

however,
it would be much better to do that in a loop

for x in range(1, 5): 
    dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))

This will generate 4 instances of your class.

Try changing the number to get lots of instances.