[Tutor] What's the error (program)

Alex Gillis Gillisai@btinternet.com
Tue Nov 5 22:33:01 2002


OK, here is the recently annotated program thats causing problems.  I hope
you can understand it and I haven't done anything blatantly wrong.  Feel
free to critise anything, I haven't had any one who knows what they're doing
look at anything I've written before so it'd be good to get some advice.

#This is a juggling simulator for 3 balls only.  Put in three integers
#(say between 1 and 9) and the program should juggle the balls at
#varying heights related to those three numbers.  If you want a full
#explaination of how the numbers work search for a notation called
"siteswap".


from visual import *

import array

#Creating the array the throw heights will be read from

a = input("What is a?")
b = input("What is b?")
c = input("What is c?")

siteswap = array.array('B',[a,b,c])

#Defining what happens between when the balls are caught and when they are
#thrown.  It's called scoop but its really just a horizontal movement.

def scoopleft(ball):
    ball.velocity.x = 0.4
    ball.velocity.y = 0

def scoopright(ball):
    ball.velocity.x = -0.4
    ball.velocity.y = 0


i = 0

next_number = siteswap[i]


#I guess this is the heart of the program.  While a ball is in the air, the
else
#statement controls its flight.  If its at either throw point then the two
#bottom elif statements deal with it.  If its not in the air but not ready
#to be thrown then it is referred to one of the scoop functions.

def throwcatch(ball):
    if ball.pos.y == 0:
        if ball.pos.x < -0.1:
            scoopleft(ball)
        elif ball.pos.x > 0.1:
            scoopright(ball)
        elif ball.pos.x == -0.1:
            ball.velocity.y = (next_number - 1)*1.25
            ball.velocity.x = (next_number - 1)*1.2
        elif ball.pos.x == 0.1:
            ball.velocity.y = (next_number - 1)*1.25
            ball.velocity.x = -((next_number - 1)*1.2)
    else:
        ball.velocity.y = ball.velocity.y - 10*dt

#The first few test programs didn't have perfect timing and would degenerate
after a while
#so every 0.25 seconds the ball going to be caught just gets moved to where
it should be.

def justify(ball):
    if t == 25:
        if ball.pos.y <= 0.1:
            ball.pos.y = 0
            if 0 < ball.pos.x < 0.15:
                ball.pos.x = 0.1
            elif -0.15 < ball.pos.x < 0:
                ball.pos.x = -0.1

#Defining the "hands"

left_hand = box(pos=(-0.15,0,0), length=0.12, height=0.03, width=0.1,
color=color.blue)

right_hand = box(pos=(0.15,0,0), length=0.12, height=0.03, width=0.1,
color=color.blue)

#Defining the balls

ball1 = sphere(pos=(0.1,0,0), radius=0.05, color=color.green)

ball1.velocity = vector(0,0,0)

ball2 = sphere(pos=(-0.2,0,0), radius=0.05, color=color.red)

ball2.velocity = vector(0,0,0)

ball3 = sphere(pos=(0.3,0,0), radius=0.05, color=color.blue)

ball3.velocity = vector(0,0,0)


dt = 0.01

t = 1



while 1:

    rate(100)

    throwcatch(ball1)
    justify(ball1)
    throwcatch(ball2)
    justify(ball2)
    throwcatch(ball3)
    justify(ball3)

    t = t + 1

    #If you've read the code carefully you'll have seen that every 0.25
seconds,
    #when t == 25 the next ball is thrown.  This bit of code changes the
next_number to be
    #thrown and resets the counter.

    if t == 25:

        t = 1

        i = i + 1

        if i == 3:
            i = 0