I've watched many videos but couldn't implement the code into my code. All I need is a smooth jump. I've written most of this from video tutorials by DaFluffyPotato. I've just begun OOP so it wud be great if there aren't too many classes and such!
Please read the comments in the code
# I have a player image and with the a and d keys, I can move left and right.
# The w key is for jumping
# I've also implemented Gravity on the Y axis.
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
# window_width
ww = 760
# window_height
wh = 520
# window_color
wc = (135, 206, 250)
# window
w = pygame.display.set_mode((ww, wh))
tile_size = 40
# loading and scaling images
player_img = pygame.image.load('player.png')
player_img = pygame.transform.scale(player_img, (40, 40))
# global variables for movement
# the 'p's stand for player
# X and Y axis for player
pcoors = [400, 300]
pleft = False
pright = False
pjump = False
pspeed = 2
gravity = 1.2
Check if they are True or False and then implement movement in the player_movement func below
def userInput():
# you cannot alter global variables from a local scope
global pleft, pright, pjump
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# keyPresses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pleft = True
if event.key == pygame.K_d:
pright = True
if pjump == False and event.key == pygame.K_SPACE and jump_offset == 0:
pjump = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
pleft = False
if event.key == pygame.K_d:
pright = False
def player_movement():
global pleft, pright, pjump, pcoors, pspeed, gravity
if pleft:
pcoors[0] -= pspeed
if pright:
pcoors[0] += pspeed
pcoors[1] += gravity
def col_detection():
global pcoors
if pcoors[1] + 40 >= wh:
pcoors[1] = wh - 40
def img_blit():
w.blit(player_img, (pcoors))
while True:
w.fill(wc)
# calling the functions to keep the while loop clean
userInput()
player_movement()
img_blit()
col_detection()
clock.tick(360)
pygame.display.update()