
-----------------------------------
TylerL
Fri May 26, 2006 1:43 am

Quick program, suggestions?
-----------------------------------
View.Set ("graphics:500,500")

var x, y, button, ballx, bally, originalx, originaly : int
var onball : boolean
var distance : real

var background : int := Pic.FileNew ("background.bmp")

procedure drawball (ballx, bally : int)

    drawfilloval (ballx, bally, 14, 14, black)

end drawball

procedure bounceback ()

    if ballx > originalx and bally > originaly then

        distance := Math.Distance (originalx, originaly, ballx, bally)

        for i : 1 .. distance div 2

            ballx -= (ballx - originalx) div (distance div 2)
            bally -= (bally - originaly) div (distance div 2)

        end for

    elsif ballx < originalx and bally > originaly then

        distance := Math.Distance (originalx, originaly, ballx, bally)

        for i : 1 .. distance div 2

            ballx += (originalx - ballx) div (distance div 2)
            bally -= (bally - originaly) div (distance div 2)

        end for

    elsif ballx > originalx and bally < originaly then

        distance := Math.Distance (originalx, originaly, ballx, bally)

        for i : 1 .. distance div 2

            ballx -= (ballx - originalx) div (distance div 2)
            bally += (originaly - bally) div (distance div 2)

        end for

    elsif ballx < originalx and bally < originaly then

        distance := Math.Distance (originalx, originaly, ballx, bally)

        for i : 1 .. distance div 2

            ballx += (originalx - ballx) div (distance div 2)
            bally += (originaly - bally) div (distance div 2)

        end for

    end if

end bounceback

procedure mousecheck ()

    if x > ballx - 28 and x < ballx + 28 and y > bally - 28 and y < bally + 28 then

        onball := true

    else

        onball := false

    end if

end mousecheck

originalx := maxx div 2 - 7
originaly := maxy div 2 - 7
ballx := originalx
bally := originaly

bally := maxy div 2 - 7

loop

    delay (25)
    Mouse.Where (x, y, button)

    mousecheck ()

    cls
    
    drawball (ballx, bally)


    if onball = true and button = 1 then

        drawball (ballx, bally)

        ballx := x
        bally := y

    else

        bounceback ()

    end if

    View.Update

end loop


It's late at night so I don't want to stay up and think about how I could fit trig into this, but paste that in Turing and run it, give me some suggestions :) First post btw!

-----------------------------------
TylerL
Fri May 26, 2006 1:45 am


-----------------------------------
Wow, I didn't clean that up before I posted it, don't mind the redundant code :)

-----------------------------------
TheOneTrueGod
Fri May 26, 2006 7:28 am


-----------------------------------
Not bad, not bad, though some code optimization wouldn't hurt.  It works pretty well, but i'll touch on some conceptual things.

#1)You could compress this entire procedure into like three lines if you just use Math.Angle and some trig.

procedure bounceback ()
    if ballx > originalx and bally > originaly then
        distance := Math.Distance (originalx, originaly, ballx, bally)
        for i : 1 .. distance div 2
            ballx -= (ballx - originalx) div (distance div 2)
            bally -= (bally - originaly) div (distance div 2)
        end for
    elsif ballx < originalx and bally > originaly then
        distance := Math.Distance (originalx, originaly, ballx, bally)
        for i : 1 .. distance div 2
            ballx += (originalx - ballx) div (distance div 2)
            bally -= (bally - originaly) div (distance div 2)
        end for
    elsif ballx > originalx and bally < originaly then
        distance := Math.Distance (originalx, originaly, ballx, bally)
        for i : 1 .. distance div 2
            ballx -= (ballx - originalx) div (distance div 2)
            bally += (originaly - bally) div (distance div 2)
        end for
    elsif ballx < originalx and bally < originaly then
        distance := Math.Distance (originalx, originaly, ballx, bally)
        for i : 1 .. distance div 2
            ballx += (originalx - ballx) div (distance div 2)
            bally += (originaly - bally) div (distance div 2)
        end for
    end if
end bounceback


#2) Instead of having mousecheck as a procedure, have it as a function.  Then you can just call it directly into the checks where it needs to be.

#3)  Your spacing kinda hurt my eyes.  Generally, you don't want a space in between every line, but if it helps you organize better, go ahead.

-----------------------------------
[Gandalf]
Sat May 27, 2006 12:09 pm


-----------------------------------
You could compress this entire procedure into like three lines if you just use Math.Angle and some trig.
I'm assuming you added a Math.Angle function to your Math module?  Tut tut, you should remember that not everyone has made this addition. ;)  I'd guess you just added your previously posted:
function angle (x1, y1, x2, y2 : real) : real
    var theta : real
    if x1 = x2 then
        if y2 > y1 then
            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end angle 

TylerL, in Turing you don't need the brackets after a procedure or function declaration if it takes no parameters.  It might help you notice that it's a function, but it's not neccessary. ;)

-----------------------------------
upthescale
Sat May 27, 2006 2:51 pm


-----------------------------------
"]You could compress this entire procedure into like three lines if you just use Math.Angle and some trig.
I'm assuming you added a Math.Angle function to your Math module?  Tut tut, you should remember that not everyone has made this addition. ;)  I'd guess you just added your previously posted:
function angle (x1, y1, x2, y2 : real) : real
    var theta : real
    if x1 = x2 then
        if y2 > y1 then
            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end angle 

TylerL, in Turing you don't need the brackets after a procedure or function declaration if it takes no parameters.  It might help you notice that it's a function, but it's not neccessary. ;)

u must no actionscript then? o and put
View.Set("offscreenonly") at the top, u got the view update, just not the offscreenonly part!

-----------------------------------
[Gandalf]
Sat May 27, 2006 5:24 pm


-----------------------------------
"]...
u must no actionscript then? o and put
View.Set("offscreenonly") at the top, u got the view update, just not the offscreenonly part!
What?  Why did you quote my post?  What does actionscript have to do with anything?  Actionscript is a programming language used with flash animations, which doesn't exactly relate to Turing.

-----------------------------------
beforelast
Sun May 28, 2006 2:13 pm


-----------------------------------
becasue what he si refering to is a method that is usedi naction script. some things require a () after it :)

-----------------------------------
wtd
Sun May 28, 2006 2:19 pm


-----------------------------------
"]TylerL, in Turing you don't need the brackets after a procedure or function declaration if it takes no parameters.  It might help you notice that it's a function, but it's not neccessary. ;)

They're parentheses, not brackets.  Don't be 99% right then get lazy and not get the terminology wrong.  ;)

Parentheses:

()

Braces:

{}

Brackets:

[]

-----------------------------------
NikG
Thu Jun 01, 2006 11:15 pm


-----------------------------------
Umm... when you refer to brackets in BEDMAS, what do you refer to? () or []?

-----------------------------------
Clayton
Thu Jun 01, 2006 11:39 pm


-----------------------------------
either way it doesnt matter, all wtd is pointing out is that terminology makes a difference in the programming world, though not so much in the math world where parnetheses () are called brackets [] :D

-----------------------------------
[Gandalf]
Fri Jun 02, 2006 11:59 pm


-----------------------------------
They're parentheses, not brackets.  Don't be 99% right then get lazy and not get the terminology wrong.  ;)
Indeed, you really have been cracking down on this lately. :)  

My excuse though is that more people will understand what I mean by brackets than parentheses. ;)  Ok, so that wasn't a good excuse...
