1

I need help desperately. I have been trying to implement collision for my pygame code for hours now but nothing is working. I have researched a lot of methods but non of them are sticking to my mind or if there is a possible way to implement them.

Here is the code:

import pygame
import  random

pygame.init()

#SCREEN
screeenWidth = 320
screenHeight = 600
screen = pygame.display.set_mode((screeenWidth, screenHeight))
screenBackground = pygame.image.load('assets/background.png')
screen.blit(screenBackground, (0,0))
pygame.display.set_caption("Evading Game")

#CLOCK
clock = pygame.time.Clock()

#PLAYER
playerX = 20 #which is lane 1
playerY = screenHeight * 0.8
playerImage = pygame.image.load('assets/car_img.png')
lane_dictionary = {1:20,2:85,3:160,4:240}
player_current_lane = 1


#OBSTACLE
obstacle_current_lane = random.randint(1,4)
obstacleX = lane_dictionary[obstacle_current_lane]
obstacleY = 0
obstacleImage = pygame.image.load('assets/obstacle_img.png')


running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN: 
            if event.key == pygame.K_RIGHT: #Lane 1 = 20, lane 2 = 80, lane 3 = 160, lane 4 = 240
                if not player_current_lane == 4:
                    player_current_lane += 1
                    e = lane_dictionary[player_current_lane]
                    playerX = e
            if event.key == pygame.K_LEFT: #Lane 1 = 20, lane 2 = 80, lane 3 = 160, lane 4 = 240
                if not player_current_lane == 1:
                    player_current_lane -= 1
                    e = lane_dictionary[player_current_lane]
                    playerX = e

    obstacleY += 1 #Obstacle movement

    screen.blit(screenBackground, (0,0)) #Blit background
    screen.blit(playerImage, (playerX, playerY)) #blit player
    screen.blit(obstacleImage, (obstacleX, obstacleY)) #Blit obstacle
    pygame.display.update()


    clock.tick(60) #fps

pygame.quit()
quit()
Yawn
  • 71
  • 7

1 Answers1

1

Create a pygame.Rect object with the size of playerImage and the location of the player (playerX, playerY).
Create a 2nd pygame.Rect, with the size of obstacleImage and the location of the obstacle (obstacleX, obstacleY).
Use colliderect to detect a collision between the 2 rectangles. e.g:

while running:
    # [...]

    playerRect = playerImage.get_rect(topleft = (playerX, playerY))
    obstacleRect = obstacleImage.get_rect(topleft = (obstacleX, obstacleY))
    if playerRect.colliderect(obstacleRect):
        print("hit")

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Yawn `topleft` is a keyword argument, that specifies the position of the rectangle. A Surface object has no position, it has a size only. See [`pygame.Surface.get_rect(**kwargs)`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect). All the pygame classes and methods are well documented. The pygame documentation provides a lot of useful information. – Rabbid76 Mar 08 '20 at 07:30