1
import pygame, time
from sys import exit

pygame.init()
pygame.font.init()

clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 18)

def update_fps():
    fps = str(int(clock.get_fps()))
    fps_text = font.render(fps, 1, pygame.Color("coral"))
    return fps_text



# Game Variables / Functions

screen_width = 1068
screen_height = 628
screen = pygame.display.set_mode((screen_width, screen_height))
original_player_pos = (50, 500)
original_player_size = (40, 60)
FPS = 69
gravity = 0
jump_force = 15
player_speed_right = 10
player_speed_left = 10
ground_grip = True
can_jump = True


# Player

class player():
    player_surf = pygame.image.load("Graphics\player_idle.png").convert_alpha()
    player = player_surf.get_rect(topleft=(original_player_pos))


class background():
    sky_surf = pygame.image.load("Graphics\Sky.png")
    sky_surf = pygame.transform.scale(sky_surf, (screen_width * 2, screen_height * 2))
    sky = sky_surf.get_rect(topleft=(0, -screen_height))


class surfaces():
    main_platform_surf = pygame.image.load("Graphics\main_ground.png")
    main_platform = main_platform_surf.get_rect(topleft=(0, screen_height - main_platform_surf.get_height() + (main_platform_surf.get_height()) / 2))

# Game Engine Functions


def y_movement():
    global gravity, ground_grip,can_jump
    
    gravity += 1
    player().player.y += gravity


    if ground_grip == True:
        if player().player.colliderect(surfaces().main_platform):
            player().player.y = surfaces().main_platform.y - original_player_size[1]

        if not player().player.colliderect(surfaces().main_platform) and player().player.y < surfaces().main_platform.y - original_player_size[1]:
            can_jump = False
        else:
            can_jump = True
    

def x_movement():
    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        player().player.x += player_speed_right
        return "Right"
    if keys[pygame.K_LEFT] or keys[pygame.K_a]:
        player().player.x -= player_speed_left
        return "Left"

    
def draw(surface, rect):
    screen.blit(surface, rect)

def jump():
    global gravity
    gravity = -jump_force


# Game Engine

while True:
    update_fps()
    screen.fill("black")

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and can_jump == True:
                ground_grip = False
                if ground_grip == False:
                    jump()
                    ground_grip = True
                    

    x_movement()
    y_movement()
    
    draw(background().sky_surf, background().sky)
    draw(surfaces().main_platform_surf, surfaces().main_platform)
    draw(player().player_surf, player().player)
    draw(update_fps(), (10,0))

    clock.tick(FPS)
    pygame.display.update()

Please try the code above out yourself, if you don't mind add some graphics to replace the pygame.image.load() or just replace it with a blank surface. When you run the program you see the main character and stuff, you can move around using WASD or arrow keys. Feel free to move around some stuff. Till now there is not a problem right? Now what I want you to do is stand still for 10 seconds (might happen sooner or later than that, should be around 10 seconds tho). When you stand still you will see the player sprite that you were moving around earlier literally just disappear. Now exit the program and add

print(player().player.y)

you will see the current player y location. Wait till the player dissapeares, and once it does, look at the y location that's gonna be printed out in your terminal. It will rapidly increase.

I tried 2 things. I added a variable that tries to grip the player into the ground if the player.y is over 0. It did not help and the player goes down anyways. I tried adding a variable that decides when the player can jump (because the y movement is linked directly to gravity and jumping) to decide when the player can jump. I was expecting the player sprite to just stay where it is.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

When the player hits the platform, they must set the bottom of the player to the top of the platform.

player().player.bottom = surfaces().main_platform.top

Also set gravity = 0. If you do not reset gravity, gravity will increase until the player falls through the platform.


def y_movement():
    global gravity, ground_grip,can_jump
    
    gravity += 1
    player().player.y += gravity

    if ground_grip == True:
        if player().player.colliderect(surfaces().main_platform):
            player().player.bottom = surfaces().main_platform.top
            gravity = 0
            can_jump = True
Rabbid76
  • 202,892
  • 27
  • 131
  • 174