Chapter II: Base engine
We're gonna make a capture the flag game. There won't be any external graphics. Just circles and boxes.
We start by making the main loop(don't bother running it yet, just type it)
A do loop will go down to loop, then skip back to do, until we end the program.
We also make the declarations section line
set display mode 1024,768,32 `if this isn't your maximum resolution, set it to your max.
sync on
sync rate 0 `i always use a custom timer, but thats just me
`^^^^^^^^^^^^^^^^^^^Declarations^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
do
sync
loop
Add the custom timer since i'm using
sync rate 0
The timer makes it wait x milliseconds before refreshing again.
set display mode 1024,768,32 `if this isn't your maximum resolution, set it to your max.
sync on `let us refresh at our own, custom rate
sync rate 0 `i always use a custom timer, but thats just me
`^^^^^^^^^^^^^^^^^^^Declarations^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
time = timer() `this timer allowes us to choose the minimum of milliseconds between each sync
do
if timer() - time > 10 `timer() increases 1 every millisecond, our "time" variable doesn't
`so when timer() is 10 bigger that "time" timer() - time = 10
sync
time = timer() `reset the "time" variable
endif
loop
Now we should add the player.
We'll use a UDT(user-defined type) array to hold all the stats for each player. This includes x position, y position, angle("dire"),team number,time 'till another shot may be fired("lsc"), and health
Add this into the declarations section:
type Entity
x as float `x and y are floats because i use a float for the speed also.
y as float
dire as float
team as integer
lsc as integer `last shot countdown
health as integer
endtype
dim prs(0) as Entity `currently only one player(slot 0) (prs = players)
We should make the player visible now
We will draw a circle for the player and a line from the center of the player, pointing the direction that the player is.
For the line, we must make use of
sine and
cosine
The following function loops through all Players and draws them.
Add the drawplayers function.
function drawplayers()
lock pixels `lock the pixels for faster drawing
for i = 0 to array count(prs()) `loop throught all the players
if prs(i).team = 1 `team 1 is the user's team
ink RGB(0,0,255),0 `blue is the user's team color
else
ink RGB(255,0,0),0 `red is the enemy's color
endif
if i = 0 then ink RGB(0,255,0),0 `slot 0 is the user, so if its the user, ink it green
circle prs(i).x,prs(i).y, 5 `draw a circle at the players position
line prs(i).x,prs(i).y,prs(i).x+(sin(prs(i).dire))*12,prs(i).y+(-cos(prs(i).dire))*12 `make a line to show which direction the player is pointing(use -cos because it returns 1 for up and -1 for down)
next
unlock pixels `you have to unlock pixels when we're done drawing
endfunction
Call
drawplayers() in the main loop.
Now add this right below the "dim prs" to position the player at 20,20
prs(0).x = 20
prs(0).y = 20
Now that we're drawing the player, we should clear the screen every loop. Otherwise Nothing will be cleared and the player will leave a trail if moved.
add
cls 0
in the main loop.(before you call anything that draws)
This is what you should have now.
set display mode 1024,768,32 `if this isn't your maximum resolution, set it to your max.
sync on `let us refresh at our own, custom rate
sync rate 0 `i always use a custom timer, but thats just me
type Entity
x as float `x and y are floats because i use a float for the speed also.
y as float
dire as float
team as integer
lsc as integer `last shot countdown time left till player can shoot another bullet
health as integer
endtype
dim prs(0) as Entity `currently only one player(slot 0) (prs = players)
prs(0).x = 20
prs(0).y = 20
`^^^^^^^^^^^^^^^^^^^Declarations^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
time = timer() `this timer allowes us to choose the minimum of milliseconds between each sync
do
if timer() - time > 10
cls 0
drawplayers()
sync `draw to the screen
time = timer()
endif
loop
function drawplayers()
lock pixels `lock the pixels for faster drawing
for i = 0 to array count(prs()) `loop throught all the players
if prs(i).team = 1 `team 1 is the user's team
ink RGB(0,0,255),0 `blue is the user's team color
else
ink RGB(255,0,0),0 `red is the enemy's color
endif
if i = 0 then ink RGB(0,255,0),0 `slot 0 is the user, so if its the user, ink it green
circle prs(i).x,prs(i).y, 5 `draw a circle
line prs(i).x,prs(i).y,prs(i).x+(sin(prs(i).dire))*12,prs(i).y+(-cos(prs(i).dire))*12
next
unlock pixels `you have to unlock pixels when we're done drawing
endfunction
Now run it.
Thats all for now. I'll continue it asap.
Next chapter : User Control