2

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.

RossC
  • 101
  • 2
  • 8
  • 3
    This is significantly better. It's frowned upon to delete and repost questions, but this is an improvement for sure. It's still missing a [mcve], but at least the amount of code was reduced, and the question is clearer. – Carcigenicate Jan 22 '18 at 13:41
  • 3
    Since you never print false, you might just be thinking it's always printing True when it's really printing True sometimes and nothing otherwise. In any case, I'd change `if col == True: print ("True")` to `print (col)` and try it again. That might help find the problem. – Feathercrown Jan 22 '18 at 13:44
  • @Carcigenicate in the vehicle class, "self.rect = self.image.get_rect()" sets the size of the sprite to the same size as it's image. – RossC Jan 22 '18 at 13:46
  • @Feathercrown Instead of printing "True" constantly, it prints "1" constantly instead. – RossC Jan 22 '18 at 13:48
  • 1
    @RossC Thanks for trying it, I figured it was worth a shot lol – Feathercrown Jan 22 '18 at 13:50
  • I believe the problem is your hitbox (self.rect) that you receive from `image.get_rect()`. The method returns a rectangle the size of the image, but since an image doesn't have any positional data, the returned rectangle will be placed at x, y = (0, 0). Assuming you're not moving the rect, all your vehicle's hitboxes will be placed at the same spot and thus always collide. – Ted Klein Bergman Jan 22 '18 at 15:31
  • @TedKleinBergman Thanks for reply, how do I get the hitbox to move with the Vehicle? – RossC Jan 22 '18 at 15:49
  • @RossC Where do you store the position of the vehicle? You have its size, but no position. The sprite class must have some position attribute. You'll likely need to use that. – Carcigenicate Jan 22 '18 at 15:50
  • Or if `rect` defines the position as well, you'll need to update that. You never seem to move the vehicle. – Carcigenicate Jan 22 '18 at 15:52
  • @Carcigenicate The xpos and ypos variables for the vehicles are defined outside of the class, but, in the end, the checkpoints the the cars run through will be stationary, so, how do I blit the location of a rect, to the location of a checkpoint image? – RossC Jan 22 '18 at 15:55
  • @RossC Again, the Sprite class probably defines the position somehow, and that'll be what `collide_rect` will use. If you're storing the position outside of the class, how is Pygame supposed to know it? – Carcigenicate Jan 22 '18 at 15:57
  • https://stackoverflow.com/questions/15988782/how-do-i-determine-a-sprites-position-in-pygame `rect` defines the position as well as the size. You'll need to update that if you want to use Pygame's collision detection. – Carcigenicate Jan 22 '18 at 15:58
  • @Carcigenicate But if I add xpos and ypos attributes to the Vehicle class, surely the rect of the Vehicle won't automatically pick those up? – RossC Jan 22 '18 at 16:01
  • @RossC No, again, you need to manually update `rect`. Read the post I linked to. – Carcigenicate Jan 22 '18 at 16:04
  • 1
    @RossC Just create a `move` method in the vehicle class that handles updating `rect` so the messy bits are tucked away. – Carcigenicate Jan 22 '18 at 16:05
  • 1
    @RossC I would suggest not having a position attribute, and instead use the rect for positioning. The rect can also be used to blit, like `screen.blit(vehicle.image, vehicle.rect)`, and if you're using sprite groups it'll automatically blit the sprites like this. However, this has a problem since rect only hold integers, which means that moving and positioning isn't exact (which is explained more [here](https://stackoverflow.com/a/39328641/6486738)). – Ted Klein Bergman Jan 22 '18 at 16:22

0 Answers0