A Guide to your Game

by Karthik Karanth

If you've started making a (graphical) game with Just BASIC, read this and then think about what you're going to do.

Alright, first make a decision:

So if you're not including the Main Window, type this:


NOMAINWIN

Next you'll need a splash window. This is important if you are loading huge BMP's or dimming huge arrays. If you're dimming an array, first open the splash window, dim the array small such as "dim game(1)" and then later re-dim by "redim game(500)". Usually your splash window should have a logo, but you can always have whatever you want, like a progress bar.

Then you have to load the pictures for making your game. Suppose we're having our BMP's in the SPRITES\ folder.


LOADBMP "smiley1", "SPRITES\smiley24.bmp"
LOADBMP "smiley2", "SPRITES\smiley4.bmp"
LOADBMP "BackGround", "SPRITES\BG3.bmp"

After you are done doing this, finish creating some variable or loading some high scores. In the program I have made a shots variable.

The next thing you're doing is closing the splash screen with the close command.

If you're making a full screen window which is covering the taskbar, then use this piece of code:


graphicbox #game.g, 26, 16, 809, 437
open "SmileyKick" for dialog_fs_nf as #game
wait

This code opens a full screen dialog window. The graphic box inside can be resized or changed. If you don't like your dialog's color, set the BackgroundColor$ variable to what you want BEFORE opening the dialog.

Now that you have opened the window, start adding the sprites and change the background.


#main "AddSprite sm1 smiley1"
#main "AddSprite sm2 smiley2"
#main "background BackGround"

The addSprite command adds a sprite. In this case, the sprite sm1 has a picture of smiley1 which has loaded with LOADBMP.

The background command sets the background image as the loaded BMP, in our case, BackGround.

If you run the program you may notice that nothing appears. This is because the drawSprites command is not yet issued.

Unless you give drawSprites, the graphics are not shown. Sprite actions are done secretly inside the memory, but are shown only when you give the drawSprites command.

Now let's give it some more shots. Now wouldn't the game be boring if the same positions were for the sprites every time the game starts? Now let's give the sprites "random" positions for the game.


#main "spritexy sm1 "; int(rnd(0)*300); " "; int(rnd(0)*100)
#main "spritexy sm2 "; int(rnd(300)*500); " "; int(rnd(300)*500)

Now the sprites start in different positions.

The objective of the game will be to hit the other sprite 17 times. So to control our sprite, we need to detect mouse input.


#main "when mouseMove [MoveSmiley]"

This line will automatically go to [MoveSmiley] when the mouse is moved, with MouseX and MouseY (two variables) the x y positions of the mouse.


#main "drawSprites"

We're going to have a timer loop so we must include a trapclose command in our program.


#main "trapclose [exit]"

Now we will activate the timer. This timer will fire on 900 milli-seconds.


timer 900, [loop]
scan
wait

The wait command holds the program still. Scan detects if any action is done by the user during the loop.

What's the use of trapclose without a branch? This code unloads the pictures, closes the game, zeros the timer and ends.


[exit]
timer 0
close #main
unloadbmp "smiley1"
unloadbmp "smiley2"
unloadbmp "BackGround"
end

Now comes the part of the code which loops and makes the smiley move.


[loop]
timer 0
#main "spritecollides sm2 col$"
if col$<>"" then [shot]
#main "spritemovexy sm2 "; int(rnd(0)*10); " "; int(rnd(0)*10)
#main "drawSprites"
timer 900, [loop]
wait

First the timer is zeroed and then it checks whether the two sprites have collided. In case this is a yes it goes to the branch [shot]. Then it moves the second sprite and resets the timer.


[MoveSmiley]
#main "spritexy sm1 "; MouseX; " "; MouseY
#main "drawSprites"
wait

The smiley is moved to the mouse cursor's position and is displayed in this part of the routine.


[shot]
shots=shot+1
#main "spritexy sm2 "; int(rnd(300)*500); " "; int(rnd(300)*500)
#main "drawSprites" 'move and shows the smiley's new position
playwave "media\beep.wav", asynch
if shots=17 then [win]
timer 900, [loop]
wait

This part of the code adds a new shot. If the shot value reaches 17 then it goes to the branch label [win].

OK, now I've taught you to create a full game. Now can you use your knowledge to create an attractive "win" animation using sprites?

Well that's your homework!

Here's the complete code with the unfinished homework. If you don't want any "bitmaps not found" message or something, place SmileyKick.bas in the Just BASIC folder.


'SmileyKick

'Original Name: SmileyKick.bas

'Move the smiley with the mouse.
'Try to hit the other smiley at least 17 times.

'Made by Karthik Karanth for JB Newsletter

'Feel free to use, modify and distribute.

NoMainWin 'disable the main window

WindowWidth=250  'splash window width
WindowHeight=250 'splash window height

UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2) 'Window position parameters

Open "Smiley Kick" for graphics_nsb_nf as #main 'splash window

#main "cls ; fill black ; color white ; backcolor black ; place 50 50" 'send commands to #main

'***text***

#main "|Smiley Kick"
#main "|"
#main "|Made using Just BASIC"

'***text***

shots=0

'***load images***

LOADBMP "smiley1", "SPRITES\smiley24.bmp"
LOADBMP "smiley2", "SPRITES\smiley4.bmp"
LOADBMP "BackGround", "SPRITES\BG3.bmp"

'***load images***

'***wait for some time***

    t=time$("milliseconds")
    while time$("milliseconds")"" then [shot] 'gets a shot

#main "spritexy sm2 "; int(rnd(300)*500); " "; int(rnd(300)*500)
#main "drawSprites" 'move and shows the smiley's new position

timer 900, [loop] 'loop again
wait

[MoveSmiley]

#main "spritexy sm1 "; MouseX; " "; MouseY
#main "drawSprites" 'move and shows the smiley's new inputted position

wait

[shot]
shots=shots+1 'increase shots

playwave "media\beep.wav", asynch 'play the beep WAV

#main "spritexy sm2 "; int(rnd(300)*500); " "; int(rnd(300)*500)
#main "drawSprites" 'move and shows the smiley's new position

if shots=17 then [win] 'win the game

timer 900, [loop]
wait

[win]

close #main

'use your knowledge to create an attractive "win" animation
'using sprites