在Python中调试乒乓球游戏
我累了,我已经尝试了一整天了。这是一种乒乓球游戏,球从墙上弹起并与用户的“板”或球拍接触。我已经调试了一段时间,结果发现我的 iCollide 和 iGet_Velocity 部件引起了很多问题。
from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Ball (games.Sprite):
def update (self):
if self.right>544:
self.dx = -self.dx
if self.top > 11 or self.bottom > 470:
self.dy = -self.dy
if self.left < 0:
self.iLost()
def iLost (self):
games.Message (screen = self.screen,
x = 340,
y = 240,
text = "Game Over",
size = 90,
color = color.red,
lifetime = 250,
after_death = self.screen.quit)
def ihandle_collision (self):
new_dx, new_dy = Slab.iVelocity()
self.dx += self.dx + new_dx #For handling collision, must take velocity of the mouse object and put add it to the velocity of the ball.
self.dy += self.dy + new_dy
class Slab (games.Sprite):
def update (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
def iCollide (self):
for Ball in self.overlapping_sprites:
Ball.ihandle_collision()
def iVelocity (self):
self.get_velocity()
def main():
#Backgrounds
pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
games.screen.background = pingpongbackground
#Ball: Initializing object and setting speed.
ballimage = games.load_image ("pingpongball.jpg", transparent = True)
theball = Ball (image = ballimage,
x = 320,
y = 240,
dx = 2,
dy = 2)
games.screen.add(theball)
#Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
slabimage = games.load_image ("pingpongpaddle.jpg", transparent = True)
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(theslab)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main ()
I'm tired, and I've tried to run this all day. IT's a game of pong where the ball bounces off the wall and comes into contact with the user's "Slab" or paddle. I've treid to debug for a while, and turns out my iCollide and the iGet_Velocity parts have been causing a lot of issues.
from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Ball (games.Sprite):
def update (self):
if self.right>544:
self.dx = -self.dx
if self.top > 11 or self.bottom > 470:
self.dy = -self.dy
if self.left < 0:
self.iLost()
def iLost (self):
games.Message (screen = self.screen,
x = 340,
y = 240,
text = "Game Over",
size = 90,
color = color.red,
lifetime = 250,
after_death = self.screen.quit)
def ihandle_collision (self):
new_dx, new_dy = Slab.iVelocity()
self.dx += self.dx + new_dx #For handling collision, must take velocity of the mouse object and put add it to the velocity of the ball.
self.dy += self.dy + new_dy
class Slab (games.Sprite):
def update (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
def iCollide (self):
for Ball in self.overlapping_sprites:
Ball.ihandle_collision()
def iVelocity (self):
self.get_velocity()
def main():
#Backgrounds
pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
games.screen.background = pingpongbackground
#Ball: Initializing object and setting speed.
ballimage = games.load_image ("pingpongball.jpg", transparent = True)
theball = Ball (image = ballimage,
x = 320,
y = 240,
dx = 2,
dy = 2)
games.screen.add(theball)
#Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
slabimage = games.load_image ("pingpongpaddle.jpg", transparent = True)
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(theslab)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main ()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,在没有更多数据的情况下,我无法确定哪里出了问题。但看起来你的碰撞响应代码有点奇怪。例如,在第一部分中,您应该这样做以正确处理边界条件:
此外,您的碰撞响应代码有点不稳定。这是未经测试的,但它做了大约正确的事情:(编辑:用更多注释重写它,以便更清楚)
您应该尝试做的是找到球与板的哪个面相交,然后反映球的速度按照那个方向。现在这段代码尚未经过测试,因为我无法运行您的示例。然而,它应该让您了解正确的解决方案应该做什么。
Well, without having some more data, I can't say for certain what is wrong. But it looks like your collision response code is a bit screwy. For example, in the first section you should do this to handle your boundary conditions properly:
Also your collision response code is a bit wonky. This is untested, but it does approximately the right thing: (EDIT: Rewrote this with more comments so that it would be more clear)
What you should try to do is find which face of the slab the ball intersects, then reflect the balls velocity according to that direction. Now this code is by no means tested, since I couldn't get your example to run. However it should give you an idea of what the correct solution ought to do.
这两行看起来很粗略:
尝试:
These two lines look sketchy:
Try: