I am pretty new to pygame and I want to make a simple platformer script that will let me move my player across a level. When I run my code my player and level both show up on my screen but my player does not fall due to the gravity I implemented and also does not move when using keyboard input. Normally for a problem like this, I would go through the code shown on the syntax error but I am not getting a syntax error so I was wondering if maybe I had looked upon the wrong documentation or built-in functions when I was creating this.
Code:
import pygame
import math
from pygame import mixer
from pygame.constants import K_LEFT, K_RIGHT, K_UP, KEYDOWN
from pygame.locals import *
from pygame.sprite import LayeredUpdates
pygame.init()
screen = pygame.display.set_mode((640, 360))
running = True
# player
#global playerRect
global levelRect
global gravity
global jump_force
global acceleration
global ressistance
global falling
global last_value
global playerX_change
global playerY_change
gravity = -1
jump_force = 14
acceleration = 2
ressistance = 0.85
speedy = 0
speedx = 0
falling = 0
last_value = 0
playerX_change = 0
playerY_change = 0
# creates the player func
def player(x, y):
global playerRect
playerRect = pygame.draw.rect(screen, (255, 255, 255), [x, y, 32, 32])
# level
def level(x, y):
global levelRect
levelRect = pygame.draw.rect(screen, (0, 0, 255), [x, y, 640, 40])
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# player movement
if event.key == K_UP:
if falling < 3:
speedy = 12
if event.key == K_LEFT:
speedx += 0 - acceleration
if event.key == K_RIGHT:
speedx += acceleration
speedx = speedx * ressistance
speedy += gravity
steps = abs(speedx) + abs(speedy)
steps = int(steps)
falling += 1
last_value = playerRect.x
playerRect.x += speedx / steps
collided = playerRect.colliderect(levelRect)
if collided:
playerRect.x = last_value
speedx = 0
last_value = playerRect.y
playerRect.y += speedy / steps
collided = playerRect.colliderect(levelRect)
if collided:
playerRect.y = last_value
if speedy < 0:
falling = 0
player(10, 275)
level(0, 325)
pygame.display.update()