Pygame Bouncy Ball Sinks Through Floor

走远了吗. 提交于 2021-01-30 02:33:29

问题


The code below bounces a ball but for some reason the ball goes through the ground after it finishes its bounces. Anyone Know Why? The idea of the code is a ball starts at the top left corner and then falls and bounces and then goes up and back down and so on until it stops bouncing, but when it stops bouncing it starts jittering and slowly sinks through the ground. Idk why and I cant figure it out. Anyone know why? Thanks for the help

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:


Since the falling speed of the ball is greater than 1 pixel, you have to make sure that the ball does not fall below the lower edge of the window.
You need to constrain the bottom of the ball to the bottom of the window:

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

    # [...]



来源:https://stackoverflow.com/questions/64864966/pygame-bouncy-ball-sinks-through-floor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!