Snake
By Yaroslav Goncharov, April 01, 2002.
Introduction
Snake is one of the most popular games for cell phones. This sample application is very simple implementation of the game.
Here I describe how it was created.
What You Need
Begin
I have decided to write sample application for Smartphone 2002. Snake is my
favorite cell phone game and it is good application to demonstrate some key points of
Smartphone 2002 development.
Program description
There are 2 objects in this sample game: a snake and a food point. Snake is the line
that consists from the cells. The number of those cells is the length of the snake.
Snake is permanently moving in one of four directions (left, right, up, down). Direction
can be changed using a Smartphone navigation button.
There is a food point on the screen. If snake reach that point it increases in size by one cell.
Game is over if snake eats itself.
Menu of the game contains only 2 buttons: "New Game" and "Quit".
Program development. Step by step
Step 1. Creating the project.
First of all I checked all samples that go with Smartphone 2002 SDK. "Etcha" application
demonstrated several techniques that could be useful for Snake game. I started from "Hello World"
project template. I tried editing resources in the resource editor but got an error connected with
I_IMAGENONE definition. This problem has been already mentioned at our site in
Questions and Answers section . I fixed the problem in the way of Microsoft.
I have added the following lines to resource.h:
// BUGBUG
#define I_IMAGENONE (-2)
Step 2. Editing the resources.
I did not use the resource editor in this application. Resource files were edited in the text
editor. Current resource editor tends to convert menu bar resource to binary format.
First of all I added
common strings to the project.
STRINGTABLE DISCARDABLE
BEGIN
APPNAME "Snake"
TITLE "Snake Sample"
IDS_SNAKE_NEW "New Game"
IDS_SNAKE_QUIT "Quit"
END
On Smartphone 2002, the menu bar is composed of two soft keys. The user can activate a soft
key by pressing the corresponding hardware key located near the display.
I wrote a menu resource using specification from Smartphone 2002 SDK documentation.
IDR_SNAKE_MENUBAR RCDATA
BEGIN
0,
2,
// New Game button
I_IMAGENONE, IDM_SNAKE_CLEAR, TBSTATE_ENABLED,
TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE, IDS_SNAKE_NEW, 0, NOMENU,
// Exit button
I_IMAGENONE, IDM_SNAKE_QUIT, TBSTATE_ENABLED,
TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE, IDS_SNAKE_QUIT, 0, NOMENU,
END
Resource editing was finished with adding definitions for the used symbols to resource.h file.
Step 3. Application initialization and events handling.
I used the WinMain method from "Etcha" sample application as an entry point.
I removed all calls related to the sample logic.
Main window procedure consists from the standard switch for message processing.
switch (message) {
case WM_CREATE :
...
break;
case WM_PAINT :
...
break;
case WM_KEYDOWN:
...
break;
case WM_COMMAND:
switch (LOWORD(uParam)) {
case IDM_SNAKE_QUIT:
...
break;
case IDM_SNAKE_NEW:
...
break;
default:
return DefWindowProc(hWnd, message, uParam, lParam);
}
break;
case WM_TIMER:
...
break;
case WM_CLOSE:
...
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, uParam, lParam);
}
WM_CREATE message is used for menu initialization. WM_PAINT indicates that windows should
be repainted. WM_KEYDOWN events are forwarded to game field to control the snake. In WM_COMMAND
case the commands from the menu are handled. WM_TIMER forces the snake to move. WM_CLOSE ends the
application.
Step 4. Adding STL support
It is know issue that STL library is not included neither to Pocket PC SDK nor to Smartphone 2002
SDK. On Pocket PC I used MFC collections. However, Smartphone 2002 SDK does not support MFC library too.
Fortunately there is a STL library for Windows CE. And it works with Smartphone 2002
SDK without any changes!
I created new folder in my project root and called it "STL". Then I copied all STL headers to this folder and
added an additional include directory "./STL" to the project settings (Project Settings->C/C++ tab->Preprocessor->Additional include directories).
From STL I only needed std::list for this application:
#include
Step 5. Business logic
There are 2 business objects in the program: game field object (CField) and snake object (CSnake).
Field object is declared as global variable. This object is accessed from WinMain and WndProc.
There are several methods called from system methods:
SetRect(const LPRECT pRect) is called from WinMain to initialize the game field with screen rectangle.
NewGame() is called from menu item handler to start new game.
OnKeyDown(long key) is forwarded from keyboard handler to control the snake
Draw(HDC hdc) is called for drawing.
MakeMove(HDC hdc) is called from the timer handler to move the snake.
Snake object is the private member of Field object and cannot be access from system methods.
It uses internal coordinates instead of screen ones.
Discuss
Discuss this sample.
Here you can write your comments and read comments of other developers.
|