简易游戏:飞机打靶


#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main(){
int i,j; 
int x=6;
int y=10;
int ny=5;
int isFire=0;
int isKilled=0;
char input;

while(1){
   system("cls");  //清屏
   
   if(!isKilled)   //如果没有被击中,输出靶子
   {
     for(j=0;j<ny;j++)
      cout<<" ";
     cout<<"+"<<endl;
   }   
   
   if(isFire==0)   //如果没有开火 输出飞机上方的回车
   {
    for(i=0;i<x;i++)
      cout<<endl;
   }
   else           //如果开火,输出飞机上方的子弹
   {
   for(i=0;i<x;i++)
      {
      for(j=0;j<y;j++)
        cout<<" ";    //输出子弹左侧的空格
      cout<<"  |"<<endl;  //输出子弹
      }
      if(y+2==ny)      //如果飞机的头部与靶子位置相同 令靶子的击中值赋值为1
        isKilled=1;
      isFire=0;        //开火完令开火的值为0; 在任意移动值未按空格的情况下使子弹消失
   } 
    //打印飞机:
  for(j=0;j<y;j++) 
   cout<<" ";
  cout<<"  *"<<endl;
  for(j=0;j<y;j++) 
    cout<<" ";
  cout<<"*****"<<endl;
    for(j=0;j<y;j++) 
   cout<<" ";
  cout<<" * * "<<endl;
  
   input=getch();   //getch(): 使从键盘输入后不按空格可以自动运行;相当于cin完后自动按enter。 头文件为:#include <conio.h>
    if(input=='a')         //按a的时候让飞机向左移动
      y--;  
    if(input=='d')      //按d的时候让飞机向右移动
      y++;
    if(input=='w')       //按w的时候让飞机向上移动
      x--;
    if(input=='s')       //按s的时候让飞机向下移动
      x++;
    if(input==' ')       //按空格的时候让飞机发射子弹
      isFire=1;
    }
  
  return 0;
}

发布了4 篇原创文章 · 获赞 2 · 访问量 113

猜你喜欢

转载自blog.csdn.net/zjp1310343359/article/details/105176423