Quote: "i take it the /2 means half,so its setting up the sprite to be half way up and half way across."
Yep. Remember that if you have any questions about a specific command, you can click on it and press F1 to see its helpfile.
Here's a version of the 2D one with more comments:
`Basic game setup
`This allows you to control how many frames per second the program draws
sync on
`This sets the frames per second to 60
sync rate 60
`This changes the program to 640 pixels by 480 pixels wide
`It leaves the bit depth unchanged
set display mode 640,480,screen depth()
`This changes the color of the backdrop - the default is blue, this makes it black.
color backdrop rgb(0,0,0)
`Draws a few circles and saves them as an image
`This sets the color we're drawing with (white)
ink rgb(255,255,255),0
`Draw one circle with radius 20 and center at 20,20
circle 20,20,20
`Draw another in the same place but with radius 10
circle 20,20,10
`Save the image as image 1
`The image area to be saved is from (0,0) to (41,41)
`The 1 at the end tells the program to save them image pixel-perfectly
`If we don't put the one, the image can get distorted
get image 1,0,0,41,41,1
`Make the circle image into an easily controllable 2D sprite
`Sprite number 1, at (100,100) with image number 1
sprite 1,100,100,1
`Line up the sprite's position in the middle of the image
offset sprite 1,sprite width(1)/2,sprite height(1)/2
`Set up some variables that tell the ball where to go and how fast
`Instead of messing around with angles, the easiest way to make a ball bounce
`Is to store its X direction and Y direction. If it hits the top or bottom of the
`screen, reverse the y. If it hits the sides, reverse the x.
`Of course, with this method you can only bounce at right angles
balldirx=1
balldiry=1
`This is how fast the ball will move
ballspeed=2
`Main loop ==========================
DO
`Move the ball
`This positions sprite number 1 at its current position plus the ball's movement
sprite 1,sprite x(1)+ballspeed*balldirx,sprite y(1)+ballspeed*balldiry,1
`Bounce off the sides of the screen
`If it hits the right side, switch the x
if balldirx=1 and sprite x(1)>screen width()-20
balldirx=-1
endif
`If it hits the bottom, switch the Y
if balldiry=1 and sprite y(1)>screen height()-20
balldiry=-1
endif
`If it hits the left, switch the x
if balldirx=-1 and sprite x(1)<20
balldirx=1
endif
`If it hits the top, switch the y
if balldiry=-1 and sprite y(1)<20
balldiry=1
endif
`Sync is important - this is what tells the program to draw all the stuff
`we did (because we're using SYNC ON, we have to do this manually)
sync
LOOP
In 3D, it's slightly easier because 3D primitive objects are already positioned at their center. However, you have a new direction to worry about - z, which is the depth of the world.
Here's a 3D example in all 3 dimensions:
`Basic game setup
sync on
sync rate 60
set display mode 640,480,screen depth()
`Disables the autocam - we'll be positioning the camera ourselves later
autocam off
color backdrop rgb(0,0,0)
`Makes a 3D sphere object with diameter 10
make object sphere 1,10
`This command seeds the rnd function with the value from the
`system's internal timer - the only really random value we can get
randomize timer()
`rnd(80) will return a random number from 0 to 80
`Make sure you use randomize timer() at the beginning
`of your program or the "random" values will always
`be the same.
`Position the sphere in a random spot, just to make it interesting
position object 1,rnd(80)-40,rnd(80)-40,rnd(80)-40
`This will make a few planes (flat objects) for the ball to bounce off of
`In 2D, we'd only need 4 - the top, bottom, left, and right. However, in
`3D we also need the front and back.
`X-axis planes
`Make a flat plane with width and height 100
make object plane 2,100,100
`Rotate the object on its y-axis so it will face into the ring
yrotate object 2,90
`Position it in the right spot
position object 2,-50,0,0
`Make another one
make object plane 3,100,100
`And so on
yrotate object 3,-90
position object 3,50,0,0
`Y-axis planes
make object plane 4,100,100
xrotate object 4,90
position object 4,0,50,0
make object plane 5,100,100
xrotate object 5,-90
position object 5,0,-50,0
`Z-axis planes
make object plane 6,100,100
xrotate object 6,0
position object 6,0,0,-50
make object plane 7,100,100
yrotate object 7,180
position object 7,0,0,50
`Now we need to account for the z direction
balldirx=1
balldiry=1
balldirz=1
ballspeed=2
`Position the camera in the right spot
`In 2D space, we're just drawing on the screen, no cameras to worry about
`In 3D space, we have to position and point the camera so we can see the right part of the world
`This will position the camera in the top-left of our box
position camera -50,50,50
`This aims it back toward the center (which is at 0,0,0)
point camera 0,0,0
`Main loop ==========================
DO
`Move the ball
`This positions object 1 (the ball) at its current position plus the ball's movement
position object 1,object position x(1)+ballspeed*balldirx,object position y(1)+ballspeed*balldiry,object position z(1)+ballspeed*balldirz
`In 3D, we have our task simplified somewhat
`We can use the command OBJECT COLLISION to see when the ball hits
`one of the sides
`Store the collision data in a variable
`1 is the ball object
`0 indicates that we want to see any object that ball collides with
temp=object collision(1,0)
`X-axis collision
if balldirx=1 and temp=3
balldirx=-1
endif
if balldirx=-1 and temp=2
balldirx=1
endif
`Y-axis collision
if balldiry=1 and temp=4
balldiry=-1
endif
if balldiry=-1 and temp=5
balldiry=1
endif
`Z-axis
if balldirz=1 and temp=7
balldirz=-1
endif
if balldirz=-1 and temp=6
balldirz=1
endif
sync
LOOP