So, I want to make a stickman parkour game in pygame. But I don't know how to check collision in horizontal and vertical. I try search online or watch youtube videos but I could'nt under stand what they do.
I want the check collision function to be like:
if collide(objectA,objectB,"Up"):
stick.dy = 0
if collide(objectA,objectB,"Down"):
stick.dy = 0
if collide(objectA,objectB,"Right"):
stick.dx = 0
if collide(objectA,objectB,"Left"):
stick.dx = 0
Here is my code:
import pygame
from spr import*
import sys
pygame.init()
win = pygame.display.set_mode((700,500))
pygame.display.set_caption("Stickman Parkour")
stick = Stick(30,30)
blocks = []
jump = False
land = False
frc = .91
grv = .2
run = True
while run:
pygame.time.delay(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
stick.dx *= frc
stick.dy += grv
stick.x += stick.dx
stick.y += stick.dy
blocks.append(Brick(0,100,False,0,0,0,30,30))
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
stick.dx = 3
stick.x += 3
if keys[pygame.K_a]:
stick.dx = -3
stick.x -= 3
if keys[pygame.K_w]:
stick.dy = -3
jump = False
stick.y -= 3
if keys[pygame.K_s]:
stick.y +=3
if keys[pygame.K_a] and keys[pygame.K_d]:
frc = 0
else:
frc = .91
win.fill((200,200,200))
for block in blocks:
pygame.draw.rect(win,(0,0,0),(block.x,block.y,block.w,block.h))
if collide(stick,block):
jump = True
land = True
if not keys[pygame.K_w]:
stick.dy = 0
stick.y = block.y - stick.h
else:
jump = False
pygame.draw.rect(win,(225,0,0),(stick.x,stick.y,30,30))
pygame.display.flip()
sys.exit(0)
Can someone help me?