📄 贪吃蛇.c.txt
字号:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <time.h>
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_HOME 0x4700
#define VK_END 0x4f00
#define VK_SPACE 0x3920
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
#define BG_COLOR WHITE
#define PPLONG_COLOR BLUE
#define SIZE 16
#define SCREENWIDTH (640/16) /* 40 X 30 */
#define SCREENHEIGHT (480/16)
typedef enum bool_e
{
true = 1,
false = 0
}BOOL;
typedef enum life_s
{
alive,
rest
}LIFE;
typedef enum direct_s
{
UP,
DOWN,
LEFT,
RIGHT
}DIRECT;
typedef struct point_s
{
char x;
char y;
}POINT;
typedef struct long_s
{
char id;
int length;
LIFE life;
DIRECT direct;
//char stageX;
//char stageY;
POINT body[SCREENWIDTH]; // the position within the game stage
}LONG;
typedef struct plane_s
{
int startX;
int startY;
char width;
char height;
int bgcolor;
char **map;
}PLANE;
typedef enum style_e
{
NOTHING = 0,
WALL = 1,
BOX = 2,
PPLONG = 3
}STYLE;
void init();
void close();
void SetBox();
void UserCmd();
void ppLongGo();
void ShowMsg(char *message);
void timer();
void initGraph();
void closeGraph();
void DrawStage(PLANE *stage);
void DrawMsgboard(PLANE *msgBoard);
void DrawPPLong(LONG *ppLong, int color);
BOOL goAvailable();
void walk();
/* Global arguments list. */
PLANE stage;
PLANE msgBoard;
LONG ppLong;
char message[40];
BOOL gamePlaying = true;
BOOL boxHere;
char speed = 12;
int
main()
{
init();
while(1)
{
timer();
if(boxHere == false)
{
SetBox();
}
UserCmd();
ppLongGo();
if(gamePlaying == false || ppLong.life == rest)
{
sprintf(message, "Game Over");
ShowMsg(message);
getch();
break;
}
}
close();
return 0;
}
void
init()
{
char counter1, counter2;
stage.startX = 5;
stage.startY = 5;
stage.width = 20;
stage.height = 20;
stage.bgcolor = LIGHTGRAY;
/* dynamic array */
stage.map = (char **)calloc(stage.width, sizeof(char *));
for(counter1 = 0; counter1 < stage.width; counter1++)
{
stage.map[counter1] = (char *)calloc(stage.height, sizeof(char));
}
/* set stage wall value */
/*
for(counter1 = 0; counter1 < stage.width; counter1++)
{
for(counter2 = 0; counter2 < stage.height; counter2++)
{
if(counter1 == 0 || counter1 == (stage.width-1)
|| counter2 == 0 || counter2 == (stage.height-1))
stage.map[counter1][counter2] = WALL;
}
}
*/
msgBoard.startX = stage.startX + stage.width + 2;
msgBoard.startY = stage.startY;
msgBoard.width = 10;
msgBoard.height = 2;
msgBoard.bgcolor = LIGHTGRAY;
ppLong.length = 2;
ppLong.life = alive;
ppLong.direct = RIGHT;
ppLong.body[0].x = 1;
ppLong.body[0].y = stage.height/2;
ppLong.body[1].x = 0;
ppLong.body[1].y = stage.height/2;
stage.map[0][stage.height/2] = PPLONG;
stage.map[1][stage.height/2] = PPLONG;
initGraph();
DrawStage(&stage);
DrawMsgboard(&msgBoard);
DrawPPLong(&ppLong, PPLONG_COLOR);
/* init the keyboard input. */
if(bioskey(1))
bioskey(0);
}
void
UserCmd()
{
int key = 0;
if(bioskey(1))
{
key = bioskey(0);
switch(key)
{
case VK_UP:
ppLong.direct = UP;
break;
case VK_DOWN:
ppLong.direct = DOWN;
break;
case VK_LEFT:
ppLong.direct = LEFT;
break;
case VK_RIGHT:
ppLong.direct = RIGHT;
break;
case VK_SPACE:
sprintf(message, "User pause the game...");
ShowMsg(message);
getch();
sprintf(message, "User resume the game!");
ShowMsg(message);
break;
case VK_ESC:
ppLong.life = rest;
gamePlaying = false;
break;
default:
break;
}
if(ppLong.life == rest || gamePlaying == false)
{
return;
}
}
/* resume the user keyboard input */
while(bioskey(1)) bioskey(0);
}
void
SetBox()
{
int x, y;
if(boxHere == true)
return;
randomize();
x = random(stage.width) ;
y = random(stage.height);
stage.map[x][y] = BOX;
setfillstyle(SOLID_FILL, RED);
bar((stage.startX + x - 1) * SIZE + 1, (stage.startY + y - 1) * SIZE + 1,\
(stage.startX + x) * SIZE - 1, (stage.startY + y) * SIZE - 1);
boxHere = true;
}
void
ppLongGo()
{
BOOL goAble;
if(gamePlaying == false || ppLong.life == rest)
return;
goAble = goAvailable();
if(goAble == true)
{
walk();
}
else
{
ppLong.life = rest;
sprintf(message, "hehe, be careful the next time!");
ShowMsg(message);
gamePlaying = false;
}
}
void
ShowMsg(char * message)
{
printf("%s\n", message);
}
void
timer()
{
static clock_t timer_prev = 0;
clock_t timer_now;
timer_now = clock();
while((timer_now - timer_prev) < (18 - speed)) /* 18 is the CLOKS_PER_SEC value */
{
timer_now = clock();
}
timer_prev = clock();
return;
}
void
close()
{
if(stage.map)
{
free(stage.map);
}
if(msgBoard.map)
{
free(msgBoard.map);
}
closeGraph();
}
void
initGraph()
{
int driver, mode, errorcode;
driver = DETECT;
initgraph(&driver, &mode, "");
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics ERROR: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:\n");
getch();
exit(1);
}
}
void
closeGraph()
{
closegraph();
}
void
DrawStage(PLANE *stage)
{
int i, j;
setfillstyle(SOLID_FILL, BG_COLOR);
bar((stage->startX - 1) * SIZE - 1, (stage->startY - 1) * SIZE - 1, \
(stage->startX + stage->width - 1) * SIZE + 1, (stage->startY + stage->height - 1) * SIZE + 1);
setcolor(YELLOW);
for(i = 0; i < stage->width; i++)
{
for(j = 0; j < stage->height; j++)
{
rectangle((stage->startX - 1 + i) * SIZE, (stage->startY - 1 + j) * SIZE, \
(stage->startX + i) * SIZE, (stage->startY + j) *SIZE);
}
}
}
void
DrawMsgboard(PLANE *msgBoard)
{
;
}
void
DrawPPLong(LONG *ppLong, int color)
{
char counter;
int x1, y1, x2, y2;
setfillstyle(SOLID_FILL, color);
for(counter = 0; counter < ppLong->length; counter++)
{
x1 = (stage.startX + ppLong->body[counter].x - 1) * SIZE + 1;
y1 = (stage.startY + ppLong->body[counter].y - 1) * SIZE + 1;
x2 = (stage.startX + ppLong->body[counter].x) * SIZE - 1;
y2 = (stage.startY + ppLong->body[counter].y ) * SIZE - 1;
bar(x1, y1, x2, y2);
}
}
BOOL
goAvailable()
{
char x ,y;
STYLE flag;
x = ppLong.body[0].x;
y = ppLong.body[0].y;
switch(ppLong.direct)
{
case UP:
y -= 1;
break;
case DOWN:
y += 1;
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
default:
break;
}
if(x < 0 || x >= stage.width || y < 0 || y >= stage.height)
return false;
flag = stage.map[x][y];
if(flag == NOTHING || flag == BOX)
{
return true;
}
else
{
return false;
}
}
void
walk()
{
char x, y;
char counter, length;
STYLE flag;
x = ppLong.body[0].x;
y = ppLong.body[0].y;
switch(ppLong.direct)
{
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
default:
break;
}
flag = stage.map[x][y];
if(flag != BOX && flag != NOTHING)
{
ppLong.life = rest;
gamePlaying = false;
return;
}
DrawPPLong(&ppLong, BG_COLOR);
/* set stage.map value */
for(counter = 0; counter < ppLong.length; counter++)
{
stage.map[ppLong.body[counter].x][ppLong.body[counter].y] = NOTHING;
}
if(flag == BOX)
{
boxHere = false; /* 吃掉盒子 */
ppLong.length += 1; /* 身体长1 */
}
for(counter = 0; counter < ppLong.length - 1; counter++)
{
ppLong.body[ppLong.length - 1 - counter].x = ppLong.body[ppLong.length - 2 - counter].x;
ppLong.body[ppLong.length - 1 - counter].y = ppLong.body[ppLong.length - 2 - counter].y;
}
ppLong.body[0].x = x;
ppLong.body[0].y = y;
/* set stage.map value */
for(counter = 0; counter < ppLong.length; counter++)
{
stage.map[ppLong.body[counter].x][ppLong.body[counter].y] = PPLONG;
}
DrawPPLong(&ppLong, PPLONG_COLOR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -