弹球小游戏C代码

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include "consoleout.h"
#pragma comment(lib,"consoleout.lib")

void main()
{
 CreateSmWindow("",0,0,40,20,14,0);//画框

 int nBSC=5;
 int nBSR=5;//球开始的位置

 int nCol=1;
 int nRow=1;//速度
 
 int nCurC=2;//下面板的位置
 int nCurR=16;


 int nCurCC=2;//上面板的位置
 int nCurRR=2;


 while(1)
 {
   MoveCursorTo(nCurC,nCurR);
   printf("________");

  if(_kbhit()==1) //键盘感应
  {  
   MoveCursorTo(nCurC,nCurR);//擦除上面前面的板 
   printf("        ");

   MoveCursorTo(nCurCC,nCurRR);//擦除上面前面的板 
   printf("        ");

   char cC;
   char cN;
   cC = _getch();
   //cN=_getch();
      if(cC == 27)//按ESC退出游戏
   {
    printf("Game Over!/n");
    break;
   }
   if(cC=='a'||cC=='A')//按A/a向左移动
   {
    if(nCurC>=4)
    nCurC=nCurC-2;
    else
     continue;
   }
   if(cC=='d'||cC=='D')//按D/d向右移动
   {
    if(nCurC<=68)
     nCurC=nCurC+2;
    else
     continue;
   }
   if(cC=='j'||cC=='J')
   {
    if(nCurCC>=4)
    nCurCC=nCurCC-2;
    else
     continue;
   }
   if(cC=='l'||cC=='L')
   {
    if(nCurCC<=4)
    nCurCC=nCurCC+2;
    else
     continue;
   }
   else
   {
   continue;
   }
   

   MoveCursorTo(nCurC,nCurR);
   printf("________");
  }


   MoveCursorTo(nCurCC,nCurRR);//擦除上面前面的板 
   printf("        ");
   MoveCursorTo(nCurCC,nCurRR);//画上面的板
   printf("________");
   if(nCurCC<3||nCurCC>75)//上面板左右边界
    {
     nCol=-nCol;
    }

   //功能:球的移动
   MoveCursorTo(nBSC,nBSR);
   printf("  ");

   nBSC=nBSC+nCol;
   nBSR=nBSR+nRow;
   
   MoveCursorTo(nBSC,nBSR);//画球
   printf("●");
   sleep(100); //延时


   if(nBSC<3||nBSC>75)
   {
    nCol=-nCol;
   }
   if((nBSC>=nCurC && nBSC<=nCurC+8 && nBSR==15)||(nBSR==3 && nBSC>=nCurCC && nBSC<=nCurCC+8))
   {
    nRow=-nRow;//判断球是否与上面和下面的滑板接触
   }
   if(nBSR>16 || nBSR<2)//判断求是否上下出界
   {
    printf("Game Over!/n  Never mind,Please a again!");
    char cH=getchar();
    if(cH=='n'||cH=='N')
    {
     break;
    }
    if(cH=='y'||cH=='Y')
    {
     MoveCursorTo(0,17);
     printf("                                ");//擦除Game Over!
     MoveCursorTo(0,18);
     printf("                             ");//擦除Never mind,Please a again!
     nBSC=2;
     nBSR=2;
    }
   } 
 }

}
 

猜你喜欢

转载自blog.csdn.net/saishangpeiqian/article/details/2520398