0
votes

EDIT: I've now updated indentation and a few other numbers (to get player to start at the floor and not jump so high). Now it jumps up to a good height, but it doesn't come down back to the floor. Any ideas on how to fix this new problem?

Messing around some more, if I put player_movement = 2 in last else (resetting variables) it slowly goes down, but then I'd need to set a barrier so it doesn't go off the bottom of the screen. But, the code I already have is supposed to do that for me...right?

Updated code:

if not(isJump):
    if keys[pygame.K_UP]:
        isJump = True
else:
    if jumpCount >= -10:
        neg = 1
        if jumpCount < 0:
            neg = -1
        player_movement -= (jumpCount ** 2) * 0.5 * neg
        jumpCount -= 10
    # This will execute when jump is finished
    else:
        # Resetting Variables
        jumpCount = 10
        isJump = False

I originally started following this tutorial: https://www.youtube.com/watch?v=UZg49z76cLw (Learning pygame by making Flappy Bird). But I decided I just wanted the "player" to be on the floor and be able to jump (instead of gravity/jumping like flappy bird).

So I then combined that tutorial with: https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/ . Before adding in the jump, the 'player' is resting on the floor. With the jump, the 'player' is flashing at the top of the screen. How do I fix this so it works correctly? I tried looking it up and all I can find is how to jump with like a box being drawn on screen, versus using screen.blit().

See my code below:

import pygame


def draw_floor():
    """Sets one floor after the first"""
    screen.blit(floor_surface, (floor_x_pos, 900))
    screen.blit(floor_surface, (floor_x_pos + 576, 900))


# Needed to start pygame
pygame.init()
# Creating the screen ((width, height))
screen = pygame.display.set_mode((576, 1024))
# Creating FPS
clock = pygame.time.Clock()

# Game variables
isJump = False
jumpCount = 10
player_movement = 0

# Importing background image into game
bg_surface = pygame.image.load('assets/bg_day.png').convert()
# Making background image larger
bg_surface = pygame.transform.scale2x(bg_surface)

# Importing floor image into the game
floor_surface = pygame.image.load('assets/base.png').convert()
# Making floor image larger
floor_surface = pygame.transform.scale2x(floor_surface)
# Floor variable to move floor
floor_x_pos = 0

# Importing player image into the game
player_surface = pygame.image.load('assets/player.png').convert()
# Making player image larger
player_surface = pygame.transform.scale2x(player_surface)
# Make a collision box around player -- center of rectangle x,y
player_rect = player_surface.get_rect(center=(100, 899))

run = True
while run:
    # Checks for all events running
    for event in pygame.event.get():
        # if event type is quitting:
        if event.type == pygame.QUIT:
            # set run to False to close loop
            run = False

    keys = pygame.key.get_pressed()

    if not(isJump):
        if keys[pygame.K_UP]:
            isJump = True
        else:
            if jumpCount >= -10:
                player_movement -= (jumpCount * abs(jumpCount)) * 0.5
                jumpCount -= 1
            # This will execute when jump is finished
            else:
                # Resetting Variables
                jumpCount = 10
                isJump = False

    # Setting the background (screen_bg,(x,y)) : (0,0) = top left
    screen.blit(bg_surface, (0, 0))
    player_rect.centery = player_movement
    # Putting player on screen
    screen.blit(player_surface, player_rect)
    # Moving the floor
    floor_x_pos += -1
    # Setting the floor surface to go infinitely
    draw_floor()
    if floor_x_pos <= -576:
        floor_x_pos = 0

    # Draws anything from above in while loop and draws on the screen
    pygame.display.update()
    # Setting FPS - won't run faster than [120] frames per second
    clock.tick(120)

# Quits game
pygame.quit()
1

1 Answers

1
votes

It's a matter of Indentation. The else case belongs to if not(isJump)::

while run:
    # [...]

    if not(isJump):
        if keys[pygame.K_UP]:
            isJump = True
    
    #<--| INDENTATION
    else:
        if jumpCount >= -10:
            player_movement -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        # This will execute when jump is finished
        else:
            # Resetting Variables
            jumpCount = 10
            isJump = False