欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言實(shí)現(xiàn)空戰(zhàn)游戲

 更新時(shí)間:2022年05月16日 11:59:27   作者:輝小歌  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)空戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)空戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下

一、項(xiàng)目描述和成果展示

項(xiàng)目描述:   在以往的程序中進(jìn)行了改進(jìn)。
例如:   可以發(fā)射多個(gè)子彈
        可以有多個(gè)敵機(jī)

飛機(jī)大戰(zhàn) 1.0版
飛機(jī)大戰(zhàn) 2.0版

現(xiàn)在這個(gè)版本是3.0版 這個(gè)版本的各個(gè)功能基本完善

效果圖如下:

二、發(fā)射多個(gè)子彈

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>

#define High 25 //游戲的尺寸
#define Width 50

//全局變量
int position_x,position_y;//飛機(jī)的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲(chǔ)游戲畫布中對應(yīng)的元素
//0為空格 1為飛機(jī) ?2為子彈 ?3為敵機(jī)

void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?position_x=High/2;
?? ?position_y=Width/2;
?? ?canvas[position_x][position_y]=1;
}

void show()//顯示畫面
{
?? ?int i,j;
?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫清屏
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("*");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("|");//輸出子彈
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?int i,j;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動(dòng)
?? ??? ??? ?{
?? ??? ??? ??? ?canvas[i][j]=0;
?? ??? ??? ??? ?if(i>0)
?? ??? ??? ??? ??? ?canvas[i-1][j]=2;
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動(dòng)
?? ??? ?if(input=='a'||input=='A')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='d'||input=='D')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y++;//位置右移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='w'||input=='W')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x--;//位置上移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='s'||input=='S')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x++;//位置下移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input==' ')//發(fā)射子彈
?? ??? ?{
?? ??? ??? ?canvas[position_x-1][position_y]=2;
?? ??? ??? ?//發(fā)射子彈的初始位置在飛機(jī)的正上方
?? ??? ?}
?? ?}
}

int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

三、多個(gè)敵機(jī)

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>

#define High 25 //游戲的尺寸
#define Width 50
#define EnemyNum 5//敵機(jī)的個(gè)數(shù)

//全局變量
int position_x,position_y;//飛機(jī)的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機(jī)的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲(chǔ)游戲畫布中對應(yīng)的元素
//0為空格 1為飛機(jī) ?2為子彈 ?3為敵機(jī)
int score;//得分

void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?int k;
?? ?position_x=High-1;
?? ?position_y=Width/2;
?? ?canvas[position_x][position_y]=1;
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?enemy_x[k]=rand()%2;
?? ??? ?enemy_y[k]=rand()%Width;
?? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ?}
?? ?score=0;
}

void show()//顯示畫面
{
?? ?int i,j;
?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫清屏
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("*");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("|");//輸出子彈
?? ??? ??? ?else if(canvas[i][j]==3)
?? ??? ??? ??? ?printf("+");//輸出敵機(jī)
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("得分:%3d\n",score);
?? ?Sleep(20);
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動(dòng)
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機(jī)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分?jǐn)?shù)加一
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動(dòng)
?? ??? ??? ??? ?canvas[i][j]=0;
?? ??? ??? ??? ?if(i>0)
?? ??? ??? ??? ?canvas[i-1][j]=2;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敵機(jī)撞到我機(jī)
?? ??? ?{
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機(jī)跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?score--;//減分
?? ??? ?}
?? ??? ?static int speed=0;
?? ??? ?if(speed<36)
?? ??? ??? ?speed++;
?? ??? ?if(speed==36)
?? ??? ?{
?? ??? ??? ?//敵機(jī)下落
?? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ?{
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ?enemy_x[k]++;
?? ??? ??? ??? ?speed=0;
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動(dòng)
?? ??? ?if(input=='a'||input=='A')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='d'||input=='D')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y++;//位置右移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='w'||input=='W')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x--;//位置上移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='s'||input=='S')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x++;//位置下移
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input==' ')//發(fā)射子彈
?? ??? ?{
?? ??? ??? ?canvas[position_x-1][position_y]=2;
?? ??? ??? ?//發(fā)射子彈的初始位置在飛機(jī)的正上方
?? ??? ?}
?? ?}
}

int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

四、增加子彈升級功能

當(dāng)分?jǐn)?shù)達(dá)到一定的值自動(dòng)升級炮彈,當(dāng)分?jǐn)?shù)下降一定數(shù)值后炮彈下降一個(gè)等級。

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>

#define High 25 //游戲的尺寸
#define Width 40
#define EnemyNum 5//敵機(jī)的個(gè)數(shù)

//全局變量
int position_x,position_y;//飛機(jī)的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機(jī)的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲(chǔ)游戲畫布中對應(yīng)的元素
//0為空格 1為飛機(jī) ?2為子彈 ?3為敵機(jī)

int score;//得分
int BulletWidth;//子彈的寬度
int EnemyMoveSpeed;//敵機(jī)的移動(dòng)速度

void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?int k;
?? ?position_x=High-1;
?? ?position_y=Width/2;
?? ?canvas[position_x][position_y]=1;
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?enemy_x[k]=rand()%2;
?? ??? ?enemy_y[k]=rand()%Width;
?? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ?}
?? ?score=0;
?? ?BulletWidth=0;
?? ?EnemyMoveSpeed=20;
}

void show()//顯示畫面
{
?? ?int i,j;
?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫清屏
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("*");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("|");//輸出子彈
?? ??? ??? ?else if(canvas[i][j]==3)
?? ??? ??? ??? ?printf("+");//輸出敵機(jī)
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("════════════════════════════════════════\n");
?? ?printf("得分:%3d\n",score);
?? ?system("date /t");
?? ?system("time /t");
?? ?Sleep(20);
}

//定義隱藏光標(biāo)函數(shù)
void HideCursor()
{
?? ?CONSOLE_CURSOR_INFO cursor; ? ?
?? ?cursor.bVisible = FALSE; ? ?
?? ?cursor.dwSize = sizeof(cursor); ? ?
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ?
?? ?SetConsoleCursorInfo(handle, &cursor);
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動(dòng)
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機(jī)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分?jǐn)?shù)加一
?? ??? ??? ??? ??? ??? ?if(score%5==0&&EnemyMoveSpeed>3)//達(dá)到一定積分后敵機(jī)變快
?? ??? ??? ??? ??? ??? ??? ?EnemyMoveSpeed--;
?? ??? ??? ??? ??? ??? ?if(score/5)//達(dá)到一定積分后子彈變厲害
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=0;
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=BulletWidth+score/5;
?? ??? ??? ??? ??? ??? ??? ?if(BulletWidth>5)//子彈加到一定威力后,會(huì)固定不變
?? ??? ??? ??? ??? ??? ??? ??? ?BulletWidth=5;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動(dòng)
?? ??? ??? ??? ?canvas[i][j]=0;
?? ??? ??? ??? ?if(i>0)
?? ??? ??? ??? ?canvas[i-1][j]=2;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敵機(jī)撞到我機(jī)
?? ??? ?{
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機(jī)跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?score--;//減分
?? ??? ??? ?if(score%5==0&&score>0)//減的分?jǐn)?shù)過多,子彈威力下降
?? ??? ??? ?{
?? ??? ??? ??? ?BulletWidth--;
?? ??? ??? ??? ?EnemyMoveSpeed++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?static int speed=0;
?? ??? ?if(speed<EnemyMoveSpeed)
?? ??? ??? ?speed++;
?? ??? ?if(speed==EnemyMoveSpeed)
?? ??? ?{
?? ??? ??? ?//敵機(jī)下落
?? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ?{
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ?enemy_x[k]++;
?? ??? ??? ??? ?speed=0;
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動(dòng)
?? ??? ?if(input=='a'||input=='A')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?if(position_y==0)
?? ??? ??? ??? ?position_y++;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='d'||input=='D')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y++;//位置右移
?? ??? ??? ?if(position_y==Width-1)
?? ??? ??? ??? ?position_y--;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='w'||input=='W')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x--;//位置上移
?? ??? ??? ?if(position_x==0)
?? ??? ??? ??? ?position_x++;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='s'||input=='S')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x++;//位置下移
?? ??? ??? ?if(position_x==High)
?? ??? ??? ??? ?position_x--;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input==' ')//發(fā)射子彈
?? ??? ?{
?? ??? ??? ?int left =position_y-BulletWidth;
?? ??? ??? ?int right=position_y+BulletWidth;
?? ??? ??? ?if(left<0)
?? ??? ??? ??? ?left=0;
?? ??? ??? ?if(right>Width-1)
?? ??? ??? ??? ?right=Width-1;
?? ??? ??? ?int k;
?? ??? ??? ?for(k=left;k<=right;k++)//發(fā)射子彈
?? ??? ??? ?canvas[position_x-1][k]=2;
?? ??? ??? ?//發(fā)射子彈的初始位置在飛機(jī)的正上方
?? ??? ?}
?? ?}
}

int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?HideCursor();
?? ?system("title 游戲中");
?? ?system("color 09");
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

五、增加一些小功能

增加一個(gè)主菜單
里面增加了一些小的功能

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>

#define High 25 //游戲的尺寸
#define Width 40
#define EnemyNum 5//敵機(jī)的個(gè)數(shù)

//全局變量
int position_x,position_y;//飛機(jī)的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機(jī)的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲(chǔ)游戲畫布中對應(yīng)的元素
//0為空格 1為飛機(jī) ?2為子彈 ?3為敵機(jī)

int score;//得分
int BulletWidth;//子彈的寬度
int EnemyMoveSpeed;//敵機(jī)的移動(dòng)速度

void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?int k;
?? ?position_x=High-1;
?? ?position_y=Width/2;
?? ?canvas[position_x][position_y]=1;
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?enemy_x[k]=rand()%2;
?? ??? ?enemy_y[k]=rand()%Width;
?? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ?}
?? ?score=0;
?? ?BulletWidth=0;
?? ?EnemyMoveSpeed=20;
}

void show()//顯示畫面
{
?? ?int i,j;
?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫清屏
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("*");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("|");//輸出子彈
?? ??? ??? ?else if(canvas[i][j]==3)
?? ??? ??? ??? ?printf("+");//輸出敵機(jī)
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("════════════════════════════════════════\n");
?? ?printf("得分:%3d\n",score);
?? ?system("date /t");
?? ?system("time /t");
?? ?printf("提示:按 z 鍵回到主菜單");
?? ?Sleep(20);
}

//定義隱藏光標(biāo)函數(shù)
void HideCursor()
{
?? ?CONSOLE_CURSOR_INFO cursor; ? ?
?? ?cursor.bVisible = FALSE; ? ?
?? ?cursor.dwSize = sizeof(cursor); ? ?
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ?
?? ?SetConsoleCursorInfo(handle, &cursor);
}

int updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動(dòng)
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機(jī)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分?jǐn)?shù)加一
?? ??? ??? ??? ??? ??? ?if(score%5==0&&EnemyMoveSpeed>3)//達(dá)到一定積分后敵機(jī)變快
?? ??? ??? ??? ??? ??? ??? ?EnemyMoveSpeed--;
?? ??? ??? ??? ??? ??? ?if(score/5)//達(dá)到一定積分后子彈變厲害
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=0;
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=BulletWidth+score/5;
?? ??? ??? ??? ??? ??? ??? ?if(BulletWidth>5)//子彈加到一定威力后,會(huì)固定不變
?? ??? ??? ??? ??? ??? ??? ??? ?BulletWidth=5;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動(dòng)
?? ??? ??? ??? ?canvas[i][j]=0;
?? ??? ??? ??? ?if(i>0)
?? ??? ??? ??? ?canvas[i-1][j]=2;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?for(k=0;k<EnemyNum;k++)
?? ?{
?? ??? ?if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敵機(jī)撞到我機(jī)
?? ??? ?{
?? ??? ??? ?system("cls");
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?printf("得分:%d\n",score);
?? ??? ??? ?printf("正在返回主菜單\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機(jī)跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產(chǎn)生新的飛機(jī)
?? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?score--;//減分
?? ??? ??? ?if(score%5==0&&score>0)//減的分?jǐn)?shù)過多,子彈威力下降
?? ??? ??? ?{
?? ??? ??? ??? ?BulletWidth--;
?? ??? ??? ??? ?EnemyMoveSpeed++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?static int speed=0;
?? ??? ?if(speed<EnemyMoveSpeed)
?? ??? ??? ?speed++;
?? ??? ?if(speed==EnemyMoveSpeed)
?? ??? ?{
?? ??? ??? ?//敵機(jī)下落
?? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ?{
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ?enemy_x[k]++;
?? ??? ??? ??? ?speed=0;
?? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?return 0;
}

void updateWithInput(char input)//與用戶輸入有關(guān)的更新
{
?? ??? ?if(input=='a'||input=='A')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?if(position_y==0)
?? ??? ??? ??? ?position_y++;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='d'||input=='D')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_y++;//位置右移
?? ??? ??? ?if(position_y==Width-1)
?? ??? ??? ??? ?position_y--;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='w'||input=='W')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x--;//位置上移
?? ??? ??? ?if(position_x==0)
?? ??? ??? ??? ?position_x++;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input=='s'||input=='S')
?? ??? ?{
?? ??? ??? ?canvas[position_x][position_y]=0;
?? ??? ??? ?position_x++;//位置下移
?? ??? ??? ?if(position_x==High)
?? ??? ??? ??? ?position_x--;
?? ??? ??? ?canvas[position_x][position_y]=1;
?? ??? ?}
?? ??? ?else if(input==' ')//發(fā)射子彈
?? ??? ?{
?? ??? ??? ?int left =position_y-BulletWidth;
?? ??? ??? ?int right=position_y+BulletWidth;
?? ??? ??? ?if(left<0)
?? ??? ??? ??? ?left=0;
?? ??? ??? ?if(right>Width-1)
?? ??? ??? ??? ?right=Width-1;
?? ??? ??? ?int k;
?? ??? ??? ?for(k=left;k<=right;k++)//發(fā)射子彈
?? ??? ??? ?canvas[position_x-1][k]=2;
?? ??? ??? ?//發(fā)射子彈的初始位置在飛機(jī)的正上方
?? ??? ?}
}

void gamemenu()//游戲菜單
{
?? ?int temp=0;
?? ?int i,j;
?? ?char input;
?? ?for(i=0;i<High;i++)//數(shù)組初始化
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
? ? ? ? ? ? canvas[i][j]=0;
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?startup();//數(shù)據(jù)的初始化
?? ?system("cls");
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?temp=updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?if(kbhit())//判斷是否有輸入
?? ??? ?{
?? ??? ??? ?input=getch();
?? ??? ??? ?updateWithInput(input);//與用戶輸入有關(guān)的更新
?? ??? ??? ?if(input=='z'||input=='Z')
?? ??? ??? ??? ?temp=1;
?? ??? ?}
?? ??? ?if(temp==1)
?? ??? ?{
?? ??? ??? ?system("cls");
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}
void help()//幫助菜單
{
?? ?char input;
?? ?system("cls");
?? ?printf("\n\n\n\n\n\n\n\n");
?? ?printf("---------------------------------------\n");
?? ?printf(" ? ? ? ? ? ? ? ? 幫助菜單 ? ? ? ? ? ? ? ?\n\n");
?? ?printf(" ? ? ?1. 按空格發(fā)射炮彈 ? ? ? ? ? ? ? ? \n");
?? ?printf(" ? ? ?2. a 鍵左移 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf(" ? ? ?3. d 鍵右移 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf(" ? ? ?4. w 鍵上移 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf(" ? ? ?5. s 鍵下移 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf("---------------------------------------\n");
?? ?printf("\n\n提示:按 z 鍵回到主菜單\n");
?? ?printf("\n\n ? ? ? ? ? ? ? 祝您玩的愉快!\n");
?? ?while(1)
?? ?{
?? ??? ?input=getch();
?? ??? ?if(input=='z')
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}
void quit()//退出菜單
{
?? ?exit(0);
}

void menu()//主菜單
{
?? ?char change;
?? ?system("cls");
?? ?printf("\n--------------------------------------------\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? 8\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? 88\n");?
?? ?printf(" ? ? ? ? ? ? ? ? ? ?888\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? ? 8888\n");
?? ?printf(" ? ? ? ? ? ? ? ? ?8888888888888\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? ? 8888\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? ?888\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? 88\n");
?? ?printf(" ? ? ? ? ? ? ? ? ? 8\n");
?? ?printf("\n\n\n ? ? ? ? ? ? ? Welcome to fly war!\n");
?? ?printf("\n\n\n\n");
?? ?printf(" ? ? ? ? ? ? ? ? 主菜單 ? ? ? ? ? ? ? ?\n\n");?
?? ?printf(" ? ? ?1. 進(jìn)入游戲 ? ? ? ? ? ? ? ? \n");
?? ?printf(" ? ? ?2. 幫助菜單 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf(" ? ? ?3. 退出游戲 ? ? ? ? ? ? ? ? ? ? ?\n");
?? ?printf("---------------------------------------\n");
?? ?printf("\n\n ? ? ? ? ? ? ? 祝您玩的愉快!\n");
?? ?change=getch();
?? ?switch(change)
?? ?{
?? ?case '1':
?? ??? ?gamemenu();//游戲菜單
?? ??? ?break;
?? ?case '2':
?? ??? ?help();//幫助菜單
?? ??? ?break;
?? ?case '3':
?? ??? ?quit();//退出菜單
?? ??? ?break; ? ? ?
?? ?}
}
int main()
{
?? ?HideCursor();
?? ?system("title 游戲中");
?? ?system("color 09");
?? ?system("mode 50,30");
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?menu();
?? ?}
?? ?return 0;
}

效果圖如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹

    C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹

    以下是對C++中的的sstream標(biāo)準(zhǔn)庫進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
    2013-09-09
  • 你真的知道C++對象大小嗎?

    你真的知道C++對象大小嗎?

    這篇文章主要給大家介紹了關(guān)于C++對象大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 使用C++繪制GDI位圖的基本編寫實(shí)例

    使用C++繪制GDI位圖的基本編寫實(shí)例

    這篇文章主要介紹了使用C++繪制GDI位圖的基本編寫實(shí)例,一般來說適用于Windwos下的C++的GUI編程,需要的朋友可以參考下
    2015-12-12
  • c++核心編程之函數(shù)的重載

    c++核心編程之函數(shù)的重載

    這篇文章主要介紹了c++核心編程之函數(shù)的重載,函數(shù)可以重復(fù)使用,提高了復(fù)用性,但前提是必須在一個(gè)作用域并且函數(shù)名稱相同,下面附代碼詳細(xì)介紹,需要的小伙伴可以參考一下
    2022-03-03
  • C++根據(jù)傳入的函數(shù)指針來解析需要的參數(shù)(推薦)

    C++根據(jù)傳入的函數(shù)指針來解析需要的參數(shù)(推薦)

    C++可以根據(jù)傳入的函數(shù)指針,獲取自己需要的參數(shù)類型,然后根據(jù)參數(shù)源中獲取需要的參數(shù),具體實(shí)現(xiàn)方式大家參考下本文
    2018-05-05
  • C語言中for循環(huán)問題(一個(gè)小坑需注意)

    C語言中for循環(huán)問題(一個(gè)小坑需注意)

    這篇文章主要給大家介紹了關(guān)于C語言中for循環(huán)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C語言實(shí)現(xiàn)簡易三子棋

    C語言實(shí)現(xiàn)簡易三子棋

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡易三子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 關(guān)于C++類的成員初始化列表的相關(guān)問題

    關(guān)于C++類的成員初始化列表的相關(guān)問題

    下面小編就為大家?guī)硪黄P(guān)于C++類的成員初始化列表的相關(guān)問題。小編覺得挺
    2016-05-05
  • C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫算法示例

    C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫算法示例

    大小寫轉(zhuǎn)換是我們作為一名程序員經(jīng)常會(huì)遇到,也必須要會(huì)的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-01-01
  • epoll多路復(fù)用的一個(gè)實(shí)例程序(C實(shí)現(xiàn))

    epoll多路復(fù)用的一個(gè)實(shí)例程序(C實(shí)現(xiàn))

    這篇文章主要為大家詳細(xì)介紹了epoll多路復(fù)用的一個(gè)實(shí)例程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論