Search code examples
pythonpygame

Pygame when moving into another object the player object should stop


I'm working on this prototype in pygame where you control a square and I'm currently working on collisions with other objects. When the player collides with the object, the player should not be able to move into the object.

import pygame,sys

pygame.init()
Clock = pygame.time.Clock()
FPS = 60
size = [1000,800]
bg = [0,0,0]
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Movement')


class Player:
    def __init__(self,vel,x,y):
        self.vel = vel
        
        self.x = x
        self.y = y
        self.jump = False

    def move(self):
        k = pygame.key.get_pressed()
        if k[pygame.K_a]:
            self.x -= self.vel
        if k[pygame.K_d]:
            self.x += self.vel
        if k[pygame.K_w]:
            self.y -= self.vel
        if k[pygame.K_s]:
            self.y += self.vel
  

    def draw(self):
        pygame.draw.rect(screen,"red",(self.x,self.y,50,50))

    def bordercollsion(self):
        if self.x <=0:
            self.x=0
        if self.x >=950:
            self.x=950
        if self.y <=0:
            self.y=0
        if self.y >=750:
            self.y=750
        
        

    def do(self):
        self.move()
        self.draw()
        self.bordercollsion()
        
    

player = Player(1,500,600)

while True:
    screen.fill(bg)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))
            if event.key == pygame.K_ESCAPE:
                run = False
                pygame.quit
                sys.exit()
        
    player.do()
    en = pygame.draw.rect(screen,"blue",(10,20,50,50))
    pygame.display.update()

Solution

  • See How do I detect collision in pygame?. Use pygame.Rect/colliderect to detect collisions of rectangles.

    Store the position of the player

    player_pos = player.x, player.y
    

    Move the palyer:

    player.do()
    

    Set up a rectangle for the player and the obstacle:

    player_rect = pygame.Rect(player.x, player.y, 50, 50)
    obstacle_rect = pygame.Rect(10, 20, 50, 50)
    

    Reset the position of the player, when the rectangles collide:

    if player_rect.colliderect(obstacle_rect):
        player.x, player.y = player_pos
    

    Complete application loop

    while True:
        screen.fill(bg)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                print(pygame.key.name(event.key))
                if event.key == pygame.K_ESCAPE:
                    run = False
                    pygame.quit
                    sys.exit()
            
        player_pos = player.x, player.y
        player.do()
        player_rect = pygame.Rect(player.x, player.y, 50, 50)
        obstacle_rect = pygame.Rect(10, 20, 50, 50)
        if player_rect.colliderect(obstacle_rect):
            player.x, player.y = player_pos
    
        en = pygame.draw.rect(screen,"blue",(10,20,50,50))
        pygame.display.update()