Hello I’m working on a simple platformer for fun and i have a little problem with collisions.
I made it work for the most part except some things and the collisions are a bit weird.
When I jump on the gray rectangle on the left it pushes me off and the black one works but it puts me inside it a little bit(it worked before I put the gray one in).
I tried changing the code a bunch but it didn’t do anything or made it worse.
I plan to make the map something like this but its a WIP.
Map
Code:
import pygame
import sys
pygame.init()
display_height = 800
display_width = 1600
display = (display_width, display_height)
screen = pygame.display.set_mode(display)
pygame.display.set_caption('Escape Building')
size_player = 50
size_platform = 100
size_bottom = 1600
x = 100
y = display_height - size_player
x1 = 500
y1 = display_height - size_platform
x2 = 0
y2 = display_height - 1
x3 = 0
y3 = 675
size_platform1_y = 125
size_platform1_x = 75
vel = 8
gravity = 1
jump_height = 20
y_velocity = jump_height
clock = pygame.time.Clock()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (100, 150, 50)
yellow = (255, 255, 0)
gray = (128, 128, 128)
jumping = False
running = True
while running:
player = pygame.Rect(x, y, size_player, size_player)
platform = pygame.Rect(x1, y1, size_platform, size_platform)
platform1 = pygame.Rect(x3, y3,size_platform1_x,size_platform1_y)
bottom = pygame.Rect(x2, y2, size_bottom, size_bottom)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
move_up = keys[pygame.K_w] or keys[pygame.K_UP] or keys[pygame.K_SPACE]
move_left = keys[pygame.K_a] or keys[pygame.K_LEFT]
move_right = keys[pygame.K_d] or keys[pygame.K_RIGHT]
collide = player.colliderect(platform)
collide1 = player.colliderect(bottom)
collide2 = player.colliderect(platform1)
if collide or collide1 or collide2:
if move_up:
jumping = True
if jumping:
jump = True
y -= y_velocity
y_velocity -= gravity
if y_velocity < -jump_height:
jumping = False
y_velocity = jump_height
if move_left and x > 0:
x -= vel
if move_right and x < display_width - size_player:
x += vel
if collide:
if y <= y1:
y = y1 - size_player + 1
elif x - size_player <= x1:
x = x1 - size_player * 1.13
elif x1 <= x + size_player:
x = x1 + size_player * 2 * 1.075
if jumping:
y -= y_velocity
y_velocity -= gravity
if y_velocity < -jump_height:
jumping = False
y_velocity = jump_height
elif collide == False and jumping == False:
if y < y2 - size_player:
y += gravity * 20
if collide2:
if y <= y3:
y = y3 - size_player
elif x3 <= x + size_player:
x = x3 + size_player * 1.5
if jumping:
y -= y_velocity
y_velocity -= gravity
if y_velocity < -jump_height:
jumping = False
y_velocity = jump_height
elif collide2 == False and jumping == False:
if y < y2 - size_player:
y += gravity * 20
screen.fill(green)
pygame.draw.rect(screen, black, platform)
pygame.draw.rect(screen, gray, platform1)
pygame.draw.rect(screen, red, player)
pygame.draw.rect(screen, white, bottom)
pygame.display.update()
clock.tick(65)
New contributor
Michal Polonec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.