Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C-lections / Game_Sample / MYNEWGAM

.CPP
Скачиваний:
25
Добавлен:
27.03.2015
Размер:
6.53 Кб
Скачать
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

#define STEP 7

#define UP 		72
#define DOWN    80
#define LEFT 	75
#define RIGHT   77

#define ESC		27
#define ENTER	13

#define MEN_ITEMS 4
char menu[MEN_ITEMS][10]={	"GAME",
							"HELP",
							"OPTIONS",
							"EXIT"};

int SCR_MODE=1;
int PIXSZ=1;

int hero[4][STEP][STEP]={
			0,0,0,1,0,0,0,
			0,2,2,1,2,2,0,
			0,2,2,1,2,2,0,
			0,2,1,1,1,2,0,
			0,2,1,1,1,2,0,
			0,2,2,2,2,2,0,
			0,2,2,2,2,2,0,


			0,0,0,0,0,0,0,
			2,2,2,2,2,2,0,
			2,2,1,1,2,2,0,
			2,2,1,1,1,1,1,
			2,2,1,1,2,2,0,
			2,2,2,2,2,2,0,
			0,0,0,0,0,0,0,


			0,2,2,2,2,2,0,
			0,2,2,2,2,2,0,
			0,2,1,1,1,2,0,
			0,2,1,1,1,2,0,
			0,2,2,1,2,2,0,
			0,2,2,1,2,2,0,
			0,0,0,1,0,0,0,


			0,0,0,0,0,0,0,
			0,2,2,2,2,2,2,
			0,2,2,1,1,2,2,
			1,1,1,1,1,2,2,
			0,2,2,1,1,2,2,
			0,2,2,2,2,2,2,
			0,0,0,0,0,0,0
			};

int FH=3;
int FW=5;

int field[2][100][100]=
				{
				{
				{1,1,1,1,1},
				{2,0,0,0,2},
				{1,1,1,1,1},
				},

				{
				{1,1,1,1,1,1,1,1,1,1},
				{1,0,0,0,0,0,0,0,0,1},
				{1,0,2,0,0,2,0,0,0,1},
				{1,0,0,0,0,0,0,1,0,1},
				{1,1,1,1,1,1,1,1,1,1}
				}
				};

int elem[3][STEP][STEP]={
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,
				0,0,0,0,0,0,0,

				4,7,4,7,4,7,4,
				7,4,7,4,7,4,7,
				4,7,4,7,4,7,4,
				7,4,7,4,7,4,7,
				4,7,4,7,4,7,4,
				7,4,7,4,7,4,7,
				4,7,4,7,4,7,4,

				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4,
				4,4,4,4,4,4,4
				};


void seeKeyCode()
{
	char ch;

	getch();
	ch=getch();

	printf("%d",ch);

 getch();
}

void putPoint(int row,int col,int color);
void drawField();
void drawFieldEl(int raw,int col,int type);
void drawHero(int raw,int col,int dir,int mode);


void setTextMode();

void showMenu(int pos);

void Game();
void Help();
void Options();



//*************************       MAIN    *************************

void main()
{
	int ex=1;   //exit flag
	int pos=0;	//menu pointer

	char ch;

	setTextMode();

	while(ex)
	{
		clrscr();
		showMenu(pos);


		ch=getch();

		switch(ch)
		{
		 case UP:
				if(pos<=0)
					pos=3;
				else
					pos--;

		 break;

		 case DOWN:
				if(pos>=3)
					pos=0;
				else
					pos++;
		 break;

		 case ESC:
				ex=0;
		 break;

		 case ENTER:
				switch(pos)
				{
				 case 0:Game();
						if(SCR_MODE) setTextMode();
						pos=0;
				 break;

				 case 1:Help();
						pos=0;
				 break;

				 case 2:Options();
                 		pos=0;
				 break;

				 case 3:ex=0;
				 break;
				}
		 break;
		}
	}
}


void showMenu(int pos)
{
	int i;

	for(i=0;i<MEN_ITEMS;i++)
	{
		textbackground(2);
		if(i==pos)
			textbackground(4);

		gotoxy(5,3+i);
		cprintf("%s",menu[i]);
	}
textbackground(0);
}


void Game()
{
	int ex=1;
	char ch;

	int grd = DETECT, grm;

	int khr=1,khc=1;	//koord hero raw,col
	int dir=0;

if(!SCR_MODE)
{
//TEXT MODE*******************
	setTextMode();
	FH=3;
	FW=5;
//****************************
}
else
{
//GRAP MODE*******************
	initgraph(&grd, &grm, "");
	PIXSZ=5;
	FH=5;
	FW=10;
//****************************
}

	drawField();
	drawHero(khr,khc,dir,1);

	while(ex)
	{
		if(kbhit())
		{
			ch=getch();

			switch(ch)
			{
			 case UP:dir=0;
					if(field[SCR_MODE][khr-1][khc]==0)
					 {
						drawHero(khr,khc,0,0);
						khr--;
					 }
			 break;

			 case DOWN:dir=2;
					if(field[SCR_MODE][khr+1][khc]==0)
					 {
						drawHero(khr,khc,0,0);
						khr++;
					 }
			 break;

			 case LEFT:dir=3;
					if(field[SCR_MODE][khr][khc-1]==0)
					 {
						drawHero(khr,khc,0,0);
						khc--;
					 }
			 break;

			 case RIGHT:dir=1;
					if(field[SCR_MODE][khr][khc+1]==0)
					 {
						drawHero(khr,khc,0,0);
						khc++;
					 }
			 break;

			 case ESC:
						ex=0;
			 break;

			}

		drawHero(khr,khc,dir,1);
		}
	}

if(SCR_MODE)
	closegraph();
}



void Help()
{
	textbackground(0);
	clrscr();
	textbackground(5);
	gotoxy(10,10);
	cprintf("THIS IS THE HELP TEXT!!!!");
	textbackground(0);
	getch();
}


void Options()
{
	char ch;

	textbackground(0);
	clrscr();
	textbackground(5);
	gotoxy(1,5);
	cprintf("SELECT GAME MODE:");
	gotoxy(5,7);
	cprintf("1 - TEXT MODE");
	gotoxy(5,8);
	cprintf("2 - GRAPHICS MODE");

	ch=getch();

	switch(ch)
	{
		case '1': SCR_MODE=0;
		break;

		case '2': SCR_MODE=1;
		break;

		default: SCR_MODE=0;
	}

	textbackground(0);
}


void setTextMode()
{
	textmode(C40);
	_setcursortype(_NOCURSOR);
	PIXSZ=1;
	textbackground(0);
	clrscr();
}


//*************************** DRAW FUNCTIONS **************************
void drawField()
{
	int i,j;

	for(i=0;i<FH;i++)
		for(j=0;j<FW;j++)
			drawFieldEl(i,j,field[SCR_MODE][i][j]);

}


void drawFieldEl(int raw,int col,int type)
{
   int i,j;
   int color;

	for(i=0;i<STEP;i++)
		for(j=0;j<STEP;j++)
		{
			switch(elem[type][i][j])
			{
			 case 0:textbackground(0);
					color=0;
			 break;


			 case 1:textbackground(1);
					color=1;
			 break;


			 case 2:textbackground(2);
					color=2;
			 break;


			 case 3:textbackground(3);
					color=3;
			 break;


			 case 4:textbackground(4);
					color=4;
			 break;

			 case 7:textbackground(7);
					color=7;
			}

			putPoint((col*STEP+1+j)*PIXSZ,(raw*STEP+1+i)*PIXSZ,color);
		}
}


void drawHero(int raw,int col,int dir,int mode)
{

  int i,j;
  int color;	//for GRah mode

	for(i=0;i<STEP;i++)
		for(j=0;j<STEP;j++)
		{
			if(mode)
			{
				switch(hero[dir][i][j])
				{
				case 0:textbackground(0);
						color=0;
				break;

				case 1:textbackground(1);
						color=1;
				break;

				case 2:textbackground(2);
						color=2;
				break;

				case 3:textbackground(3);
						color=3;
				break;

				case 4:textbackground(4);
						color=4;
				break;
				}
			}
			else
			{
				textbackground(0);
				color=0;
			}


			putPoint((col*STEP+1+j)*PIXSZ,(raw*STEP+1+i)*PIXSZ,color);
		}
}


void putPoint(int x,int y,int color)
{
	switch(SCR_MODE)
	{
		case 0:
				gotoxy(x,y);
				cprintf(" ");
		break;

		case 1:
			   setfillstyle(1,color);
			   bar(x,y,x+PIXSZ,y+PIXSZ);
		break;
	}
}


Соседние файлы в папке Game_Sample