如何使用Graphics绘制一个炮弹

时间:2014-03-02 21:35:44

标签: python graphics computer-science

你好,现在必须写一个班来画一个炮弹的飞行。我写了这段代码,但我似乎无法在图上画出球。我需要在每个时间间隔内画球。

from graphics import*
from math import sin, cos, radians

class Tracker:

    def __init__(self,window,objToTrack):
         self.objToTrack = objToTrack
         self.circ = Circle(Point(objToTrack.getX(), objToTrack.getY()), 2)
         self.circ.draw(window)

    def update(self):
         point = self.circ.getCenter()
         x = point.getX()
         y = point.getY()
        self.circ.move(self.objToTrack.getX() - x, self.objToTrack.getY() - y)




class Projectile:

    def __init__(self, angle, velocity, height):
         self.xpos = 0.0
         self.ypos = height
         theta = radians(angle)
         self.xvel = velocity * cos(theta)
         self.yvel = velocity * sin(theta)

    def update(self, time):
         self.xpos = self.xpos + time * self.xvel
         yvel1 = self.yvel - 9.8 * time
         self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0
         self.yvel = yvel1

    def getY(self):
         return self.ypos

    def getX(self):
         return self.xpos

    def getInputs():
         a = eval(input("Enter the launch angle (in degrees): "))
         v = eval(input("Enter the initial velocity (in meters/sec): "))
         h = eval(input("Enter the initial height (in meters): "))
         t = eval(input("Enter the time interval between position calculations: "))
         return a,v,h,t

    def main():
        angle, vel, h0, time = getInputs()
        cball = Projectile(angle, vel, h0)
        while cball.getY() >= 0:
           cball.update(time)        
        print("\nDistance traveled: {0:0.1f} meters.".format(cball.xpos))

       Tracker(GraphWin("Tracker",500,500),cball)




 if __name__ == "__main__": 
     main()

1 个答案:

答案 0 :(得分:0)

首先要考虑以下问题和提示:

  • Tracker.update()什么时候被调用?
  • 您的窗口何时实例化?
  • 你什么时候在窗口上画圆圈?
  • 查看observer pattern

您的Tracker类未获知其对象的更新。你也可以在模拟炮弹飞行后建造追踪器和窗口。现在你的电话Tracker(GraphWin("Tracker",500,500),cball)将导致对炮弹的最终位置进行一次平局操作。

要实现曲线图:

def main():
    angle, vel, h0, time = getInputs()
    cball = Projectile(angle, vel, h0)

    win = GraphWin("Tracker",500,500,autoflush=False)
    while cball.getY() >= 0:
       cball.update(time)
       Tracker(win,cball)
    print("\nDistance traveled: {0:0.1f} meters.".format(cball.xpos))

    input("Ready to close? Type any key")
    win.close()

现在每次更新后都会创建一个Tracker,它会在图形窗口上绘制一个圆圈。这导致许多圆圈被绘制在彼此之上,绘制曲线。图纸仍然是颠倒的,所以再次考虑你的坐标系。还要在Tracker对象外部创建窗口,否则一旦删除跟踪器对象,它将被关闭。

对于炮弹的动画,您需要更改main(),以便跟踪器知道某些内容已发生变化。对于更复杂的情况,观察者模式很有用,但在这种情况下,只需在炮弹更新后调用Tracker :: update():

import time as sleeper
def main():
    angle, vel, h0, time = getInputs()
    cball = Projectile(angle, vel, h0)

    win = GraphWin("Tracker",500,500)
    win.setCoords(0, 0, 500, 500) #fixed coordinates

    tracker = Tracker(win,cball)
    while cball.getY() >= 0:
        sleeper.sleep(0.1) # wait 0.1 seconds for simple animation timing
        cball.update(time)
        tracker.update()
    print("\nDistance traveled: {0:0.1f} meters.".format(cball.xpos))

    input("Ready to close? Type any key") #keep the window open
    win.close()