0
import pygame
import time
pygame.init()

display_width = (1000)
display_height = (480)


black = (255,50,0)
white  = (0,60,7)
blue = (0,205,205)

kled1_width = 30
kled1_height = 45

ground1_height = 300

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption(" kled ")
clock = pygame.time.Clock()

gameDisplay = screen

kled1IMG = pygame.image.load("IMG.png")

def kled1(x,y):
    gameDisplay.blit(kled1IMG,(x,y))


def game_loop():

    x = (1)
    y = (340)


    x_change = 0

    y_change = 0


    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5

                elif event.key == pygame.K_RIGHT:
                    x_change =  5

                elif event.key == pygame.K_UP:
                    y_change = -15

                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT and pygame.K_r or event.key == pygame.K_RIGHT and pygame.K_2:
                    x_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0


        x += x_change
        y += y_change

        gameDisplay.fill(blue)

        pygame.draw.rect(gameDisplay, white, [1, 400, 1000,ground1_height])

        kled1(x,y)

#prevents from going into ground
        if y > ground1_height:
            y_change = 0

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

game_loop()
pygame.quit()
quit()

How would I make my code so that if I press once, the image moves up 15 spaces from the image's current position, and then goes down 15 spaces after about .5 seconds? Essentially, it is a jumping game like Mario that I will add platforms and other things to it. Also, I want to know how to make sure you can't double jump while in the air! Sorry that I am not a good programmer, I just need help for this game.

I am using Python 2.7

Trooper Z
  • 1,617
  • 14
  • 31
kied
  • 27
  • 1
  • [Program Arcade Games With Python And Pygame - Lab 16: Pygame Platformer Examples](http://programarcadegames.com/index.php?&chapter=example_code_platformer) – furas Dec 17 '17 at 19:05

1 Answers1

1

Add a GRAVITY constant which you add to the y_change each frame to accelerate the player downwards. When the player touches the ground set a on_ground variable to True and when the user jumps check if s/he's on_ground and then set it to False to prevent double jumps.

import pygame

pygame.init()
screen = pygame.display.set_mode((1000, 480))
clock = pygame.time.Clock()

GREEN = (110, 190, 27)
BLUE = (0, 185, 225)

PLAYER_IMG = pygame.Surface((30, 45))
PLAYER_IMG.fill((250, 120, 0))

GRAVITY = 1


def game_loop():
    x = 1
    y = 340
    x_change = 0
    y_change = 0
    on_ground = True
    ground_height = 400

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change =  5
                elif event.key == pygame.K_UP:
                    if on_ground:
                        # Set the y-velocity to a negative
                        # value to jump.
                        y_change = -20
                        on_ground = False
                elif event.key == pygame.K_DOWN:
                    y_change = 5
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and x_change < 0:
                    x_change = 0
                elif event.key == pygame.K_RIGHT and x_change > 0:
                    x_change = 0

        # Adjust the y-velocity by adding the gravity each frame.
        y_change += GRAVITY
        # Move the player.
        x += x_change
        y += y_change
        # Stop the player on the ground.
        if y > ground_height:
            # Stop the movement along the y-axis.
            y_change = 0
            y = ground_height
            on_ground = True

        screen.fill(BLUE)
        pygame.draw.rect(screen, GREEN, [0, 445, 1000, 35])
        screen.blit(PLAYER_IMG, (x, y))

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

game_loop()
pygame.quit()

If you want to see a minimal, complete platformer example, take a look at this answer.

skrx
  • 19,980
  • 5
  • 34
  • 48