标题:今天改了个弹力球程序
取消只看楼主
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
结帖率:100%
 问题点数:0 回复次数:0 
今天改了个弹力球程序
代码比较乱,但基本功能都能实现。
这段代码是在以前别人编的贪吃蛇基础上改的。
大神们可以扩展和修改,欢迎指出不足之处,大家共同学习进步
#include<stdio.h>//基本库
#include<stdlib.h>//系统库
#include<windows.h>//光标定位、字符颜色函数库
#include<time.h>//时间函数库
#include<conio.h>//键值读入函数库
#define width 60
#define height 40
struct ball
 {
    int x;
     int y;//球坐标
};
struct block //方块
{
    int x;
    int y;
};
struct qiup
{
    int x;
    int y;
};
char pel[]="  ●■====";
void color(int b)         //颜色函数
{
     HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
     SetConsoleTextAttribute(hConsole,b) ;
 }
void HideCursor()//隐藏光标
{
     HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_CURSOR_INFO cursor_info={1,0};
     SetConsoleCursorInfo(hOut,&cursor_info);
 }

void gotoxy(int x,int y)//设置字符显示位置
{
     HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
     COORD loc={y,x};
     SetConsoleCursorPosition(hOut,loc);
 }
void drawmap(char *mp,char *omp,ball balln,int level)
 {//画场景
   
     int i,j,k;
        mp[balln.x*width+balln.y]=2;  //画球  
     for(k=0;k<level*2*width+29*width;k+=2)
     {
         if(mp[k]!=omp[k]&&mp[k]!=6)
         {
             i=k/width;j=k%width;
             color(15);
             omp[k]=mp[k];
             gotoxy(i,j);
             printf("%c",pel[mp[k]]);
             printf("%c",pel[mp[k]+1]);
         }
     }
 }
void drawblock(char *mp,block *bk,int level)
 {//画方块
     int i,j,k,m;
     m=level*width*2;
     for(k=0;k<m;k+=2)
     {
        i=k/width;j=k%width;
        color(15);
        gotoxy(i,j);
        printf("%c%c",pel[4],pel[5]);
        mp[k]=4;
        bk[k].x=i;
        bk[k].y=j;
     }
 }
 void drawqiup(qiup &qiupn,int qy)
 {
        qiupn.y+=qy;
           if(qiupn.y<0||qiupn.y>width-4)
         {
             qiupn.y=0;
            qiupn.y=width-4;
            qy=0;
         }
     if(qy==2)
         {
            
             gotoxy(qiupn.x,qiupn.y-2);
             printf("  %c%c%c%c",pel[6],pel[7],pel[8],pel[9]);
         }
    else if(qy==-2)
    {
            
            gotoxy(qiupn.x,qiupn.y);
             printf("%c%c%c%c  ",pel[6],pel[7],pel[8],pel[9]);
    }
    else
    {
                gotoxy(qiupn.x,qiupn.y);
             printf("%c%c%c%c",pel[6],pel[7],pel[8],pel[9]);   
    }
 }
 
int sucess(char *mp,block *bk,int level)
{
    int k;
    for(k=0;k<level*width*2;k+=2)  //判断消除
  {
      if(mp[k]==4)
      {
          break;
      }
  }
  if(k==level*width*2)
  return 3;
  else return 4;
}

int main(void)
 {
     char map[height][width]={0},oldmap[height][width]={0};//场景 0:空 2:球 4:方块 6:球拍
     char *mp0=&map[0][0];
    int mx=1,my=2;//球移动方向
    int qy=0;//球拍移动方向
    int i,j,k,l,sc=0,level=1,ef=0,sp=210,flag=0;
     long t1,nt;//计时
    char a;
     bool pf=false;//暂停标志
     ball balln={0};//球
     block bk[(height-10)*width]={0};//方块
     qiup qiupn={0};//球拍
    nt=clock();
     t1=nt;
     system("mode con cols=80 lines=40");
     HideCursor();
     for(i=0;i<height;i++){gotoxy(i,width);printf("‖");}
     color(14);
     gotoxy(12,width+4);printf("控制:←↑→↓");
     gotoxy(14,width+4);printf("空格:暂停");
     gotoxy(16,width+4);printf("ESC :退出");
     gotoxy(18,width+4);printf("LEVEL:%d",level);
     gotoxy(20,width+4);printf("得分:%d",sc);
     balln.x=level*2+10;balln.y=10;//球位置
     qiupn.x=level*2+30;qiupn.y=30;
     while(1)
     {
         l=-1;
         if(level==1&&flag==0)
         {
         drawblock(&map[0][0],bk,level);
         flag=1;
         }
         nt=clock();

         if((nt-t1)/400)  //球移动
         {
             if(mx!=0)
         {
             mp0[balln.x*width+balln.y]=0;
         }
            
             balln.x+=mx;balln.y+=my;
             t1=nt;
                      if(qiupn.x-1==balln.x&&(qiupn.y==balln.y||qiupn.y+2==balln.y))//与球拍撞击
         {
             mx=-1;
         }
         if(balln.y==0)
          my=2;
          if(balln.y==width-2)
          my=-2;
          if(balln.x==0)
          mx=1;
        if(mp0[(balln.x-1)*width+balln.y]==4)
        {
            mp0[(balln.x-1)*width+balln.y]=0;
            mx=1;
            sc+=10;
         }

             drawmap(&map[0][0],&oldmap[0][0],balln,level);
         }
 
         qy=0;
         if(kbhit())   //判断按键
        {
                 a=getch();
                 if(a=='K'){qy=-2;}
                 if(a=='M'){qy=2;}//左右移动,由于用汉字符号表示,所以步长为2
                 if(a==27){ef=1;break;}//人为退出
                 if(a==32)pf=!pf;
                 
         }
         drawqiup(qiupn,qy);

        ef=sucess(&map[0][0],bk,level);
         if(!pf&&ef==3)
         {

            gotoxy(40,40);printf("恭喜你通关了!\n");
            level++;
            flag=0;
         }
         if(ef==3)
         system("pause");
         if(balln.x>qiupn.x+1)
         {
             gotoxy(25,23);
            color(15);
            printf("没接到死翘翘!Game Over!\n");
             break;
         }
         gotoxy(20,width+4);printf("得分:%d",sc);
         Sleep(100);
     }
     gotoxy(25,23);
     color(15);
     if(ef==2)printf("没接到死翘翘!Game Over!\n");
 }
搜索更多相关主题的帖子: include 贪吃蛇 弹力 
2016-10-19 22:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470005-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.167390 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved