How to Simulate Jumping in Pygame for This Particular Code

How to simulate Jumping in Pygame for this particular code

See How to make a character jump in Pygame?. Add a variable jump and initialize it by 0, before the main loop:

jump = 0  
while run:
# [...]

Only react on pygame.K_SPACE, if player is allowed to jump and stays on the ground. If this is fulfilled then set jump to the desired "jump" height:

if keys[pygame.K_SPACE] and Ycord == ScreenLenY - height:
jump = 300

As long as jump is greater than 0, move the player upwards and decease jump by the same amount, in the main loop.

If the player is not jumping that let him fall dawn, till he reaches the ground:

 if jump > 0:
Ycord -= vel
jump -= vel
elif Ycord < ScreenLenY - height:
Ycord += 1

See the demo, where I applied the suggestions to your code:

Sample Image

import pygame
pygame.init()

ScreenLenX, ScreenLenY = (1000, 500)
win = pygame.display.set_mode((ScreenLenX, ScreenLenY))
pygame.display.set_caption("aman")
Xcord, Ycord = (100, 100)
length, height = (10, 10)
xmove, ymove = (1, 1)
vel = 2
jump = 0

run = True
clock = pygame.time.Clock()
while run:
#clock.tick(60)
for event in pygame.event.get():
print(event)
if event.type ==pygame.QUIT:
run = False

keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and Xcord <= ScreenLenX-length:
Xcord += vel
if keys[pygame.K_LEFT] and Xcord >= 0:
Xcord -= vel

if keys[pygame.K_SPACE] and Ycord == ScreenLenY - height:
jump = 300

if jump > 0:
Ycord -= vel
jump -= vel
elif Ycord < ScreenLenY - height:
Ycord += 1

win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (Xcord, Ycord, length, height))

pygame.display.update()

How to create a jump movement in pygame

Never try to implement a loop that controls the game within the application loop. Use the application loop. Add a variable jump = False. Set the variable when SPACE is pressed. Use the KEYDOWN event instead of pygame.key.get_pressed(). Implement the jump algorithm in the application loop, instead of an extra loop:

jump = False

run = True
while run:
# [...]

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
jump = True

# Unfinished jump movement.
if jump:
if jumping_points >= -10:
y -= jumping_points
jumping_points -= 1
else:
jumping_points = 10
jump = False

# [...]

Additionally you need to change the formula used to calculate the jump. The result of (jump_points ** 2) is always a positive value and the player will never come down:

y -= (jumping_points ** 2) / 5

y -= jumping_points

or

y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)

runs 60 times per second.


Minimal example:

Sample Image

import pygame
pygame.init()

xscreen, yscreen = 1000, 500
screen = pygame.display.set_mode((xscreen, yscreen))
pygame.display.set_caption("Pygame")
clock = pygame.time.Clock()

def animation():
screen.fill(0)
pygame.draw.rect(screen, (255, 0, 0), (x, y, 20, 20))
pygame.display.flip()

width, height = 50, 50
x, y = 950, 200
vel, jumping_points = 10, 10
jump = False

run = True
while run:
clock.tick(60)

# Checks if the player wants to quit the game.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
jump = True

# Checks if a key was pressed and moves the square accordingly.
keys = pygame.key.get_pressed()

if keys[pygame.K_RIGHT] and x + width < xscreen:
x += vel
if keys[pygame.K_LEFT] and x > 0:
x -= vel

# Unfinished jump movement.
if jump:
if jumping_points >= -10:

# either
# y -= jumping_points
# or
y -= (jumping_points ** 2) / 5 * (1 if jumping_points > 0 else -1)

jumping_points -= 1
else:
jumping_points = 10
jump = False

# Updates the screen surface.
animation()

pygame.quit()

unable to make character jump in Pygame endless jumper

Do not try to control the game with an additional loop in the application loop. Use the application loop.
Add the variables is_jumping adn jump_count:

is_jumping = False
jump_count = 0

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
clock.tick(50)

runs 50 times per second.

Use the KEYDOWN event instead of pygame.key.get_pressed() to jump:

run = True
while run:
clock.tick(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not is_jumping:
is_jumping = True
jump_count = 30

pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action.

Change the position of the player depending on is_jumping adn jump_count in the application loop:

while run:
# [...]

#movement code
if is_jumping and jump_count > 0:
if jump_count > 15:
player_y -= player_vel
jump_count -= 1
elif jump_count > 0:
player_y += player_vel
jump_count -= 1
is_jumping = jump_count > 0

Complete example:

Sample Image

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Infinite Runner")

#defines the paremeters for the wall
wall_x = 800
wall_y = 300
wall_width = 20
wall_height = 20

score = 0

player_x = 400
player_y = 300
player_width = 40
player_height = 60
player_vel = 5

is_jumping = False
jump_count = 0

#where the main code is run
print(score)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not is_jumping:
is_jumping = True
jump_count = 30

#movement code
if is_jumping and jump_count > 0:
if jump_count > 15:
player_y -= player_vel
jump_count -= 1
elif jump_count > 0:
player_y += player_vel
jump_count -= 1
is_jumping = jump_count > 0

#draws the player and the coin
screen.fill((0,0,0))
wall = pygame.draw.rect(screen, (244, 247, 30), (wall_x, wall_y, wall_width, wall_height))
player = pygame.draw.rect(screen, (255, 255, 255), (player_x, player_y, player_width, player_height))
pygame.display.update()

pygame.quit()


Related Topics



Leave a reply



Submit