| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1924 人关注过本帖
标题:弹力球程序运行不正常
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
 问题点数:0 回复次数:2 
弹力球程序运行不正常
反弹板跑到上面去了,是程序逻辑不对吗?编译tc,运行环境win7+dosbox。
#include <graphics.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <bios.h>
#define R 4
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
int Keystate;
int MouseX;
int MouseY=400;
int dx=1,dy=1;
int sizex=20,sizey=10;
int Ide,Key;
struct wall
{
   int x;
   int y;
   int color;
}a[20][20];
void draw(int x,int y)
{
   int sizx=sizex-1;
   int sizy=sizey-1;
   setcolor(15);
   line(x,y,x+sizx,y);
   line(x,y+1,x+sizx,y+1);
   line(x,y,x,y+sizy);
   line(x+1,y,x+1,y+sizy);
   setcolor(4);
   line(x+1,y+sizy,x+sizx,y+sizy);
   line(x+2,y+sizy-1,x+sizx,y+sizy-1);
   line(x+sizx-1,y+1,x+sizx-1,y+sizy);
   line(x+sizx,y+2,x+sizx,y+sizy);
   setfillstyle(1,12);
   bar(x+2,y+2,x+sizx-2,y+sizy-2);
}
void picture(int r,int l)
{
   int i,j;
   setcolor(5);
   rectangle(100,50,482,461);
   for(i=0;i<r;i++)
   for(j=0;j<l;j++)
   {
      a[i][j].color=0;
      a[i][j].x=106+j*25;
      a[i][j].y=56+i*15;
      draw(106+j*25,56+i*15);
   }
   sizex=50,sizey=5;
}
void MouseOn(int x,int y)
{
   draw(x,y);
}
void MouseSetX(int lx,int rx)
{
   _CX=lx;
   _DX=rx;
   _AX=0x07;
   geninterrupt(0x33);
}
void MouseSetY(int uy,int dy)
{
   _CX=uy;
   _DX=dy;
   _AX=0x08;
   geninterrupt(0x33);
}
void MouseSetXY(int x,int y)
{
   _CX=x;
   _DX=y;
   _AX=0x04;
   geninterrupt(0x33);
}
void MouseSpeed(int vx,int vy)
{
   _CX=vx;
   _DX=vy;
   _AX=0x0f;
   geninterrupt(0x33);
}
void MouseGetXY()
{
   _AX=0x33;
   geninterrupt(0x33);
   MouseX=_CX;
   MouseY=_DX;
}
void MouseStatus()
{
   int x;
   int status;
   status=0;
   x=MouseX;
   if(x==MouseX&&status==0)
   {
      MouseGetXY();
      if(MouseX!=x)
     if(MouseX+50<482)
        status=1;
   }
   if(status)
   {
      setfillstyle(1,0);
      bar(x,MouseY,x+sizex,MouseY+sizey);
      MouseOn(MouseX,MouseY);
   }
}
void Play(int r,int l)
{
   int ballX;
   int ballY=MouseY-R;
   int i,j,t=0;
   srand((unsigned long)time(0));
   do
   {
      ballX=rand()%477;
   }
   while(ballX<=107||ballX>=476);
   while(kbhit)
   {
      MouseStatus();
      if(ballY<=(59-R))
      dy*=(-1);
      if(ballX>=(482-R)||ballX<=(110-R))
      dx*=(-1);
      setcolor(YELLOW);
      circle(ballX+=dx,ballY-=dy,R-1);
      delay(250);
      setcolor(0);
      circle(ballX,ballY,R-1);
      for(i=0;i<r;i++)
     for(j=0;j<l;j++)
     if(t<l*r&&a[i][j].color==0&&ballX>=a[i][j].x&&ballX<=a[i][j].x+20&&ballY>=a[i][j].y&&ballY<=a[i][j].y+10)
     {
        t++;
        dy*=(-1);
        a[i][j].color=1;
        setfillstyle(1,0);
        bar(a[i][j].x,a[i][j].y,a[i][j].x+20,a[i][j].y+10);
     }
     if(ballX==MouseX||ballX==MouseX-1||ballX==MouseX-2&&ballX==(MouseX+50+2)||ballX==(MouseX+50+1)||ballX==(MouseX+50))
     if(ballY>=(MouseY-R))
     {
        dy*=(-1);
        dx*=(-1);
     }
     if(t==l*r)
     {
        sleep(1);
        cleardevice();
        setcolor(RED);
        settextstyle(0,0,4);
        outtextxy(130,200,"GAME OVER");
        sleep(1);
        break;
     }
   }
   dx=1,dy=1;
   sizex=20,sizey=10;
}
void Rule()
{
   int n;
   char *s[5]=
   {
      "move the mouse left or right to let the ball rebound",
      "when the ball bounce the wall","the wall will disappear",
      "when the wall disappea","you will win!"
   };
   settextstyle(0,0,1);
   setcolor(GREEN);
   for(n=0;n<5;n++)
   outtextxy(150,170+n*20,s[n]);
}
void DrawMenu(int j)
{
   int n;
   char *s[4]=
   {
      "1,Mession One","2.Mession Two","3.rule","4.Exit Game"
   };
   settextstyle(0,0,1);
   setcolor(GREEN);
   for(n=0;n<4;n++)
   outtextxy(250,170+n*20,s[n]);
   setcolor(RED);
   outtextxy(250,170+j*20,s[j]);
}
void MainMenu()
{
   void JudgeIde();
   setbkcolor(BLACK);
   cleardevice();
   Ide=0,Key=0;
   DrawMenu(Ide);
   do
   {
      if(bioskey(1))
      {
     Key=bioskey(0);
     switch(Key)
     {
        case Key_Down:
        {
           Ide++;
           Ide=Ide%4;
           DrawMenu(Ide);
           break;
        }
        case Key_Up:
        {
           Ide--;
           Ide=(Ide+4)%4;
           DrawMenu(Ide);
           break;
        }
     }
      }
   }
   while(Key!=Key_Enter);
   JudgeIde();
}
void JudgeIde()
{
   switch(Ide)
   {
     case 0:
     {
      cleardevice();
      picture(6,15);
      MouseSetX(101,431);
      MouseSetY(MouseY,MouseY);
      MouseSetXY(150,MouseY);
      Play(6,15);
      MainMenu();
      break;
     }
     case 1:
     {
    cleardevice();
    picture(9,15);
    MouseSetX(101,431);
    MouseSetY(MouseY,MouseY);
    MouseSetXY(150,MouseY);
    Play(9,15);
    MainMenu();
    break;
     }
     case 2:
     {
    cleardevice();
    Rule();
    sleep(8);
    MainMenu();
    break;
     }
     case 3:
     {
    cleardevice();
    settextstyle(0,0,4);
    outtextxy(150,200,"goodbye!");
    sleep(1);
    exit(0);
     }

   }
}
main()
{
   int gdriver=DETECT,gmode;
   registerbgidriver(EGAVGA_driver);
   initgraph(&gdriver,&gmode,"");
   MainMenu();
   closegraph();
   return 0;
}
搜索更多相关主题的帖子: 运行环境 include color 弹力 
2016-07-23 21:12
Alien_Lee
Rank: 8Rank: 8
来 自:Linux帝国
等 级:蝙蝠侠
威 望:7
帖 子:149
专家分:739
注 册:2016-7-19
收藏
得分:0 
兄弟,那么长的代码,还一个注释都没有。怎么看嘛?
看了一小段,心累,果断放弃。
建议你做好注释,找到问题的关键部分,然后在问,问之前记得先把你的逻辑什么的交代清楚。

  DEBUG的过程就是进步的过程,每一个小错误都是大问题!...
2016-07-27 16:43
迪克杰
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-7-28
收藏
得分:0 
累坏了  不懂阿
2016-07-28 09:55
快速回复:弹力球程序运行不正常
数据加载中...
 
   



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

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