import pygame from pygame.locals import * pygame.init() SCREEN_WIDTH = 500 SCREEN_HEIGHT = 400 # set up window window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Gravity Test') # set up colors BLACK = (0,0,0) WHITE = (255,255,255) GRAY = (47, 79, 79) # Dark Slate Gray SKY_BLUE = (0, 191, 255) # Deep Sky Blue GOLD = (218, 165, 32) # Gold DARK_RED = (178, 34, 34) # Firebrick SEA_GREEN = (60, 179, 113) # Medium Sea Green PLAYER_SIZE = 20 PLAYER_SPEED = 10 # jumping vars GRAVITY = 4 jumpForce = [0, GRAVITY] def handleKeys(player): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: # left arrow key, move player left player.x = max(player.x - PLAYER_SPEED, 0) if keys[pygame.K_RIGHT]: # right arrow key, move player right player.x = min(player.x + PLAYER_SPEED, SCREEN_WIDTH - PLAYER_SIZE) if keys[pygame.K_UP]: # only jump if player is on the ground if player.y == SCREEN_HEIGHT - PLAYER_SIZE: print('changing jump force') jumpForce[0] = 20 player = Rect(20, SCREEN_HEIGHT - PLAYER_SIZE, PLAYER_SIZE, PLAYER_SIZE) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False window.fill(GRAY) handleKeys(player) print('jumpForce = ' + str(jumpForce[0])) if player.y < SCREEN_HEIGHT - PLAYER_SIZE or jumpForce[0] > 0: player.y -= jumpForce[0] jumpForce[0] -= GRAVITY pygame.draw.rect(window, SEA_GREEN, player) pygame.display.update() pygame.time.Clock().tick(30) pygame.quit()