Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick program, suggestions?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TylerL




PostPosted: Fri May 26, 2006 1:43 am   Post subject: Quick program, suggestions?

code:
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 Smile First post btw!
Sponsor
Sponsor
Sponsor
sponsor
TylerL




PostPosted: Fri May 26, 2006 1:45 am   Post subject: (No subject)

Wow, I didn't clean that up before I posted it, don't mind the redundant code Smile
TheOneTrueGod




PostPosted: Fri May 26, 2006 7:28 am   Post subject: (No subject)

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.
code:

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]




PostPosted: Sat May 27, 2006 12:09 pm   Post subject: (No subject)

TheOneTrueGod wrote:
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. Wink I'd guess you just added your previously posted:
code:
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. Wink
upthescale




PostPosted: Sat May 27, 2006 2:51 pm   Post subject: (No subject)

[Gandalf] wrote:
TheOneTrueGod wrote:
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. Wink I'd guess you just added your previously posted:
code:
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. Wink


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]




PostPosted: Sat May 27, 2006 5:24 pm   Post subject: (No subject)

upthescale wrote:
[Gandalf] wrote:
...

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




PostPosted: Sun May 28, 2006 2:13 pm   Post subject: (No subject)

becasue what he si refering to is a method that is usedi naction script. some things require a () after it Smile
wtd




PostPosted: Sun May 28, 2006 2:19 pm   Post subject: (No subject)

[Gandalf] wrote:
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. Wink


They're parentheses, not brackets. Don't be 99% right then get lazy and not get the terminology wrong. Wink

Parentheses:

code:
()


Braces:

code:
{}


Brackets:

code:
[]
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Thu Jun 01, 2006 11:15 pm   Post subject: (No subject)

Umm... when you refer to brackets in BEDMAS, what do you refer to? () or []?
Clayton




PostPosted: Thu Jun 01, 2006 11:39 pm   Post subject: (No subject)

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 [] Very Happy
[Gandalf]




PostPosted: Fri Jun 02, 2006 11:59 pm   Post subject: (No subject)

wtd wrote:
They're parentheses, not brackets. Don't be 99% right then get lazy and not get the terminology wrong. Wink

Indeed, you really have been cracking down on this lately. Smile

My excuse though is that more people will understand what I mean by brackets than parentheses. Wink Ok, so that wasn't a good excuse...
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: