So I have a gravity game where the player can switch gravity and I'm having a problem with collision. when my player collides with the side of the platform, it will just go inside the platform but I want it to stop with it collides with the sides. I have a collision with the sides but that collision dose not work on my game, it only makes the player just freeze up. Some stuff I have tried is making it so when it is not colliding with my platform to stop moving but that also just froze my player, I also have tried telling it when it is colliding with the sides, but the player is always colliding with the platform.https://gyazo.com/0aa35529f5ecd7933f9e6c7c9d16f541
the collision I have tried using
px,py = playerman.x,playerman.y
if keys[pygame.K_a] and px > playerman.speed:
px += playerman.speed
if keys[pygame.K_d] and px > playerman.speed:
px -= playerman.speed
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
My code
import pygame
pygame.init()
# width and height of winddow
window = pygame.display.set_mode((700,500))
# window name
pygame.display.set_caption("Game")
#Player class
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 = 4
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)
class Platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
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)
class Platform2:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
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
white = (255,255,255)
green = (255,0,0)
# class's cordination, size, and color
#Player
playerman = Player(300,250,40,70,white)
#Platform
platform1 = Platform(0,460,800,40,white)
platform2 = Platform(400,200,40,40,green)
#The top platform
platfom2 = Platform2(0,0,800,40,green)
# List's
platforms = [platform1,platform2]
platforms2 = [platfom2]
# drawing things in main loop
def redraw():
# background color
window.fill((0,0,0))
# drawing the player in main loop
playerman.draw()
# Drawing all the bottom platforms in the main loop
for Platform in platforms:
Platform.draw()
# Drawining all the top platforms in the main loop
for Platform2 in platforms2:
Platform2.draw()
# Timer for player falling
#fps for the game
fps = 30
clock = pygame.time.Clock()
up = True
down = False
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
px,py = playerman.x,playerman.y
if down == False:
for Platform in platforms2:
if keys[pygame.K_SPACE] and playerman.rect.colliderect(Platform):
up = True
if up:
playerman.y += 10
fall = False
for Platform in platforms:
if playerman.rect.colliderect(Platform.rect):
up = False
if up == False and keys[pygame.K_SPACE]:
for Platform in platforms:
if playerman.rect.colliderect(Platform.rect):
down = True
up = False
if down:
playerman.y -= 10
playerman.direction = "tjump"
fall = False
for Platform in platforms2:
if playerman.rect.colliderect(Platform.rect):
down = False
playerman.direction = "uidle"
if keys[pygame.K_a] and px > playerman.speed:
for Platform in platforms:
Platform.x += playerman.speed
for Platform2 in platforms2:
Platform2.x += playerman.speed
elif keys[pygame.K_d] and px < 2000 - playerman.width + playerman.speed:
for Platform in platforms:
Platform.x -= playerman.speed
for Platform2 in platforms2:
Platform2.x -= playerman.speed
redraw()
pygame.display.update()
pygame.quit()