c语言字符串去重简单,C语言实现简单飞机大战
本文實例為大家分享了C語言實現飛機大戰的具體代碼,供大家參考,具體內容如下
定義四個函數實現飛機大戰
#include
#include
#include
//定義全局變量
int high,width; //定義邊界
int position_x,position_y; //飛機位置
int bullet_x,bullet_y; //子彈位置
int enemy_x,enemy_y;
int score;
int flag; //飛機狀態
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 HideCursor() // 用于隱藏光標
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個值為0表示隱藏光標
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() //數據初始化
{
high=18;
width=26;
position_x=high-3;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
enemy_x=0;
enemy_y=position_y;
score=0;
flag=0; //飛機完好
HideCursor();
}
void show() //顯示界面
{
int i,j;
for(i=0;i
{
for(j=0;j
{
if(flag)
break;
else if((i==position_x)&&(j==position_y)) //飛機坐標
printf("*");
else if((i==enemy_x)&&(j==enemy_y)) //敵機坐標
printf("*");
else if((i==bullet_x)&&(j==bullet_y)) //子彈坐標
printf("|");
else if ((j==width-1)||(i==high-1)||(j==0)||(i==0)) //打印邊界
printf("#");
else
printf(" ");
}
printf("\n");
}
printf("\n");
if((position_x==enemy_x)&&(position_y==enemy_y))
{
flag=1; //飛機撞毀 游戲結束
printf("得分: %d\n",score);
printf("游戲結束");
}
else
printf("得分: %d\n",score);
}
void withoutInpute() //與用戶輸入無關
{
if(bullet_x>0) //子彈上升效果
bullet_x--;
if((bullet_x==enemy_x)&&(bullet_y==enemy_y)) //子彈命中敵機
{
score++;
bullet_x=-1;
enemy_x=1;
enemy_y=2+rand()%width-2;
}
static int speed;
if(speed<30) //減慢敵機速度,不影響飛機和子彈速度
speed++;
if(speed==30)
{
if(enemy_x
enemy_x++;
else
{
enemy_x=0;
enemy_y=2+rand()%width-2;
}
speed=0;
}
}
void withInpute() //與用戶輸入有關
{
char input;
if(kbhit()) //控制飛機方向
{
input=getch();
if((input=='w')&&position_x>1)
position_x--;
if((input=='s')&&position_x
position_x++;
if((input=='a')&&position_y>1)
position_y--;
if((input=='d')&&position_y
position_y++;
if(input==' ')
{
bullet_x=position_x-1;
bullet_y=position_y;
}
}
}
int main()
{
system("color 2f");
startup(); // 數據初始化
while(1) // 游戲循環執行
{
gotoxy(0,0);
show(); // 顯示畫面
withoutInpute(); // 與用戶輸入無關的更新
withInpute(); // 與用戶輸入有關的更新
}
}
作者的另一段代碼:C語言實現空戰游戲,也很棒,分享給大家:
#include
#include
#include
#define High 27 //定義邊界
#define Width 45
#define EnemyNum 5 //敵機數目
//定義全局變量
int canvas[High][Width]={0}; //定義元素,0為空格,1為飛機,2為子彈,3為敵機,4為右下邊界
int position_x,position_y; //飛機坐標
int enemy_x[EnemyNum],enemy_y[EnemyNum]; //敵機坐標
int score; //得分
int Speed; //敵機速度
int bulletwidth; //子彈寬度
void HideCursor() //隱藏光標
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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() //數據初始化
{
position_x=High-2; //初始化飛機位置
position_y=Width/2;
canvas[position_x][position_y]=1;
bulletwidth=0; //初始化子彈寬度
Speed=25; //敵機初始最小速度
int k;
for(k=0;k
{
enemy_x[k]=rand()%2; //初始化敵機位置
enemy_y[k]=rand()%Width;
canvas[enemy_x[k]][enemy_y[k]]=3;
}
score=0; //得分初始化
HideCursor();
}
void show() //顯示界面
{
int i,j;
gotoxy(0,0);
for(i=0;i<=High;i++)
{
for(j=0;j<=Width;j++)
{
if(canvas[i][j] == 1)
printf("*"); //輸出飛機
else if(canvas[i][j]==2)
printf("|"); //輸出子彈
else if(canvas[i][j]==3)
printf("@"); //輸出敵機
else if(canvas[i][j]==4)
printf("#"); //輸出邊界#
else
printf(" "); //輸出空格
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updateWithoutInput() //無需用戶輸入的更新
{
int i,j,k;
for(i=0;i
{
for(j=0;j
{
if(canvas[i][j]==2)
{
for(k=0;k
{
if(i==enemy_x[k] && j==enemy_y[k]) //擊中敵機
{
score++;
if(score==5||score==10) //得分達到標準子彈加寬
bulletwidth++;
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; //子彈自動上升
if(i>0)
canvas[i-1][j]=2;
}
}
}
for(k=0;k
{
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;
}
}
static int speed=0;
if(speed
speed++;
if(speed==Speed)
{
for(k=0;k
{
canvas[enemy_x[k]][enemy_y[k]]=0; //敵機自動下落
enemy_x[k]++;
canvas[enemy_x[k]][enemy_y[k]]=3;
}
speed=0;
}
for(k=0;k
{
if(enemy_x[k]==position_x&&enemy_y[k]==position_y) //飛機撞毀
{
printf("游戲結束\n");
exit(0);
}
}
}
void updateWithInput() //需用戶輸入的更新
{
char input;
if(kbhit())
{
input=getch();
if(input=='w' && position_x>0) //控制飛機方向
{
canvas[position_x][position_y]=0;
position_x--;
canvas[position_x][position_y]=1;
}
else if(input=='s' && position_x
{
canvas[position_x][position_y]=0;
position_x++;
canvas[position_x][position_y]=1;
}
else if(input=='a' && position_y>0)
{
canvas[position_x][position_y]=0;
position_y--;
canvas[position_x][position_y]=1;
}
else if(input=='d' && position_y
{
canvas[position_x][position_y]=0;
position_y++;
canvas[position_x][position_y]=1;
}
else if(input=' ') //space發射子彈
{
int left,right;
int x;
left=position_y-bulletwidth;
if(left<0)
left=0;
right=position_y+bulletwidth;
if(right>Width-1)
right=0;
for(x=left;x<=right;x++)
canvas[position_x-1][x]=2;
}
}
}
int main()
{
startup();
system("color 2f");
while(1)
{
show(); //顯示界面
updateWithoutInput(); //無需用戶輸入的更新
updateWithInput(); //需用戶輸入的更新
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的c语言字符串去重简单,C语言实现简单飞机大战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言中动态数组的作用,C语言实现动态数
- 下一篇: C语言中7除以14的答案,2015年计算