Part 3: The Blocks to Dodge
In this section, we’re going to add the blocks that the player has to dodge.
Add the following code under the code that created the player.
MAKE OBJECT BOX 2,4.5,4.5,20
COLOR OBJECT 2,RGB(255,25,25)
POSITION OBJECT 2,RND(37.5),5,-90
The only code that you might not know in this section is the X value in the position object command; RND(37.5). The RND command generates a random number, up to the value in the parenthesis, 37.5 in this case. A number generated by the RND command will always be positive.
Go down under the player movement controls and type the following:
MOVE OBJECT 2,1.8
IF OBJECT POSITION Z(2)>145
POSITION OBJECT 2,RND(37.5),5,-90
ENDIF
This code shouldn’t be too hard to understand. It moves the object, and if the object is a distance behind the player, it repositions it randomly. An issue that we are running into now is that this object will only respawn on the left side of the field. The reason for this, which I mentioned earlier, is that the RND command will always give us a positive number, so it will never generate a negative X value for the position. Since our playing field is right on 0,0,0; one half of the field will never have an object positioned on it. We fix that like this. Underneath the code that created object 2, add this:
MAKE OBJECT BOX 3,4.5,4.5,20
COLOR OBJECT 3,RGB(255,25,25)
POSITION OBJECT 3,RND(37.5)*-1,5,-90
This creates an identical object as object 2, but you’ll notice that the positioning code now reads RND(37.5)*-1. This multiples the value by negative 1, making it negative. Now go back under the movement code for object 2, and add:
MOVE OBJECT 3,1.4
IF OBJECT POSITION Z(3)>145
POSITION OBJECT 3,RND(37.5)*-1,5,-90
ENDIF
Now object 3 will act like object 2, but on the other side of the map, and just a bit slower so they seem different. Now go ahead and add as many objects as you want. I had a total of 5, and had them all move at different speeds. You can choose the speed and shape of the objects, or you can just use the code that I used:
MAKE OBJECT BOX 4,18,4.5,4.5
COLOR OBJECT 4,RGB(255,25,25)
POSITION OBJECT 4,RND(20),5,-95
MAKE OBJECT BOX 5,18,4.5,4.5
COLOR OBJECT 5,RGB(255,25,25)
POSITION OBJECT 5,RND(20)*-1,5,-95
MAKE OBJECT BOX 6,30,4.5,4.5
COLOR OBJECT 6,RGB(255,25,25)
POSITION OBJECT 6,0,5,-95
*NOTE: object 6 is a wide block that comes straight down the middle
MOVE OBJECT 4,1
IF OBJECT POSITION Z(4)>145
POSITION OBJECT 4,RND(25),5,-95
ENDIF
MOVE OBJECT 5,1.2
IF OBJECT POSITION Z(5)>145
POSITION OBJECT 5,RND(25)*-1,5,-95
ENDIF
MOVE OBJECT 6,.5
IF OBJECT POSITION Z(6)>145
POSITION OBJECT 6,RND(3),5,-95
ENDIF
Your code so far should looks something like this (It will differ if you used your own code for the boxes):
SET WINDOW TITLE "Dodging Game"
SET WINDOW OFF
SET DISPLAY MODE 1024,768,32
SYNC ON
SYNC RATE 60
HIDE MOUSE
AUTOCAM OFF
MAKE OBJECT BOX 9999,75,5,200
COLOR OBJECT 9999,RGB(180,150,90)
MAKE OBJECT BOX 1,5,15,5
COLOR OBJECT 1,RGB(0,200,150)
POSITION OBJECT 1,0,10,95
YROTATE OBJECT 1,180
MAKE OBJECT BOX 2,4.5,4.5,20
COLOR OBJECT 2,RGB(255,25,25)
POSITION OBJECT 2,RND(37.5),5,-90
MAKE OBJECT BOX 3,4.5,4.5,20
COLOR OBJECT 3,RGB(255,25,25)
POSITION OBJECT 3,RND(37.5)*-1,5,-90
MAKE OBJECT BOX 4,18,4.5,4.5
COLOR OBJECT 4,RGB(255,25,25)
POSITION OBJECT 4,RND(20),5,-95
MAKE OBJECT BOX 5,18,4.5,4.5
COLOR OBJECT 5,RGB(255,25,25)
POSITION OBJECT 5,RND(20)*-1,5,-95
MAKE OBJECT BOX 6,30,4.5,4.5
COLOR OBJECT 6,RGB(255,25,25)
POSITION OBJECT 6,0,5,-95
DO
POSITION MOUSE SCREEN WIDTH()/2,SCREEN HEIGHT()/2
POSITION CAMERA 0,30,145
POINT CAMERA 0,10,95
IF LEFTKEY()=1 AND OBJECT POSITION X(1)<34.5
MOVE OBJECT LEFT 1,.2
ENDIF
IF RIGHTKEY()=1 AND OBJECT POSITION X(1)>-34.5
MOVE OBJECT RIGHT 1,.2
ENDIF
MOVE OBJECT 2,1.8
IF OBJECT POSITION Z(2)>145
POSITION OBJECT 2,RND(37.5),5,-90
ENDIF
MOVE OBJECT 3,1.4
IF OBJECT POSITION Z(3)>145
POSITION OBJECT 3,RND(37.5)*-1,5,-90
ENDIF
MOVE OBJECT 4,1
IF OBJECT POSITION Z(4)>145
POSITION OBJECT 4,RND(25),5,-95
ENDIF
MOVE OBJECT 5,1.2
IF OBJECT POSITION Z(5)>145
POSITION OBJECT 5,RND(25)*-1,5,-95
ENDIF
MOVE OBJECT 6,.5
IF OBJECT POSITION Z(6)>145
POSITION OBJECT 6,RND(3),5,-95
ENDIF
SYNC
LOOP