Python 皮加梅弹跳球穿过地板

Python 皮加梅弹跳球穿过地板,python,pygame,Python,Pygame,下面的代码反弹一个球,但由于某种原因,球在完成反弹后会穿过地面。有人知道为什么吗?该代码的思想是一个球从左上角开始,然后下落和反弹,然后上下移动,如此类推,直到它停止反弹,但当它停止反弹时,它开始抖动,并慢慢下沉到地面。我不知道为什么,我也不明白。有人知道为什么吗?谢谢你的帮助 import pygame pygame.init() #All keyboard and mouse input will be handled by the following function def handl

下面的代码反弹一个球,但由于某种原因,球在完成反弹后会穿过地面。有人知道为什么吗?该代码的思想是一个球从左上角开始,然后下落和反弹,然后上下移动,如此类推,直到它停止反弹,但当它停止反弹时,它开始抖动,并慢慢下沉到地面。我不知道为什么,我也不明白。有人知道为什么吗?谢谢你的帮助

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()
导入pygame
pygame.init()
#所有键盘和鼠标输入将由以下功能处理
def handleEvents():
#下一行代码允许此函数访问dx和dy
#变量,而不将它们传递给函数或返回它们。
全局dx,dy
#检查新事件
对于pygame.event.get()中的事件:
#这使得点击X实际上关闭了游戏
#奇怪的是,这不会是默认值。
如果event.type==pygame.QUIT:
pygame.quit();退出()
#有没有按过键?
elif event.type==pygame.KEYDOWN:
#退出键也关闭游戏。
如果event.key==pygame.K_退出:
pygame.quit();退出()
elif event.key==pygame.K_RIGHT或event.key==pygame.K_d:
dx=dx+5
elif event.key==pygame.K_LEFT或event.key==pygame.K_a:
dx=dx-5
elif event.key==pygame.K_UP或event.key==pygame.K_w:
dy=dy-5
elif event.key==pygame.K_DOWN或event.key==pygame.K_s:
dy=dy+5
宽度=1000
高度=600
大小=(宽度、高度)
黑色=(0,0,0)#RGB
screen=pygame.display.set_模式(大小)
clock=pygame.time.clock()
ball=pygame.image.load(“ball.gif”)
ballrect=ball.get_rect()
x=0
y=0
dx=3
dy=3
完成=错误
虽然没有这样做:
handleEvents()
#移动球
x=x+dx
y=y+dy
ballrect.topleft=(x,y)
#A部分
如果ballrect.left<0或ballrect.right>宽度:
dx=-dx
如果ballrect.top<0或ballrect.bottom>高度:
dy=-dy
''如果球超出范围delta y,
然后△y变成它的负数,同样的
对于delta x,如果它在delta x的边界之外
'''
#乙部
dy=dy*0.99
dx=dx*0.99
''如果你想的话可能会有用
球在一定时间后停止移动,
或者相反,它可以使球在每帧“”中缓慢移动更长的距离
#丙部
dy=dy+0.3
''dy慢慢地在每一帧上加上.3,产生反弹
每次都变小,直到最终完全停止
#画一切
屏幕填充(黑色)
屏幕。blit(球、球)
pygame.display.flip()
#延迟30 fps
时钟滴答(30)
pygame.quit()

由于球的下落速度大于1个像素,因此必须确保球不会落到窗口下边缘以下。
您需要将球的底部约束到窗口的底部:

done=False
虽然没有这样做:
# [...]
x=x+dx
y=y+dy
ballrect.topleft=(x,y)
#A部分
如果ballrect.left<0或ballrect.right>宽度:
dx=-dx
如果ballrect.top<0:
dy=-dy
如果ballrect.bottom>高度:

ballrect.bottom=height#可以使用if语句检查球的y位置是否低于地板,如果低于地板,可以将其设置为地板的y坐标遇到的问题是由于使用的抽象物理模型。如果需要接近真实生活的模拟,我建议使用真实的物理引擎。Pygame通常与物理引擎配对。此外,这里还有Pygame+Pymunk。摘自《圣经》。