Introduction to Objects tutorial
Author |
Message |
Drakain Zeil
|
Posted: Thu Apr 07, 2005 4:38 pm Post subject: Introduction to Objects tutorial |
|
|
For you to get any use out of this, you need to know these:
Arrays
2-dimensional arrays
And knowing how to do pong wouldn't hurt either.
Great. Well then, let's begin.
(the entire final code is an attachment)
Objects are things that you can make many of at once, with minimal code. It's part of OOP.
This is just a simple introduction to the concept.
(not sure why I called it nothing.t, but that's also not important)
Anyway, if you are going to manage 1 object, you will need to know what to tell your program. Let's say we want to make a dot that bounces around a 100x100 box.
We would make a variable for the x and y positions of it, then set up IF statements to check where it's headed, and reflect accordingly. Also, you would (if logical about it) make a variable for the direction.
code: | %these are the co-ords of the dot:
var x := 50
var y := 20
var xd, yd := 1 %these are the direction/increment
loop
if x + xd > 100 or x + xd < 0 then %If the ball will move beyond the box's X axis
xd *= -1 %Multiply xd by a negative one, changing it's direction (-1*-1=1 too)
end if
x += xd %Increments the X direction accordingly
if y + yd > 100 or y + yd < 0 then %Same as above, but for Y axis
yd *= -1
end if
y += yd
delay (10)
Draw.Dot (x, y, black)
end loop
|
(the above code simply bounces a black dot around on a white screen, then over-laps it's self since CLS would make it harder to see it by contrast, it's not important to the tutorial anyway)
Now, normaly if you wanted more of these you would need a lot of variables, and repetative code.
Guess what, you can have as many of these as you want, with a bit of object management, and less code.
How do you supose we would manage 25 objects? Well, we can keep track of them in an array.
An array can be exactly like a database infact.
Databases (in general anyway) are a bunch of numbers, with different sets of numbers inside of them that store... suprise! numbers.
Let me explain.
You have 25 balls, let's call them all ball 1 through 25 seperately.
How many values do we care about? Well, the X and Y axis (that's two), so the object has 2 values, with 25 seperate identities.
(if you want to get creative, after the tutorial try giving each ball it's object colour on your own).
So, let's set it up.
code: | var xy : array 1 .. 25, 1 .. 2 of int |
Note that I used one variable for both directions (you can use two, but this is less code, later I prove you can use different variables) So, for 25 objects, there are 2 numbers (one the X position, and the other the Y).
Great, we've done the entire database. Easy enough?
But, that's only 25 objects, let's make as many as we want... well, as much as our computers memory can take anyway (this shouldn't become a problem really).
So, just set up a get and get a number. What are you waiting for?
code: |
var objs:=0
put "How many cloned objects?" %basic put.
get objs %Basic get.
var xy : array 1 .. objs, 1 .. 2 of int
|
Wow, that was easy.
Now what do we need? Moving the ball you say? Well okay, but first we need the variable for that. You can either make another variable (which is what I did to prove a point) or you can change that 1..2, to a 1..4. Either way you get 2 more data sets for each object.
Let's just do it quickly then, stick it under the "xy" array.
code: | var xymove : array 1 .. objs, 1 .. 2 of int |
Well, now what? Let's start giving numbers to all of those numbers.
What do you mean you don't want to enter in hundreds of numbers? Just use Rand.Int The ultimate in easy.
So, we want to go give numbers from 1 to the last object, and for both data sets on both variables (or 4 on one variable depending on what you want to do, both work fine, but one variable becomes easier to keep track of then two, however, lots of sub-sets become harder to keep track of).
Hmm, what could do this? The wonderful FOR loop ofcourse. Hey, let's even go do something wild here, and NEST one inside of the other! I know it may sound like the talk of madmen, but don't sign me away just yet.
code: | for a : 1 .. objs %Begining a for loop for all of the objects
locate (10, 10) %Position for the text on screen (row 10, col 10)
put (a / objs) * 100 : 5 : 2, "% done" %Basic/easy percent loader
for b : 1 .. 2 %Loop between all switches on the variable.
xy (a, b) := Rand.Int (1, 600) %Just setting a random position
if Rand.Int (-1, 1) = 1 then %Just setting a random direction
xymove (a, b) := 1 %Go positive direction
else
xymove (a, b) := -1 %Go negative direction
end if
end for
end for |
Not only that, I decided to be nice and toss in an extreamly simple % compleat meter. (and yes, for the xymove, you could have just gone from 0 and 1, and set 1 to positive, and 0 to the other, but that's not of prime importance, heck, you could even set one axis movement to 0, and have it on one axis, and remove a few lines of code).
So, now we need to move everything. What are you ever going to do with these numbers?!
Well, how did you give them values to begin with? All you have to do now is change that up a bit.
You know what? Dot's are hard to see, let's make some nice red circles.
Okay, here's the deal, we want to run a big loop. (loop..end loop)
Inside of that, we need to access all objects (for..end for)
Inside of that we need to access all of the sub-values on the object (for..end for)
Inside of that we need to run our checks, to see if we want to keep going or reflect.
code: |
loop %Inf. loop, no real use.
for rend : 1 .. objs %For all of the objects
Draw.FillOval (xy (rend, 1), xy (rend, 2), 5, 5, red) %Draw object
for j : 1 .. 2 %For x and y axis...
if xy (rend, j) + 1 > 600 then %Movement/reflection
xymove (rend, j) *= -1 %Movement/reflection
elsif xy (rend, j) - 1 < 1 then %Movement/reflection
xymove (rend, j) *= -1 %Movement/reflection
end if
xy (rend, j) += xymove (rend, j) %Make the movement in data
end for
end for
cls
loop |
Great. You're done. You have a basic understanding of objects
If there was something you missed, the entire program I wrote is attached, with PLENTY of comments, and even a small management system for up to 5 objects (I commented it out), and I included flicker-free animation inside of it.
(not sure why I called it nothing.t, that's not important...
There is however, one line, 89, that includes a commented out CLS statement, if you remove the % sine you will see the dots without trailings)
Description: |
This is the entire program. It's a bunch of red balls moving around on the screen. |
|
Download |
Filename: |
nothing.t |
Filesize: |
3.08 KB |
Downloaded: |
296 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Mr. T
|
Posted: Thu Apr 07, 2005 11:03 pm Post subject: (No subject) |
|
|
the ball doesnt erase itself
|
|
|
|
|
|
Drakain Zeil
|
Posted: Fri Apr 08, 2005 5:55 am Post subject: (No subject) |
|
|
Quote: (not sure why I called it nothing.t, that's not important...
There is however, one line, 89, that includes a commented out CLS statement, if you remove the % sine you will see the dots without trailings)
Sure it does.
|
|
|
|
|
|
jamonathin
|
Posted: Fri Apr 08, 2005 7:45 am Post subject: (No subject) |
|
|
just change red to Rand.Int(1,maxcolor). Kinda looks 3D.
|
|
|
|
|
|
Drakain Zeil
|
Posted: Sat Apr 09, 2005 7:53 am Post subject: (No subject) |
|
|
Yes, but the point was for the person learning to make a static colour, not one that changes each time it's re-drawn.
|
|
|
|
|
|
|
|