I'm trying to make a simple platformer game with Python/PyGame. I cannot seem to figure out a simple way to program the jumping mechanism. I got to the point where the character (a blue square) will move follow a parabola, but it only moves in the parabola at certain x coordinates. Run the source code and you'll see what I mean. Ideally, the block would move like mario moves in a super mario game. I will appreciate any help.
Here is my code:
import pygame, math
pygame.init()
screen = pygame.display.set_mode((1000, 700))
clock = pygame.time.Clock()
pygame.display.set_caption('Platformer')
def main():
x = 0
y = 0
x_move = 0
jump = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_move = 5
if event.key == pygame.K_LEFT:
x_move = -5
if event.key == pygame.K_SPACE:
jump = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
x_move = 0
if event.key == pygame.K_SPACE and y == 0:
jump = 0
if jump == 1:
y = -0.01 * (x ** 2) + (x/50) + 300
y = round(y)
if y < 0:
y = 0
x += x_move
screen.fill((40,40,40))
pygame.draw.rect(screen, (0,0,255), (x+500, 650-y, 50, 50))
pygame.display.update()
clock.tick(60)
main()