0

I can get pygame to display a ball in the lower left hand corner of the window that I set. But for some reason the ball does not move and bounce off of the walls of the window. The goal of this is to build a simple "physics engine". Im not sure where I am going wrong. Any and all help is greatly appreciated.

Here is the code: `import pygame

pygame.init()

w, h = 1024, 1024
# screen set up
screen = pygame.display.set_mode([w, h])
clock = pygame.time.Clock()

# this will run the window until I choose to quit it
running = True
while running:
    # click close button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


     
        def main(g=9.8, hdamp=0.05, damp=0.05, ballx=0, bally=0):
            ballx_v = 5
            bally_v = 5

            while True:

                bally_v += (g / 50)

                ballx_v += ballx_v

                bally_v += bally_v

                if ballx <= 0:
                    ballx_v = -ballx_v * (1 - damp)
                    ballx = 1

                if bally_v <= 0:
                    bally_v = -bally_v * (1 - damp)
                    ballx_v = -ballx_v * (1 - hdamp)
                    bally = 1

                if ballx >= w:
                    ballx_v = -ballx_v * (1 - damp)
                    var = w - 1

                if bally_v >= h:
                    bally_v = -bally_v * (1 - damp)
                    ballx_v = -ballx_v * (1 - hdamp)
                    bally = h - 1

                pygame.draw.circle(screen, (255, 255, 255), (int(ballx), (int(bally))), 5)

                pygame.display.flip()
                pygame.event.get()
                clock.tick(40)
if __name__ == "__main__":
    main()

    # filling the background with color
    screen.fill((255, 255, 255))

    # drawing a shape in the window with tuple of RGB numbers and defining center and radius of circle
    # pygame.draw.circle(screen, (255, 255, 255), (int(ball_x), (int(ball_y))), 5)

    # flipping the display or updating it. with out this nothing shows
    #pygame.display.flip()
    #clock.tick(40)

# time to quit set up
pygame.quit()`

0 Answers0