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 语句之前工作正常。 Programm 只是在 yKoords
中的最后一个位置显示 Ball 并等待 for 循环完成。如果我删除 for 循环,Ball 会显示在 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 - 弹跳球不会回来pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55001320/