C语言制作飞机大战
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "windows.h"
int px,py;
int bx,by;
int ex,ey;
int high,width;
int score;
int ispause=0;
int time=100;
int level=0;
void gotoxy(int x,int 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=20;
width=30;
px=high/2;
py=width/2;
bx=0;
by=py;
ex=0;
ey=py;
score=0;
HideCursor();
}
void show()
{
gotoxy(0,0);
int i,j;
for (i=0;i<high;i++)
{
for (j=0;j<width;j++)
{
if (i==px&&j==py)
{
printf("*");
}
else if (i==px+1&&j==py-2)
{
printf("*****");
}
else if(i==px+2&&j==py-1)
{
printf ("* * ");
}
else if (i==bx&&j==by)
{
printf("|");
}
else if (i==ex&&j==ey)
{
printf("@");
}
else
printf(" ");
}
printf("\n");
}
printf("得分:%d\n",score);
printf("敌机速度:%d\n",level);
}
void updatewithoutinput()
{
if (bx>-1)
{
bx--;
}
if (bx==ex&&by==ey)
{
score++;
ex=-1;
ey=rand()%width;
bx=-2;
if (score%3==0&&time>10)
{
time-=5;
level++;
}
}
if (ex>high)
{
ex=-1;
ey=rand()%width;
}
static int speed=0;
if (speed<time)
{
speed++;
}
if (speed==time)
{
ex++;
speed=0;
}
}
void updatewithinput()
{
char input;
if (kbhit())
{
input=getch();
if (input=='a')
{
if (py==0)
{
py=width;
}
else
py--;
}
if (input=='d')
{
if (py==width)
{
py=0;
}
else
py++;
}
if (input=='w')
{
if (px==0)
{
px=high;
}
else
px--;
}
if (input=='s')
{
if (px==high)
{
px=0;
}
else
px++;
}
if (input==' ')
{
bx=px-1;
by=py;
}
}
}
int main ()
{
startup();
while (1)
{
show();
updatewithoutinput();
updatewithinput();
}
return 0;
}