资源简介

用C语言写的一个贪吃蛇游戏,控制台版,无需图形驱动。在Dev-C++上通过。

资源截图

代码片段和文件信息

#include 
#include “pcc32.h“

#define FALSE 0
#define TURE 1
#define OVER 2

#define WIDTH 22
#define HEIGH 18
#define baseTIME 300

#define TIME(x) baseTIME-23*(x)

enum DIR{
LEFT
RIGHT
UP
DOWN
};
struct COOR{
uint8 x;
uint8 y;
};
struct SNAKE{
uint16 length;
struct COOR body[328];
enum DIR dir;
};

struct SNAKE snake={0{00}0};
struct COOR food;
uint16 score=0;

void Ready();
void Play();
void Over();
void Wall();
void InitSnake();
void InitFood();
uint8 IsOver(uint8 x uint8 y);
uint8 InSnake(uint8 x uint8 y);
uint8 MoveSnake();

void Ready()
{
printf(“说明:\n“);
printf(“开始游戏前请将输入法切换成英文输入法。\n“);
printf(“↑↓←→分别控制蛇上下左右移动。\n“);
printf(“回车键暂停或继续游戏。\n“);
printf(“\n\n请按任意键开始游戏“);
jkGetKey();
}
void Play()
{
uint8 level=1;
system(“cls“);
Wall();
setTextColor(YELLOW);
InitSnake();
InitFood();
do
{
if(level<10)
level=(snake.length+5)/10;
gotoTextPos(0HEIGH+1);
printf(“SCORE:   %5u           LEVEL:   %u“scorelevel);
sleep(TIME(level));
if(jkHasKey())
switch(jkGetKey())
{
case JK_LEFT:
if(snake.dir!=RIGHT)
snake.dir=LEFT;
break;
case JK_RIGHT:
if(snake.dir!=LEFT)
snake.dir=RIGHT;
break;
case JK_UP:
if(snake.dir!=DOWN)
snake.dir=UP;
break;
case JK_DOWN:
if(snake.dir!=UP)
snake.dir=DOWN;
break;
case JK_ENTER:
for(;jkGetKey()!=JK_ENTER;);
break;
default:;
}
}while(MoveSnake());
}
void Over()
{
gotoTextPos(WIDTH-10HEIGH/2-1);
setTextColor(WHITE);
printf(“Your score is:%u“score);
sleep(1500);
jkGetKey();
}
void Wall()
{
uint8 x=0y=0;
setTextColor(RED_ORANGE);
gotoTextPos(00);
while(x++ printf(“■“);
while(++y {
gotoTextPos(0y);
printf(“■“);
gotoTextPos((WIDTH-1)*2y);
printf(“■“);
}
gotoTextPos(0HEIGH-1);
while(--x>0)
printf(“■“);
}
void InitFood()
{
srand((unsigned)time(NULL));
do
{
food.x=rand()%(WIDTH-2)+1;
food.y=rand()%(HEIGH-2)+1;
}while(InSnake(food.x food.y));
gotoTextPos(food.x*2 food.y );
printf(“●“);
}
void InitSnake()
{
uint8 i=0;
snake.length=5;
snake.dir=RIGHT;
setTextColor(YELLOW);
do
{
snake.body[i].x=5-i;
snake.body[i].y=4;
gotoTextPos(snake.body[i].x*2snake.body[i].y);
printf(“■“);
}while(++i}
uint8 IsOver(uint8 xuint8 y)
{
if(InSnake(xy)||0==x||WIDTH-1==x||0==y||HEIGH-1==y)
return TURE;
return FALSE;
}
uint8 InSnake(uint8 x uint8 y)
{
uint8 i= snake.length;
while(i--)
if(snake.body[i].x==x&&snake.body[i].y==y)
return TRUE;
return FALSE;
}
uint8 MoveSnake()
{
uint8 ix=snake.body[0].x y=snake.body[0].y;
switch(snake.dir)
{
case LEFT: --x; break;
case RIGHT: ++x; break;
case UP: --y; break;
default: ++y;
}
if(x==food.x&&y==food.y)
{
++snake.length;
InitFood();
score+=50;
}
else
{
gotoT

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3544  2010-08-22 12:57  main.c

     文件       7622  2010-08-22 12:57  main.o

     文件        767  2010-08-22 12:57  Makefile.win

     文件       9334  2010-08-03 12:27  pcc32.h

     文件        946  2010-08-20 12:55  Snake.dev

     文件      26303  2010-08-22 12:57  Snake.exe

----------- ---------  ---------- -----  ----

                48516                    6


评论

共有 条评论