Thread: Python help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Python help


    I am trying to modify the following code into two different programs, one is supposed to make a ball start bouncing where the user has clicked and the other is supposed to make the ball decrease in altitude a little everytime. Please help. I am so confused right now.
    Code:
    from math import *
    from graphics import *
    from time import *
    
    
    def goodSine():
        veloc = 1 #The Speed of the animation
        amp = 100 #How High the ball goes.
        freq = .01 #How Many times per second the object is printed.
    
        win = GraphWin("Good Sine Waves",400,200) #Makes a window
        win.setCoords(0.0, -100.0, 200.0, 100.0) #Sets coordinates for the planes
    
        p1 = Point(0,0) 
        p2 = Point(200,0)
        xAxis = Line(p1,p2) #Makes a line through previously mentioned points
        xAxis.draw(win) #Draws The Line on the Goodsine window.
    
    
        #Loops an expression which constantly draws a ball at different points on the graph.
        for time in range (1000):
            x = time*veloc
            y = abs(amp*sin(freq*time*2*pi))
            ball = Circle(Point(x,y), 2)
            ball.draw(win)
            sleep(0.05)
    
        win.getMouse()
        win.close()
    
    goodSine()

    This is what i got so far for the User click start. I get it to draw a single circle and thats it.

    Code:
    from math import *
    from graphics import *
    from time import *
    
    
    def goodSine():
        veloc = 1 #The Speed of the animation
        amp = 100 #How High the ball goes.
        freq = .01 #How Many times per second the object is printed.    
    
        win = GraphWin("Good Sine Waves",400,200) #Makes a window
        win.setCoords(0.0, -100.0, 200.0, 100.0) #Sets coordinates for the planes
    
        p1 = Point(0,0) 
        p2 = Point(200,0)
        xAxis = Line(p1,p2) #Makes a line through previously mentioned points
        xAxis.draw(win) #Draws The Line on the Goodsine window.
    
        start = win.getMouse()
        x = start.getX
        y = start.getY
    
    
        #Loops an expression which constantly draws a ball at different points on the graph.
        for time in range (1000):
            ball = Circle(start, 2)
            ball.draw(win)
            start.getX = (time*veloc)
            start.getY = abs(amp*sin(freq*time*2*pi))        
            sleep(0.05)
    
        win.getMouse()
        win.close()
    
    goodSine()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    freq = .01 #How Many times per second the object is printed.

    In other words, "print the object once every 100 seconds"

    Careful, dude/tte

    Fortune Cookie say "Lucky Day! You don't have graphics.py"

    Height that depends on a sine function won't look like a ball bouncing in the constant acceleration so familiar to us grounded earthlings. I can't, thus needn't, figure out your program. This is a better representation of said familiar ball bouncing using turtle graphics. Better still would be to include a discrete loss that takes place on bounces, and then include transfer of energy between rotation (moment of inertia of ball) and Vx when at the y=0 occurrences.

    Code:
    import sys
    import math
    import turtle
    
    drag_coefficient = 0.005
    
    Vx = 1
    Vy = 9
    x = 0
    y = 0
    Ay = -0.1
    Ax = 0
    dt = 1
    
    for t in range(0,1000,dt):
        angle = math.atan2(Vy,Vx)
        speed = (1-drag_coefficient)*math.hypot(Vx,Vy)
        Vx = speed*math.cos(angle)+Ax*dt
        Vy = speed*math.sin(angle)+Ay*dt
        dx = Vx*dt
        dy = Vy*dt
        x += dx
        y += dy
        turtle.goto(x,y)
        if (y < 0) and (Vy < 0):            # bounce
            Vy = abs(Vy)
            y = max(y,0)
    
    sys.stdout.write('press enter')
    sys.stdin.read(1)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo