1

I'm just trying something with collisions and found the way to check one side of the rectangle with the other side.

I have the following problem:

If I move my game character (pink box) from the left against the object, my game character just moves through it:

Picture where it moves through it

If I come from the other side, everything works and my game character stops.

Picture where it stops

I mean to say that I need the same code for both sides but have to change the sides from if not player_rect.left == other_rect.right: to if not player_rect.right == other_rect.left:. But this does not work for one side.

import pygame
import sys

pygame.init()

clock = pygame.time.Clock()
window = pygame.display.set_mode([1200, 800])
pygame.display.set_caption("Collision Test")

x = 300
y = 300
width = 48
height = 96
velocity = 5

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

        is_pressed = pygame.key.get_pressed()
        player_rect = pygame.Rect(x, y, width, height)
        other_rect = pygame.Rect(400, 300, 50, 50)

        if is_pressed[pygame.K_d]:
            if not player_rect.right == other_rect.left:
                x += velocity
        if is_pressed[pygame.K_a]:
            if not player_rect.left == other_rect.right:
                x -= velocity

    window.fill((100, 150, 50))
    pygame.draw.rect(window, (255, 50, 100), player_rect)
    pygame.draw.rect(window, (255, 100, 50), other_rect)
    pygame.display.update()
    clock.tick(60)

Max
  • 25
  • 4

1 Answers1

1

Use collideRect().

Move the object and test if the rectangles are colliding. When a collision is detected, change the position of the object according to the moving direction:

is_pressed = pygame.key.get_pressed()
move_right = is_pressed[pygame.K_d]
move_left = is_pressed[pygame.K_a]

if move_right:
    x += velocity
if move_left:
    x -= velocity

player_rect = pygame.Rect(x, y, width, height)
other_rect = pygame.Rect(400, 300, 50, 50)

if player_rect.colliderect(other_rect):
    if move_right:
        player_rect.right = other_rect.left
        x = player_rect.left
    if move_left:
        player_rect.left = other_rect.right
        x = player_rect.left

For a smooth movement you've to do evaluate pygame.key.get_pressed() in the application loop rather than the event loop.
See also What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN.

import pygame
import sys

pygame.init()

clock = pygame.time.Clock()
window = pygame.display.set_mode([1200, 800])
pygame.display.set_caption("Collision Test")

x = 300
y = 300
width = 48
height = 96
velocity = 5

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

    is_pressed = pygame.key.get_pressed()
    move_right = is_pressed[pygame.K_d]
    move_left = is_pressed[pygame.K_a]

    if move_right:
        x += velocity
    if move_left:
        x -= velocity

    player_rect = pygame.Rect(x, y, width, height)
    other_rect = pygame.Rect(400, 300, 50, 50)

    if player_rect.colliderect(other_rect):
        if move_right:
            player_rect.right = other_rect.left
            x = player_rect.left
        if move_left:
            player_rect.left = other_rect.right
            x = player_rect.left

    window.fill((100, 150, 50))
    pygame.draw.rect(window, (255, 50, 100), player_rect)
    pygame.draw.rect(window, (255, 100, 50), other_rect)
    pygame.display.update()
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Now how do I implement a jump or gravity.Of course this should work with the collisions above and below. I have already tried a lot of things, but I can't find a solution. I had added a jump but then I was on the rectangle and when I tried to run down it, I didn't fall down anymore. Please help me – Max Oct 20 '20 at 13:22
  • @Max If you have a new question, please [Ask a public question](https://stackoverflow.com/questions/ask). The comments section is not intended to be used to ask additional questions. Don't extend this question either, you'll have to ask a completely new question. Please share in the new question what you have tried so far. – Rabbid76 Oct 20 '20 at 13:25
  • Okay, sorry. But it says i need to wait 2 more days to ask a new Question. – Max Oct 20 '20 at 13:32
  • @Max Are you sure about that. Do you have to wait at all even though this question was upvoted and has an accepted answer? – Rabbid76 Oct 20 '20 at 13:37
  • "You have reached your question limit It looks like you might need a break - take a breather and come back soon! You've asked 2 questions recently, some of which have not been received very well by the..." It seems like it :/ – Max Oct 20 '20 at 13:45