游戏综合C++

#include<bits/stdc++.h>
#include<windows.h> 
#include<conio.h>
#include<dos.h> 
#define random(a,b) (rand()%(b-a+1)+a)
#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70 
using namespace std;
char a[21][21];
void loading()
{
	cout<<"loading... (0%) \n\n作者:young__kacker";
	Sleep(600); 
	system("cls");
	for(int i=0;i<=100;i++)
	{
		system("cls"); 
		int x=rand()%(100-i+1)+i;
		i=x;
		cout<<"loading... ("<<i<<"%)\n";
		for(int j=1;j<=i;j++)
		{
			cout<<'.'; 
		}
		cout<<"\n作者:young__kacker";
		Sleep(500);
	} 
	Sleep(1000);
	system("cls");
	cout<<"***************1.play***************\n\n";
	cout<<"***************2.exit***************\n";
}
int winner_five()
{
	//3为满,2为x,1为o,0为平
	bool flag=true;
	for(int i=1;i<=20;i++)
	{
		for(int j=1;j<=20;j++)
		{
			if(a[i][j]=='.')
			{
				flag=false;
				continue; 
			}
			if(a[i][j]=='x')
			{
				if(a[i][j]==a[i+1][j+1]&&a[i+1][j+1]==a[i+2][j+2]&&a[i+2][j+2]==a[i+3][j+3]&&a[i+3][j+3]==a[i+4][j+4])
				{
					return 2;
				}
				if(a[i][j]==a[i+1][j-1]&&a[i+1][j-1]==a[i+2][j-2]&&a[i+2][j-2]==a[i+3][j-3]&&a[i+3][j-3]==a[i+4][j-4])
				{
					return 2;
				}
				if(a[i][j]==a[i+1][j]&&a[i+1][j]==a[i+2][j]&&a[i+3][j]==a[i+2][j]&&a[i+3][j]==a[i+4][j])
				{
					return 2;
				}
				if(a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]&&a[i][j+2]==a[i][j+3]&&a[i][j+3]==a[i][j+4])
				{
					return 2;
				}
			}
			if(a[i][j]=='o')
			{
				if(a[i][j]==a[i+1][j+1]&&a[i+1][j+1]==a[i+2][j+2]&&a[i+2][j+2]==a[i+3][j+3]&&a[i+3][j+3]==a[i+4][j+4])
				{
					return 1;
				}
				if(a[i][j]==a[i+1][j-1]&&a[i+1][j-1]==a[i+2][j-2]&&a[i+2][j-2]==a[i+3][j-3]&&a[i+3][j-3]==a[i+4][j-4])
				{
					return 1;
				}
				if(a[i][j]==a[i+1][j]&&a[i+1][j]==a[i+2][j]&&a[i+3][j]==a[i+2][j]&&a[i+3][j]==a[i+4][j])
				{
					return 1;
				}
				if(a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]&&a[i][j+2]==a[i][j+3]&&a[i][j+3]==a[i][j+4])
				{
					return 1;
				}
			}
		}
	}
	if(flag=false)
	{
		return 3;
	}
	return 0;
}
void playing_five()
{
	system("cls");
	for(int i=1;i<=20;i++)
	{
		for(int j=1;j<=20;j++)
		{
			a[i][j]='.';
		}
	}
	int tmp=0;
	while(1)
	{
		cout<<"   ";
		for(int i=1;i<=20;i++)
		{
			cout<<i;
			if(i/10==0)
			{
				cout<<"  ";
			}
			else
			{
				cout<<" ";
			}
		} 
		cout<<endl;
		for(int i=1;i<=20;i++)
		{
			for(int j=0;j<=20;j++)
			{
				if(j==0)
				{
					cout<<i;
					if(i/10==0)
					{
						cout<<"  ";
					}
					else 
					{
						cout<<" ";
					}
					continue;
				}
				cout<<a[i][j]<<"  ";
			}
			cout<<endl; 
		}
		int cnt=winner_five();
		if(cnt==1)
		{
			cout<<"\no 方胜利\n 游戏结束\n作者:young__kacker";
			return ; 
		}
		if(cnt==2)
		{
			cout<<"\nx 方胜利\n 游戏结束\n作者:young__kacker";
			return ;
		}
		if(cnt==3)
		{
			cout<<"双方打平\n游戏结束\n作者:young__kacker";
			return ; 
		}
		if(tmp%2==1)
		{
			cout<<"\n请先输入行,在输入列(空格隔开)(  x  方)\n          作者:young__kacker\n";
		}
		if(tmp%2==0)
		{
			cout<<"\n请先输入行,在输入列(空格隔开)(  o  方)\n          作者:young__kacker\n";
		}
		int n,m;
		cin>>n>>m;
		if(a[n][m]!='.')
		{
			cout<<"输入的点不合法,请重新输入(等待两秒)\n";
			Sleep(2000); 
			system("cls");
			continue; 
		}
		else if(tmp%2==0)
		{
			a[n][m]='o';
		}
		else if(tmp%2==1)
		{
			a[n][m]='x';
		}
		system("cls");
		tmp++;
	}
	return ;
}



/* 贪吃蛇 */ 
void gotoxy(int x,int y,char c)
{
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
	HANDLE hConsoleOut;
	hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
	csbiInfo.dwCursorPosition.X=x; 
	csbiInfo.dwCursorPosition.Y=y;
	SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
	printf("%c",c);
}  
int X1=1,Y1=1;
int X2=60,Y2=30;
struct Snake
{
	int x,y;
};
vector<Snake>snake;
void init()
{ 
	srand((int)time(0));
	for(int i=X1;i<=X2;i++)
	{
		gotoxy(i,Y1,'#');
		gotoxy(i,Y2,'#');
	}
	for(int j=Y1;j<=Y2;j++)
	{
		gotoxy(X1,j,'#');
		gotoxy(X2,j,'#');
	}
	snake.clear();
	Snake t;
	t.x=(X1+X2)/2;
	t.y=(Y1+Y2)/2;
	snake.push_back(t);
}
int XX[4]={0,0,-1,1};
int YY[4]={-1,1,0,0};
int food_x,food_y; 
bool search(int L,int R)
{
	if(L>R)return false;
	int MID=random(L,R);
	int d=X2-X1-1;
	int y=(MID+d)/d+X1;
	int t=MID%d;
	int x=Y1;
	if(t==0)
	x+=d;
	else x+=t; 
	bool flag=false;
	for(int i=0;i<snake.size();i++)
	{
		if(snake[i].x==x&&snake[i].y==y)
		{
			flag=true;
			break;
		}
	}
	if(flag)
	{
		bool res=search(L,MID-1);
		if(res)return true;
		res=search(MID+1,R);
		if(res)return true;
		return false;
	}
	else 
	{
		food_x=x;
		food_y=y;
		return true;
	}
}
void Print(int direction)
{
	int n=snake.size()-1;
	gotoxy(snake[n].x,snake[n].y,' ');
	for(int i=n;i>=1;i--)
	snake[i]=snake[i-1];
	snake[0].x+=XX[direction];
	snake[0].y+=YY[direction]; 
	for(int i=1;i<=n;i++)
	gotoxy(snake[i].x,snake[i].y,'*');
	gotoxy(snake[0].x,snake[0].y,'@'); 
	gotoxy(food_x,food_y,'O');
} 
bool game_over()
{
	for(int i=1;i<snake.size();i++)
	{
		if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y)
		{
			return false;
		}
	}
	if(snake[0].x>=X2||snake[0].x<=X1)
	{
		return false;
	}
	if(snake[0].y>=Y2||snake[0].y<=Y1)
	{
		return false;
	}
	return true;
}
void snack()
{
	init();
	int direction=1;
	bool is_food=false;
	while(true)
	{

		if(is_food==false)
		{
			search((X1+2)*(Y1+2),(X2-2)*(Y2-2));
			is_food=true;
		}
		if(snake[0].x==food_x&&snake[0].y==food_y)
		{
			Snake t;
			t.x=food_x;
			t.y=food_y;
			is_food=false;
			snake.insert(snake.begin(),t);
		}
		Print(direction);		
		if(game_over()==false)
		{
			gotoxy(X1,Y2+1,' ');
			cout<<"游戏结束!!!\n";
			return ;
		}
		if(kbhit())
		{
			char c=getch();
			if(c=='w')//往上 
			{
				direction=0;
			}
			else if(c=='s')//往下 
			{
				direction=1;
			}
			else if(c=='a')//往左 
			{
				direction=2;
			}
			else if(c=='d')//往右 
			{
				direction=3;	
			}
		}
		gotoxy((X1+X2)/2,Y2+2,' ');
		cout<<"当前得分:"<<snake.size()*10<<endl;
		Sleep(150);
	} 
	return ;
} 

/* 贪吃蛇结束 */

/* 赛车游戏 */
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
int enemyY[3];
int enemyX[3];
int enemyFlag[3];
char car[4][4]={' ','±','±',' ', 
            	'±','±','±','±', 
                ' ','±','±',' ',
                '±','±','±','±'}; 
 
int carPos=WIN_WIDTH/2;
int score=0; 
void gotoxy(int x, int y)
{
    CursorPosition.X=x;
    CursorPosition.Y=y;
    SetConsoleCursorPosition(console,CursorPosition);
}
void setcursor(bool visible, DWORD size)
{
    if(size==0)
	{
		size=20;  
	}
    CONSOLE_CURSOR_INFO lpCursor;   
    lpCursor.bVisible = visible;
    lpCursor.dwSize = size;
    SetConsoleCursorInfo(console,&lpCursor);
}
void drawBorder()
{  
    for(int i=0; i<SCREEN_HEIGHT; i++)
	{
        for(int j=0; j<17; j++)
		{
            gotoxy(0+j,i);
			cout<<"±";
            gotoxy(WIN_WIDTH-j,i);
			cout<<"±";
        }
    } 
    for(int i=0; i<SCREEN_HEIGHT; i++)
	{
        gotoxy(SCREEN_WIDTH,i);
		cout<<"±";
    } 
}
void genEnemy(int ind)
{
    enemyX[ind]=17+rand()%(33);  
}
void drawEnemy(int ind)
{
    if( enemyFlag[ind] == true )
	{
        gotoxy(enemyX[ind], enemyY[ind]);
		cout<<"****";  
        gotoxy(enemyX[ind], enemyY[ind]+1);
		cout<<" ** "; 
        gotoxy(enemyX[ind], enemyY[ind]+2);
		cout<<"****"; 
        gotoxy(enemyX[ind], enemyY[ind]+3);
		cout<<" ** ";  
    } 
}
void eraseEnemy(int ind)
{
    if( enemyFlag[ind] == true )
	{
        gotoxy(enemyX[ind], enemyY[ind]);
		cout<<"    ";  
        gotoxy(enemyX[ind], enemyY[ind]+1);
		cout<<"    "; 
        gotoxy(enemyX[ind], enemyY[ind]+2);
		cout<<"    "; 
        gotoxy(enemyX[ind], enemyY[ind]+3);
		cout<<"    "; 
    } 
}
void resetEnemy(int ind)
{
    eraseEnemy(ind);
    enemyY[ind]=1;
    genEnemy(ind);
}
 
void drawCar()
{
    for(int i=0; i<4; i++)
	{
        for(int j=0; j<4; j++)
		{
            gotoxy(j+carPos, i+22);
			cout<<car[i][j];
        }
    }
}
void eraseCar()
{
    for(int i=0; i<4; i++)
	{
        for(int j=0; j<4; j++)
		{
            gotoxy(j+carPos, i+22);
			cout<<" ";
        }
    }
}
 
int collision()
{
    if(enemyY[0]+4>=23)
	{
        if(enemyX[0]+4-carPos>=0&&enemyX[0]+4-carPos<9)
		{
            return 1;
        }
    }
    return 0;
} 
void gameover()
{
    system("cls");
    cout<<endl;
    cout<<"\t\t--------------------------"<<endl;
    cout<<"\t\t-------- Game Over -------"<<endl;
    cout<<"\t\t--------------------------"<<endl<<endl;
    cout<<"\t\tPress any key to go back to menu.";
    getch();
}
void updateScore()
{
    gotoxy(WIN_WIDTH + 7, 5);
	cout<<"Score: "<<score<<endl;
}
 
void instructions()
{
 
    system("cls");
    cout<<"Instructions";
    cout<<"\n----------------";
    cout<<"\n Avoid Cars by moving left or right. ";
    cout<<"\n\n Press 'a' to move left";
    cout<<"\n Press 'd' to move right";
    cout<<"\n Press 'escape' to exit";
    cout<<"\n\nPress any key to go back to menu";
    getch();
}
void play()
{
    carPos = -1 + WIN_WIDTH/2;
    score = 0;
    enemyFlag[0] = 1;
    enemyFlag[1] = 0;
    enemyY[0] = enemyY[1] = 1;
 
    system("cls"); 
    drawBorder(); 
    updateScore();
    genEnemy(0);
    genEnemy(1);
 
    gotoxy(WIN_WIDTH + 7, 2);cout<<"Car Game";
    gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
    gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
    gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
    gotoxy(WIN_WIDTH + 7, 13);cout<<"-------- ";
    gotoxy(WIN_WIDTH + 2, 14);cout<<" A Key - Left";
    gotoxy(WIN_WIDTH + 2, 15);cout<<" D Key - Right"; 
 
    gotoxy(18, 5);cout<<"Press any key to start";
    getch();
    gotoxy(18, 5);cout<<"                      ";
 
    while(1)
	{
        if(kbhit())
		{
            char ch = getch();
            if( ch=='a' || ch=='A' )
			{
                if( carPos > 18 )
                {
                	carPos -= 4;
				}
            }
            if( ch=='d' || ch=='D' )
			{
                if(carPos<50)
                {
                	carPos+=4;
				}
            } 
            if(ch==27)
			{
                break;
            }
        }
        drawCar(); 
        drawEnemy(0); 
        drawEnemy(1); 
        if(collision()==1)
		{
            gameover();
            return;
        } 
        Sleep(50);
        eraseCar();
        eraseEnemy(0);
        eraseEnemy(1);   
        if(enemyY[0]==10)
        {
        	if(enemyFlag[1]==0)
            {
            	enemyFlag[1]=1;
			}
		}
        if(enemyFlag[0]==1)
        {
        	enemyY[0]+=1;
		}
        if(enemyFlag[1]==1)
        {
        	enemyY[1]+=1;
		}
        if(enemyY[0]>SCREEN_HEIGHT-4)
		{
            resetEnemy(0);
            score++;
            updateScore();
        }
        if(enemyY[1]>SCREEN_HEIGHT-4)
		{
            resetEnemy(1);
            score++;
            updateScore();
        }
    }
}
void car_game() 
{
	setcursor(0,0); 
    srand((unsigned)time(NULL)); 
    do
	{
        system("cls");
        gotoxy(10,5); cout<<" -------------------------- "; 
        gotoxy(10,6); cout<<" |        Car Game        | "; 
        gotoxy(10,7); cout<<" --------------------------";
        gotoxy(10,9); cout<<"1. Start Game";
        gotoxy(10,10); cout<<"2. Instructions";  
        gotoxy(10,11); cout<<"3. Quit";
        gotoxy(10,13); cout<<"Select option: ";
        char op = getche();
 
        if( op=='1') play();
        else if( op=='2') instructions();
        else if( op=='3') exit(0);
 
    }while(1);
    return ;
}

/* 赛车游戏结束 */

int main()
{
	srand(time(NULL)); 
	loading();
	Sleep(1000); 
	int n;
	cin>>n;
	if(n==2)
	{
		return 0;
	}
	if(n==1)
	{
		system("cls");
		cout<<"*************** 1.五子棋 ***************\n\n";
		cout<<"***************2.赛车游戏***************\n\n";
		cout<<"*************** 3.贪吃蛇 ***************\n\n";
		cout<<"***************  4.exit  ***************\n\n";
		int x;
		cin>>x;
		system("cls");
		if(x==1)
		{
			playing_five();
		}
		if(x==2)
		{
			car_game();
		}
		if(x==3)
		{
			snack();
		} 
		if(x==4)
		{
			return 0;
		}
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值