栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

10段代码教你玩转C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

10段代码教你玩转C++

1.随机迷宫:

#include 

#include 

#include 

#include 

#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) //移动坐标

{



COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );

}

void hidden()//隐藏光标

{



HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);



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  
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有一家药店"<>xiaozhen; 
while(xiaozhen!=5)//输入5时退出游戏 
{ 
if(shengmingli<=0)//主角生命力小于等于0时游戏结束 
{ 
cout<<"你死啦!"<>xiaozhen; 
} 
if(xiaozhen==5) 
{ 
cout<<"正在退出游戏……"<>wuqidian; 
while(wuqidian!=7)//输入7时结束函数 
{ 
switch(wuqidian) 
{ 
case 1 : if(money<10) 
cout<<"你的钱不够"<>wuqidian; 
} 
if(wuqidian==7) 
{	  //返回main()主函数 
cout<<"欢迎下次再来!"<>yaodian; 
while(yaodian!=4) 
{ 
switch(yaodian) 
{ 
case 1 : if(money<10) 
cout<<"你的钱不够"<>yaodian; 
} 
if(yaodian==4) 
{	   
cout<<"欢迎下次再来!"<0 && shengmingli>0 && (*xuanze)!=2) 
{ 
cout<<"现在是"<<"第"<<*huihe<<"回合!"<>*xuanze; 
switch((*xuanze)) 
{ 
case 1 : cout<<"你对小怪物发动了攻击!"<0 && shengmingli>0 && (*xuanze)!=2) 
{ 
cout<<"现在是"<<"第"<<*huihe<<"回合!"<>*xuanze; 
switch((*xuanze)) 
{ 
case 1 : cout<<"你对大怪物发动了攻击!"< 

3.另一个文字游戏

#include
#include
#include
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("你当前剩余血量:%lfn",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("对手当前剩余血量:%lfnn",boss);
        if(boss<=0)
        {
            printf("胜利!n获得%lf金币nn当前剩余血量:%lfn",shanghai[k]+3,wj_health);
            wj_money+=shanghai[k]+3;
            printf("n余额:%lfn",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("生命值:%lfn",wj_health);
        printf("最大生命值:%lfn",wj_max_health);
        printf("攻击力:%lfn",wj_shanghai);
        printf("金币:%lfn",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 

4.坦克大战

#include 
#include  
#include 


#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,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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
#include
#include
#include
#define N 21
#include
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;i0;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<<"★"< 

6.再来一个文字游戏

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define clear() cout << "33c" << flush
using namespace std;

const int SIZE = 9;
int Queen[15][15]; 
// 值为0表示不在攻击范围内,可以放置新皇后;
// 1表示在攻击范围内,不可放置
// 9和-9表示皇后

// 游戏规则展示
void intro()
{
	cout << endl <<"                               生化危机 "<> 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 
#include 
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 
#include 
#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
#include
#include
#include
#include
using namespace std;
 

 
typedef struct frame
{
	COORD position[2];
	int flag;
}frame;
 
 

 
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<=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	<<"( "<>";
 
	//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;
					}
			}
		}
	}
}
 

 
 

 
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<= 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 

10.电脑预热器

#include 
using namespace std;
int main()
{
    for(int i=0;;i++)
    {
        cout< 

祝大家游玩愉快!

 请在dev c++里运行!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号