#include
#include
#include
#include
void startup();void show(); void keyscan();//函数
//定义全局变量
int width=20,high=20;
int plane_x,plane_y,enemy_x,enemy_y;
int bullet_x,bullet_y;
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 HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
int main() {
startup();
while(1){
gotoxy(0,0);
show();
keyscan();
if(bullet_y>=0) bullet_y--;
if(enemy_x==bullet_x&&enemy_y==bullet_y){
score++;
}
//游戏交互,让子弹和第几不一样
static int speed=0;
if(speed<=20) speed++;
if(speed==20){
enemy_y++;
speed=0;
}
printf("\n得分=%d",score);
}
return 0;
}
//定义初始化变量
void startup(){
plane_x=width/2;plane_y=high-3;
enemy_x=10;enemy_y=1;
bullet_x=plane_x;bullet_y=-2;
}
//定义显示函数
void show(){
int i,j;
for(j=0;j<=high;j++){
for(i=0;i<=width;i++){
if (i==plane_x&&j==plane_y){
printf("*");i=i+1;
}
if(i==plane_x-2&&j==plane_y+1){
printf("*****");i=i+5;//显示我方飞机
}
if(i==plane_x-1&&j==plane_y+2){
printf("* *");i+=3;
}
if(i==enemy_x&&j==enemy_y){
printf("$");i+=1;//显示地方飞机
}
if(i==bullet_x&&j==bullet_y){
printf("^");i+=1;}//xianshizidan
if(i==width||i==0) printf("|");
else printf(" ");
}printf("\n");
}
}
//anjiankongzhihanshu
void keyscan(){
char input;
if(kbhit()){
input=getch();
printf("input%d",input);
if(input=='a'||input==97) plane_x--;
if(input=='d'||input==100) plane_x++;
if(input=='w'||input==119) plane_y--;
if(input=='s'||input==115) plane_y++;
if(input==' ') bullet_y=plane_y-1-1;
}
}
1参照弹跳小球的设计方案,改写打飞机游戏,首先实现飞机、靶子、边界面的静态显示。
2实现靶子自动下降,飞机可以根据按键自主移动。
3增加空格键发子弹,子弹向上发射。
4设计游戏规则与得分标准,子弹击中靶子加分,靶子降落到底部,浪费子弹减分,靶子撞到飞机,机毁人亡,退出游戏。
5靶子被击中以后,原有靶子消失,新的靶子马上随机出现在第一行上方。
6游戏规则与得分等文字可以放底部或游戏界面右边。
7考虑增加游戏难度,比如靶子下降速度加快,靶子数量随机增加,关卡升级等;也可以增加飞机变成双人游戏,追加用上下左右的箭头来表示移动?与adws保持一致,增加变异子弹等。
所有程序要求规范,使用函数设计。
7为增加内容,选一到2个即可