python - Pygame 弹力球从地板下沉

标签 python pygame

下面的代码使球反弹,但由于某种原因,球在完成反弹后穿过地面。有人知道为什么吗?代码的想法是,一个球从左上角开始,然后落下并弹起,然后向上和向后落下,直到它停止弹跳,但当它停止弹跳时,它开始抖动并慢慢沉入地面。我不知道为什么,我想不通。有人知道为什么吗?感谢您的帮助

import pygame
pygame.init()

#All keyboard and mouse input will be handled by the following function
def handleEvents():
#This next line of code lets this function access the dx and dy
#variables without passing them to the function or returning them.
    global dx,dy
#Check for new events
    for event in pygame.event.get():
    #This if makes it so that clicking the X actually closes the game
    #weird that that wouldn't be default.
        if event.type == pygame.QUIT:
            pygame.quit(); exit()
    #Has any key been pressed?
        elif event.type == pygame.KEYDOWN:
        #Escape key also closes the game.
            if event.key == pygame.K_ESCAPE:
                pygame.quit(); exit()
            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                dx = dx + 5
            elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                dx = dx - 5
            elif event.key == pygame.K_UP or event.key == pygame.K_w:
                dy = dy - 5
            elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                dy = dy + 5

width = 1000
height = 600
size = (width, height)
black = (0, 0, 0) #r g b

screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()

x = 0
y = 0
dx = 3
dy = 3

done = False
while not done:
    handleEvents()

    #Move the ball
    x = x + dx
    y = y + dy
    ballrect.topleft = (x,y)

    #PART A

    if ballrect.left < 0 or ballrect.right > width:
        dx = -dx
    if ballrect.top < 0 or ballrect.bottom > height:
        dy = -dy

'''If the ball is outside of the range delta y,
then delta y becomes the negative version of it, and the same goes
for delta x if it is outside the boundries of delta x
'''
#PART B

    dy = dy * 0.99
    dx = dx * 0.99
'''Could be useful if you want
the ball to stop moving after a certain amount of time,
or the opposite, it could make the ball slowly move a greater distance each frame'''

#PART C

    dy = dy + 0.3

'''dy slowly gets .3 added to itself each frame, making the bounce
smaller each time until eventually it stops fully'''



#Draw everything
    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

#Delay to get 30 fps
    clock.tick(30)

pygame.quit()

最佳答案

由于球的下落速度大于1个像素,你必须确保球不会落在窗口下边缘以下。
您需要将球的底部限制在窗口的底部:

done = False
while not done:
    # [...]

    x = x + dx
    y = y + dy
    ballrect.topleft = (x,y)

    #PART A

    if ballrect.left < 0 or ballrect.right > width:
        dx = -dx
    if ballrect.top < 0:
        dy = -dy
    if ballrect.bottom > height:
        ballrect.bottom = height                       # <---
        y = ballrect.top                               # <---
        dy = -dy

    # [...]

关于python - Pygame 弹力球从地板下沉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64864966/

相关文章:

python - 如何判断屏幕上是否有比屏幕大的矩形?

python - 如何使用office365-rest-python-api从sharepoint下载文件

python - 在seaborn中绘制两列dataFrame

python - 在 pytz 中创建新时区

python - Pygame时间控制

python - 无法在 pygame 中改变颜色?

python - Pygame.movi​​e 丢失

python - 如何使用 Python 将按钮(加载项)添加到 Outlook

python - 使用 Pandas、Python 将数据附加到 HDF5 文件

python - 如何解决pygame‑1.9.3‑cp36‑cp36m‑win32.whl is not a supported wheel on this platform