Tuesday, 6 October 2020

How do I make my player collide with my platform for all sides

So I been wondering how to make it so when my player touches any side of my platform, the player will act like there is a wall there, for example,

https://gyazo.com/272b729154b0790fd3e004c761cdb658

Its just fazing though the sides and is only colliding with the top. I want it to stop doing that and actually collide with the sides and also the bottom to, I have tried to watch some tutorials but most of them were neither talking about a different topic or I just did not get it and also I have tried the code shown bellow but that's not working.

The code I have tried


    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
             x_change = -7
        if event.key == pygame.K_a:
             x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
             x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x

My full code

import pygame
pygame.init

window = pygame.display.set_mode((700,500))

pygame.display.set_caption("Noobs First Game")


# Playerman
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

class bullet(object):
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = 10
        self.color = color
        self.rect.topleft = (self.x,self.y)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y =y
        self. width = width
        self.color = color
        self.height = height
        self.color = color
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


# Colors for hitbox

white = (255,255,255)

# Drawing Player
playerman = Player(255,255,40,40,white)

#Drawing Platforms
platform1 = Platform(200,470,100,30,white)
platform2 = Platform(400,410,100,30,white)



# List
platforms = [platform1,platform2]




# Windows color
def redrawwindow():
    window.fill((0,0,0))

# Drawing the player and other stuff to the screen
    playerman.draw()

    for Platform in platforms:
        Platform.draw()


x = 10
y = 10

x_change = 0
y_change = 0
old_x = x
old_y = y

        


fps = (30)
clock = pygame.time.Clock()


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


    
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
             x_change = -7
        if event.key == pygame.K_a:
             x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
             x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x

            
    # lets player move
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]and playerman.x > playerman.speed:
            playerman.x -= playerman.speed

    if keys[pygame.K_d]and playerman.x < 700 - playerman.height - playerman.speed:
        playerman.x += playerman.speed

    if keys[pygame.K_w]and playerman.y > playerman.speed:
        playerman.y -= playerman.speed

    if keys[pygame.K_s]and playerman.y <500 - playerman.width - playerman.speed:
        playerman.y += playerman.speed

  
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.rect.colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height + 1
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
        
           
                    
            # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    

                

    
    
        




    redrawwindow()
    pygame.display.update()
pygame.quit()
        


        
        





from How do I make my player collide with my platform for all sides

No comments:

Post a Comment