一个简易的贪吃蛇游戏C语言代码:
语言:C
代码量:516行
主要算法:循环链表
run:
代码如下:
#include
#include
#include
#include
#include
#define SNAKE_SIZE 10
#define MOVABLE 1
#define UNMOVABLE 0
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
static struct tagRECT* sr=(struct tagRECT*)malloc(sizeof(struct tagRECT));
HDC hdc;
PAINTSTRUCT ps;
PRECT foodpr;
int direction=2;
int start=0;
int score=0;
int food=0;
int speed=150;
int dscore=7;
int foodsum=1;
int foodtype=1;
int score_t=0;
int score_f1=5;
int score_f2=10;
char* score_c;
char* best=(char*)"0";
PRECT foodpos=NULL;
typedef struct Link
{
int posx;
int posy;
struct Link* next;
}*link;
typedef struct LoopLink
{
int size;
struct Link* head;
struct Link* tail;
}*p_link;
typedef struct InRect
{
int x;
int y;
}*INRECT,IRECT;
void Init(struct LoopLink* plink) //init the snake's body
{
plink->size=4;
plink->tail=(link)malloc(sizeof(struct Link));
plink->head=(link)malloc(sizeof(struct Link));
plink->tail->next=(link)malloc(sizeof(struct Link));
plink->tail->next->next=(link)malloc(sizeof(struct Link));
plink->tail->next->next->next=plink->head;
plink->head->next=plink->tail;
plink->head->posx=30;
plink->head->posy=200;
plink->head->next->posx=0;
plink->head->next->posy=200;
plink->head->next->next->posx=10;
plink->head->next->next->posy=200;
plink->head->next->next->next->posx=20;
plink->head->next->next->next->posy=200;
}
int ClearLink(struct LoopLink* plink)
{
if(plink==NULL) return 0;
int i=1;
struct Link* p=plink->head;
struct Link* q=p;
while(i<=plink->size)
{
q=p;
p=p->next;
free(q);
i++;
}
}
static struct LoopLink* plink=(struct LoopLink*)malloc(sizeof(struct LoopLink));
int AddElement(struct LoopLink* plink) /*increase the length of the body*/
{
link p=(link)malloc(sizeof(struct Link));
plink->head->next=p;
p->next=plink->tail;
plink->tail=p;
plink->size++;
return 1;
}
PRECT Move_Right(struct LoopLink* plink) //move toward the right side
{
PRECT inrect=(PRECT)malloc(sizeof(RECT));
inrect->left=plink->tail->posx;
inrect->top=plink->tail->posy;
inrect->right=inrect->left+10;
inrect->bottom=inrect->top+10;
if(plink->head->posx+10<=270)
plink->tail->posx=plink->head->posx+10;
else plink->tail->posx=0;
plink->tail->posy=plink->head->posy;
plink->head=plink->tail;
plink->tail=plink->head->next;
return inrect;
}
PRECT Move_Left(struct LoopLink* plink) /*move toward the left side*/
{
PRECT inrect=(PRECT)malloc(sizeof(RECT));
inrect->left=plink->tail->posx;
inrect->top=plink->tail->posy;
inrect->right=inrect->left+10;
inrect->bottom=inrect->top+10;
if(plink->head->posx-10>=0)
plink->tail->posx=plink->head->posx-10;
else plink->tail->posx=270;
plink->tail->posy=plink->head->posy;
plink->head=plink->tail;
plink->tail=plink->head->next;
return inrect;
}
PRECT Move_Up(struct LoopLink* plink) /*move toward the up side*/
{
PRECT inrect=(PRECT)malloc(sizeof(RECT));
inrect->left=plink->tail->posx;
inrect->top=plink->tail->posy;
inrect->right=inrect->left+10;
inrect->bottom=inrect->top+10;
plink->tail->posx=plink->head->posx;
if(plink->head->posy-10>=30)
plink->tail->posy=plink->head->posy-10;
else plink->tail->posy=340;
plink->head=plink->tail;
plink->tail=plink->head->next;
return inrect;
}
PRECT Move_Down(struct LoopLink* plink) /*move down*/
{
PRECT inrect=(PRECT)malloc(sizeof(RECT));
inrect->left=plink->tail->posx;
inrect->top=plink->tail->posy;
inrect->right=inrect->left+10;
inrect->bottom=inrect->top+10;
plink->tail->posx=plink->head->posx;
if(plink->head->posy+10<=340)
plink->tail->posy=plink->head->posy+10;
else plink->tail->posy=30;
plink->head=plink->tail;
plink->tail=plink->head->next;
return inrect;
}
int Check_Crash(struct LoopLink* plink) /*to check if the head crash it's body*/
{
int index=1;
struct Link* p=plink->head;
struct Link* q=p->next;
while(q!=p)
{
if((q->posx==p->posx)&&(p->posy==q->posy)&&(index<=plink->size))
{
return 1;
}
q=q->next;
index++;
}
return 0;
}
typedef struct Snake_Link
{
struct Snake_Pos* p[80];
}*Snake_L;
void InitRect(struct tagRECT* rect)
{
rect->left=0;
rect->top=0;
rect->right=10;
rect->bottom=10;
}
void ShowSnake(HDC hdc)
{
struct Link* p=plink->head;
int i=1;
while(i<=plink->size)
{
PRECT rect=(PRECT)malloc(sizeof(RECT));
rect->left=p->posx;
rect->top=p->posy;
rect->right=p->posx+SNAKE_SIZE;
rect->bottom=p->posy+SNAKE_SIZE;
FillRect(hdc,rect,(HBRUSH)GetStockObject(GRAY_BRUSH));
p=p->next;
++i;
}
}
PRECT ProduceFood()
{
int x;
int y;
if(food!=0) return NULL;
if(food==0)
{
srand((unsigned)time(NULL));
int t=1,i=1,j=0;
for(j=0;j<300;j++)
{
x=rand()%27;
y=rand()%31+3;
struct Link* p=plink->head;
for(i=1;i<=plink->size;i++)
{
if(p->posx==x*10&&p->posy==y*10) t=0;
p=p->next;
}
if(t==1) break;
}
PRECT r=(PRECT)malloc(sizeof(RECT));
r->left=x*10;
r->top=y*10;
r->right=x*10+10;
r->bottom=y*10+10;
food=1;
if((x+y)%5==0) foodtype=2;
return r;
}
}
int ShowFood(HDC hdc)
{
if((foodpr=ProduceFood())!=NULL)
{
if(foodtype==1)
{
FillRect(hdc,foodpr,(HBRUSH)GetStockObject(GRAY_BRUSH));
}
if(foodtype==2)
{
FillRect(hdc,foodpr,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
foodpos=foodpr;
}
return 1;
}
int EatFood(PRECT rect)
{
if(rect==NULL) return 0;
int i=1;
struct Link* p=plink->head;
if((p->posx==rect->left)&&(p->posy==rect->top))
{
food=0;
AddElement(plink);
if(foodtype==1)
score+=(score_f1+dscore*5);
if(foodtype==2)
{
score+=(score_f2+dscore*7);
foodtype=1;
}
dscore=7;
return 1;
}
return 0;
}
int SaveBest(char* best)
{
FILE *f;
if((f=fopen("c://tcs_best.txt","w"))==NULL)
{
if((f=fopen("e://tcs_best.txt","w"))==NULL)
return FALSE;
}
fputs(best,f);
fclose(f);
return TRUE;
}
char* ReadBest()
{
FILE *f;
char* best=(char*)malloc(sizeof(char)*4);
if((f=fopen("c://tcs_best.txt","r"))==NULL)
{
if((f=fopen("e://tcs_best.txt","r"))==NULL)
return (char*)"0";
}
fgets(best,5,f);
fclose(f);
return best;
}
int GameOver(HWND hwnd,HDC hdc,struct LoopLink* plink)
{
PRECT prect=(PRECT)malloc(sizeof(RECT));
PRECT prect2=(PRECT)malloc(sizeof(RECT));
PRECT prect3=(PRECT)malloc(sizeof(RECT));
char* best=(char*)malloc(sizeof(char)*4);
prect->left=0;
prect->top=30;
prect->bottom=400;
prect->right=300;
prect2->left=46;
prect2->top=0;
prect2->bottom=22;
prect2->right=80;
prect3->top=0;
prect3->left=117;
prect3->right=157;
prect3->bottom=22;
itoa(score,best,10);
int t=atoi(ReadBest());
food=0;
foodpr=NULL;
foodpos=NULL;
if(tleft=260;
rect->top=7;
rect->right=270;
rect->bottom=17;
FillRect(hdc,rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
TextOut(hdc,184,6,TEXT("8:00 AM"),7);
MoveToEx(hdc,151,18,NULL);
LineTo(hdc,151,16);
MoveToEx(hdc,154,18,NULL);
LineTo(hdc,154,14);
MoveToEx(hdc,157,18,NULL);
LineTo(hdc,157,10);
MoveToEx(hdc,160,18,NULL);
LineTo(hdc,160,8);
TextOut(hdc,162,6,TEXT("4G"),2);
TextOut(hdc,3,6,TEXT("score:"),6);
TextOut(hdc,80,6,TEXT("best:"),5);
best=ReadBest();
TextOut(hdc,117,6,best,strlen(best));
ReleaseDC(hwnd,hdc);
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
原创,存在bug,懒得改了,不喜勿喷,谢谢!