2014-02-21 59 views
0
from math import * 
from graphics import * 
from time import * 

def main(): 

    veloc = .5 #horizontal velocity (pixels per second) 
    amp = 50 #sine wave amplitude (pixels) 
    freq = .01 #oscillations per second 

    #Set up a graphics window: 
    win = GraphWin("Good Sine Waves",400,200) 
    win.setCoords(0.0, -100.0, 200.0, 100.0) 

    #Draw a line for the x-axis: 
    p1 = Point(0,0) 
    p2 = Point(200,0) 
    xAxis = Line(p1,p2) 
    xAxis.draw(win) 

    #Draw a ball that follows a sine wave 
    for time in range(1000): 
     amp = amp * 2 
     x = time*veloc 
     y = amp*sin(freq*time*2*pi) 
     #y = abs(amp*sin(freq*time*2*pi)) 
     ball = Circle(Point(x,y),2) 
     ball.draw(win) 
     sleep(0.1) #Needed so that animation runs slowly enough to be seen 

#win.getMouse() 
#win.close()     
main() 

我遇到的問題是試圖在for循環內慢慢減少amp變量。該放大器變量設置50.我知道,以減少它應該是這樣的:在Python中操作變量

amp = amp 
amp = amp/2 

,但每次我試圖在這些發言的時間爲循環這是行不通的。

+0

爲什麼不把放一個列表? – ThePredator

+2

「它不起作用」 - 會發生什麼?你能顯示代碼嗎? – user2357112

+1

'amp = amp * 2' - 這只是我,還是你正在做的與你想做的事情完全相反? – user2357112

回答

0

變化

for time in range(1000): 
    amp = amp * 2      #this line multiplies 
    x = time*veloc 
    y = amp*sin(freq*time*2*pi) 
    #y = abs(amp*sin(freq*time*2*pi)) 
    ball = Circle(Point(x,y),2) 
    ball.draw(win) 
    sleep(0.1) 

到:

for time in range(1000): 
    amp /= 2        #this line now divides 
    x = time*veloc 
    y = amp*sin(freq*time*2*pi) 
    #y = abs(amp*sin(freq*time*2*pi)) 
    ball = Circle(Point(x,y),2) 
    ball.draw(win) 
    sleep(0.1) 
1
for i in range(0,10): 
    amp= amp/2 

此打印

25.0 
12.5 
6.25 
3.125 
1.5625 
0.78125 
0.390625 
0.1953125 
0.09765625 
0.048828125 
>>> 
+0

'''* = 2'''也有同樣快速的增長問題。 –