C言語は簡単な飛行機大戦を実現します。
本論文の実例はC言語で飛行機大戦を実現する具体的なコードを共有しています。
四つの関数を定義して飛行機大戦を実現します。
四つの関数を定義して飛行機大戦を実現します。
#include<stdio.h>
#include<windows.h>
#include<conio.h>
//
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<high;i++)
{
for(j=0;j<width;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("
");
}
printf("
");
if((position_x==enemy_x)&&(position_y==enemy_y))
{
flag=1; //
printf(" : %d
",score);
printf(" ");
}
else
printf(" : %d
",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<high)
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<high-2)
position_x++;
if((input=='a')&&position_y>1)
position_y--;
if((input=='d')&&position_y<width-2)
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<stdio.h>
#include<windows.h>
#include<conio.h>
#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<EnemyNum;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("
");
}
printf(" :%d
",score);
}
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++;
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<EnemyNum;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) //
speed++;
if(speed==Speed)
{
for(k=0;k<EnemyNum;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<EnemyNum;k++)
{
if(enemy_x[k]==position_x&&enemy_y[k]==position_y) //
{
printf("
");
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<High-1)
{
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<Width-1)
{
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(); //
}
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。