Computer Science Canada

Rand.Int Tutorial (With Boxes)

Author:  upthescale [ Sun May 14, 2006 2:46 pm ]
Post subject:  Rand.Int Tutorial (With Boxes)

Ok, i decided to make a ranint tutorial...I looked the the turing walkthourgh and didn't see it so ill makeo ne, but ill take it more advanced...what i mean by advanced is not number randint, but having boxes appear ranomly on the screen

First, create the variable

code:

var Boxx,Boxy:int


Still with me? hopefully because that shouldn;t be to hard. Now you know when you create a variable you alway have to give it a value in this case we would use a Rant.Int statement. Instead of going

code:

Boxx:=200
Boxy:=300



we can use the rand int way. If boxx is = to 200, and boxy = 300, no matter what, when you run the screen it will always in appear in the same spot, at x 200, y 300

What if you want the box to randomly go on the screen? Hmm, not that hard, i will begin the randint statement


code:

setscreen("graphics:900;600")
var boxx,boxy:int %Creating the box

Rand.Int (boxx,1,800)%Will appear anywhere on the x axis between 1 and 800

Rand.Int (boxy,1,500)%Will appear anywhere on the y axis between 1 and 500


That's basically how to use a randint statement..Now if your turing run window is small, then alot of the times the box will not appear because it is set from 1 to 800. So if your run window is 1 -600, then you might not see the box sometimes, fori t goes all the way to 800, so always check to make sure the box will stay in..I do this by going

code:

Rand.Int (boxx,700,700)

Notice how the two numbers are the same? this is the same as going

code:

boxx:=700


That was wayto check to make sure the box will never go off the screen

Here is a little program:

code:

setscreen ("graphics:1000;800")
var x, y : int
randint (x, 1, 900)
randint (y, 1, 700)
loop
    drawfillbox (x, y, x + 30, y + 30, 7)
end loop



So there's a box that will randomly be placed on the screen..you can make it move by going x+=1 or x-=1.

To make this fun, take the randint statement, and put it inside the loop! Wink then the box will constantly go all over the place!

I hope this tutorial helped

Robbie

Author:  Cervantes [ Sun May 14, 2006 4:09 pm ]
Post subject: 

Arrr...

  • You need to understand the difference between Rand.Int and randint. They are not the same thing. Rand.Int is a function (it returns a value -- a random integer between given bounds, in this case), while randint is a procedure (it doesn't return a value. Rather, you feed it your variable, and it mercilessly changes its value -- that's terrible coding style. Why hasn't Holtsoft removed randint yet?)
    Code like the following is wrong and will not execute:
    upthescale wrote:

    code:

    setscreen("graphics:900;600")
    var boxx,boxy:int %Creating the box

    Rand.Int (boxx,1,800)%Will appear anywhere on the x axis between 1 and 800

    Rand.Int (boxy,1,500)%Will appear anywhere on the y axis between 1 and 500


    because Rand.Int is a function, not a procedure.

  • upthescale wrote:

    code:
    Rand.Int (boxx,700,700)

    Notice how the two numbers are the same? this is the same as going
    code:
    boxx:=700


    Not quite, since the first bit of code doesn't work. If you're using Rand.Int, you would have to do it like this:
    code:
    boxx := Rand.Int (700, 700)


  • Variables don't start with capital letters.

  • The last bit of code shouldn't have a loop. It only needs a loop if you want the box to move around the screen.

  • If your doing all these graphics, you should be using maxx and maxy for the size of the window, rather than hardcoding it all over the place.

Author:  TheOneTrueGod [ Sun May 14, 2006 4:50 pm ]
Post subject: 

You forgot spelling / grammer checks Cervantes.

Edited for spelling / grammer Razz

Author:  Delos [ Sun May 14, 2006 6:16 pm ]
Post subject: 

TheOneTrueGod wrote:
You forgot spelling / grammer checks Cervantes.

Edited for spelling / grammer Razz


Not even worth his while. That was so past abysmal that having spent the time to point out his lexical shortcomings would have been like profusely pounding one's cranium against a concrete-reinforced, elevated enclosure qua obstacle...
But not to worry too much. I've heard that in V3, tuts will have to be mod approved before they can be posted. Melikes this idea!


: