0

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()
  • 1
    I'm finding it a little hard to see what you're trying to do. Why does the y coord depend on the x when you are jumping? Should it rely on a time instead? i.e. `y = s + 1/2*a*t**2` where t is the time since the jump, s is initial y coord and a is gravity? – xthestreams Apr 27 '16 at 02:53

1 Answers1

0

Have a look at the code here, ignore the camera part for now. Is this how you want the jump to work? Add scrolling to a platformer in pygame

Community
  • 1
  • 1
marienbad
  • 1,461
  • 1
  • 9
  • 19