簡體   English   中英

Bouncing Ball不會回來pygame

[英]Bouncing Ball doesn't come back 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

這是我的一個球加速下來然后跳回來的代碼。 我的問題是,代碼在if語句之前正常工作。 在那里,程序只在yKoords的最后位置顯示Ball,並等待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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM