Computer Science Canada

My ball won't come back ~ LOL

Author:  jolly50 [ Sun Nov 04, 2007 10:19 am ]
Post subject:  My ball won't come back ~ LOL

Hey,

I'm working on some animation and i'm close to figuring it out, but i still have a while to go....

code:

setscreen ("graphics")

var x :int

x:=0

loop


drawfilloval (x,200,100,100,brightblue)
delay (10)
drawfilloval (x,200,100,100,white)
x:=x+5

if x >maxx then
    drawfilloval (x,200,100,100,brightblue)
    delay (10)
    drawfilloval (x,200,100,100,white)
    x:=x-5
end if

end loop


I know it has something to do with the if statement....

What do i have to fix to make this work?

Author:  Clayton [ Sun Nov 04, 2007 10:26 am ]
Post subject:  RE:My ball won\'t come back ~ LOL

Look closely at your if statement. If your ball goes beyond maxx, then you subtract 5 from it's x value. Afterwards, you assume you're below maxx, so you add 5 again, and this process just keeps on going in an infinite loop. Try using a velocity variable to keep track of whether you should add or subtract to your x position.

Author:  CodeMonkey2000 [ Sun Nov 04, 2007 1:33 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

Basically (as Clayton said) make a variable called xIncrement (this is x component if your velocity). This will initially equal 5. Your x value will increase by xIncrement every time you go through the loop. If there is a collision, xIncrement should equal the negative of itself (ie. xIncrement=xIncrement*-1). And that's it.

Author:  jolly50 [ Mon Nov 05, 2007 7:12 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

So... something like this?

code:

setscreen ("graphics")

var x :int
var vx: int

x:=0
vx:=-5


loop


    drawfilloval (x,200,100,100,brightblue)
    delay (10)
    drawfilloval (x,200,100,100,white)
    x:=x+5

        if x>maxx then
        x:=x+vx
        end if
end loop


but the ball still wont come back?

Author:  HeavenAgain [ Mon Nov 05, 2007 7:51 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

change your if statement to
code:
    if x >= maxx then
        vx := -5
    elsif x <= 0 then
        vx := 5
    end if

and your condition for x to
code:
x:=x+vx

im just "fixing" your code, but this is not the best way to do this kind of grahpics, im sure...
look up on View.Update to fix the flicker problem
and some other stuffs.... Rolling Eyes

Author:  CodeMonkey2000 [ Mon Nov 05, 2007 8:01 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

And that can be further broken down to

code:
if x >= maxx or elsif x <= 0 then
         vx=vx*-1
         end if

Author:  Clayton [ Mon Nov 05, 2007 8:02 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

A slight modification:

Turing:

%draw the ball, etc.
...
%update ball's position
x += vx

if x <= 0 or x >= maxx then
    vx *= -1
end if


EDIT: Fixed comments to be lead in by a %, not // Confused

Author:  CodeMonkey2000 [ Mon Nov 05, 2007 8:05 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

Set the vx to 5 before entering the loop, or else it will get stuck.

Author:  jolly50 [ Mon Nov 05, 2007 9:27 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

ok, So i figured out my code...problem is.... I need help with the flicker....

code:


setscreen ("graphics")

var x, y :int
var vx, vy: int


x:=0
vx:=5
y:=0
vy:=Rand.Int (1,10)


loop


    drawfilloval (x,y,100,100,brightblue)
    delay (10)
    drawfilloval (x,y,100,100,white)
    x:=x+vx
    y:=y+vy

        if x>maxx then
        vx:=-5   
        elsif x<0 then
        vx:=5
        end if
       
        if y<0 then
        vy:=5
        elsif y>maxy then
        vy:=-5
        end if
end loop


Sorry if I'm getting aggravating....I'm just really interested in animation! Laughing

Author:  HeavenAgain [ Mon Nov 05, 2007 11:04 pm ]
Post subject:  Re: RE:My ball won\'t come back ~ LOL

HeavenAgain @ Mon Nov 05, 2007 8:51 pm wrote:
change your if statement to
look up on View.Update to fix the flicker problem

oh my god, im quoting myself Laughing
code:
View.Set ("offscreenonly")
View.Update

add viewset at the beginning of your code, and viewupdate before delay, or play around with it, i dont know Rolling Eyes

Author:  Clayton [ Tue Nov 06, 2007 8:07 am ]
Post subject:  RE:My ball won\'t come back ~ LOL

Take a look at my tutorial in the Turing Walkthrough for View.Set() and View.Update(). That should answer all of your questions.

Author:  Zampano [ Tue Nov 06, 2007 11:50 am ]
Post subject:  Re: My ball won't come back ~ LOL

That's good except there's probably one frame where the ball appears to be offscreen; the frame where the ball first goes offscreen. To solve that, why not rearrange your ifs and your drawfillovals? Then you use some math to restart the ball at the very edge of the screen.

Author:  jolly50 [ Tue Nov 06, 2007 9:51 pm ]
Post subject:  RE:My ball won\'t come back ~ LOL

THANKS HeavenAgain!!!! it worked great!!!! and thanks to the all the rest of you for all your help!!! Smile


: