1.隨機迷宮:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 31 //迷宮的高度,必須為奇數
#define Width 25 //迷宮的寬度,必須為奇數
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
int map[Height+2][Width+2];
void gotoxy(int x,int y) //移動坐標
{
/* typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD, *PCOORD; */
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隱藏游標
{
/* typedef void *HANDLE; */
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
/* typedef struct _CONSOLE_CURSOR_INFO {
DWORD dwSize;
BOOL bVisible;
} CONSOLE_CURSOR_INFO *PCONSOLE_CURSOR_INFO; */
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible = 0;//賦1為顯示,賦0為隱藏
SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) //隨機生成迷宮
{
int c[4][2] = {0,1,1,0,0,-1,-1,0}; //四個方向
int i,j,t;
//將方向打亂
for(i=0;i<4;i++)
{
j = rand()%4;
t = c[i][0];
c[i][0] = c[j][0];
c[j][0] = t;
t = c[i][1];
c[i][1] = c[j][1];
c[j][1] = t;
}
map[x][y] = Road;
for(i = 0;i < 4;i++)
if(map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
{
map[x + c[i][0]][y + c[i][1]] = Road;
create(x+ 2 * c[i][0],y + 2 * c[i][1]);
}
}
int get_key() //接收按鍵
{
char c;
while(c = getch())
{
if(c == 27)
return Esc; //Esc
if(c != -32)
continue;
c = getch();
if(c == 72)
return Up; //上
if(c == 80)
return Down; //下
if(c ==75 )
return Left; //左
if(c == 77)
return Right; //右
}
return 0;
}
void paint(int x,int y) //畫迷宮
{
gotoxy(2 * y - 2,x - 1);
switch(map[x][y])
{
case Start:
printf("入");break; //畫入口
case End:
printf("出");break; //畫出口
case Wall:
printf("※");break; //畫墻
case Road:
printf(" ");break; //畫路
}
}
void game()
{
int x=2,y=1; //玩家當前位置,剛開始在入口處
int c; //用來接收按鍵
while(1)
{
gotoxy(2 * y - 2,x - 1);
printf("☆"); //畫出玩家當前位置
if(map[x][y] == End) //判斷是否到達出口
{
gotoxy(30,24);
printf("到達終點,按任意鍵結束");
getch();
break;
}
c = get_key();
if(c == Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y] != Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+1][y] != Wall)
{
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-1] != Wall)
{
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+1] != Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
int i,j;
srand((unsigned)time(NULL)); //初始化隨即種子
hidden(); //隱藏游標
for(i = 0;i <= Height + 1;i++)
for(j = 0;j <= Width + 1;j++)
if(i == 0 || i == Height + 1 || j == 0 || j == Width + 1) //初始化迷宮
map[i][j] = Road;
else
map[i][j] = Wall;
create(2 * (rand() % (Height / 2)+1),2 * (rand() % (Width / 2) + 1)); //從隨機一個點開始生成迷宮,該點行列都為偶數
for(i = 0;i <= Height+1;i++) //邊界處理
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //邊界處理
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //給定入口
map[Height-1][Width]=End; //給定出口
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) //畫出迷宮
paint(i,j);
game(); //開始游戲
getch();
return 0;
}
2.文字游戲
#include <iostream>
using namespace std;
double shengmingli=200000000000;//定義主角初始生命力
int gongjili=150000;//定義主角初始攻擊力
int fangyuli=2000000;//定義主角初始防御力
int money=2000000;//定義主角初始金錢數量
bool guoguan;//定義是否通關判定
void wuqidian();//定義武器店函式
void yaodian();//定義藥店函式
void guaiwu1();//定義小怪物函式
void guaiwu2();//定義大怪物函式
int main()
{
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
int xiaozhen;//定義選擇專案
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//輸入5時退出游戲
{
if(shengmingli<=0)//主角生命力小于等于0時游戲結束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通關!"<<endl;
break;
}
if(xiaozhen==6)//輸入6可檢測自己的狀態
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻擊力:"<<gongjili<<endl;
cout<<"你的防御力:"<<fangyuli<<endl;
cout<<"你擁有的錢:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"請不要亂選!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出游戲……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"歡迎來到武器店!"<<endl;
cout<<"1、買小刀(1M加2攻擊力)"<<endl;
cout<<"2、買短劍(2M加20攻擊力)"<<endl;
cout<<"3、買大砍刀(5M加40攻擊力)"<<endl;
cout<<"4、買雙節棍(7M加60攻擊力)"<<endl;
cout<<"5、買盾牌(2M加30防御力)"<<endl;
cout<<"6、買鎧甲(5M加60防御力)"<<endl;
cout<<"7、離開武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//輸入7時結束函式
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的錢不夠"<<endl;//錢不夠時回傳Flase
else
cout<<"購買成功!"<<endl;//錢足夠時回傳True
gongjili+=2;
money-=1;
break;
case 2 : if(money<80)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=20;
money-=80;
break;
case 3 : if(money<140)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=40;
money-=140;
break;
case 4 : if(money<200)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=60;
money-=200;
break;
case 5 : if(money<60)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
fangyuli+=30;
money-=60;
break;
fangyuli+=60;
money-=100;
break;
default : cout<<"無"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //回傳main()主函式
cout<<"歡迎下次再來!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
}
/*
yaodian()的設定與wuqidian()相同,可參照閱讀.
*/
void yaodian()
{
cout<<"歡迎來到藥品店!"<<endl;
cout<<"1、買1號補血藥(10M加200生命)"<<endl;
cout<<"2、買2號補血藥(50M加1000生命力)"<<endl;
cout<<"3、買3號補血藥(100M加2200生命力)"<<endl;
cout<<"4、離開武器店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=4)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=200;
money-=10;
break;
case 2 : if(money<50)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=1000;
money-=50;
break;
case 3 : if(money<100)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=2200;
money-=100;
break;
default : cout<<"無"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"歡迎下次再來!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
}
/*這里是兩個戰斗函式,使用指標來處理.避免造成記憶體崩潰.*/
void guaiwu1()
{
cout<<"開始與小怪物戰斗!!!"<<endl;
double* g_shengmingli=new double;//定義怪物生命
int* g_gongjili=new int;//定義怪物攻擊力
int* g_fangyuli=new int;//定義怪物防御力
int* g_money=new int;//定義怪物金錢
*g_shengmingli=100;
*g_gongjili=5;
*g_fangyuli=3;
*g_money=5;
double* tongji1=new double;//用來計算主角對怪物的殺傷
double* tongji2=new double;//用來計算怪物對主角的殺傷
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定義回合數
*huihe=1;
cout<<"你開始對小怪物進行攻擊!"<<endl;
int* xuanze=new int;
/*
攻擊計算公式
殺傷=攻擊力*2-防御力
玩家每回合可以選擇攻擊與逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"現在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"請選擇你的動作:\n";
cout<<"1、攻擊\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你對小怪物發動了攻擊!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物還剩"<<(*g_shengmingli)-(*tongji1)<<"點生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物對你發動了攻擊!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你還剩"<<shengmingli-(*tongji2)<<"點生命"<<endl;break;
case 2 : cout<<"你決定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"請不要亂選!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//殺死怪物后的回傳
cout<<"小怪物被你殺死了!你真厲害!!!"<<endl;
money+=(*g_money);
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
else
if(shengmingli<=0)
{//被怪物殺死后的回傳
cout<<"你被小怪物殺死了!游戲結束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的回傳
cout<<"你逃回了小鎮!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
設定均與void函式guaiwu1()相同,可參照上例閱讀.
*/
void guaiwu2()
{
cout<<"開始與大怪物戰斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=3600;
*g_gongjili=500;
*g_fangyuli=500;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你開始對大怪物進行攻擊!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"現在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"請選擇你的動作:\n";
cout<<"1、攻擊\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你對大怪物發動了攻擊!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物還剩"<<(*g_shengmingli)-(*tongji1)<<"點生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"大怪物對你發動了攻擊!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你還剩"<<shengmingli-(*tongji2)<<"點生命"<<endl;break;
case 2 : cout<<"你決定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"請不要亂選!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你殺死了!你真厲害!!!"<<endl;
guoguan=true;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物殺死了!游戲結束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小鎮!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮,周圍有一條河,有一片樹林,很多房子和很多人,\n有一家藥店"<<endl;
cout<<"和一家武器店,\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去藥品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
3.另一個文字游戲
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
double shanghai[20]={0.6,1.1,2,3.16,5.5,7,10,20,50,100,146.23,254.13,312,403,601,1023};
double bosshealth[20]={2,3,4,5.9,8,14,19,32,73,157,200,403,801,1200,3630,20123};
double wj_shanghai=5000,wj_health=10000000,wj_max_health=10000000,boss,wj_money=10000000000000000000000000000000;
void chushihua();
void game();
void gongji();
void goumai();
void shangdian();
void zhujiemian();
void fangyu();
void cend();
void chushou();
void print(char[]);
int bishou=0,caidao=0,jian=0,shenjian=0;
double bishou_1=5,caidao_1=17,jian_1=58,shenjian_1=124;
int hat=0,douhui=0,hudun=0,hunjia=0,shendun=0;
double hat_1=7,douhui_1=21,hudun_1=49,hunjia_1=89,shendun_1=210.4;
void cend()
{
system("cls");
print("GAME OVER");
exit(1);
}
void game()
{
int k;
chushihua();
IO:
printf("請輸入對手等級 (0~15)\n");
scanf("%d",&k);
if(k>15||k<0)
{
system("cls");
goto IO;
}
boss=bosshealth[k];
system("cls");
while(wj_health>=0)
{
srand(time(NULL));
QP:
printf("1.逃跑 2.進攻\n");
char s=getch();
if(s<'1'||s>'2')
{
system("cls");
goto QP;
}
if(s=='1')
{
system("cls");
zhujiemian();
}
system("cls");
double l=shanghai[k]*((rand()%2)+1)+fabs(double(rand()%100/100-2));
printf("對手對你造成了%lf點傷害\n",l);
wj_health-=l;
printf("你當前剩余血量:%lf\n",wj_health);
if(wj_health<=0)
cend();
double o=wj_shanghai*((rand()%2)+1)+double(rand()%10/10);
boss-=o;
printf("你對對手造成了%lf點傷害\n",o);
printf("對手當前剩余血量:%lf\n\n",boss);
if(boss<=0)
{
printf("勝利!\n獲得%lf金幣\n\n當前剩余血量:%lf\n",shanghai[k]+3,wj_health);
wj_money+=shanghai[k]+3;
printf("\n余額:%lf\n",wj_money);
getch();
if(k==15)
{
printf("恭喜玩家!游戲勝利!\n");
getch();
exit(1);
}
system("cls");
zhujiemian();
}
}
}
void zhujiemian()
{
PO:
printf("1.商店 2.戰斗 3.回血 4.狀態\n");
char k=getch();
if(k>'4'||k<'1')
{
system("cls");
goto PO;
}
if(k=='1')
{
system("cls");
shangdian();
return;
}
if(k=='2')
{
system("cls");
game();
return;
}
if(k=='3')
{
system("cls");
if(wj_money>0)
{
wj_money=wj_money*4/5-1;
chushihua();
wj_health=wj_max_health;
printf("回血成功!\n");
getch();
system("cls");
goto PO;
}
else
{
printf("余額不足!\n");
getch();
system("cls");
goto PO;
}
}
if(k=='4')
{
chushihua();
system("cls");
printf("生命值:%lf\n",wj_health);
printf("最大生命值:%lf\n",wj_max_health);
printf("攻擊力:%lf\n",wj_shanghai);
printf("金幣:%lf\n",wj_money);
getch();
system("cls");
goto PO;
}
if(k=='5')
{
string a;
system("cls");
printf("輸入密碼!\n");
cin>>a;
if(a=="gu"||a=="gu")
{
wj_money+=1000;
printf("外掛生效\n");
Sleep(1000);
system("cls");
goto PO;
}
printf("外掛失敗\n");
Sleep(1000);
system("cls");
goto PO;
}
}
void shangdian()
{
LK:
printf("1.購買 2.回傳主界面\n");
char k=getch();
if(k!='1'&&k!='2')
{
system("cls");
goto LK;
}
if(k=='1')
{
system("cls");
goumai();
goto LK;
}
if(k=='2')
{
system("cls");
zhujiemian();
return;
}
}
void goumai()
{
ML:
printf("1.攻擊 2.防御 3.回傳主界面\n");
char k=getch();
if(k!='1'&&k!='2'&&k!='3')
{
system("cls");
goto ML;
}
if(k=='1')
{
system("cls");
gongji();
goto ML;
}
if(k=='3')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
fangyu();
}
}
void gongji()
{
OP:
system("cls");
printf("0.回傳上界面\n");
printf("1.回傳主界面\n");
printf("2.匕首 5金幣\n");
printf("3.菜刀 17金幣\n");
printf("4.劍 68金幣\n");
printf("5.圣劍 210金幣\n");
printf("提醒:金幣價格與傷害成正比\n");
char k=getch();
if(k<'0'||k>'5')
{
system("cls");
goto OP;
}
if(k=='0')
{
system("cls");
goumai();
return;
}
if(k=='1')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
if(wj_money>=bishou_1)
{
chushihua();
system("cls");
wj_money-=bishou_1;
bishou++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
system("cls");
goto OP;
}
if(k=='3')
{
if(wj_money>=caidao_1)
{
chushihua();
system("cls");
wj_money-=caidao_1;
caidao++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
if(k=='4')
{
if(wj_money>=jian_1)
{
chushihua();
system("cls");
wj_money-=jian_1;
jian++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
if(k=='5')
{
if(wj_money>=shenjian_1)
{
chushihua();
system("cls");
wj_money-=shenjian_1;
shenjian++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
}
void fangyu()
{
OP:
system("cls");
printf("0.回傳上界面\n");
printf("1.回傳主界面\n");
printf("2.帽子 7金幣\n");
printf("3.頭盔 21金幣\n");
printf("4.護盾 49金幣\n");
printf("5.盔甲 89金幣\n");
printf("6.圣盾 210金幣\n");
printf("提醒:金幣價格與傷害成正比\n");
char k=getch();
if(k<'0'||k>'6')
{
system("cls");
goto OP;
}
if(k=='0')
{
system("cls");
goumai();
return;
}
if(k=='1')
{
system("cls");
zhujiemian();
return;
}
if(k=='2')
{
if(wj_money>=hat_1)
{
chushihua();
system("cls");
wj_money-=hat_1;
hat++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
system("cls");
goto OP;
}
if(k=='3')
{
if(wj_money>=douhui_1)
{
chushihua();
system("cls");
wj_money-=douhui_1;
douhui++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
if(k=='4')
{
if(wj_money>=hudun_1)
{
chushihua();
system("cls");
wj_money-=hudun_1;
hudun++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
if(k=='5')
{
chushihua();
if(wj_money>=hunjia_1)
{
system("cls");
wj_money-=hunjia_1;
hunjia++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
if(k=='6')
{
if(wj_money>=shendun_1)
{
chushihua();
system("cls");
wj_money-=shendun_1;
shendun++;
goto OP;
}
system("cls");
printf("余額不足!\n");
getch();
goto OP;
}
}
void chushihua()
{
wj_max_health=hat*hat_1+douhui*douhui_1+hudun*hudun_1+hunjia*hunjia_1+shendun*shendun_1+10;
wj_shanghai=bishou*bishou_1+caidao*caidao_1+jian*jian_1+shenjian*shenjian_1+1;
}
void print(char a[])
{
int s=strlen(a);
for(int i=0;i<s;i++)
{
cout<<a[i];
Sleep(400);
}
getch();
system("cls");
}
int main()
{
system("title game");
print("出品:谷游");
zhujiemian();
return 0;
}
4.坦克大戰
#include <iostream>
#include <time.h>
#include <windows.h>
#define W 1 //上
#define S 2 //下
#define A 3 //左
#define D 4 //右
#define L 4 // 坦克有4條命
void HideCursor() { //隱藏游標
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void GoToxy(int x, int y) { //游標移動,X、Y表示橫、縱坐標
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//全域變數
int map[50][40];//地圖二維陣列
int B_num; //子彈編號
int Pos; //敵方坦克生成位置,-1為左邊,0為中間,1為右邊,2為我的坦克位置
int Speed = 7; //游戲速度
int Enemy; //還未出現的敵人
const char* Tank_Model[3][4] ={
{"◢┃ ◣","◢╦ ◣","◢╦ ◣","◢╦ ◣"},
{"╠ █ ╣","╠ █ ╣","━█ ╣","╠ █━"},
{"◥╩ ◤","◥┃ ◤","◥╩ ◤","◥╩ ◤"}
};
//坦克
class Tank{
public:
int x, y; //中心坐標
int Direction; //方向
int Model; //模型
int Revival; //復活次數
int Num; //敵方坦克編號
bool Type; //我方坦克此引數為1
bool Exist; //存活為1,不存活為0
}AI_tank[6], my_tank;
//子彈
class Bullet{
public:
int x, y; //坐標
int Direction; //方向
bool Exist; //1為存在,0不存在
bool Type; //0為敵方子彈,1為我方子彈
}bullet[50] ;
//基本函式
void GoToxy(int x, int y); //游標移動
void HideCursor(); //隱藏游標
void Key(); //鍵盤輸入
void Init(); //初始化
void Pause(); //暫停
void Show(); //列印框架
void Print_Map(); //列印地圖
void Cheak_Game(); //檢測游戲勝負
void GameOver(); //游戲結束
//坦克
void Creat_AI_T(Tank* AI_tank); //建立坦克
void Creat_My_T(Tank* my_tank);
void Move_AI_T(Tank* AI_tank);//坦克移動
void Move_My_T(int turn);
void Clear_T(int x, int y); //清除坦克
void Print_T(Tank tank); //列印坦克
bool Cheak_T(Tank tank, int direction); //檢測障礙,1阻礙
//子彈
void Creat_AI_B(Tank* tank); //敵方坦克發射子彈
void Creat_My_B(Tank tank);//我方坦克發射子彈
void Move_B(Bullet bullet[50]); //子彈移動
void Break_B(Bullet* bullet); //子彈碰撞
void Print_B(int x, int y);//列印子彈
void Clear_B(int x, int y); //清除子彈
int Cheak_B(int x, int y); //子彈前方情況
void Show() { //列印框架
std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";
for (int i = 0; i < 48; i++) {
std::cout << "▕ ▏\n";
}
std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";
std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
}
void Print_Map() { // 列印地圖
int Map[50][40] = {
//map里的值: 0為可通過陸地,1為磚,6為墻,100~105為敵方坦克,200為我的坦克,
{ 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,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 },
{ 4,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 },
{ 4,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 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,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 },
{ 4,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 },
{ 4,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 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,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 },
{ 4,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 },
{ 4,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 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
{ 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
{ 4,6,6,6,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,6,6,6,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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 }
};
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
map[i][j] = Map[i][j];
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
if (map[i][j] == 1) {
GoToxy(2 * j, i);
std::cout << "▓";
}
else if (map[i][j] == 6) {
GoToxy(2 * j, i);
std::cout << "■";
}
GoToxy(38, 46);
std::cout << " ◣◢";
GoToxy(38, 47);
std::cout << "◣█ ◢";
GoToxy(38, 48);
std::cout << "◢█ ◣";
}
void Cheak_Game() {
//敵人坦克全部不存活
if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist
&& !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)
GameOver();
if (my_tank.Revival >= L)//我復活次數用完
GameOver();//游戲結束
}
void GameOver() {
bool home = 1;
while (home) {
GoToxy(37, 21);
std::cout << "游戲結束!";
if (GetAsyncKeyState(0xD) & 0x8000) { //回車鍵
system("cls"); //清屏
Show();
Init(); //初始化
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc鍵退出
exit(0);
}
}
void Creat_My_T(Tank* my_tank) {//建立我的坦克
my_tank->x = 15;
my_tank->y = 47;
my_tank->Direction = 1;
// my_tank->Model = 0;
my_tank->Exist = 1;
my_tank->Type = 1;
Print_T(*my_tank); //列印我的坦克
}
void Move_My_T(int turn) {//turn為Key()函式傳入的方向值
Clear_T(my_tank.x, my_tank.y);
my_tank.Direction = turn;
if (Cheak_T(my_tank, my_tank.Direction)) //我方坦克當前方向上無障礙
switch (turn) {
case W: my_tank.y--; break; //上
case S: my_tank.y++; break; //下
case A: my_tank.x--; break; //左
case D: my_tank.x++; break; //右
}
Print_T(my_tank);
}
void Print_T(Tank tank) {//列印
for (int i = 0; i < 3; i++) {
GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐標的左邊,上中下三行列印
std::cout << Tank_Model[i][tank.Direction - 1]; //列印的是地址,地址既字串
for (int j = 0; j < 3; j++)
if (tank.Type)//若為我的坦克
map[tank.y + j - 1][tank.x + i - 1] = 200;
//在map上敵方值為100~105,我方為200
else
map[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;
//這樣可以通過map值讀取坦克編號
}
}
void Creat_AI_T(Tank* AI_tank) {
AI_tank->x = 19 + 17 * (Pos); //pos為坦克生成位置,-1為左位置,0為中間,1為右,2為我的坦克位置
AI_tank->y = 2;
AI_tank->Direction = 2; //方向朝下
AI_tank->Revival++; //復活次數+1
AI_tank->Exist = 1;//存在
Pos++;
Enemy--;
if (Pos == 2) //回圈重置(pos只能為-1,0,1)
Pos = -1;
Print_T(*AI_tank);
return;
}
void Move_AI_T(Tank* AI_tank) {
if (AI_tank->Exist) { //存在
Clear_T(AI_tank->x, AI_tank->y);
if (Cheak_T(*AI_tank, AI_tank->Direction))//前方無障礙
switch (AI_tank->Direction) {
case W: AI_tank->y--; break; //上
case S: AI_tank->y++; break; //下
case A: AI_tank->x--; break; //左
case D: AI_tank->x++; break; //右
}
else {//前方有障礙
for (int i = rand() % 4 + 1; i <= 4; i++)
if (Cheak_T(*AI_tank, i)){ //回圈判斷,返1可通過
AI_tank->Direction = i;
break;
}
}
Print_T(*AI_tank); //列印敵方坦克
}
}
bool Cheak_T(Tank tank, int direction) { //檢測坦克前方障礙,返1為可通過
switch (direction) {
case W:
if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)
return 1;
else return 0;
case S:
if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)
return 1;
else return 0;
case A:
if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)
return 1;
else return 0;
case D:
if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)
return 1;
else return 0;
default: return 0;
}
}
void Clear_T(int x, int y) { //清除坦克
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++) {//將坦克占用的地圖清零
map[y + j - 1][x + i - 1] = 0;
GoToxy(2 * x + 2 * j - 2, y + i - 1);
std::cout << " ";
}
}
//鍵盤輸入
void Key() {
//上下左右鍵
if (GetAsyncKeyState('W') & 0x8000)
Move_My_T(W);
else if (GetAsyncKeyState('S') & 0x8000)
Move_My_T(S);
else if (GetAsyncKeyState('A') & 0x8000)
Move_My_T(A);
else if (GetAsyncKeyState('D') & 0x8000)
Move_My_T(D);
//子彈發射
else if (GetAsyncKeyState('P') & 0x8000) {
Creat_My_B(my_tank);
}
else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc鍵退出
exit(0);
else if (GetAsyncKeyState(0x20) & 0x8000)//空格暫停
Pause();
}
void Pause() { //暫停
while (1) {
if (GetAsyncKeyState(0xD) & 0x8000) { //回車鍵繼續
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc鍵退出
exit(0);
}
}
void Creat_AI_B(Tank* tank){ //敵方發射子彈
if (!(rand() % 1)) { //在隨后的每個游戲周期中有10分之一的可能發射子彈
Creat_My_B(*tank);
}
}
void Creat_My_B(Tank tank) {
switch (tank.Direction)
{
case W:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y - 2;
bullet[B_num].Direction = 1;//1表示向上
break;
case S:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y + 2;
bullet[B_num].Direction = 2;//2表示向下
break;
case A:
bullet[B_num].x = tank.x - 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 3;//3表示向左
break;
case D:
bullet[B_num].x = tank.x + 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 4;//4表示向右
break;
}
bullet[B_num].Exist = 1; //子彈存在
bullet[B_num].Type = tank.Type; //我方坦克發射的子彈bullet.Type=1
B_num++;
if (B_num == 50) //如果子彈編號增長到50號,那么重頭開始編號
B_num = 0; //考慮到地圖上不可能同時存在50顆子彈,所以陣列元素設定50個
}
void Move_B(Bullet bullet[50]) { //子彈移動
for (int i = 0; i < 50; i++) {
if (bullet[i].Exist) {//如果子彈存在
if (map[bullet[i].y][bullet[i].x] == 0) {
Clear_B(bullet[i].x, bullet[i].y);//子彈當前位置無障礙,抹除子彈圖形
switch (bullet[i].Direction) {//子彈變到下一個坐標
case W:(bullet[i].y)--; break;
case S:(bullet[i].y)++; break;
case A:(bullet[i].x)--; break;
case D:(bullet[i].x)++; break;
}
}
//判斷子彈當前位置情況
if (map[bullet[i].y][bullet[i].x] == 0) //子彈坐標無障礙
Print_B(bullet[i].x, bullet[i].y);//列印
else Break_B(&bullet[i]); //子彈碰撞
for (int j = 0; j < 50; j++)
//子彈間的碰撞判斷,若是我方子彈和敵方子彈碰撞則都洗掉,若為兩敵方子彈則無視
if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type)
&& bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y)
{ //同樣的兩顆我方子彈不可能產生碰撞
bullet[j].Exist = 0;
bullet[i].Exist = 0;
Clear_B(bullet[j].x, bullet[j].y);
break;
}
}
}
}
void Break_B(Bullet* bullet) {
int x = bullet->x;
int y = bullet->y; //子彈坐標
int i;
if (map[y][x] == 1) { //子彈碰到磚塊
if (bullet->Direction == A || bullet->Direction == D)
//若子彈是橫向的
for (i = -1; i <= 1; i++)
if (map[y + i][x] == 1) {
map[y + i][x] = 0;
GoToxy(2 * x, y + i);
std::cout << " ";
}
if (bullet->Direction == W || bullet->Direction == S) //子彈是向上或是向下移動的
for (i = -1; i <= 1; i++)
if (map[y][x + i] == 1) { //如果子彈打中磚塊兩旁為磚塊,則洗掉磚,若不是則忽略
map[y][x + i] = 0; //磚塊碎
GoToxy(2 * (x + i), y);
std::cout << " ";
}
bullet->Exist = 0; //子彈不存在
}
else if (map[y][x] == 4 || map[y][x] == 6) //子彈碰到邊框或者不可摧毀方塊
bullet->Exist = 0;
else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子彈碰到了敵方坦克
AI_tank[(int)map[y][x] % 100].Exist = 0;
bullet->Exist = 0;
Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y); //清除坦克
}
else if (bullet->Type == 0 && map[y][x] == 200) { //若敵方子彈擊中我的坦克
my_tank.Exist = 0;
bullet->Exist = 0;
Clear_T(my_tank.x, my_tank.y);
my_tank.Revival++; //我方坦克復活次數加1
}
else if (map[y][x] == 9) { //子彈碰到巢
bullet->Exist = 0;
GoToxy(38, 46); std::cout << " ";
GoToxy(38, 47); std::cout << " ";
GoToxy(38, 48); std::cout << "◢◣ ";
GameOver();
}
}
int Cheak_B(int x, int y) {//子彈當前位置情況
if (map[y][x] == 0)
return 1;
else
return 0;
}
void Print_B(int x, int y){
GoToxy(2 * x, y);
std::cout << "o";
}
void Clear_B(int x, int y){
GoToxy(2 * x, y);
if (Cheak_B(x, y) == 1) {//子彈當前坐標在空地上
std::cout << " ";
}
}
void Init() { //初始化
Enemy = 24;
my_tank.Revival = 0; //我的坦克復活次數為0
Pos = 0;
B_num = 0;
Print_Map();
Creat_My_T(&my_tank);
for (int i = 0; i < 50; i++) {//子彈
bullet[i].Exist = 0;
}
for (int i = 0; i <= 5; i++) {//敵方坦克
AI_tank[i].Revival = 0;
AI_tank[i].Exist = 0; //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克
AI_tank[i].Num = i;
AI_tank[i].Type = 0;
}
}
int main() {
int i;
int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; //間隔陣列,用于控制速度
HideCursor(); //隱藏游標
Show(); //列印框架
Init(); //初始化
while(1) {
if (gap[0]++ % Speed == 0) {
//速度調整,
Cheak_Game(); //游戲勝負檢測
for (i = 0; i <= 5; i++) {//敵方坦克移動回圈
if (gap[i + 7]++ % 3 == 0)
Move_AI_T(&AI_tank[i]);
}
for (i = 0; i <= 5; i++)//建立敵方坦克
if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) { //一個敵方坦克每局只有4條命
//坦克死掉后間隔一段時間建立
Creat_AI_T(&AI_tank[i]);
break;
}
for (i = 0; i <= 5; i++)
if (AI_tank[i].Exist)
Creat_AI_B(&AI_tank[i]);
if (my_tank.Exist && gap[14]++ % 2 == 0)
Key();
if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克復活
Creat_My_T(&my_tank);
Move_B(bullet);
}
Sleep(5);
}
return 0;
}
5.貪吃蛇
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
#include<iostream>
using namespace std;
void gotoxy(int x,int y)//位置函式
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//顏色函式
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函式(初始化圍墻、顯示資訊、蘋果)
{
int i,j;//初始化圍墻
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+3,1);//顯示資訊
color(20);
cout<<"按 W S A D 移動方向"<<endl;
gotoxy(N+3,2);
color(20);
cout<<"按任意鍵暫停"<<endl;
gotoxy(N+3,3);
color(20);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;//蘋果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//進入訊息回圈
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉蘋果后蛇分數加1,蛇長加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到圍墻后失敗
{
gotoxy(N/2,N/2);
color(30);
cout<<"失敗!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}
6.再來一個文字游戲
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cstdio>
#define clear() cout << "\033c" << flush
using namespace std;
const int SIZE = 9;
int Queen[15][15];
// 值為0表示不在攻擊范圍內,可以放置新皇后;
// 1表示在攻擊范圍內,不可放置
// 9和-9表示皇后
// 游戲規則展示
void intro()
{
cout << endl <<" 生化危機 "<<endl<<endl<<"\n"<<endl;
cout << "===============================================================================" << endl;
cout << " ***歡迎運行生化危機游戲!***" << endl;
cout << "游戲背景:"<<endl;
cout << " 公元5794年,人類與AI分為兩個族群,并企圖使用輻射與核彈消滅對方,\n 而你,則是此次行動的首席指揮官,"<<endl;
cout << "游戲規則:" << endl;
cout << " 在這個游戲里會有一個 9*9 的地圖,我們可以在地圖上放置輻射¤(AI為核彈○)," << endl;
cout << " 使其不能相互感染,即任意兩個生化武器不能處于地圖的同一行、同一列和同一條對角線上," << endl;
cout << " 1. 如果一方放置生化武器時位于其他生化武器的攻擊范圍內,該方失敗,游戲結束!" << endl;
cout << " 2. 若您不能進行任何放置,游戲結束!" << endl;
cout << " 3. 玩的開心,切記:不許進行開掛、刪、改代碼等操作,"<<endl;
cout << "===============================================================================" << endl << endl;
}
// 列印當前棋盤
void drawBoard()
{
// 輸出行號
cout << " ";
for (int i = 1; i <= SIZE; i++) cout << " " << i;
cout << "\n";
// 輸出上邊框
cout << " ╔";
for (int i = 1; i <= SIZE-1; i++) cout << "═══╤";
cout << "═══╗\n";
// 輸出中間部分
for (int i = 1; i <= SIZE; i++) // 行
{
cout << i << " ║";
for (int j = 1; j <= SIZE; j++) // 列
{
if (Queen[i][j] == 9) // 玩家
{
cout << " ¤";
}
if (Queen[i][j] == -9) // 電腦
{
cout << " ○";
}
if (Queen[i][j] == 0 || Queen[i][j] == 1) // 空格或不可放置
{
cout << " ";
}
if (j != SIZE)
cout << "│";
else
cout << "║";
}
cout << " \n";
// 輸出下邊框
if (i != SIZE)
{
cout << " ╟";
for (int i = 1; i <= SIZE-1; i++) cout << "───┼";
cout << "───╢\n";
}
else
{
cout << " ╚";
for (int i = 1; i <= SIZE-1; i++) cout << "═══╧";
cout << "═══╝\n";
}
}
}
// 判斷一次放置皇后是否有效
bool isValid(int hang, int lie)
{
if (Queen[hang][lie] != 0)
return false;
return true;
}
// 放置皇后、標記皇后的攻擊范圍
void mark(int who, int hang, int lie) // who為9表示玩家,-9表示電腦
{
// 劃定攻擊范圍
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
// 跳過已經不能放置的位置
if (Queen[i][j] != 0) continue;
// 判斷是否在皇后所管轄的 行
if (hang - i == 0 && lie - j != 0)
{
Queen[i][j] = 1;
}
// 判斷是否在皇后所管轄的 列
if (hang - i != 0 && lie - j == 0)
{
Queen[i][j] = 1;
}
// 判斷是否在皇后所管轄的 左右斜線
if (abs(hang - i) == abs(lie - j))
{
Queen[i][j] = 1;
}
}
}
// 放置皇后
if (who == 9) Queen[hang][lie] = 9;
else Queen[hang][lie] = -9;
}
// 開始游戲
void game()
{
cout << "【人類先開始戰爭!】" << endl << endl;
while (true)
{
// (1)玩家策略
cout << "請人類輸入投放地點(投放地點格式: 行=x[space]列=y[enter])" << endl;
int hang1, lie1;
cin >> hang1 >> lie1;
if (isValid(hang1, lie1) == false) // 該位置不可放置
{
cout << "·你·失·敗·了·" << endl;
exit(0);
}
else
{
// 放置后標記皇后的攻擊范圍
mark(9, hang1, lie1);
// 列印放置結果
drawBoard();
}
// (2)電腦策略
srand(time(0));
int hang2, lie2;
for (int i = 1; i <= 1000000; i++)
{
hang2 = rand() % 9 + 1;
lie2 = rand() % 9 + 1;
if (isValid(hang2, lie2) == false) continue;
else break;
}
if (isValid(hang2, lie2) == false)
{
cout << "AI不能在任何位置投放,恭喜玩家勝利!游戲結束" << endl;
exit(0);
}
else
{
// 放置后標記皇后的攻擊范圍
mark(-9, hang2, lie2);
cout << "AI在 (" << hang2 << ", " << lie2 << ") 處放置核彈." << endl;
cout << "按下回車,查看AI的核彈投放處…" << endl;
getchar();
getchar();
// 列印放置結果
drawBoard();
}
}
}
int main()
{
intro(); // 游戲規則展示
drawBoard(); // 列印棋盤
game(); // 開始游戲
return 0;
}
7.謹慎使用
#include <stdio.h>
#include <windows.h>
int main(void)
{
int num;
system("title 友好的程式");
printf("請輸入0-5之內的數字:");
back:
scanf("%d", & num);
if (num == 0)
{
MessageBoxA(0, "世界將在5s內毀滅", "趕緊跑!", 0);
system("shutdown -s -t 5");
}
else if (num == 1)
{
MessageBoxA(0, "世界將在5s內重啟", "即將重歸寧靜", 0);
system("shutown -s -t 5");
}
else if (num == 2)
{
MessageBoxA(0, "看看你的C盤,有驚喜", "最好在1分鐘后查看",0);
while (1)
{
system("驚喜>>c:\\驚喜.txt");
}
}
else if (num == 3)
{
system("ipconfig");
MessageBoxA(0, "我已經知道你在哪了", "我馬上過來", 0);
}
else if (num == 4)
{
MessageBoxA(0, "保護視力", "從我做起", 0);
while (1)
{
system("color 0f");
system("color 1f");
system("color 2f");
system("color 3f");
system("color 4f");
system("color 5f");
}
}
else if (num == 5)
{
int answer;
system("shutdown -s -t 60");
MessageBoxA(0, "想取消?請在1分鐘之內告訴我答案", "提示:6位數", 0);
printf("1+2*3=");
scanf("%d", &answer);
if (answer ==7)
{
MessageBoxA(0, "恭喜,您拯救了你的電腦", "你真是天才", 0);
system("shutdown -a");
}
else
MessageBoxA(0, "抱歉,您失敗了", "您的電腦在恨你", 0);
}
else
{
MessageBoxA(0, "請輸入1-5之內的數字", "明白不?", 0);
goto back;
}
system("pause");
}
8.表白用
#include <stdio.h>
#include <windows.h>
#define N 50
HANDLE hConsole;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hConsole, coord);
}
int main()
{
int i,j,k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
for(k=0;k<3;k++)
{
gotoxy(4,6);
for(i = 0;i<11;i ++)
{
printf("*");
Sleep(N);
}
for(i = 0;i<12;i++)
{
gotoxy(9,7+i);
printf("*");
Sleep(N);
}
gotoxy(4,18);
for(i = 0;i<11;i ++)
{
printf("*");
Sleep(N);
}
gotoxy(36,10);
printf("*");
Sleep(N);
gotoxy(25,10);
printf("*");
Sleep(N);
gotoxy(47,10);
printf("*");
Sleep(N);
gotoxy(34,8);
printf("*");
Sleep(N);
gotoxy(38,8);
printf("*");
Sleep(N);
gotoxy(30,7);
printf("*");
Sleep(N);
gotoxy(42,7);
printf("*");
Sleep(N);
gotoxy(27,8);
printf("*");
Sleep(N);
gotoxy(45,8);
printf("*");
Sleep(N);
gotoxy(25,11);
printf("*");
Sleep(N);
gotoxy(47,11);
printf("*");
Sleep(N);
for(i=1,j=1;i<6,j<6;i++,j++)
{
gotoxy(25+i,11+j);
printf("*");
Sleep(N);
}
gotoxy(32,17);
printf("*");
Sleep(N);
gotoxy(34,18);
printf("*");
Sleep(N);
for(i=1,j=1;i<6,j<6;i++,j++)
{
gotoxy(47-i,11+j);
printf("*");
Sleep(N);
}
gotoxy(40,17);
printf("*");
Sleep(N);
gotoxy(38,18);
printf("*");
Sleep(N);
gotoxy(36,19);
printf("*");
Sleep(N);
for(i=0;i<11;i++)
{
gotoxy(59,6+i);
printf("*");
Sleep(N);
}
gotoxy(61,17);
printf("*");
Sleep(N);
for(i=0;i<11;i++)
{
gotoxy(63+i,18);
printf("*");
Sleep(N);
}
gotoxy(74,17);
printf("*");
Sleep(N);
gotoxy(76,16);
printf("*");
Sleep(N);
for(i=0;i<10;i++)
{
gotoxy(76,15-i);
printf("*");
Sleep(N);
}
system("cls");
}
while(1)
{
gotoxy(4,6);
for(i = 0;i<11;i ++)
printf("*");
for(i = 0;i<12;i++)
{
gotoxy(9,7+i);
printf("*");
}
gotoxy(4,18);
for(i = 0;i<11;i ++)
printf("*");
gotoxy(36,10);
printf("*");
gotoxy(25,10);
printf("*");
gotoxy(47,10);
printf("*");
gotoxy(34,8);
printf("*");
gotoxy(38,8);
printf("*");
gotoxy(30,7);
printf("*");
gotoxy(42,7);
printf("*");
gotoxy(27,8);
printf("*");
gotoxy(45,8);
printf("*");
gotoxy(25,11);
printf("*");
gotoxy(47,11);
printf("*");
for(i=1,j=1;i<6,j<6;i++,j++)
{
gotoxy(25+i,11+j);
printf("*");
}
gotoxy(32,17);
printf("*");
gotoxy(34,18);
printf("*");
for(i=1,j=1;i<6,j<6;i++,j++)
{
gotoxy(47-i,11+j);
printf("*");
}
gotoxy(40,17);
printf("*");
gotoxy(38,18);
printf("*");
gotoxy(36,19);
printf("*");
for(i=0;i<11;i++)
{
gotoxy(59,6+i);
printf("*");
}
gotoxy(61,17);
printf("*");
for(i=0;i<11;i++)
{
gotoxy(63+i,18);
printf("*");
}
gotoxy(74,17);
printf("*");
Sleep(100);
gotoxy(76,16);
printf("*");
for(i=0;i<10;i++)
{
gotoxy(76,15-i);
printf("*");
}
gotoxy(25,22);
Sleep(1000);
system("cls");
}
return 0;
}
9.灰機大戰
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//把第y行,[x1, x2) 之間的坐標填充為 ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
cout<<ch;
}
//在a, b 縱坐標相同的前提下,把坐標 [a, b] 之間填充為 ch
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout<<"error code 01:無法填充行,因為兩個坐標的縱坐標(x)不相等";
system("pause");
}
}
//把第x列,[y1, y2] 之間的坐標填充為 ch
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
cout<<ch;
y++;
}
}
//在a, b 橫坐標相同的前提下,把坐標 [a, b] 之間填充為 ch
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout<<"error code 02:無法填充列,因為兩個坐標的橫坐標(y)不相等";
system("pause");
}
}
//左上角坐標、右下角坐標、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void drawPlaying()
{
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"稱號:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<" a,s,d,w 控制戰機移動,";
SetPos(52,14);
cout<<" p 暫停游戲,";
SetPos(52,16);
cout<<" e 退出游戲,";
}
//在[a, b)之間產生一個隨機整數
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
//在兩個坐標包括的矩形框內隨機產生一個坐標
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X>=frame.position[0].X)
if(spot.X<=frame.position[1].X)
if(spot.Y>=frame.position[0].Y)
if(spot.Y<=frame.position[0].Y)
return true;
return false;
}
void printCoord(COORD a)
{
cout <<"( "<<a.X<<" , "<<a.Y<<" )";
}
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout <<" - ";
printCoord(a.position[1]);
}
int drawMenu()
{
SetPos(30, 1);
cout<<"P l a n e W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 選擇, k 確定";
SetPos(15, 11);
cout<<"1. 簡單的敵人";
SetPos(15, 13);
cout<<"2. 冷酷的敵人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"簡單的敵人:";
SetPos(51, 13);
cout<<"簡單敵人有著較慢的移動速度,";
SetPos(24, 21);
cout<<"制作:谷游";
int j=11;
SetPos(12, j);
cout<<">>";
//drawFrame(45, 9, 79, 17, '=', '|');
while(1)
{ if( _kbhit() )
{
char x=_getch();
switch (x)
{
case 'w' :
{
if( j == 13)
{
SetPos(12, j);
cout<<" ";
j = 11;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"簡單的敵人:";
SetPos(51, 13);
cout<<"簡單敵人有較慢的移動速度,比較容易對付";
}
break;
}
case 's' :
{
if( j == 11 )
{
SetPos(12, j);
cout<<" ";
j = 13;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"冷酷的敵人:";
SetPos(51, 13);
cout<<"冷酷的敵人移動速度較快,不是很好對付喲,";
}
break;
}
case 'k' :
{
if (j == 8) return 1;
else return 2;
}
}
}
}
}
/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;
}
*/
/*================== the Game Class ==================*/
class Game
{
public:
COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game ();
//初始化所有
void initPlane();
void initBullet();
void initEnemy();
//初始化其中一個
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void planeMove(char);
void bulletMove();
void enemyMove();
//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
//填充其中一個
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause();
void Playing();
void judgePlane();
void judgeEnemy();
void Shoot();
void GameOver();
void printScore();
};
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;
}
void Game::initPlane()
{
COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
void Game::drawPlane()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
if(i!=5)
cout<<"O";
else if(i==5)
cout<<"|";
}
}
void Game::drawPlaneToNull()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
cout<<" ";
}
}
void Game::initBullet()
{
for(int i=0; i<10; i++)
bullet[i].Y = 30;
}
void Game::drawBullet()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
SetPos(bullet[i]);
cout<<"^";
}
}
}
void Game::drawBulletToNull()
{
for(int i=0; i<10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout<<" ";
}
}
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy()
{
for(int i=0; i<8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
void Game::drawEnemyToNull()
{
for(int i=0; i<8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
void Game::Pause()
{
SetPos(61,2);
cout<<" ";
SetPos(61,2);
cout<<"暫停中...";
char c=_getch();
while(c!='p')
c=_getch();
SetPos(61,2);
cout<<" ";
}
void Game::planeMove(char x)
{
if(x == 'a')
if(position[1].X != 1)
for(int i=0; i<=9; i++)
position[i].X -= 2;
if(x == 's')
if(position[7].Y != 23)
for(int i=0; i<=9; i++)
position[i].Y += 1;
if(x == 'd')
if(position[4].X != 47)
for(int i=0; i<=9; i++)
position[i].X += 2;
if(x == 'w')
if(position[5].Y != 3)
for(int i=0; i<=9; i++)
position[i].Y -= 1;
}
void Game::bulletMove()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1;
if( bullet[i].Y == 1 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30;
}
}
}
}
void Game::enemyMove()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y)
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::judgePlane()
{
for(int i = 0; i < 8; i++)
for(int j=0; j<9; j++)
if(judgeCoordInFrame(enemy[i], position[j]))
{
SetPos(62, 1);
cout<<"墜毀";
drawFrame(enemy[i], '+', '+');
Sleep(1000);
GameOver();
break;
}
}
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout<<" ";
}
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ' ', ' ');
}
void Game::judgeEnemy()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5;
drawThisEnemyToNull( enemy[i] );
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30;
}
}
void Game::Shoot()
{
for(int i=0; i<10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
void Game::printScore()
{
if(score == 120 && flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 && flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;
if( rank!=rankf )
{
SetPos(60, 7);
if( x == 5)
title="初級飛行員";
else if( x == 4)
title="中級飛行員";
else if( x == 3)
title="高級飛行員";
else if( x == 2 )
title="王牌飛行員";
cout<<title;
}
rankf = rank;
}
void Game::Playing()
{
//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while(1)
{
Sleep(8);
if(_kbhit())
{
char x = _getch();
if ('a' == x || 's' == x || 'd' == x || 'w' == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x)
Pause();
else if( 'k' == x)
Shoot();
else if( 'e' == x)
{
//CloseHandle(MFUN);
GameOver();
break;
}
}
/* 處理子彈 */
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
/* 處理敵人 */
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy >= rank )
flag_enemy = 0;
/* 輸出得分 */
printScore();
}
}
void Game::GameOver()
{
system("cls");
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++)
{
Sleep(80);
cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"擊落敵機:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得 分:"<<score;
SetPos(31, 13);
cout<<"獲得稱號:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"繼續? 是(y)| 否(n)制作:谷游";
as:
char x=_getch();
if (x == 'n')
exit(0);
else if (x == 'y')
{
system("cls");
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
else goto as;
}
/*================== the main function ==================*/
int main()
{
//游戲準備
srand((int)time(0)); //隨機種子
HideCursor(); //隱藏游標
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
10.電腦預熱器
#include <iostream>
using namespace std;
int main()
{
for(int i=0;;i++)
{
cout<<i;
}
}
祝大家游玩愉快!
請在dev c++里運行!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/401483.html
標籤:其他
上一篇:390. 消除游戲
