Pygame 弹力球从地板下沉 [英] Pygame Bouncy Ball Sinks Through Floor

查看:25
本文介绍了Pygame 弹力球从地板下沉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

    # [...]

这篇关于Pygame 弹力球从地板下沉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆