Warning: Undefined array key "keywords_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 234 Warning: Undefined array key "description_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 235 Warning: Undefined array key "commercials" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 261 Tutorial 2

Tutorial 2

GL (very) Basic Tutorial

aka "so, you want to write a game, do you?"
© 2008 PeeJay

Lesson 2 - Getting things moving

Right, it's time to get our man moving around the screen. If you look at the source code you will see reference to another function, namely UpdatePlayer. You will also see the code for this function - let's go through it simply (though not as simply as lesson 1 - as things move on, you will find I start to explain the mechanics behind the game rather than the commands themselves - this is deliberate, as it will get you using the built in help, and because within no time you will already know the common commands.)
LOCAL dirx,diry
We will use dirx and diry to store information on the direction our man is moving in the x and y direction. Since this is only a working variable, so can make it LOCAL, which means it will only be effective for this section of code (in this case, the function.)
IF KEY(203) THEN dirx=-1
Ah, and IF ...... THEN decision - this will only operate if a circumstance is true. In this case, if KEY(203) then set the variable dirx to -1. Remember, dirx is a local variable - it has not been made global, since I only want to use it within this function. dirx was chosen with dir being short for direction. Checking the scancodes again, you will find the code 203 is the left arrow - so, if you press left, set the x direction to -1.
Similarly, this logic is then applied to the other directions - right, up and down. Then we need to apply these directions to our man, so the next two lines do just that:
px=px+(2*dirx)
py=py+(2*diry)
Why use double quantity? Only because simply doing px=px+dirx leads to a rather slow moving figure - just one pixel at a time. Doubling the number of pixels will double the speed. Couldn't we just have made the dirx and diry -2 or +2? Yes, we could, but then if we decided to change the speed later, we'd have had 4 lines of code to change - this way, we only have 2. Can you think of how we might only need to make one alteration to adjust the speed? There is an answer at the end of this lesson (and you will see it used in the source code for lesson 3).
Now, we want to make sure our man doesn't go off the screen - so we now check the value of px to make sure it is still within screen boundaries, with the following line:
IF px<0 THEN px=0
For those of you not familiar with mathematical notation, < means less than, > means more than, and <> means is not equal to. Also, <= means less than or equal to, and similarly >= means greater than or equal to.
So, if px is less than 0, that is, if we are trying to move off the extreme left of the screen, then put it back to 0.
Now for the right of the screen with
IF px>608 THEN px=608
But wait a minute! Our screen is 640 pixels wide - why are we limiting out chap to 608? Simple, our graphic is 32 pixels wide (and high) and since the px,py co-ordinate refers to the top left of our image, that means the right hand side of our image would, in fact, be on the right edge of our screen.
Then we do the same check for the top and bottom of the screen.
Now try running the program again. You should now find you can move your player around with the cursor keys, and that you can't move off the screen. Now isn't this easy?
Answer to the problem set:
If we set a global variable, in this instance we will call it speed, and set it to 2 at the start of the program, then we could multiply the direction by speed instead of a fixed number. Then, if we decided to change the speed at a later date, we could simply alter the value stored in speed. You will see this used in lesson 3

Continue

http://www.glbasic.com/files/tutorial/ImgLesson2.png
If you worked out how to do this, well done, you are well on your way to programming. If not, don't worry - there is plenty of time to get to grips with how things work.
Download the lesson files
Previous Lession
Next Lession