Habib Ismail
Habib Ismail

Reputation: 47

Pygame// Collision On a Square isnt working well

so the problem here with my collision is that when ever I Jumping on one of my squares when I jump on the middle its good but when I jump on the side it pushes me back??? is there a way to fix this so when I jump on my square it doesn't push me back it just lands on the square without any problems? here's a gif https://gyazo.com/a5ee7bba49dc557a2a42b64808618d7b everything is good when you jump on the middle of the square but when you jump on top of the square sides you will get pushed back but on the middle you stay there my collision is located at the bottom of the script

import pygame
pygame.init()


window = pygame.display.set_mode((500,500))
pygame.display.set_caption("for test")

# player1
x = 50
y = 340
height = 50
width = 50
isJump = False
jumpCount = 10
speed = 5
fall = 0
#----------------------

# Square2
xcord = 390
ycord = 320
heights = 60
widths = 60
#-----------------------

# Square2
cordx = 300
cordy = 390
hts = 60
wts = 60
#-----------------------
#FPS
FPS = 60
clock = pygame.time.Clock()
#--------------------
# main Loop

runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

    window.fill((0,0,0))
    Player = pygame.draw.rect(window, (105, 0, 105), (x,y,height,width))
    Enemy = pygame.draw.rect(window, (255, 0, 255), (xcord,ycord,heights,widths))
    Enemy2 = pygame.draw.rect(window, (155, 155, 155), (cordx,cordy,hts,wts))


    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed

    if not isJump:

        y += fall
        fall += 1
# ----------------------------------------------------- # enem1 collisio
# both of my 2 enemy squares collisions push me back when ever I Jump on the top of them on there sides but when I jump on the middle of of both of them it seems to work if I just want it so when I jump on both of my squares I just don't get pushed back 
        Player.topleft = (x, y)
        collide = False
        if Player.colliderect(Enemy):
            collide = True
            y = Enemy.top - Player.height
            if Player.right > Enemy.left and  Player.left < Enemy.left:
                x = Enemy.left - Player.width
            if Player.left < Enemy.right and  Player.right > Enemy.right:
                x = Enemy.right

#------------------------------------------- Enemy 2 Colision
        if Player.colliderect(Enemy2):
            collide = True
            y = Enemy2.top - Player.height
            if Player.right > Enemy2.left and  Player.left < Enemy2.left:
                x = Enemy2.left - Player.width
            if Player.left < Enemy2.right and  Player.right > Enemy2.right:
                x = Enemy2.right

        if Player.bottom >= 500:
            collide = True
            y = 500 - Player.height

        if collide:
            if keys[pygame.K_SPACE]:
                isJump = True
            fall = 0
#------------------------------------------------------------------------
    else:
        if jumpCount > 0:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: 
            jumpCount = 10
            isJump = False

    pygame.display.update()
pygame.quit  

Upvotes: 1

Views: 52

Answers (1)

kaktus_car
kaktus_car

Reputation: 986

line 77 under "Enemy 2 Collision" add -Player.width

if Player.right > Enemy2.left and Player.left < Enemy2.left - Player.width:

It is fix only for the first block (on its left side), if this is the behaviour you want then your code needs few quick fixes and you are good to go. Also one note, do not use uppercase for Player, Enemy, Enemy2, use all lowercase because the uppercase refers to a Class name.

Upvotes: 2

Related Questions