I apologise if you have seen a post of mine regarding this issue already, I am desperate to solve this.
I am creating a 2-player top down racing game and need to create a way for the program to know when the users have completed a lap. I have chosen to do this by placing sprites on the track, with the same texture as the track, so that they blend in. The idea is to create a list for each player's vehicle, containing boolean variables, one variable for each checkpoint. Every boolean variable in the list has to equal "True" in order for a lap to be counted.
Before I attempt this, I want to figure out how it can be done by detecting when the player's vehicles collide with each other. I have attempted, with help from a previous post, to do this.
My vehicle class can be seen, with my checkCollision function, below.
class Vehicle(pygame.sprite.Sprite):
'Base class for all vehicles (Cars and Motorbikes) in the game'
vehicleCount = 0
def __init__(self, max_speed, acceleration, turning_radius, image):
#pygame.sprite.Sprite.__init__(self)
self.max_speed = max_speed
self.acceleration = acceleration
self.turning_radius = turning_radius
self.image = image
self.rect = self.image.get_rect()
Vehicle.vehicleCount = Vehicle.vehicleCount + 1
def displayAmount():
print ("Total number of Vehicle enteries: ", Vehicle.vehicleCount)
def displayVehicle(self):
print ("max speed: ", self.max_speed, "acceleration: ", self.acceleration, "turning radius: ", self.turning_radius)
def checkCollision(self, sprite2):
col = pygame.sprite.collide_rect(self, sprite2)
if col == True:
print ("True")
Here is an example of one of my vehicle objects being defined:
sportscar = Vehicle(4.5, 0.2, 2.01, sportsimage)
I am checking if the vehicles have intersecting by running the following line in the main game loop:
choice1.checkCollision(choice2)
However, when I run the program, the console outputs "True" constantly, even when the vehicles aren't touching. Does anyone know where I have gone wrong with this? Thanks.