Search code examples
pythonpygamegame-physics

Pygame character movement collision? (confusion)


def Player_Movement():
    global x_plr, y_plr, X_vel, Y_vel, Main_Player
    keys = pygame.key.get_pressed()
    speed = 5
    if X_vel > 0:
        X_vel -= 1
    if X_vel < 0:
        X_vel += 1
    if Y_vel > 0:
        Y_vel -= 1
    if Y_vel < 0:
        Y_vel += 1

    if keys[pygame.K_d]:
        X_vel = speed
    if keys[pygame.K_a]:
        X_vel = -speed
    if keys[pygame.K_w]:
        Y_vel = -speed
    if keys[pygame.K_s]:
        Y_vel = speed
    
    if Main_Player.colliderect(Block):
        Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))
        X_vel += abs(X_vel)
        print(X_vel)
    x_plr += X_vel

    if Main_Player.colliderect(Block):
        Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))
        Y_vel += abs(Y_vel)
        print(Y_vel)
    y_plr += Y_vel

Here's my player movement and all i want is just to make it stop when touching the "Block" so i don't phase through it. Im new to Pygame and python in general so help would be appreciated!

I don't have much idea of any other ways i could do it but just for help here's my entire code:

import pygame
pygame.init()

clock = pygame.time.Clock()
winWidth = 1100
winHeight = 700
win = pygame.display.set_mode((winWidth, winHeight))
pygame.display.set_caption('my caption')

# variables

x_plr = 200
y_plr = 100
X_vel = 0
Y_vel = 0
size = 25
playercolor = (255, 70, 70)
Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))

Block = pygame.draw.rect(win, (230, 230, 230), (500, 100, 50, 50))

def Displaying():
    global Main_Player, Block
    Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))
    Block = pygame.draw.rect(win, (230, 230, 230), (350, 100, 300, 50))
    

def Player_Movement():
    global x_plr, y_plr, X_vel, Y_vel, Main_Player
    keys = pygame.key.get_pressed()
    speed = 5
    if X_vel > 0:
        X_vel -= 1
    if X_vel < 0:
        X_vel += 1
    if Y_vel > 0:
        Y_vel -= 1
    if Y_vel < 0:
        Y_vel += 1

    if keys[pygame.K_d]:
        X_vel = speed
    if keys[pygame.K_a]:
        X_vel = -speed
    if keys[pygame.K_w]:
        Y_vel = -speed
    if keys[pygame.K_s]:
        Y_vel = speed
    
    if Main_Player.colliderect(Block):
        Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))
        X_vel += abs(X_vel)
        print(X_vel)
    x_plr += X_vel

    if Main_Player.colliderect(Block):
        Main_Player = pygame.draw.rect(win, playercolor, (x_plr, y_plr, size, size))
        Y_vel += abs(Y_vel)
        print(Y_vel)
    y_plr += Y_vel
        
        


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
    win.fill((50,50,50))

    Player_Movement()

   

    
    Displaying()
    pygame.display.update()
    pygame.display.update(Main_Player)
    clock.tick(60)

I hope someone could help me!


Solution

  • Restrict the position of the player with the bounds of the block. Update Main_Player before the collision detection:

    def Player_Movement():
        global x_plr, y_plr, X_vel, Y_vel, Main_Player
        keys = pygame.key.get_pressed()
        speed = 5
        
        X_vel = (keys[pygame.K_d] - keys[pygame.K_a]) * speed
        Y_vel = (keys[pygame.K_s] - keys[pygame.K_w]) * speed
         
        x_plr += X_vel
        Main_Player = pygame.Rect(x_plr, y_plr, size, size)
        if Main_Player.colliderect(Block):
            if X_vel > 0:
                x_plr = Block.left - size
            elif X_vel < 0:
                x_plr = Block.right 
    
        y_plr += Y_vel
        Main_Player = pygame.Rect(x_plr, y_plr, size, size)
        if Main_Player.colliderect(Block):
            if Y_vel > 0:
                y_plr = Block.top - size
            elif Y_vel < 0:
                y_plr = Block.bottom