作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码使球反弹,但由于某种原因,球在完成反弹后穿过地面。有人知道为什么吗?代码的想法是,一个球从左上角开始,然后落下并弹起,然后向上和向后落下,直到它停止弹跳,但当它停止弹跳时,它开始抖动并慢慢沉入地面。我不知道为什么,我想不通。有人知道为什么吗?感谢您的帮助
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/
菜鸟问题:我试图将一个球扔到地板上并使其粘在那里,甚至滚过平面。现在它穿过飞机。我不确定我在哪里犯了错误,或者我是否做错了什么。 var world, timeStep=1/60, scene, re
我的代码工作正常,直到我在编译时突然收到这条消息: c:\program files (x86)\microsoft visual studio 12.0\vc\include\math.h(700)
如何在 jq jq-1.5-1-a5b5cbe 中对数字进行舍入、地板、天花板和截断? 例如,使用 {"mass": 188.72} , 我要 {"mass": 188}带地板,{"mass": 18
我知道在 Java(可能还有其他语言)中,Math.pow 是在 double 上定义的并返回一个 double 。我想知道到底为什么写 Java 的人不也写一个返回 int 的 pow(int, i
我是一名优秀的程序员,十分优秀!