My game closes instant

i was making the gravity of my game wenn out of nothing always wenn i open my game it closes itself

i did delete the part i edidet wenn that akured but still it didn´t work anymore

backgruond information:i was using visual studio code

import pygame

# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0
pygame.mouse.set_cursor(*pygame.cursors.broken_x)

#creating the positions

player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)


while running:
    # poll for event
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # fill the screen with a color to wipe away anything from last frame
    screen.fill("black")

#drawing the stuff

    player_zone = pygame.draw.rect(screen,"red",(player_pos.x-20,player_pos.y-20 , 40, 40))
    pygame.draw.circle(screen, "black", player_pos, 5)
    pygame.draw.circle(screen, "white", player_pos, 1)
 
     
 
 
 #developer code
    keys = pygame.key.get_pressed()
    if keys[pygame.K_p]:
         print(player_pos)
 
    
    #player code
    if keys[pygame.K_w]:
         player_pos.y = player_pos.y-1000 * dt
         
    if keys[pygame.K_s]:
         player_pos.y += 100 * dt
         
    if keys[pygame.K_a]:
         player_pos.x -= 100 * dt
         
    if keys[pygame.K_d]:
         player_pos.x += 100 * dt

    player_pos.y=player_pos.y+player_pos.y/(player_pos)-100 * dt
         
         
     
    if player_pos.x<0:
         player_pos.x=0
         
    if player_pos.x>1280:
         player_pos.x=1280
         
    if player_pos.y<0:
         player_pos.y=1
         
    if player_pos.y>720:
           player_pos.y=720


    # flip() the display to put your work on screen
    pygame.display.flip()

    # limits FPS to 60
    # dt is delta time in seconds since last frame, used for framerate-
    # independent physics.
    dt = clock.tick(60) / 1000

pygame.quit()

What’s with the above, and this?:slight_smile:

player_pos.y=player_pos.y+player_pos.y/(player_pos)-100 * dt

Does this really work as the AI you used claims too? I don’t use PyGame, but it just seems like an extra moving part that could go wrong.

dt = clock.tick(60) / 1000

one is jumping the other one gravity

Firstly, (not that this proves anything in the slightest) I don’t understand that how dividing the position by something else to do with the position and adding that on, models gravity. Double check for a mistake in the formula.

Secondly. If the division is correct, e.g. some alternative model, ot that’s just what you want in your game. In Python or PyGame, can you divide a float by a player_pos object? Doesn’t this error?:

i dont get an error and i tried to use the formula for an aceleration

@NoahRG, what @JamesParrott is stating is correct. You have to check your units:

  • Position: m (meters)
  • Velocity: m / s (meters / second)
  • Acceleration: m / s^2 (meters per second squared)

(remember: gravity would be in units of m / s^2)

If you’re performing a calculation for position, then the corresponding units from the equation better be in meters (or feet depending on the unit system that you’re using but I will assume metric). Keep in mind that you can only add and subtract values with identical units. Thus, you cannot add a velocity to a position. This would be an invalid mathematical operation. You can, however, multiply or divide values with different units as units would either compound or cancel out, depending on the mathematical operation.

In short, perform a unit sanity check for all calculations to make sure that you are setting up your formulas correctly.