【发布时间】:2012-07-28 16:54:11
【问题描述】:
我已将这个弹球游戏程序作为我班级的作业,但我一直无法解决弹球运动和碰撞问题。
第一个问题是,无论用户将速度设置为哪个方向,球都只会以特定角度移动。
我真的不知道为什么它不应该工作,根据我的笔记、讲座幻灯片和讨论讲义,它应该没问题。那么有人知道为什么它不起作用吗?我环顾四周,找不到明确的答案。任何帮助将不胜感激。我被难住了:(
不工作意味着无论用户将弹球设置为哪个方向,它都只会向一个方向移动(例如,用户设置弹球向左,弹球向右;用户设置弹球向上,它是正确的;等等) 此外,弹球不会与墙壁或任何目标发生碰撞。
图形是graphics.py:http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html
这里是碰撞代码(连同速度反转,只保持与游戏板右墙的碰撞):
def checkHit(ball,target,dispX,dispY,VelX,VelY,hit): ###pulled the definition out of the loop but keeping it here for easier reference
center = ball.getCenter() ###defines the center of the pinball as a point
hit = 0 ###used for differentiating between objects collided with
if center.getX() + 1 <= 45 and center.getX() + 1 + dispX > 45: ####if the pinball collides with the right wall of the board
VelX = VelX *(-1) ###velocity in the x direction reverses
hit = 0 ###did not collide with a target
for j in range(1000):####1000 frames (ball isn't expected to last long in the air, only a couple seconds)
vy = vy - 9.8 ###effect of gravity
dx = vx / math.sqrt(vx**2 + vy**2) ###speed in x direction over time
dy = vy / math.sqrt(vx**2 + vy**2) ###speed in y direction over time
checkHit(pinball,target_front1,dx,dy,vx,vy,0) ####runs function each frame for collision testing
pinball.move(dx , dy) ###moves pinball
【问题讨论】:
-
根据您的问题定义“不起作用”。
-
你提到了“讲义”。这是作业吗?如果是,请标记它。
-
一般情况下,尽量避免发布整个代码(尤其是当我们无法访问必要的模块时,
graphics)并将其细化为尽可能小的代码块。让我们更容易为您提供帮助,让您自己了解问题。 -
Graphics.py 在这里mcsp.wartburg.edu/zelle/python
标签: python game-physics collision