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

这是我的代码,一个球加速下降,然后跳起来。
我的问题是,代码在if语句之前工作正常。在那里,程序只显示yKoords中最后一个位置的球,并等待for循环完成。如果我移除for循环,球会显示为y=580并停止,但这没关系。
请帮助我,我不知道这有什么不对。

最佳答案

不要在主循环中执行单独的流程循环。
当球在地面上反弹(abs(y - hight))或球到达顶部(t == 0)时,反转方向就足够了。

direction = 1
while running:
    evHandler()
    update()
    y += (a * (t ^ 2)) * direction
    t += direction

    if abs(y - hight) < tol:
        y = 580
        t -= 1
        direction *= -1
    elif t == 0:
        direction *= -1

python - 弹跳球不会回来-LMLPHP

关于python - 弹跳球不会回来,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55001320/

10-16 12:02