| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1615 人关注过本帖, 1 人收藏
标题:今天改了个弹力球程序
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏(1)
 问题点数:0 回复次数:3 
今天改了个弹力球程序
代码比较乱,但基本功能都能实现。
这段代码是在以前别人编的贪吃蛇基础上改的。
大神们可以扩展和修改,欢迎指出不足之处,大家共同学习进步
#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
我是过来人灬
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-12-1
收藏
得分:0 
有问题。无限循环。。。。自己试下。
2016-12-10 16:31
我是过来人灬
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-12-1
收藏
得分:0 
看错了。不过如果可以把初点设为任意横坐标就好了,
2016-12-10 16:52
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:0 
/* tension_ball.c 弹力球 */

#include<stdio.h>//基本库
#include<stdlib.h>//系统库
#include<windows.h>//光标定位、字符颜色函数库
#include<time.h>//时间函数库
#include<conio.h>//键值读入函数库
#define width 60
#define height 40
enum bool{false, true};
typedef enum bool bool;
struct ball
{
    int x;
    int y;//球坐标
};
typedef struct ball ball;
struct block //方块
{
    int x;
    int y;
};
typedef struct block block;
struct qiup
{
    int x;
    int y;
};
typedef struct qiup qiup;
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')    //左右移动,由于用汉字符号表示,所以步长为2
                qy=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");
}

//楼主什么编译器?这也能通过得了。。。
//修改了一下,能通过我的编译器,但球拍无法移动,对这种程序不熟悉

一切都在学习、尝试、摸索中
2016-12-10 20:58
快速回复:今天改了个弹力球程序
数据加载中...
 
   



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

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