-2
import pygame
import sys
from pygame import *
from pygame.locals import RESIZABLE

pygame.init()

WINDOW_SIZE = (800, 600)
screen = pygame.display.set_mode(WINDOW_SIZE, RESIZABLE, 32)

player_img = pygame.image.load('ClipartKey_738895_adobespark.png')
player_X = 130
player_Y = 500
player_change_X=0

def player():
    screen.blit(player_img, (player_X, player_Y))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                player_change_X = 0.3
            if event.key == K_LEFT:
                player_change_X = -0.3
            if event.key == K_SPACE:
                player_Y += 40
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                player_change_X = 0
            if event.key == K_LEFT:
                player_change_X = 0

    screen.fill((0, 200, 255))
    player()
    player_X += player_change_X

    pygame.display.update()

I want to make the player jump approximately by 4y but not able to do so. Please tell me how can I do so and if telling any function please also tell what and how it does it as I'm new to pygame.

D_00
  • 1,440
  • 2
  • 13
  • 32
  • 1
    when space is pressed You have to make it "disconect" so that continuous space pressing isn't going to increase the y coords. and You also need to make sure that once a certain height is reached the y coords get decreased, also I am pretty sure that most of the tutorials on pygame cover jumping mechanism – Matiiss Apr 26 '21 at 12:45
  • 2
    What did you try so far? See [How to make a character jump in Pygame?](https://stackoverflow.com/questions/65873880/how-to-make-a-character-jump-in-pygame/65874132#65874132) – Rabbid76 Apr 26 '21 at 14:25
  • 2
    Don't put spam, add more details or rephrase what you say. – Anonymous Apr 26 '21 at 17:35
  • 1
    Be careful, you import pygame twice: `import pygame` and `from pygame import *` – D_00 Apr 27 '21 at 11:32
  • 1
    Also the `y` value decreases when you jump up, but in your code, when Space is pressed, the player goes down. – D_00 Apr 27 '21 at 11:37

1 Answers1

1

You can use a variable like on_floor. If it is on the floor (use collisions), then it allows the program to start the jump (use another variable, like y_speed. I usually make the player jump like this:

y_speed = 0
on_floor = False

# main loop
    if on_floor:
        if pygame.key.get_pressed()[K_SPACE]:
            y_speed = -40 # start the jump if space pressed
            # set to the value you used, but you should move according the the framerate
    if y_speed > -40 # speed limit
        y_speed += 1 # change the speed, to make a parabol-shape fall

    player.y += y_speed

Also, you could make the player jump like this with this answer, which does a similar job.

D_00
  • 1,440
  • 2
  • 13
  • 32
  • 1
    The answer to the linked question uses a quadratic formula: `y -= (Jumpcount ** 2) * 0.5 * Neg`. Hence I think it is smooth. Note it is sufficient to compute the position for each frame. In any case, the straight lines in your drawing do not represent the curve generated in the linked answer. – Rabbid76 Apr 27 '21 at 12:07