trouble with pygame's colliderect

Question:

trying to use pygames collide rect method to detect collision for the x axis of the player.

the collisions are not registering at all… which is my problem
im trying to mave a square to the right when i hold down the d button, and when i reach the platform, the program below should restrict me from passing through it

player_x = 400
player_y = 300
pygame.init()

player = pygame.image.load(the player)
player_rect = player.get_rect(midbottom = (player_x,player_y))

platform=pygame.image.load(the platform)
platform_rect=platform.get_rect(midbottom = (500,320))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit
            exit()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_d]:
        player_x += 1

    platforms = [platform_rect]
    x_collision = False
    new_player_rect = player.get_rect(midbottom = (player_x,player_y))

    for p in platforms:
        if p.colliderect(new_player_rect):
            x_collision = True
            break
     if not x_collision:
     current_player_x = player_x

     screen.blit(BACKGROUND, (0,0))
     screen.blit(platform,platform_rect)
     screen.blit(player,(current_player_x, player_y))
     pygame.display.flip()
Asked By: SlowMoMoose

||

Answers:

You need to set player_x = current_player_x before you change player_x, becuase If the plyer collides with the platform player_x needs to set to its previous position:

current_player_x = player_x
run = True
while run:
    # [...]

    player_x = current_player_x
    if keys[pygame.K_d]:
        player_x += 1

However you can simplify the code using new_player_rect. Minimal example:

import pygame

pygame.init()
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()

BACKGROUND = pygame.Surface(screen.get_size())
player_x = 400
player_y = 300

player = pygame.Surface((20, 20))
player.fill((255, 0, 0))
player_rect = player.get_rect(midbottom = (player_x,player_y))

platform = pygame.Surface((20, 100))
platform.fill((128, 128, 128))
platform_rect=platform.get_rect(midbottom = (500,320))

current_player_x = player_x
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit
            exit()

    keys = pygame.key.get_pressed()
    new_player_rect = player.get_rect(midbottom = (player_x, player_y))
    if keys[pygame.K_d]:
        new_player_rect.x += 1

    platforms = [platform_rect]
    x_collision = False
    for p in platforms:
        if p.colliderect(new_player_rect):
            print("collide")
            x_collision = True
            break
    if not x_collision:
        player_x = new_player_rect.centerx

    screen.blit(BACKGROUND, (0,0))
    screen.blit(platform,platform_rect)
    screen.blit(player, player.get_rect(midbottom = (player_x, player_y)))
    pygame.display.flip()

Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.