Hi,
Not sure what section this goes in so i've put it here. Today I was browsing around and looking at TGC products and I got the urge to start game development again. I looked on my Windows 7 machine and I still had DarkGDK installed with VS 2008.
I thought I would try and make something useful to release to the community, so today I have started with a Snake game. After a couple of hours work I had a working game.
The source is around 200 lines, it includes a start screen, simple control system, timer based movement, highscore etc.
Note there are no pieces for the snake to eat, the tail just gets longer as time goes on. You will have to add this yourself along with the suggestions below.
Expansions to this could be save/load the highscore to a file, add music/sound effects, use sprites or base a 3D version of snake on the logic in this version. Also needs expanding into classes for at least the tail data structure and X/Y co-ordinates etc.
Hope this is of some use to someone. Over the next few weeks I will hopefully releasing some other small projects like this, if you have any suggestions for short games let me know. I'm thinking Breakout or Asteroids next.
Finally, here is a quick screenshot:
Source code etc is available in Snake.zip attachment. Project is a bit messy, ignore the Backup.cpp, this was just to take parts from the default 2D project.
The Main.cpp source is:
// DGDKSnake
// Description: A simple snake game using DarkGDK (www.thegamecreators.com)
// Date: 13/10/2013
// License: GPLv2
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
void DarkGDK ( void )
{
// setup
dbSyncOn ( );
dbSyncRate ( 60 );
dbDisableEscapeKey ( );
// load background
dbLoadImage ( "backdrop.bmp", 1 );
dbSprite ( 1, 0, 0, 1 );
// set image color key (for logo)
dbSetImageColorKey ( 0, 0, 0 );
// set initial positions & game info
int x = 320;
int y = 240;
int blockSize = 20;
int roomWidth = 640;
int roomHeight = 480;
int currentTime = dbTimer();
int direction = 2; // Right: 0, Down: 1, Left: 2, Up: 3
char buffer [50];
// snake tail
int lastPositions [1000][2];
int currentPos = 0;
int snakeSize = 10;
// score counter(s)
int score = 0;
int highscore = 0;
int level = 1;
int timing = 0;
// font, size, screenmode
dbSetTextFont("Arial Black");
dbSetTextSize(18);
int screenmode = 0;
// title screen logo
dbLoadImage ( "DGDK_Snake.bmp", 2 );
dbSprite ( 2, 100, 100, 2 );
// main loop
while ( LoopGDK ( ) )
{
// Title screen
if (screenmode == 0){
dbDrawSpritesFirst();
dbInk(dbRGB(255,255,255),dbRGB(255,255,255));
dbText(200,250,"Welcome to DGDKSnake!\n\nPress 'S' to begin a new game");
if (dbScanCode() == 31){
dbHideSprite(2);
for (int i=0; i < snakeSize;i++){
lastPositions[i][0] = NULL;
lastPositions[i][1] = NULL;
}
screenmode = 1;
}
if ( dbEscapeKey ( ) ) // Repeating these :-(
break;
dbSync ( );
continue;
}
// main timer loop (update snake movement, score, positions etc)
if (400-(level*10) > 100)
timing = 400 - (level*10);
if ((currentTime + timing) <= dbTimer()){
switch(direction){
case 0:
x += blockSize;
break;
case 1:
y += blockSize;
break;
case 2:
x -= blockSize;
break;
case 3:
y -= blockSize;
break;
}
// store positions
if (currentPos >= snakeSize-2)
currentPos = 1;
else
currentPos += 1;
// don't store snake head
if (currentPos > 0){
lastPositions[currentPos][0] = x;
lastPositions[currentPos][1] = y;
}
// if score reaches multiple of 400 1-up level
if (score % 400 == 0){
level += 1;
snakeSize += 1;
}
score += 40;
// reset timer
currentTime = dbTimer();
}
// user input (arrow keys)
if (dbRightKey())
direction = 0;
if (dbDownKey())
direction = 1;
if (dbLeftKey())
direction = 2;
if (dbUpKey())
direction = 3;
// draw snake
for (int i=0; i < snakeSize-1; i++)
{
dbInk (dbRGB(255,10,10),dbRGB(255,10,10));
int lastPosX = lastPositions[i][0];
int lastPosY = lastPositions[i][1];
if (lastPositions[i][0] != NULL && lastPositions[i][1] != NULL)
dbBox(lastPosX,lastPosY,lastPosX+blockSize,lastPosY+blockSize);
if (lastPositions[i][0] != NULL && lastPositions[i][1] != NULL && // Corner cases (edges)
lastPositions[i][0] < 0)
dbBox(0,lastPosY,blockSize,lastPosY+blockSize);
if (lastPositions[i][0] != NULL && lastPositions[i][1] != NULL && // Corner cases (edges)
lastPositions[i][0] > roomWidth)
dbBox(0,lastPosY,blockSize,lastPosY+blockSize);
if (lastPositions[i][0] != NULL && lastPositions[i][1] != NULL && // Corner cases (edges)
lastPositions[i][1] > roomHeight)
dbBox(lastPosX,0,lastPosX+blockSize,blockSize);
if (lastPositions[i][0] != NULL && lastPositions[i][1] != NULL && // Corner cases (edges)
lastPositions[i][1] < 0)
dbBox(lastPosX,0,lastPosX+blockSize,blockSize);
dbInk (dbRGB(0,10,255),dbRGB(0,10,255));
dbBox(x,y,x+blockSize,y+blockSize);
}
// Debug text
dbInk (dbRGB(255,255,255),dbRGB(255,255,255));
sprintf (buffer, "Score: %d\nHighscore: %d\nLevel: %d", score, highscore, level);
dbText(10,10,buffer);
// leaving room
if (x < 0 || x > roomWidth-blockSize || y < 0 || y > roomHeight-blockSize)
dbText(roomWidth/2-80,roomHeight/2,"Leaving the room, bye!");
// collision is really simple: check against previous positions & reset if found
for (int j = 0; j < snakeSize-1; j++){
if (j != currentPos){
if (lastPositions[j][0] != NULL && lastPositions[j][1] != NULL){
if (lastPositions[j][0] == x && lastPositions[j][1] == y){
x = roomWidth/2;
y = roomHeight/2;
// reset position counter & clear array
currentPos = 0;
for (int k = 0; k < snakeSize; k++){
lastPositions[k][0] = NULL;
lastPositions[k][1] = NULL;
}
// set highscore
if (score > highscore)
highscore = score;
score = 0;
level = 1;
// (extra: save/load highscore from text file)
break;
}
}
}
}
// check for leaving room
if (x > roomWidth)
x = 0;
if (x < 0)
x = roomWidth;
if (y < 0)
y = roomHeight;
if (y > roomHeight)
y = 0;
// check escape key
if ( dbEscapeKey ( ) )
break;
// update screen contents
dbSync ( );
}
// when escape pressed loop above will break to here
// delete background
dbDeleteImage ( 1 );
// delete logo
dbDeleteImage( 2 );
// return back to Windows
return;
}
Hope someone finds this useful!
And if you find any bugs let me know.
Thanks, Alex.
Learning DarkGDK, then AppGameKit!
AMD X2 7750, 2GB, 250GB HD, Nvidia 9600GT, XP SP3 & Ubuntu