aka "so, you want to write a game, do you?"
© 2008 PeeJay
© 2008 PeeJay
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
LOCAL t$="000"+scoreOkay, so we are setting up a local variable holding "000" and tagging the score on the end. This means, for scores from 0 to 9, it will have 3 leading zeroes and be 4 characters long. Perfect. Oh, but what happens when the score goes from 10 to 99? We still have 3 leading zeroes, but we now have 5 characters in the string, and we want to limit it to four!
t$=MID$(t$,LEN(t$)-4,4)We have 2 new commands here on one line - MID$ and LEN - so lets look at what each one does. LEN is short for LENgth, and returns the length of a character string. MID$ is used to take a section of characters from a string, using the format: MID$(string name$,character to start from, length) So, for our problem, since the LEN(t$) is now 5, we want to start from the second character in (the first character is in position 0, the second in position 1). By putting zeroes at the start of the string, we get the effect of drawing leading zeroes on the score, so, for example, if your current score is 123, then "000"+score would make "000123", but by truncating this to 4 characters by removing excess from the left, the result is "0123" - neat eh? Now we do exactly the same for bruises, but we draw the text "BUMPS" on the screen - this is only because the word "BRUISES" would take up too much space and not fit in the HUD design. Again, we pass the variable bruises to the function, and precede it with leading zeroes. Next we do the same for "TIME". But we need to work out how long the player has actually been playing for - luckily this is easy. If we set up another variable, time, and referring back to how I mentioned how the figure could be calculated, you can see how easy it really is in practice. So again, we can draw this. And that's it - all our information is on the screen, so we can end the function (so it jumps back into the DrawScreen function) ready to show the back buffer. I'm sure you can appreciate how useful the DrawText function has been, and it has appeared in nearly everything I have written in one format or another, so I had better explain what it does and how it works. To demonstrate precisely what is going on, I'm going to refer back to the first time we called the function with this:-
DrawText("SCORE",0,0)so we can see precisely what it is doing. Firstly, we are passing variables to the function, and storing them in txt$, tx and ty
FOR loop=0 TO LEN(txt$)-1Now we are going to take each character in turn, from 0 to the length of the string minus 1 (since the first character is 0)
cr=ASC(MID$(txt$,loop,1))-32and using the MID$ command, we will extract that individual character. The ASC command returns the ASCII code, but 32 is space, which is the first printable character, so we subtract that from the result. That may seem confusing, so let me explain. Computers store numbers - if you are writing a letter, it is stored as a series of numbers. If you are drawing a pretty picture, it is also stored as numbers (as we introduced with the Red,Green,Blue format of picking a colour.) Everything a computer does is converted into numbers for it to work with. So what? Well, a long time ago, some clever Americans came up a system that they called the American Standardised Code for Information Interchange (shortened to ASCII - pronounced ask-ee), and this has stuck with us until the present day. The idea was simple - to devise a method that computers would use that means a particular number always corresponds to the same letter, no matter what computer is was running on. So, in ASCII, value 32 is space, 65 is capital "A" - and this will be the same on every computer. What we are doing is turning the character back into it's ASCII code, and this can be used to display the correct character in our animated image. Neat, huh?
DRAWANIM 5,cr,tx,tySo now we will draw the right frame of the animated image on the screen
tx=tx+32and move the x pointer ready to draw the next character in the string (the font is 32 pixels wide.) And now we can move on to the next character. Once all the characters in txt$ have been processed, then the loop will end and we can terminate the function, then return to where we left off. If you managed to work your way through everything so far, then hopefully you'll start to have enough confidence to write something for yourself by now. If you struggled during the description of that last function, don't worry, as it is quite complicated to get to grips with as a complete novice. If you're still totally baffled, though, consider starting the tutorials all over again, as they may well make more sense the second time through.