Introduction to Game Development
(skip this section if you don't want to hear the lecture)
Making games is hard: that's the first lesson of game development. If you only ever complete one project in your life then be proud of it, because programming is a skilled craft that takes years to learn and can never truly be mastered. Maybe it is harder for onlookers to appreciate the work that goes into creating a game, or any form of software, if while we work there is no hammering of nails or sawing of wood. All we coders appear to be doing is incessantly tapping at a keyboard for hours; the only time we make any real noise is when we're having a bad day. It doesn't look like we're doing much, and if we do our job right, the finished product will seem so intuitive and simple that it belies the complexity hidden within.
So, now that we know how hard it is to make games what hope is there for a beginner? Well, you're going to have to forget about that epic game idea that more than likely got you interested in programming to begin with; okay, you can still talk about it and think about the game mechanics but don't waste any time trying to make it. It will be years, possibly decades, before you are ready to make the type of game you are used to playing, and you wont be making it in Dark Basic.
You probably feel demotivated now but that's only because you don't know the possibilities that remain. Being able to write programs allows you to experiment with ideas and be creative in unique and powerful ways. Even if you only have a low spec PC, your computer is a powerful machine capable of doing amazing things, all you need is the patience and perseverance to learn how to get the most out of it.
Let's Start Making Games!
Where can we start today? As a beginner it can be difficult to find interesting and fun projects to learn from: pong, tic-tac-toe, hangman, these are all very boring but the step up to something like tetris or space invaders is surprisingly steep. However, I have found a collection of games that I believe would make great projects for beginners:
Simon Tatham's Portable Puzzle Collection, try them out (you can download them or play in the browser). Some of these games are incredibly simple, others are deceptively difficult to recreate, but all of them are fun to play and mentally challenging and there are a tonne of them!
I have decided to recreate some of these games myself — possibly the whole lot if I get around to it! I don't often complete projects so it's nice to have a solid goal that I know I can achieve, and since they are all relatively simple and I have a fair amount of programming experience I thought it would be a nice idea to bring you along with me.
Game 1: Flip
The first game I will tackle is called
Flip. It's probably the simplest of them all, but that's a good place to start. The player is given a grid of tiles that are white on one side and black on the other, each tile can be flipped over but this will also flip the tiles immediately adjacent horizontally and vertically. The objective is to have all white faces up.
I'm going to make this into a proper tutorial but for now I'll just be posting the code I have. The game is finished but I'd like to add some features to it and sort out any bugs that might have crept in.
FULL SOURCE CODE:
rem Flip
rem by Libervurto
rem 27 November 2013
rem -------------------------------------
rem Controls:-
rem flip tile - mouseclick()
rem solve - spacekey()
rem =====================================
set display mode 800,600,32
sync on
sync rate 30
rem grid width in tiles (later we'll let the user define it)
gridWid = 4
rem array to store the state of each tile
dim grid(gridWid,gridWid)
rem scaling factor to keep the grid the same size on screen
gridScale = 500 / (gridWid+1)
rem colours for both sides of the tiles
dim colour(1)
colour(0) = rgb(223,223,223)
colour(1) = rgb(32,32,32)
rem set up a flip list to track which tiles have been flipped and randomize the grid
dim flipList(gridWid,gridWid)
for x = 0 to gridWid
for y = 0 to gridWid
if rnd(1) then flip(x,y,gridWid)
next y
next x
rem == main =============================================================================
while winner=0
solve = silkey(spacekey(), solve)
flip = silkey(mouseclick()>0, flip)
if flip = 1
mx = mousex()
my = mousey()
if between(mx, 50, 550) and between(my, 50, 550)
rem now we know we're in the grid convert pixel position to grid position
mx = (mx - 50) / gridScale
my = (my - 50) / gridScale
rem flip tiles
flip(mx,my,gridWid)
inc moves
rem check for win
for x = 0 to gridWid
for y = 0 to gridWid
if flipList(x,y) = 1 then x = gridWid+2 : y = gridWid+1
next y
next x
if x = gridWid+1 then winner = 1
endif
endif
rem == display ==
rem colour the backdrop
cls rgb(128,128,128)
rem draw grid border (we can use this to check our scaling and positioning is correct)
ink 0,1
box 49, 49, (gridWid+1)*gridScale+49, (gridWid+1)*gridScale+49
rem show player how many moves they have taken
print "Hold SPACEBAR to reveal solution"
print "Moves: "; moves
rem draw grid
for x = 0 to gridWid
for y = 0 to gridWid
rem store coords for ease of use
tileX = 50 + x*gridScale
tileY = 50 + y*gridScale
rem draw tile
ink colour( grid(x,y) ), 1
box tileX, tileY, (tileX + gridScale - 2), (tileY + gridScale - 2)
rem draw guides
centreX = tileX + gridscale/2 -1
centreY = tileY + gridscale/2 -1
tiny = 30/gridWid
ink rgb(128,128,128),0
box centreX-tiny, centreY-tiny, centreX+tiny, centreY+tiny
if x > 0 then wirebox(centreX-3*tiny, centreY-tiny, centreX, centreY+tiny, 1)
if x < gridWid then wirebox(centreX, centreY-tiny, centreX+3*tiny, centreY+tiny, 1)
if y > 0 then wirebox(centreX-tiny, centreY-3*tiny, centreX+tiny, centreY, 1)
if y < gridWid then wirebox(centreX-tiny, centreY, centreX+tiny, centreY+3*tiny, 1)
rem show flipped tiles if solve key held
if solve = 3
if flipList(x,y)=1
ink rgb(223,0,0),0
wirebox(tilex, tiley, tilex+gridscale-2, tiley+gridscale-2, 4)
endif
endif
next y
next x
sync
endwhile
rem wait for player to release mouse button
while mouseclick() : endwhile
rem game over
ink 0,1
print "YOU WIN!"
sync
suspend for mouse
end
rem == functions ========================================================================
function between(x,min,max)
if x >= min
if x <= max then exitfunction 1
endif
endfunction 0
function flip(x,y,gridWid)
flipList(x,y) = 1 - flipList(x,y)
grid(x,y) = 1 - grid(x,y)
if x > 0 then grid(x-1,y) = 1 - grid(x-1,y)
if x < gridWid then grid(x+1,y) = 1 - grid(x+1,y)
if y > 0 then grid(x,y-1) = 1 - grid(x,y-1)
if y < gridWid then grid(x,y+1) = 1 - grid(x,y+1)
endfunction
rem return values: 0 = idle, 1 = pressed, 2 = released, 3 = held
function silkey(trigger,oldstate)
newstate = 3 & (oldstate*2 | trigger)
endfunction newstate
function wirebox(l,t,r,b,lineWidth)
dec lineWidth
box l,t,r,t+lineWidth
box l,t,l+lineWidth,b
box r-lineWidth,t,r,b
box l,b-lineWidth,r,b
endfunction
I am hopeless at this game, I haven't beaten it once.
Formerly OBese87.