So earlier I posted a question about programming forces for a physics engine I've been making in pygame. I edited my code a little bit, so now all I want to add is something in to the event.key == K_UP: snippet of code. When the user press up, I want it to project the square upwards. I've tried many things with slopes and framerates and new coordinate systems, but haven't been able to get anything.
import pygame
pygame.init()
display_width = 800
display_height = 600
block_size = 30
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Physics Engine')
clock = pygame.time.Clock()
x = display_width / 2
y = display_height / 2 + 200
y_change = 0
x_change = 0
gameExit = False
def force(ang, mag):
# code about the force goes here
x
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN: # check for key presses
if event.key == pygame.K_LEFT: # left arrow turns left
x_change = -10
y_change = 0
elif event.key == pygame.K_RIGHT: # right arrow turns right
x_change = 10
y_change = 0
elif event.key == pygame.K_UP:
# force(angle, magnitude)
# I want to make a force that pushes the block up
elif event.type == pygame.KEYUP: # check for key presses
if event.key == pygame.K_LEFT: # left arrow turns left
x_change = 0
y_change = 0
elif event.key == pygame.K_RIGHT: # right arrow turns right
x_change = 0
y_change = 0
y += y_change
x += x_change
gameDisplay.fill((255, 255, 255))
pygame.draw.rect(gameDisplay, (255, 0, 0), [x, y, block_size, block_size])
pygame.display.update()
clock.tick(25)
pygame.quit()
quit()
thanks so much for reading, I'd seriously appreciate all help given to me! A generous guy gave me some tips last time, but I wasn't quite able to follow along :(.