飞机大战V1.0

这次的飞机大战把激光子弹换成了普通的子弹,使敌机可以随机出现并向下移动

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

//定义全局变量
     int high,width;   //定义游戏屏幕的大小
     int position_x,position_y;  //定义我方飞机的位置
     int enemy_x,enemy_y;     //定义敌机的位置
     int bullet_x,bullet_y;    //定义子弹的位置
     int score;   //定义得分


void HideCursor()    //使控制台的光标被隐藏 

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

void gotoxy(int x,int y)    //类似于清屏函数,光标移动到原点位置进行重画
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void startup()     //初始化各种参数的值
{

    high=18;
    width=20;
    score = 0;

     position_x=high/2;
     position_y=width/2;
     enemy_x=0;
     enemy_y=width/4;
     bullet_x=0;
     bullet_y=position_y;
}
void show()
{
      //此清屏函数需要头文件 windows.h
      gotoxy(0,0);     //此为替换system的清屏函数,使画布每次都从左上角(0,0)开始         
      //此隐藏光标函数需要头文件  <windows.h>
      HideCursor();     //隐藏光标           
     // system ("cls");    // 显示前先使用清屏函数
    int i,j;
     // 显示我方飞机的位置
    for(i=0 ; i<high ; i++)
    {
    for(j=0 ; j<width ; j++)
       {
       if( (i==position_x) && (j == position_y) )         
		     cout<<"*";	  //打印飞机 			                   
       else if ((i == bullet_x) && ( j == bullet_y))
            cout<<"|";   //打印子弹
       else if ((i == enemy_x) && ( j == enemy_y))
            cout<<"@";   //打印敌机
        else
            cout<<" ";   //打印空格
        }
        cout<<endl;
     }
}
void updatewithoutinput()
{
      if (bullet_x>=0)   // 使子弹不停地往上移动
          bullet_x--;
     //定义局部静态变量
     static int speed = 0;   //定义敌机下落的速度
     if(speed < 50 )      //此为缓冲区,使敌机每次下落前都要经历speed从0~50的变化
       speed++;
     else 
    {   
	  if (enemy_x <=high)  //使敌机不停地往下移动
         enemy_x++;
      else
      {
      enemy_x = -1;      //使敌机在屏幕上消失
      enemy_y = rand() % width ;   //使敌机在0~width-1 的位置处再生         
	  }	 
	   speed = 0;      //使speed重置为0 
    }  
      if ((bullet_x == enemy_x-1) &&(bullet_y == enemy_y))  //当子弹击中敌机(改成enemy-1是因为发现当敌机正好在我机上方时发射子弹不能击中敌机) 
      {
           bullet_x = -1;     //使子弹在屏幕上消失
           enemy_x = -1;      //使敌机在屏幕上消失
           enemy_y = rand() % width ;   //使敌机在0~width-1 的位置处再生
           score++;     
      }
      
}
void updatewithinput()
{
      char input;
    
    
      if (kbhit())     //hbhit() 键盘敲击,检测到用户按下某键时就会返回
      {
	    input = getch();    //getch函数:输入字符后自动回车运行,头文件是conio.h,且字符不会在屏幕上显示
      if (input =='a')     //当输入a时,飞机左移
          position_y--;
      if (input =='d')      //当输入b时,飞机右移
          position_y++;    
      if (input =='w')     //当输入w时,飞机上移
          position_x--;    
      if (input =='s')    //当输入s时,飞机下移
          position_x++;   
      if (input == ' ')
         {
           bullet_x = position_x -1;
           bullet_y = position_y ;
           }   
     }   
	 cout<<"得分:  "<<score<<endl;   
}

int main(){
   startup();  //初始化各种参数
   while (1) 
   {
     show();   //使画布初始化
     updatewithoutinput();   //更新与输入无关的数值
     updatewithinput();    //更新与输入有关的数值
    }
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值