2014-03-02 32 views
0

您好目前必須寫一個類來繪製一個炮彈球的飛行。我寫了這段代碼,但我似乎無法在圖上畫出球。我需要在每個時間間隔內抽出球。如何使用圖形繪製一個炮彈球

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() 

回答

0

首先採取下列問題,並暗示考慮:

  • 什麼時候Tracker.update()曾經被調用?
  • 你的窗口何時實例化?
  • 你什麼時候在窗戶上畫一個圓圈?
  • 退房的observer pattern

您的跟蹤類是沒有得到了解它的對象的更新。你也可以在模擬炮彈飛行後建立Tracker和Window。現在你的電話號碼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對象之外創建窗口,否則一旦跟蹤器對象被刪除,它將被關閉。

對於大炮球的動畫,您需要更改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()