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