可爱扎 2018-10-25 08:44 采纳率: 100%
浏览 644
已采纳

用VS写了一个下一百层的代码,出现bug。

我定义了五块板子,标写有ABCDE,现在的问题是,
我的球碰到板子不会上升,会穿过,每过一波,
BE的板子可以带着球一起走,然后下一波是C的板子可以带着球走,其他的板子都会穿过
下面是代码,请大佬看一下谢谢

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>

#define High 640
#define Width 480
#define barNumber 5
int bar_x[barNumber],bar_y[barNumber];   //板子坐标
int bar_high,bar_width;    //板子长宽
int radius;
int ball_x,ball_y;  //球坐标
int ball_v;  //球速度
int bar_v;   //板子速度
char input;
TCHAR s1[] = _T("1");

void startup()  //初始化
{
    bar_high = High/20;
    bar_width = Width/(barNumber-1);
    radius = 20;
    ball_x = Width/2;
    ball_y = radius;
    ball_v = 1;

    for(int i=0;i<barNumber;i++)
    {
        bar_x[i] = rand()%Width;
        bar_y[i] = rand()%High;
    }


    initgraph(Width,High);
    BeginBatchDraw();
}

void show()
{
    setcolor(RED);
    setfillcolor(RED);
    fillcircle(ball_x,ball_y,radius);


    int bar_left,bar_top,bar_right,bar_bottom;
    for(int i=0;i<barNumber;i++)
    {
        setcolor(BLUE);
        setfillcolor(BLUE);
        bar_left = bar_x[i] - bar_width/2;
        bar_top = bar_y[i] - bar_high/2;
        bar_right = bar_x[i] + bar_width/2;
        bar_bottom = bar_y[i] + bar_high/2;
        bar(bar_left,bar_top,bar_right,bar_bottom);
        setcolor(WHITE);
        outtextxy(bar_x[i],bar_y[i],i+65);

    }
    FlushBatchDraw();
    Sleep(50);
}

void clean()
{
    setcolor(BLACK);
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    int bar_left,bar_top,bar_right,bar_bottom;
    for(int i=0;i<barNumber;i++)
    {
        bar_left = bar_x[i] - bar_width/2;
        bar_top = bar_y[i] - bar_high/2;
        bar_right = bar_x[i] + bar_width/2;
        bar_bottom = bar_y[i] + bar_high/2;
        bar(bar_left,bar_top,bar_right,bar_bottom);
    }

}

void withinput()   //输入相关
{
    if(kbhit())
    {
        input = getch();
        if(input=='a')
            ball_x = ball_x-15;
        if(input=='d')
            ball_x = ball_x+15;
    }

}

void withoutinput()  //输入无关
{
    static int speed = 0;
    static int k = 0;

    speed++;
    for(int i=0;i<barNumber;i++)
    {

        if( ball_y == bar_y[i]-bar_high/2-radius )
        {
            if(ball_x>=bar_x[i]-bar_width/2 && ball_x<=bar_x[i]+bar_width/2)
            {
                ball_v = -5;
                break;
            }
            else
                ball_v = 1;
        }

    }


    ball_y = ball_y + ball_v;
    for(int i=0;i<barNumber;i++)
    {

        if(bar_y[i]>(-bar_high/2)-5)
            bar_y[i] = bar_y[i] - 5;
        else
        {
            bar_x[i] = rand()%Width;
            bar_y[i] = High - bar_high/2;   
        }
    }



}

int main()
{
    startup();
    while(1)
    {
        clean();     
        withinput();
        withoutinput();
        show();
    }



}

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-26 06:46
    关注
     void withoutinput()  //输入无关
    {
        static int speed = 0;
        static int k = 0;
    
        speed++;
        for(int i=0;i<barNumber;i++)
        {
    
            if( ball_y - (bar_y[i]-bar_high/2-radius) < 5 && ball_y - (bar_y[i]-bar_high/2-radius) > -5) //改这里,因为不一定刚刚好相等,所以会错过
            {
                if(ball_x>=bar_x[i]-bar_width/2 && ball_x<=bar_x[i]+bar_width/2)
                {
                    ball_v = -5;
                    break;
                }
                else
                    ball_v = 1;
            }
        }
    

    图片说明

    完整代码:https://download.csdn.net/download/caozhy/10746184

    如果问题得到解决,麻烦点下我回答右边的采纳,谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条