Case study: mymsnakes3d: My mobile phone greedy snake game class 3D version trick: draw 3D effect by 2D to 3D, draw 3D effect ./res.txt#include
Info metainfo {"Title": "Mymsnakes3D" "Vendor": "Gowithfox@163.com" "Copyright Info": "USD" "Program Version": "0.02" "Help": "Help ME!"}
// ******************************************************** ******************************************************* / / CERTIFICATION STUFF // ********************************************************* ******************************************************** DATACT_AND_SECURITY_RESOURCREDITS DATA {Fill 32,0}
Section data // ************************************************ // THE FONT / / *************************************************** FontA Data "GFX / SystemFont4x6.fnt "
TILES TILESET 8 8 FORMAT RGB332 "GFX / TILES.BMP" MYSPRITE SPRITE FORMAT RGB332 "GFX / SPRITE.BMP" My_M_SNAKE_3D_TITLE SPRITE FORMAT RGB332 "GFX / Title.BMP"
/Mymsnake.mak# *************************************** ## name: Makefile # Project: mymsnakes3d My Mobile Greed Snake 3D # Date: 2004-10-09 # by: gaogao goithfox@163.com# version: 0.02 # USD Right Brand # Join class 3D Image Mode # *** ***********************************************
# ************************************************** # Flags # ***** ********************************** CFLAGS = -O2 -wall -fvstudio
# **************************************** # Objektfiles # ***** ********************************** Objs = res.o mymsnake.o
All: mymsnake.mpn
# **************************************** # resource # ***** ******************************************* Res.: res.txt morc -v res.txt
# *************************************** # compiler # ***** *******************************************. CO: PIP-GCC -C $ (cflags) -o $ @ <
# ***************************************** # linker # ***** ***************************************************** MYMSNAKE.MPN: $ (OBJS) PIP-GCC -O $ @ $ (Objs) -mstack = 1024 -mdata = 10000-LDATACERT -S
/MYMSNAKE.H / / ************************************************************* * // Eat Snake Mophun version for SE T618 @Author US dollar Right Brand // @version 0.02 @163.com// Add to class 3D image mode // ****** ***********************************************
#ifndef mymsnake_h # define mymsnake_h
#include
Snake body size #define SNAKE_ELEMENT_SIZE // // direction element 8 #define UP_DIRECTION 0 # define DOWN_DIRECTION 1 # define LEFT_DIRECTION 2 # define RIGHT_DIRECTION 3 // map in the initial state #define EMPTY 0 // Snake1 map is a map of the body State #define snake_in 1 // Map of Snake2 Body Mapping State #define snake_overflow 2 // Snake's body storage typedf struct {struct snakelement * next; int16_t x; int16_t y; int16_t direction;} snakelement;
// snake's head and tail pointer SnakeElement * snakehead, * snaketail;
// Snake's food storage typef struct {INT16_T X; INT16_T Y; INT16_T VAR;} Snakefood;
// Whether the food is eating int16_t isfoodbeeaten = false;
// Cute greedy snake died because of greed? INT16_T ISTHESNAKEDEAD = FALSE;
// Map of wide #define x_max 16 // map high #define y_max 20 // Map storage INT16_T BackgroundArray [y_max] [x_max];
// Eat the initial position of the snake setting #define x_init 8 # Define y_init 10
// Game counter #define level_num 10INT16_T level_counter [] = {30, 25, 20, 18, 16, 13, 11, 10, 5, 3}; level_numint16_t currentlevel = 0; int16_t counter = 0; // Initial location of food Snakefood Snakefood; #define frame_controler 30
// Method Void InIntgame (Void); // Initialize the screen void getScreensize (void); // Initialize the font void initfont (void); // Map Initiation Void Initmap (Void); // Greedy Snake InitSnake (Void) ; // Move the greedy snake void movesnake (void); // Generate food void generatefood (void); // Draw a game scene void PaintObjects (void); // Destroy the memory fragment void destroyObjects (Void) in all games;
// S3D effect // S3D coordinates get int16_t gets3dx (int16_t x, int16_t y); int16_t gets3dy (int16_t y);
#ENDIF
/Mymsnake.c// *************************************************** * // Eat Snake Mophun version for SE T618 @author US dollar Right Brand // @version 0.02 @163.com// Add to class 3D image mode // ******* ************************************** # include "mymsnake.h"
Void inIntgame (void) {isthesnakedead = false; isfoodbeeaten = false; currentlevel = 0; counter = 0;}
// ******************************************************** ******* // Get screen size // ******************************************** ******************* void getScreenSize (void) {VIDEOCAPS videocaps; videocaps.size = sizeof (VIDEOCAPS); if (vGetCaps (CAPS_VIDEO, & videocaps)) {screen_width = videocaps = VideoCaps.Height;}} // ************************************************ ***************** // New font // ********************************** ************************************** VOID INITFONT (VOID) {fonta.width = 4; fonta.height = 6; fonta.bpp = 1; fonta.palindex = 0; fonta.chartbl = fonta 16; fonta.fontdata = fonta 16 256; vsetactivefont (& fonta);} // map initialization void initmap (void) {Int i = 0, j = 0; for (i = 0; i SnakeElement * temp; snakeTail = (SnakeElement *) malloc (sizeof (SnakeElement)); snakeTail-> x = X_INIT - 3; snakeTail-> y = Y_INIT; snakeTail-> direction = RIGHT_DIRECTION; snakeTail-> next = (SnakeElement *) Malloc (SNAKEELEMENT)); Temp = SnakeTail-> Next; Temp-> x = x_init - 2; Temp-> Y = Y_INIT; TEMP-> Direction = Right_Direction; Temp-> Next = (SnakeElement *) Malloc (Sizeof (SnakeElement); temp = temp-> next; temp-> x = x_init - 1; temp-> y = y_init; temp-> Direction = Right_Direction; Temp-> Next = (SnakeElement *) Malloc (Strib (SnakeElement) ); Temp = temp-> next; temp-> y = y_init; temp-> Direction = Right_Direction; temp-> next = null; snakehead = Temp; BackgroundArray [y_init] [x_init - 3] = Snake_in; backgroundArray [y_init] [x_init - 2] = snake_in; backgroundArray [y_init] [x_init - 1] = snake_in; backgroundArray [y_init] [x_init - 0] = snake_in;} // Mobile Greed Snake Voidsnake (void ) {// Clean up not BackgroundArray; snakelement * temp = snaketail; while (temp! = Null) {BackgroundArray [TEMP-> Y] [T emp-> x] = EMPTY; temp = temp-> next;} // move Snake SnakeElement * tempTailToo = (SnakeElement *) malloc (sizeof (SnakeElement)); tempTailToo-> x = snakeTail-> x; tempTailToo-> y = SnakeTail-> Y; TemptailToo-> Direction = snaketail-> Direction; snakeElement * TempNew Head = snaketail; snaketail = snaketail-> next; tempnewhead-> next = NULL; switch (snakeHead-> direction) {case UP_DIRECTION: tempNewHead-> y = snakeHead-> y - 1; tempNewHead-> x = snakeHead-> x; tempNewHead-> direction = snakeHead-> direction; break; case DOWN_DIRECTION: tempNewHead- > y = snakeHead-> y 1; tempNewHead-> x = snakeHead-> x; tempNewHead-> direction = snakeHead-> direction; break; case LEFT_DIRECTION: tempNewHead-> x = snakeHead-> x - 1; tempNewHead-> y = snakeHead-> y; tempNewHead-> direction = snakeHead-> direction; break; case RIGHT_DIRECTION: tempNewHead-> x = snakeHead-> x 1; tempNewHead-> y = snakeHead-> y; tempNewHead-> direction = snakeHead -> direction; break;} snakeHead-> next = tempNewHead; snakeHead = snakeHead-> next; if (snakeHead-> x == snakeFood.x && snakeHead-> y == snakeFood.y) {isFoodBeEaten = TRUE;} if (isFoodBeEaten == TRUE) {// isFoodBeEaten = FALSE; tempTailToo-> next = snakeTail; snakeTail = tempTailToo; generateFood ();} // after moving temp = snakeTail; while (temp = NULL!) {backgroundArray [temp-> Y] [TEMP-> x] = snake_in; if (Ba CkgroundArray [Temp-> Y] [Temp-> X]> Snake_in) {iSthesnakeDead = true;} Temp = Temp-> Next;} if (snakehead-> x <0 || snakehead-> y <0 || snakehead- > x> = x_max || snakehead-> y> = y_max) {isthesnakedead = true;}} // Generate food void generatefood (void) {if (isfoodbeeaten == false) {Return;} else {INT X; int y ; vsetrandom (Vgettime ()); x = ABS (VgetrandOM ()% x_max); vsetrandom (Vgettime ()); y = ABS (vgetrandom ()% y_max); if (BackgroundArray [Y] [x] == EMPTY) { Snakefood.x = x; snakefood.y = y; snakefood.var = 1; isfoodbeeaten = false;} generatefood ();}} // Draw game scene void PaintObjects (void) {SnakeElement * Temp = snaketail; // - - INIT Graphics ------------------------------- vsetclipwindow (0, 0, screen_width, screen_height); // set Maximum Clipwindow vSetForeColor (vRGB (255,255,255)); // White forecolor vSetBackColor (vRGB (0,0,0)); // Black background vClearScreen (vRGB (0,0,0)); // Fill Screen with black vSetTransferMode ( Mode_Trans); // set transfer mode to transparent // The following is blocked in the new version // while // {// vDrawobject (Temp-> x * snake_element_size, temp-> y * snake_element_size, & mysprite); // Temp = TEMP-> Next ; //} // vdrawobject (snakefood.x * snake_element_size, snakefood.y * snake_element_size, & mysprite); // is blocked in the new version // While (Temp! = Null) // {// vDrawobject (Temp-> x * snake_element_size, temp-> y * snake_element_size, & mysprite); // Temp = Temp-> Next; //} // vDrawobject (snakefood .x * SNAKE_ELEMENT_SIZE, snakeFood.y * SNAKE_ELEMENT_SIZE, & mYSPRITE); // S3D vSetForeColor (vRGB (0,255,0)); vDrawLine (X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2, 0, Y_MAX * SNAKE_ELEMENT_SIZE); vDrawLine ( X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2, X_MAX * SNAKE_ELEMENT_SIZE / 4, 0); vDrawLine (3 * X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2, X_MAX * SNAKE_ELEMENT_SIZE, Y_MAX * SNAKE_ELEMENT_SIZE); vDrawLine (3 * X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2, 3 * X_MAX * SNAKE_ELEMENT_SIZE / 4, 0); vDrawLine (1 * X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2, 3 * X_MAX * SNAKE_ELEMENT_SIZE / 4, Y_MAX * SNAKE_ELEMENT_SIZE / 2); // s3dnd vsetforecolor (VRGB (255, 255, 255)); while (Temp! = Null) {INT16_T X_0 = TEMP-> X; in T16_T X_1 = TEMP-> x 1; INT16_T Y_0 = Temp-> Y; INT16_T Y_1 = TEMP-> Y 1; // Firstly, divide the coordinate into standard coordinate // 2D test // x_0 = (x_max / 2) * SNAKE_ELEMENT_SIZE x_0 * SNAKE_ELEMENT_SIZE; // x_1 = (X_MAX / 2) * SNAKE_ELEMENT_SIZE x_1 * SNAKE_ELEMENT_SIZE; // vDrawLine (x_0, y_0, x_1, y_0); // vDrawLine (x_0, y_0, x_0, y_1); // vDrawline (x_0, y_1, x_1, y_1); // vDrawline (x_1, y_0, x_1, y_1); // 2D TESTEND / / S3D INT16_T X_LEFT_TOP = GETS3DX (x_0, y_0); int16_t x_right_top = Gets3DX (x_1, y_0) INT16_T X_LEFT_BOTTOM = GETS3DX (x_0, y_1); INT16_T X_Right_BOTTOM = GETS3DX (X_1, Y_1); INT16_T Y_3D_0 = Gets3DY (Y_0); INT16_T Y_3D_1 = Gets3dy (y_1); vDrawLine (x_left_top, y_3D_0, x_right_top, y_3D_0); vDrawLine (x_left_top, y_3D_0, x_left_bottom, y_3D_1); vDrawLine (x_left_bottom, y_3D_1, x_right_bottom, y_3D_1); vDrawLine (x_right_top, y_3D_0, x_right_bottom, y_3D_1); // S3D END // Coordinate transformation Temp = Temp-> Next;} // S3D Food INT16_T X_0 = snakefood.x; int16_t x_1 = snakefood.x 1; INT16_T Y_0 = snakefood.y; int16_t y_1 = snakefood.y 1; int16_t x_left_top = getS3DX (x_0, y_0); int16_t x_right_top = getS3DX (x_1, y_0); int16_t x_left_bottom = getS3DX (x_0, y_1); int16_t x_right_bottom = getS3DX (x_1, y_1); int16_t y_3D_0 = getS3DY (y_0); int16_t y_3D_1 = getS3DY (y_1); vSetForeColor (vRGB (255,255,0)); vDrawLine (x_left_top, y_3D_0, x_right_top, y_3D_0); vDrawLine (x_left_top, y_3D_0, x_left_bottom, y_3D_1); vDrawLine (x_left_bottom, y_3D_1, x_right_bottom, y_3D_1); vDrawLine (X_Right_Top, y_3d_0, x_right_bottom, y_3d_1); // S3D FOOD END // 3D Backgound vDrawObject (1 * X_MAX * SNAKE_ELEMENT_SIZE / 4, 0, & MY_M_SNAKE_3D_TITLE); // 3D Backgound END vFlipScreen (1);} // destroy all game memory fragmentation void destroyObjects (void) { ///} int main (void) { initGame (); InitFont (); GetScreenSize (); initMap (); initSnake (); uint32_t frame; int16_t currentDirection = snakeHead-> direction; while (1) {switch (vGetButtonData ()) {case KEY_UP: if (currentDirection! = DOWN_DIRECTION) {snakeHead-> direction = UP_DIRECTION;} break; case KEY_DOWN:! if (currentDirection = UP_DIRECTION) {snakeHead-> direction = DOWN_DIRECTION;} break; case KEY_LEFT:! if (currentDirection = RIGHT_DIRECTION) {snakeHead-> direction = LEFT_DIRECTION;} break; case KEY_RIGHT: if (! currentDirection = LEFT_DIRECTION) {snakeHead-> direction = RIGHT_DIRECTION;} break;} frame = vGetTickCount (); if (counter > level_counter [currentLevel]) {counter = 0; if ( ! isTheSnakeDead = TRUE) {moveSnake (); currentDirection = snakeHead-> direction;}} paintObjects (); while (vGetTickCount () - frame