In order to assign different movement directions to different instances of our class we need to use Vectors.
The best way to use Vectors is to also use a Position as a vector instead of x and y values.
for example
instead of
x=220, y = 100
we use
position(220,100)
STEP ONE: MODIFY OUR INIT FUNCTION
Combine our x and y parameters into one position parameter
change
def __init__(self, x, y):
to
def __init__(self, position):
and create a self._____ for the position variable
self.position = position
def __init__(self, position):
self.position = position
STEP TWO: MODIFY OUR CODE WHICH CREATES AN INSTANCE
Previously we have created an instance by providing and x and y value
Now we have to create an instance by providing a position
i.e. dog(100,100)
to dog((100,100))
change
dogArray.append(dog(random.randrange(10, display_width-50),random.randrange(10, display_height-100)))
to
dogArray.append(dog((random.randrange(10, display_width-50),random.randrange(10, display_height-100))))
STEP THREE: MODIFY OUR DRAW FUNCTION TO USE THE NEW POSITION PARAMETER
change
gameDisplay.blit(dogImg, (self.x, self.y))
to
gameDisplay.blit(dogImg, self.position)
def draw(self):
gameDisplay.blit(dogImg, self.position)
STEP FOUR: MODIFY OUR MOVE FUNCTION TO USE POSITION
Instead of using the y variable (self.y) we need to use the second component of position
change
self.y += 1
to
self.position = (self.position[0], self.position[1] + 1)
def move(self):
self.position = (self.position[0], self.position[1] + 1)
Now we have to change the code which looks to see if the object has moved of the bottom of the screen.
change
if self.y > display_height – 50:
to
if self.position[1] > display_height – 50:
Lastly, we need to modify our code which adds a new instance once one has moved off the bottom of the screen
change
dogArray.append(dog(random.randrange(10, display_width-50),10))
to
dogArray.append(dog((random.randrange(10, display_width-50),10)))