旧时光丶


私信TA

用户名:1475318935

访问量:3607

签 名:

等  级
排  名 1699
经  验 2673
参赛次数 0
文章发表 5
年  龄 0
在职情况 学生
学  校 安徽理工大学
专  业

  自我简介:

#include<easyx.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>

#define width 20
#define height 20
#define unit 20

enum draw
{
	tou,
	sheng,
	shi,
	clear
};

enum cmd
{
	up,
	left,
	right,
	down,
	quit
};

struct block
{
	char x,y;
}g_curblock,g_nextblock;

struct node
{
	char x,y;
	struct node *next;
};
struct node n1,n2,m1;
struct node *p,*q,*k,*t,*h;
struct node f[500];

byte g_world[width][height]={0};
int zuobiao[1000];
int wei[2];
DWORD oldtime,newtime;



bool checkblock(int x,int y);
void dispatchcmd(cmd _cmd);
void Down();
void drawblock(block _block,draw _draw);
void eat();
void g_worbian(node _node);
cmd getcmd();
void init();
void Left();
void newblock();
void newgame();
cmd qiangetcmd();
void qianQuit();
void Quit();
void Right();
void shengbian(node _node);
void Up();
void weizuo(node _node);
void gameover();
void clearqian(node _node);
void drawhou(node _node);
void kong();

void main()
{
	init();
	while(true)
	{
		if(kbhit())
		{
			switch(getch())
			{
			case 'w':
			case 'W':
				Up();
				goto qfy;
			case 'a':
			case 'A':
				Left();
				goto qfy;
			case 'd':
			case 'D':
				Right();
				goto qfy;
			case 27:
				qianQuit();
			}
		}
	}
qfy:
	cmd e;
	while(true)
	{
		e=getcmd();
		dispatchcmd(e);
		if(n1.x==g_nextblock.x&&n1.y==g_nextblock.y)
			eat();
	}
}


void init()
{
	initgraph(640,480);
	setbkcolor(WHITE);
	cleardevice();
	setorigin(120,40);
	setlinecolor(LIGHTGREEN);
	setfillstyle(BS_HATCHED,HS_DIAGCROSS);
	setfillcolor(LIGHTGREEN);
	fillrectangle(-1*unit,-1*unit,(width+1)*unit,(height+1)*unit);
	setfillcolor(WHITE);
	setfillstyle(BS_SOLID);
	fillrectangle(-1,-1,width*unit,height*unit);

	newgame();
}

void newgame()
{
	ZeroMemory(g_world,width*height);

	setfillcolor(WHITE);
	solidrectangle(0,0,width*unit-1,height*unit-1);

	n1.x=width/2;
	n1.y=height/2;
	n1.next=&n2;
	g_world[n1.x][n1.y]=1;
	n2.x=width/2;
	n2.y=height/2-1;
	n2.next=NULL;
	g_world[n2.x][n2.y]=1;
	g_curblock.x=n1.x;
	g_curblock.y=n1.y;
	drawblock(g_curblock,tou);
	g_curblock.x=n2.x;
	g_curblock.y=n2.y;
	drawblock(g_curblock,sheng);

	newblock();
}

void newblock()
{
	srand((unsigned) time(NULL));
	while(true)
	{
		g_nextblock.x=rand()%40;
	    g_nextblock.y=rand()%40;
		if(checkblock(g_nextblock.x,g_nextblock.y))
		{
			drawblock(g_nextblock,shi);
			break;
		}
	}
	oldtime=GetTickCount();
}

cmd getcmd()
{
	while(true)
	{
		newtime=GetTickCount();
		if(kbhit())
		{
			switch(getch())
			{
			case 'w':
			case 'W':
				return up;
			case 's':
			case 'S':
				return down;
			case 'a':
			case 'A':
				return left;
			case 'd':
			case 'D':
				return right;
			case 27:
				return quit;
			}
		}
		
		if(newtime-oldtime>=200)
		{
			if(n1.x==n2.x+1) return right;
			else if(n1.x==n2.x-1) return left;
			else if(n1.y==n2.y+1) return up;
			else if(n1.y==n2.y-1) return down;
		}
	}
}

void dispatchcmd(cmd _cmd)
{
	switch(_cmd)
	{
	case up:
		Up();
		break;
	case down:
		Down();
		break;
	case left:
		Left();
		break;
	case right:
		Right();
		break;
	case quit:
		Quit();
		break;
	}
}

void drawblock(block _block,draw _draw)
{
	int left=_block.x*unit;
	int top=(height-_block.y-1)*unit;
	int right=(_block.x+1)*unit-1;
	int buttom=(height-_block.y)*unit-1;

	switch(_draw)
	{
	case sheng:
		setfillcolor(DARKGRAY);
		solidrectangle(left+1,top+1,right-1,buttom-1);
		break;
	case clear:
		setfillcolor(WHITE);
		solidrectangle(left,top,right,buttom);
		break;
	case shi:
		setfillcolor(LIGHTRED);
		solidrectangle(left+1,top+1,right-1,buttom-1);
		break;
	case tou:
		if(n1.x==n2.x+1)
		{
			setfillcolor(BLACK);
		    solidrectangle(left+1,top+1,right-1,buttom-1);
		    setfillcolor(WHITE);
		    solidrectangle(right-1-8,top+1+3,right-1-5,top+1+6);
		    solidrectangle(right-1-8,buttom-1-3,right-1-5,buttom-1-6);
		}
		else if(n1.y==n2.y+1)
		{
			setfillcolor(BLACK);
			solidrectangle(left+1,top+1,right-1,buttom-1);
			setfillcolor(WHITE);
			solidrectangle(left+1+3,top+1+5,left+1+6,top+1+8);
			solidrectangle(right-1-3,top+1+5,right-1-6,top+1+8);
		}
		else if(n1.x==n2.x-1)
		{
			setfillcolor(BLACK);
			solidrectangle(left+1,top+1,right-1,buttom-1);
			setfillcolor(WHITE);
			solidrectangle(left+1+5,top+1+3,left+1+8,top+1+6);
			solidrectangle(left+1+5,buttom-1-3,left+1+8,buttom-1-6);
		}
		else if(n1.y==n2.y-1)
		{
			setfillcolor(BLACK);
			solidrectangle(left+1,top+1,right-1,buttom-1);
			setfillcolor(WHITE);
			solidrectangle(left+1+3,buttom-1-5,left+1+6,buttom-1-8);
			solidrectangle(right-1-3,buttom-1-5,right-1-6,buttom-1-8);
		}
		break;
	}
}

bool checkblock(int x,int y)
{
	if(x<0||x>=width||y<0||y>=height)
		return false;
	else if(g_world[x][y]==1)
		return false;
	else return true;
}

void qianQuit()
{
	HWND wnd=GetHWnd();
	if(MessageBox(wnd,"您要退出游戏吗?","退出游戏",MB_YESNO|MB_ICONQUESTION)==IDYES)
		Quit();
}

void Quit()
{
	closegraph();
	exit(0);
}

void Up()
{
	if(checkblock(n1.x,n1.y+1))
	{
		clearqian(n1);
		weizuo(n1);
		shengbian(n1);
		n1.y++;
		g_worbian(n1);
		drawhou(n1);
	}
	else if(n1.y+1==n2.y) return;
	else gameover();

	oldtime=GetTickCount();
}

void Down()
{
	if(checkblock(n1.x,n1.y-1))
	{
		clearqian(n1);
		weizuo(n1);
		shengbian(n1);
		n1.y--;
		g_worbian(n1);
		drawhou(n1);
	}
	else if(n1.y-1==n2.y) return;
	else gameover();

	oldtime=GetTickCount();

}

void Left()
{
	if(checkblock(n1.x-1,n1.y))
	{
		clearqian(n1);
		weizuo(n1);
		shengbian(n1);
		n1.x--;
		g_worbian(n1);
		drawhou(n1);
	}
	else if(n1.x-1==n2.x) return;
	else gameover();

	oldtime=GetTickCount();
}

void Right()
{
	if(checkblock(n1.x+1,n1.y))
	{
		clearqian(n1);
		weizuo(n1);
		shengbian(n1);
		n1.x++;
		g_worbian(n1);
		drawhou(n1);
	}
	else if(n1.x+1==n2.x) return;
	else gameover();

	oldtime=GetTickCount();
}

cmd qiangetcmd()
{
	if(kbhit())
	{
		switch(getch())
		{
		case 'w':
		case 'W':
			return up;
		case 'a':
		case 'A':
			return left;
		case 's':
		case 'S':
			return down;
		case 'd':
		case 'D':
			return right;
		case 27:
			return quit;
		}
	}
}

void shengbian(node _node)
{
	ZeroMemory(zuobiao,1000);
	int i=0;
	p=&_node;
	while(p->next!=NULL)
	{
		zuobiao[i++]=p->x;
		zuobiao[i++]=p->y;
		p=p->next;
	}
	i=0;
	p=_node.next;
	while(p->next!=NULL)
	{
		p->x=zuobiao[i++];
		p->y=zuobiao[i++];
		p=p->next;
	}
	p->x=zuobiao[i++];
	p->y=zuobiao[i];
}

void g_worbian(node _node)
{
	ZeroMemory(g_world,width*height);
	p=&_node;
	while(p->next!=NULL)
	{
		g_world[p->x][p->y]=1;
		p=p->next;
	}
	g_world[p->x][p->y]=1;
}

void xiaowei(node _node)
{
	p=&_node;
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,clear);
	drawblock(g_curblock,sheng);
	p=p->next;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,clear);
}

void toubian(node _node)
{
	p=&_node;
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,clear);
	drawblock(g_curblock,sheng);
}

void weizuo(node _node)
{
	k=&_node;
	while(k->next!=NULL)
	{
		k=k->next;
	}
	wei[0]=k->x;
	wei[1]=k->y;
}

int w=0;
void eat()
{
	drawblock(g_nextblock,clear);
	printf("\a");
	
	f[w].x=wei[0];
	f[w].y=wei[1];
	f[w].next=NULL;
	t=&n1;
	while(t->next!=NULL)
	{
		t=t->next;
	}
	t->next=&f[w++];

	ZeroMemory(g_world,width*height);
	t=&n1;
	while(t->next!=NULL)
	{
		g_world[t->x][t->y]=1;
		t=t->next;
	}
	g_world[t->x][t->y]=1;

	t=&n1;
	g_curblock.x=t->x;
	g_curblock.y=t->y;
	drawblock(g_curblock,tou);
	t=t->next;
	while(t->next!=NULL)
	{
		g_curblock.x=t->x;
		g_curblock.y=t->y;
		drawblock(g_curblock,sheng);
		t=t->next;
	}
	g_curblock.x=t->x;
	g_curblock.y=t->y;
	drawblock(g_curblock,sheng);

	newblock();
}

void gameover()
{
	HWND wnd=GetHWnd();
	if(MessageBox(wnd,"游戏结束,还要来一局吗?","游戏结束",MB_YESNO|MB_ICONQUESTION)==IDYES)
		kong();
	else Quit();
}

void clearqian(node _node)
{
	p=&_node;
	while(p->next!=NULL)
	{
		g_curblock.x=p->x;
		g_curblock.y=p->y;
		drawblock(g_curblock,clear);
		p=p->next;
	}
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,clear);
}

void drawhou(node _node)
{
	p=&_node;
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,tou);
	p=p->next;
	while(p->next!=NULL)
	{
		g_curblock.x=p->x;
		g_curblock.y=p->y;
		drawblock(g_curblock,sheng);
		p=p->next;
	}
	g_curblock.x=p->x;
	g_curblock.y=p->y;
	drawblock(g_curblock,sheng);
}

void kong()
{
	newgame();
	while(true)
	{
		if(kbhit())
		{
			switch(getch())
			{
			case 'w':
			case 'W':
				Up();
				goto qfy;
			case 'a':
			case 'A':
				Left();
				goto qfy;
			case 'd':
			case 'D':
				Right();
				goto qfy;
			case 27:
				qianQuit();
			}
		}
	}
qfy:
	cmd e;
	while(true)
	{
		e=getcmd();
		dispatchcmd(e);
		if(n1.x==g_nextblock.x&&n1.y==g_nextblock.y)
			eat();
	}
}



























		


 

0.0分

0 人评分

  评论区

这个不错,要是有注解就更好了
2017-11-22 13:53:01
  • «
  • 1
  • »