Bouncing Ball pygame

import pygame

pygame.init()
width = 400
hight = 600
screen = pygame.display.set_mode((width, hight))
pygame.display.set_caption(“Engine”)
dot = pygame.image.load(“KreisSchwarz.png”)
clock = pygame.time.Clock()
running = True
WHITE = (255, 255, 255)

# Set (x, y) for Dot
def updateDot(x, y):
screen.blit(dot, (x, y))

# Display Dot at (x, y)
def update(fps=30):
screen.fill(WHITE)
updateDot(x, y)
pygame.display.flip()
return clock.tick(fps)

# Quit if User closes the window
def evHandler():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False

yKoords = []
(x, y) = (300, 200)
t = 1 # time variable
a = 2 # acceleration constant
tol = 40 # tolerance
i = 0 # just some iterator

# MAIN LOOP
while running:
evHandler()
update()
y += a * (t ^ 2)
t += 1
yKoords.append(int(y))
i += 1

if (y < (hight + tol)) and (y > (hight - tol)):
y = 580
yKoords.reverse()
update()

for q in range(i):
evHandler()
y = yKoords[q]
update()
if q == i - 1: # Because i didn’t write the Part for the Dot coming back down
running = False

This is my Code for a Ball accelerating down and then jumping back up.
My Problem is, that the code works fine until the if statement. There the Programm just displays the Ball at the last position in yKoords and waits until the for loop finishes. If i remove the for loop the Ball gets displayed at y=580 and stops but thats fine.

Please help i have no idea whats wrong about this.

import pygame

pygame.init()
width = 400
hight = 600
screen = pygame.display.set_mode((width, hight))
pygame.display.set_caption("Engine")
dot = pygame.image.load("KreisSchwarz.png")
clock = pygame.time.Clock()
running = True
WHITE = (255, 255, 255)


# Set (x, y) for Dot
def updateDot(x, y):
    screen.blit(dot, (x, y))


# Display Dot at (x, y)
def update(fps=30):
    screen.fill(WHITE)
    updateDot(x, y)
    pygame.display.flip()
    return clock.tick(fps)

# Quit if User closes the window
def evHandler():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False


yKoords = []
(x, y) = (300, 200)
t = 1 # time variable
a = 2 # acceleration constant
tol = 40 # tolerance
i = 0 # just some iterator


# MAIN LOOP
while running:
    evHandler()
    update()
    y += a * (t ^ 2)
    t += 1
    yKoords.append(int(y))
    i += 1

    if (y < (hight + tol)) and (y > (hight - tol)):
        y = 580
        yKoords.reverse()
        update()

        for q in range(i):
            evHandler()
            y = yKoords[q]
            update()
            if q == i - 1: # Because i didn't write the Part for the Dot coming back down
                running = False

Did not try, but pretty sure this is due to the fact that python evaluates q==1 to True or False, and then tries to do True-1 (or False-1), which results in an error. You should try q == (i-1).

@alexanderhaaswurst I’ve just quickly tried and it worked, the ball (or actually the random image I used) “bounces” (even though that’s more of a recording of the fall, played in reverse). Are you sure to actually trigger the test y<hight+tol and y>hight-tol? This test seems strange anyway, shouldn’t it be something such as y+dot.get_height()>=hight?
Edit: Actually don’t mind that, nothing in your code depends on the size of the image used (for some reason I assumed it had to be the case, but apparently it’s not, or it’s hidden behind something, but I don’t think so), hence I have no idea what your problem is as it works for me.

@matleg No, that’s (fortunately) not the problem. Boolean (comparison & logical) operators generally have a lower precedence than arithmetic & bitwise ones. See this page for more details.

1 Like