Reputation: 11
I am relatively new to Pygame and trying to make a top-down racing game. Everything has currently been made and is ready to go! However, I am unable to make proper collision detection between walls and background of the "level".
What I want to happen is that the car cannot leave the boundaries of the track it is pushed back onto the track and speed is reduced. Previously this was attempted with pygame.sprite.collide_rect to no luck.
Here is a screenshot of the first track within the game and one of the cars in-game. A background will be added at a later date.
Any advice on which functions to use would be much appreciated! Would be possible to even use .collidepoint like done in menus?
Thanks, Adam.
Upvotes: 0
Views: 931
Reputation: 137
What you could do is, check if the two images overlap, but one condition: the image of the track must be full opacity, and everything else transparent. Then use this code to check if overlapping.
Car_mask = pygame.mask.from_surface(CarImage)
Track_mask = pygame.mask.from_surface(TrackImage)
offset_x, offset_y = (Car.x - Track.y), (Car.y - Track.y)
if (Track_mask.overlap(Car_mask, (offset_x,offset_y)) != None):
print("overlaps")
else:
#Push Car Back On Track, Slow Speed Stuff
#Cause it not touching track at all.
Upvotes: 1