源程序:
MyMSnake.mak
# *****************************************
#
# NAME: makefile
# PROJECT: MyMSnake我的移动贪吃蛇
# DATE: 2004-10-09
# BY: Gaogao gowithfox@163.com
#
# 美元右括号
#
# *****************************************
# *****************************************
# FLAGS
# *****************************************
CFLAGS = -O2 -Wall -fvstudio
# *****************************************
# OBJEKTFILES
# *****************************************
OBJS = res.o MyMSnake.o
all: MyMSnake.mpn
# *****************************************
# RESOURCE
# *****************************************
res.o: res.txt
morc -v res.txt
# *****************************************
# COMPILER
# *****************************************
.c.o:
pip-gcc -c $(CFLAGS) -o $@ $<
# *****************************************
# LINKER
# *****************************************
MyMSnake.mpn: $(OBJS)
pip-gcc -o $@ $(OBJS) -mstack=1024 -mdata=10000 -ldatacert -s
??????????????????????????????????????????????????????????????????????????????????????
MyMSnake.h
// *********************************************
// 贪吃蛇的Mophun版本for SE T618
// @author 美元右括号
// @version 0.01
// @connect gowithfox@163.com
// *********************************************
#ifndef MYMSNAKE_H
#define MYMSNAKE_H
#include <vmgp.h>
#include "res.h"
// *********************************************
// GLOBAL variables for MyMSnake GAME
// *********************************************
#define TRUE 1
#define FALSE 0
// 屏幕宽
int32_t screen_width = 0;
#define SCREEN_WIDTH = 128;
// 屏幕高
int32_t screen_height = 0;
#define SCREEN_HEIGHT = 160;
// 字体设置
VMGPFONT FontA;
// 蛇身体元素的大小
#define SNAKE_ELEMENT_SIZE 8
// 方向设置
#define UP_DIRECTION 0
#define DOWN_DIRECTION 1
#define LEFT_DIRECTION 2
#define RIGHT_DIRECTION 3
// 地图的初始状态
#define EMPTY 0
// 地图被Snake1个身体映射的状态
#define SNAKE_IN 1
// 地图被Snake2个身体映射的状态
#define SNAKE_OVERFLOW 2
// 蛇的身体的存储
typedef struct
{
struct SnakeElement * next;
int16_t x;
int16_t y;
int16_t direction;
}SnakeElement;
// Snake的头和尾指针
SnakeElement * snakeHead, * snakeTail;
// 蛇的食物的存储
typedef struct
{
int16_t x;
int16_t y;
int16_t var;
}SnakeFood;
// 食物是否被吃
int16_t isFoodBeEaten = FALSE;
// 可爱的贪吃蛇是不是因为贪吃而死亡?
int16_t isTheSnakeDead = FALSE;
// 地图的宽
#define X_MAX 16
// 地图的高
#define Y_MAX 20
// 地图存储
int16_t backgroundArray[Y_MAX][X_MAX];
//贪吃蛇的初始位置设定
#define X_INIT 8
#define Y_INIT 10
//游戏计数器
#define LEVEL_NUM 10
int16_t level_counter[] = {30, 25, 20, 18, 16, 13, 11, 10, 5, 3};
////////LEVEL_NUM
int16_t currentLevel = 0;
int16_t counter = 0;
//食物的初始位置
SnakeFood snakeFood;
#define FRAME_CONTROLER 30
// 方法
void initGame(void);
// 初始化屏幕
void GetScreenSize(void);
// 初始化字体
void InitFont(void);
// 地图初始化
void initMap(void);
// 贪吃蛇初始化
void initSnake(void);
// 移动贪吃蛇
void moveSnake(void);
// 生成食物
void generateFood(void);
// 绘制游戏场景
void paintObjects(void);
// 销毁一切游戏中的内存碎片
void destroyObjects(void);
#endif
??????????????????????????????????????????????????????????????????????????????????????
// *********************************************
// 贪吃蛇的Mophun版本for SE T618
// @author 美元右括号
// @version 0.01
// @connect gowithfox@163.com
// *********************************************
#include "MyMSnake.h"
void initGame(void)
{
isTheSnakeDead = FALSE;
isFoodBeEaten = FALSE;
currentLevel = 0;
counter = 0;
}
// *******************************************************
// 获得屏幕尺寸
// *******************************************************
void GetScreenSize(void)
{
VIDEOCAPS videocaps;
videocaps.size = sizeof(VIDEOCAPS);
if(vGetCaps(CAPS_VIDEO,&videocaps))
{
screen_width = videocaps.width;
screen_height = videocaps.height;
}
}
// *******************************************************
// 新建字体
// *******************************************************
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);
}
// 地图初始化
void initMap(void)
{
int i = 0, j = 0;
for (i = 0; i < Y_MAX; i++)
{
for (j = 0; j < X_MAX; j++)
{
backgroundArray[i][j] = EMPTY;
}
}
}
// 贪吃蛇初始化
void initSnake(void)
{
SnakeElement *temp;
snakeTail = (SnakeElement *)malloc(sizeof(SnakeElement));
snakeTail->x = X_INIT - 3;
snakeTail->y = Y_INIT;
snakeTail->direction = RIGHT_DIRECTION;
snakeTail->next = (SnakeElement *)malloc(sizeof(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(sizeof(SnakeElement));
temp = temp->next;
temp->x = X_INIT;
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;
}
// 移动贪吃蛇
void moveSnake(void)
{
//清理不backgroundArray;
SnakeElement * temp = snakeTail;
while (temp!=NULL)
{
backgroundArray[temp->y][temp->x] = EMPTY;
temp = temp->next;
}
//移动Snake
SnakeElement * tempTailToo = (SnakeElement *)malloc(sizeof(SnakeElement));
tempTailToo->x = snakeTail->x;
tempTailToo->y = snakeTail->y;
tempTailToo->direction = snakeTail->direction;
SnakeElement * tempNewHead = 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();
}
//移动后
temp = snakeTail;
while (temp!=NULL)
{
backgroundArray[temp->y][temp->x] += SNAKE_IN;
if (backgroundArray[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;
}
}
// 生成食物
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();
}
}
// 绘制游戏场景
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
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);
vFlipScreen(1);
}
// 销毁一切游戏中的内存碎片
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 < FRAME_CONTROLER);
}
return 0;
}
?????????????????????????????????????????????????????????????????????????????????
res.TXT
#include <datacert.h>
INFO METAINFO
{
"Title" : "MyMSnake"
"Vendor" : "gowithfox@163.com"
"Copyright info" : "美元右括号"
"Program version" : "0.01"
"Help" : "Help me!"
}
// ***********************************************************************************************
// CERTIFICATION STUFF
// ***********************************************************************************************
DATACERT_AND_SECURITY_RESOURCE
CREDITS 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"
图片展示: