'FFFFF RRR OOO M M OOO EEEEE 'F R R O O MM MM O O E 'F R R O O M MM M O O EEE 'FFF RRR O O M M O O E 'F R R O O M M O O E 'F R R OOO M M OOO EEEEE ' 'TTTTT U U TTTTT OOO RRR ' T U U T O O R R ' T U U T O O R R ' T U U T O O RRR ' T U U T O O R R ' T UUU T OOO R R 'Game Programming Series: '---Timing 'Many first time programmers in qbasic want to create games. 'A few number guessing games later, they decide that they want 'to create a real game with awesome graphics, and enemies and 'stuff. They start designing graphics, and learn the keycodes 'for the arrows, and they have a pretty good game started. Un- 'fortunately, the only thing stopping their game from going at 'warp speed is a for loop to pause the game between frames. 'Eventually the game looks pretty good for a newbie, and it 'gets saved to a floppy. A year later, this new programmer gets 'a new computer, and puts the floppy in to try the old game. 'What he doesn't realize, is that when he puts the game in, it 'will once again go at warp speed because in the change of 'computer speeds... 'There is a way to make games that go the same speed on all 'computers, using a basic function called TIMER, and this tutor 'will teach you how... 'The TIMER command 'TIMER is a built in function in qbasic that returns the amount 'of time (in seconds) that has elapsed since midnight. 'Using timer, we can make our program pause until a certain amount 'of time has elapsed with a loop. You can use a do-while, do-until, 'or while-wend loop. This shows how to do it using a do-while loop: 't! = TIMER ' 'DO 'LOOP WHILE TIMER - t! < .1 ' .1 is the length of the delay, ' ' You can easily use a variable, or ' ' different number here 'Many people will put this in a sub program to reduce clutter in 'their programs. 'Anyways, since you know how this works now, here is some sample 'code that shows how to use it in a real program 'If you take out the timer loop, you can see the difference it makes SCREEN 13 DIM face(51) 'array for image DIM x AS INTEGER DIM y AS INTEGER DIM d AS INTEGER ' direction DIM t! FOR y = 1 TO 10 'this loads the sprite FOR x = 1 TO 10 READ d PSET (x, y), d NEXT NEXT GET (1, 1)-(10, 10), face CLS x = 155 'This sets the sprite position y = 95 'to the middle of the screen d = 1 ' DO ' main program loop t! = TIMER DO LOOP UNTIL TIMER - t! >= .01 press$ = INKEY$ 'This changes the direction if an arrow was pressed IF press$ = CHR$(0) + CHR$(72) THEN d = 1 IF press$ = CHR$(0) + CHR$(75) THEN d = 2 IF press$ = CHR$(0) + CHR$(77) THEN d = 3 IF press$ = CHR$(0) + CHR$(80) THEN d = 4 'This draws a black box where face last was LINE (x, y)-STEP(9, 9), 0, BF 'This moves the face IF d = 1 THEN y = y - 5 IF d = 2 THEN x = x - 5 IF d = 3 THEN x = x + 5 IF d = 4 THEN y = y + 5 'This keeps the coordinates of the face in the screen IF x > 309 THEN x = 309 IF x < 0 THEN x = 0 IF y > 189 THEN y = 189 IF y < 0 THEN y = 0 'This is self explanitory PUT (x, y), face, PSET LOOP UNTIL press$ = CHR$(27) DATA 00,00,00,00,00,00,00,00,00,00 DATA 00,00,14,14,14,14,14,14,00,00 DATA 00,14,14,00,14,14,00,14,14,00 DATA 00,14,14,00,14,14,00,14,14,00 DATA 00,14,14,00,14,14,00,14,14,00 DATA 00,14,14,14,14,14,14,14,14,00 DATA 00,14,00,14,14,14,14,00,14,00 DATA 00,14,14,00,00,00,00,14,14,00 DATA 00,00,14,14,14,14,14,14,00,00 DATA 00,00,00,00,00,00,00,00,00,00