C語言實現(xiàn)空戰(zhàn)游戲
本文實例為大家分享了C語言實現(xiàn)空戰(zhàn)游戲的具體代碼,供大家參考,具體內容如下
一、項目描述和成果展示
項目描述: 在以往的程序中進行了改進。
例如: 可以發(fā)射多個子彈
可以有多個敵機
飛機大戰(zhàn) 1.0版
飛機大戰(zhàn) 2.0版
現(xiàn)在這個版本是3.0版 這個版本的各個功能基本完善
效果圖如下:


二、發(fā)射多個子彈
代碼如下:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#define High 25 //游戲的尺寸
#define Width 50
//全局變量
int position_x,position_y;//飛機的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲游戲畫布中對應的元素
//0為空格 1為飛機 ?2為子彈 ?3為敵機
void gotoxy(int x,int y)//將光標移動到(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);//將光標移動到原點位置,以下重畫清屏
?? ?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()//與用戶輸入無關的更新
{
?? ?int i,j;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動
?? ??? ??? ?{
?? ??? ??? ??? ?canvas[i][j]=0;
?? ??? ??? ??? ?if(i>0)
?? ??? ??? ??? ??? ?canvas[i-1][j]=2;
?? ??? ??? ?}
?? ??? ?}
?? ?}
}
void updateWithInput()//與用戶輸入有關的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動
?? ??? ?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ā)射子彈的初始位置在飛機的正上方
?? ??? ?}
?? ?}
}
int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ?}
?? ?return 0;
}效果圖如下:

三、多個敵機
代碼如下:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#define High 25 //游戲的尺寸
#define Width 50
#define EnemyNum 5//敵機的個數(shù)
//全局變量
int position_x,position_y;//飛機的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲游戲畫布中對應的元素
//0為空格 1為飛機 ?2為子彈 ?3為敵機
int score;//得分
void gotoxy(int x,int y)//將光標移動到(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);//將光標移動到原點位置,以下重畫清屏
?? ?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("+");//輸出敵機
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("得分:%3d\n",score);
?? ?Sleep(20);
}
void updateWithoutInput()//與用戶輸入無關的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分數(shù)加一
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動
?? ??? ??? ??? ?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]) )//敵機撞到我機
?? ??? ?{
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ?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)
?? ??? ?{
?? ??? ??? ?//敵機下落
?? ??? ??? ?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()//與用戶輸入有關的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動
?? ??? ?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ā)射子彈的初始位置在飛機的正上方
?? ??? ?}
?? ?}
}
int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ?}
?? ?return 0;
}效果圖如下:

四、增加子彈升級功能
當分數(shù)達到一定的值自動升級炮彈,當分數(shù)下降一定數(shù)值后炮彈下降一個等級。
代碼如下:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#define High 25 //游戲的尺寸
#define Width 40
#define EnemyNum 5//敵機的個數(shù)
//全局變量
int position_x,position_y;//飛機的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲游戲畫布中對應的元素
//0為空格 1為飛機 ?2為子彈 ?3為敵機
int score;//得分
int BulletWidth;//子彈的寬度
int EnemyMoveSpeed;//敵機的移動速度
void gotoxy(int x,int y)//將光標移動到(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);//將光標移動到原點位置,以下重畫清屏
?? ?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("+");//輸出敵機
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("════════════════════════════════════════\n");
?? ?printf("得分:%3d\n",score);
?? ?system("date /t");
?? ?system("time /t");
?? ?Sleep(20);
}
//定義隱藏光標函數(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()//與用戶輸入無關的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分數(shù)加一
?? ??? ??? ??? ??? ??? ?if(score%5==0&&EnemyMoveSpeed>3)//達到一定積分后敵機變快
?? ??? ??? ??? ??? ??? ??? ?EnemyMoveSpeed--;
?? ??? ??? ??? ??? ??? ?if(score/5)//達到一定積分后子彈變厲害
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=0;
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=BulletWidth+score/5;
?? ??? ??? ??? ??? ??? ??? ?if(BulletWidth>5)//子彈加到一定威力后,會固定不變
?? ??? ??? ??? ??? ??? ??? ??? ?BulletWidth=5;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動
?? ??? ??? ??? ?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]) )//敵機撞到我機
?? ??? ?{
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?score--;//減分
?? ??? ??? ?if(score%5==0&&score>0)//減的分數(shù)過多,子彈威力下降
?? ??? ??? ?{
?? ??? ??? ??? ?BulletWidth--;
?? ??? ??? ??? ?EnemyMoveSpeed++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?static int speed=0;
?? ??? ?if(speed<EnemyMoveSpeed)
?? ??? ??? ?speed++;
?? ??? ?if(speed==EnemyMoveSpeed)
?? ??? ?{
?? ??? ??? ?//敵機下落
?? ??? ??? ?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()//與用戶輸入有關的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();//根據(jù)用戶的不同輸入來移動
?? ??? ?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ā)射子彈的初始位置在飛機的正上方
?? ??? ?}
?? ?}
}
int main()
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?HideCursor();
?? ?system("title 游戲中");
?? ?system("color 09");
?? ?while(1)//游戲循環(huán)執(zhí)行
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ?}
?? ?return 0;
}效果圖如下:

五、增加一些小功能
增加一個主菜單
里面增加了一些小的功能
代碼如下:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#define High 25 //游戲的尺寸
#define Width 40
#define EnemyNum 5//敵機的個數(shù)
//全局變量
int position_x,position_y;//飛機的位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敵機的位置
int canvas[High][Width]={0};
//二維數(shù)組存儲游戲畫布中對應的元素
//0為空格 1為飛機 ?2為子彈 ?3為敵機
int score;//得分
int BulletWidth;//子彈的寬度
int EnemyMoveSpeed;//敵機的移動速度
void gotoxy(int x,int y)//將光標移動到(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);//將光標移動到原點位置,以下重畫清屏
?? ?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("+");//輸出敵機
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("════════════════════════════════════════\n");
?? ?printf("得分:%3d\n",score);
?? ?system("date /t");
?? ?system("time /t");
?? ?printf("提示:按 z 鍵回到主菜單");
?? ?Sleep(20);
}
//定義隱藏光標函數(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()//與用戶輸入無關的更新
{
?? ?int i,j,k;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==2)//子彈向上移動
?? ??? ??? ?{
?? ??? ??? ??? ?for(k=0;k<EnemyNum;k++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子彈擊中敵機
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?score++;//分數(shù)加一
?? ??? ??? ??? ??? ??? ?if(score%5==0&&EnemyMoveSpeed>3)//達到一定積分后敵機變快
?? ??? ??? ??? ??? ??? ??? ?EnemyMoveSpeed--;
?? ??? ??? ??? ??? ??? ?if(score/5)//達到一定積分后子彈變厲害
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=0;
?? ??? ??? ??? ??? ??? ??? ?BulletWidth=BulletWidth+score/5;
?? ??? ??? ??? ??? ??? ??? ?if(BulletWidth>5)//子彈加到一定威力后,會固定不變
?? ??? ??? ??? ??? ??? ??? ??? ?BulletWidth=5;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ??? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ??? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ??? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ??? ??? ??? ?canvas[i][j]=0;//子彈消失
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//子彈向上移動
?? ??? ??? ??? ?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]) )//敵機撞到我機
?? ??? ?{
?? ??? ??? ?system("cls");
?? ??? ??? ?printf("失敗\n");
?? ??? ??? ?printf("得分:%d\n",score);
?? ??? ??? ?printf("正在返回主菜單\n");
?? ??? ??? ?Sleep(3000);
?? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?if(enemy_x[k]>High)//敵機跑出屏幕
?? ??? ?{
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=0;
?? ??? ??? ?enemy_x[k]=rand()%2;//產生新的飛機
?? ??? ??? ?enemy_y[k]=rand()%Width;
?? ??? ??? ?canvas[enemy_x[k]][enemy_y[k]]=3;
?? ??? ??? ?score--;//減分
?? ??? ??? ?if(score%5==0&&score>0)//減的分數(shù)過多,子彈威力下降
?? ??? ??? ?{
?? ??? ??? ??? ?BulletWidth--;
?? ??? ??? ??? ?EnemyMoveSpeed++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?static int speed=0;
?? ??? ?if(speed<EnemyMoveSpeed)
?? ??? ??? ?speed++;
?? ??? ?if(speed==EnemyMoveSpeed)
?? ??? ?{
?? ??? ??? ?//敵機下落
?? ??? ??? ?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)//與用戶輸入有關的更新
{
?? ??? ?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ā)射子彈的初始位置在飛機的正上方
?? ??? ?}
}
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();//與用戶輸入無關的更新
?? ??? ?if(kbhit())//判斷是否有輸入
?? ??? ?{
?? ??? ??? ?input=getch();
?? ??? ??? ?updateWithInput(input);//與用戶輸入有關的更新
?? ??? ??? ?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. 進入游戲 ? ? ? ? ? ? ? ? \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;
}效果圖如下:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++11/14 線程的創(chuàng)建與分離的實現(xiàn)
這篇文章主要介紹了C++11/14 線程的創(chuàng)建與分離的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
C++實現(xiàn)LeetCode(889.由先序和后序遍歷建立二叉樹)
這篇文章主要介紹了C++實現(xiàn)LeetCode(889.由先序和后序遍歷建立二叉樹),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07

