C++编写的在控制台界面操作的吃豆子游戏的代码

将代码过程中比较重要的内容做个珍藏,下边代码是关于C++编写的在控制台界面操作的吃豆子游戏的代码,希望能对各朋友有所用处。

#include "lib.h"

#pragma once

extern int level;

int main()
{
	FOOD food;
	WALL wall;
	BODY CurPos;
	HALL hall;

	int iExit = 0;
	while(1)
	{
		if(iExit)
			break;

		Init(&food,&wall,&CurPos,&hall);
		MakeWall(&wall);
		while(1)
		{
			Sleep(200);
			int res = Move(&food,&wall,&CurPos);
				break;
			{
				iExit = 1;
				break;
			}
			ShowScore(&CurPos,&food);
		}
	}
	return 0;
}


                                
                        
                                
lib.h
                                
                        
                                

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>

#define KEY_UP				WM_USER + 1
#define KEY_DOWN			WM_USER + 2
#define KEY_LEFT			WM_USER + 3
#define KEY_RIGHT			WM_USER + 4

#define DIRECTION_UP		WM_USER + 5
#define DIRECTION_DOWN		WM_USER + 6
#define DIRECTION_LEFT		WM_USER + 7
#define DIRECTION_RIGHT		WM_USER + 8

#define PRINT_WALL			WM_USER + 9
#define PRINT_FOOD			WM_USER + 10
#define PRINT_BODY			WM_USER + 11
#define PRINT_CLEAN			WM_USER + 12

#define KNOCK_WALL			WM_USER + 13
#define KNOCK_FOOD			WM_USER + 14



struct POS
{
	int x;
	int y;
};
struct WALL
{
	POS pos[9999];
	int len;
};
struct FOOD
{
	POS pos[8];
	int len;
	int IsHidden;
};
struct BODY
{
	POS pos;
	int Direction;
};
struct HALL{
	POS pos[200];
	int len;
};



                                
                        
                                
lib.cpp
                                
                        
                                

#include "lib.h"

#pragma once

HANDLE hMain_Out;
HANDLE hMain_In;
CONSOLE_CURSOR_INFO info;
int iBeans = 0;
int level = 1;

short wall[17][24] = {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,-1,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,
					0,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,0,0,
					0,-1,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,-1,0,0,0,0,
					0,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,0,-1,0,-1,-1,-1,-1,-1,-1,-1,0,0,
					0,-1,0,0,-1,0,-1,0,0,0,0,-1,0,-1,-1,-1,0,0,0,0,0,0,0,0,
					0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,0,
					0,-1,0,0,0,0,0,0,0,0,0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0,
					0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,-1,0,0,0,0,
					0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,0,0,-1,0,0,0,0,
					0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,-1,-1,-1,0,-1,-1,0,
					0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,
					0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,
					0,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,
					0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,
					0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,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
};
{
	if(KNOCK_WALL == TYPE)
	{
		int i;
		for(i = 0 ; i < pWall->len ; ++i)
		{
			if(IsOver(pBody->pos,pWall->pos[i],KNOCK_WALL))
		}
	}
	else if(KNOCK_FOOD == TYPE)
	{
		int i;
		for(i = 1 ; i <= pFood->len ; ++i)
		{
			if(IsOver(pFood->pos[i],pBody->pos,KNOCK_WALL))
		}
	}

	return 0;
}

{
	int iKey = GetKey();

		return -1;
	if(iKey)
	{
		pBody->Direction = iKey + 4;
		iKey = 0;
	}
	if(0 == iKey)
	{
		if(DIRECTION_UP == pBody->Direction)
			--pBody->pos.y;
		else if(DIRECTION_DOWN == pBody->Direction)
			++pBody->pos.y;
		else if(DIRECTION_LEFT == pBody->Direction)
			--pBody->pos.x;
		else if(DIRECTION_RIGHT == pBody->Direction)
			++pBody->pos.x;
	}
	
	if(IsKnock(pWall,pBody,pFood,KNOCK_WALL))
	{
		return 0;
	}

	int ix = IsKnock(pWall,pBody,pFood,KNOCK_FOOD);
	if(ix)
	{
		++iBeans;
		int i;
		for(i = ix ; i <= (pFood->len - 1) ; ++i)
			pFood->pos[i] = pFood->pos[i + 1];
		--(pFood->len);

		{
			++level;
			return 1;
		}
	}

	Print(&pBody->pos,PRINT_BODY);

	return 0;
}

int IsOver(POS pos1,POS pos2,int TYPE)
{
		if((pos1.x == pos2.x && pos1.y == pos2.y) || (pos2.x + 1 == pos1.x && pos2.y == pos1.y))
			return 1;
	return 0;
}


{
	int x,y;
	int ix = 0;

	for(x = 0 ; x <= 16 ; ++x)
	{
		for(y = 0 ; y <= 23 ; ++y)
		{
			if(0 == wall[x][y])
			{
				pWall->pos[ix].y = x;
				Print(&pWall->pos[ix++],PRINT_WALL);
			}
		}
	}


}

{
	hMain_Out = GetStdHandle(STD_OUTPUT_HANDLE);
	hMain_In = GetStdHandle(STD_INPUT_HANDLE);

	GetConsoleCursorInfo(hMain_Out,&info);
	info.bVisible = FALSE;
	SetConsoleCursorInfo(hMain_Out,&info);

	pFood->len = 0;
	pWall->len = 0;
	pHall->len = 0;

	system("mode con cols=50 lines=21");

	int x,y;
	int ix = 0;
	for(x = 0 ; x < 17 ; ++x)
	{
		for(y = 0 ; y < 24 ; ++y)
		{
			{
				pHall->pos[ix++].y = x;
			}
		}
	}
	pHall->len = ix;

	pBody->pos.x = 2;
	pBody->pos.y = 1;
	pBody->Direction = DIRECTION_DOWN;

	printf("%d %dn",pHall->pos[0].x,pHall->pos[0].y);

	int i;
	MakeFood(pHall,pFood);
	for(i = 1 ; i <= 7 ; ++i)
	{
		Print(&pFood->pos[i],PRINT_FOOD,i);
	}

}

int GetKey()
{
	if(GetAsyncKeyState(VK_UP))
		return KEY_UP;
	if(GetAsyncKeyState(VK_DOWN))
		return KEY_DOWN;
	if(GetAsyncKeyState(VK_LEFT))
		return KEY_LEFT;
	if(GetAsyncKeyState(VK_RIGHT))
		return KEY_RIGHT;
	if(GetAsyncKeyState(VK_ESCAPE))

}

{
	COORD pos;
	pos.X = p->x;
	pos.Y = p->y;
	SetConsoleCursorPosition(hMain_Out,pos);

	if(PRINT_WALL == TYPE)
		printf("■");
	else if(PRINT_FOOD == TYPE)
		printf("%d",ix);
	else if(PRINT_BODY == TYPE)
		printf("1");
	else if(PRINT_CLEAN == TYPE)
		printf(" ");
}

{
	COORD pos;
	pos.X = 0;
	pos.Y = 18;
	SetConsoleCursorPosition(hMain_Out,pos);

	printf("tBeans left : %d | pos : x=%d,y=%dn",pFood->len,pBody->pos.x,pBody->pos.y);
	printf("ttLevel : %d",level);
}

{
	srand((unsigned)time(NULL));
	int tot;
	for(tot = 1 ; tot <= 7 ; ++tot)
	{
		pFood->pos[tot].x = pHall->pos[ixFood].x;
		pFood->pos[tot].y = pHall->pos[ixFood].y;
	}
	pFood->len = 7;
}

猜你喜欢

转载自blog.csdn.net/weixin_44073507/article/details/86490497