Hi I'm making a platformer game, nothing really special for now. Only a cube that can move when you press the arrows keys and a rectangle around the screen to make the limits. Now I want the door to be up so I have to make a gravity and a jumping, I have an idea for the jumping but I really don't know what to do for the gravity. Here's my code so far
# Importing Libraries
import pygame
from pygame.locals import *
pygame.init()
# Variable Stockage
color = (0, 0, 0)
x = 385
y = 400
velocity = 12
background_color = (255, 255, 255)
clock = pygame.time.Clock()
# Screen
screen = pygame.display.set_mode((1050, 650))
pygame.display.set_caption('Shooter')
# Game Loop
running = True
while running:
# Setting to 60fps
clock.tick(60)
screen.fill(background_color)
px, py = x, y
# Closing the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Player Movement
key_pressed_is = pygame.key.get_pressed()
if key_pressed_is[K_LEFT]:
x -= 8
if key_pressed_is[K_RIGHT]:
x += 8
# Player
player = pygame.draw.rect(screen, color, pygame.Rect(x, y, 25, 25))
# Drawing The Bounds
pygame.draw.line(screen, color, (50, 50), (1000, 50), 5)
pygame.draw.line(screen, color, (50, 600), (1000, 600), 5)
pygame.draw.line(screen, color, (50, 48), (50, 602), 5)
pygame.draw.line(screen, color, (1000, 48), (1000, 602), 5)
# Barrier Around The Screen
barrierRect = pygame.Rect(5, 5, 1040, 640)
if not barrierRect.contains(player):
x, y = px, py
pygame.display.update()
I've also tried this but it just makes the square go up.
if key_pressed_is[K_SPACE]:
y -= 15
If you have some tips to make my code better I'll take them. Thank's in advance.