I've been trying to add jumping to my player but i can't figure out a way to do it.
import pygame
pygame.init()
SCREENX,SCREENY = 500,500
screen = pygame.display.set_mode([SCREENX, SCREENY])
clock = pygame.time.Clock()
VEL = 5
VELY = 5
FPS = 60
PODLOGAY = 450
podloga = pygame.draw.rect(screen,(0,0,0),pygame.Rect(0,PODLOGAY,500,50))
onground = False
class Gracz():
def __init__(self,x,y,jumptime):
self.x = x
self.y = y
self.isjump = False
self.jumptime = jumptime
self.speed = jumptime
def draw(self):
pygame.draw.rect(screen,(255,255,255),pygame.Rect(self.x,self.y,50,50))
def move(self):
if pygame.key.get_pressed()[pygame.K_d]:
self.x += VEL
elif pygame.key.get_pressed()[pygame.K_a]:
self.x -= VEL
def checkcollision(self):
global onground
if not podloga.colliderect(pygame.Rect(self.x,self.y + VELY,50,50)) and self.isjump == False:
self.y += VELY
onground = False
elif self.y + 50 > PODLOGAY:
overlap = self.y + 50 - PODLOGAY
self.y -= overlap
else:
onground = True
def jump(self):
global onground
if self.isjump:
if self.speed >= self.jumptime - (self.jumptime // 2) and onground == False:
self.y -= self.speed
self.speed -= 1
gracz = Gracz(225,0,10)
running = True
while running:
screen.fill("deepskyblue2")
podloga = pygame.draw.rect(screen,(0,0,0),pygame.Rect(0,PODLOGAY,500,50))
gracz.draw()
gracz.move()
gracz.checkcollision()
gracz.jump()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and onground:
gracz.isjump = True
onground = False
clock.tick(FPS)
pygame.quit()
I was looking for any tutorial on google but i couldn't find one that would suit my case without changing most of the code. The jumping i was trying to do was similiar to this. If someone is wondering why there is podlogay it's just ground in my language
Thanks!