| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 424 人关注过本帖
标题:小弟刚刚入门,写了个 4 球碰撞,但是第四个球一直不能碰,求指出错误
只看楼主 加入收藏
大柱子
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-5-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
小弟刚刚入门,写了个 4 球碰撞,但是第四个球一直不能碰,求指出错误
如题,小弟刚刚入门,写了个4球碰撞,但是第四个球一直不能碰,求指出错误,代码如下:

void mainloop()
{
   
    int x = 0, y = 0, dx = 1, dy = 1, r = 30;
    int x1=0,y1=getheight()/2,dx1=1,dy1=1;
    int x2=getwidth(),y2=getheight()/2,dx2=-1,dy2=1;
    int x3=getwidth()/2,y3=getheight(),dx3=1,dy3=-1;
    int i;
    double distance1_0,distance2_1,distance2_0,distance3_0;
   
    for ( ; is_run();delay_ms(10))
    {
        cleardevice();
        distance1_0=sqrt((double)(abs((x-x1)*(x-x1)+(y-y1)*(y-y1))));
        distance2_1=sqrt((double)(abs((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))));
        distance2_0=sqrt((double)(abs((x2-x)*(x2-x)+(y2-y)*(y2-y))));
        distance3_0=sqrt((double)(abs((x-x3)*(x-x3)+(y-y3)*(y-y3))));
        if(distance1_0<=2*r)
        {
            dx=-dx;
            dy=-dy;
            dx1=-dx1;
            dy1=-dy1;
        }
        if(distance2_0<=2*r)
        {
            dx2=-dx2;
            dy2=-dy2;
            dx=-dx;
            dy=-dy;
        }
        if(distance2_1<=2*r)
        {
            dx2=-dx2;
            dy2=-dy2;
            dx1=-dx1;
            dy1=-dy1;
        }
        if(distance3_0<=2*r)
        {
            dx3=-dx3;
            dy2=-dy3;
            dx=-dx;
            dy=-dy;
        }
        {
        x += dx;
        y += dy;
        if (x < r) dx = 1;
        if (y < r) dy = 1;
        if (x >= ege::getwidth()  - r) dx = -1;
        if (y >= ege::getheight() - r) dy = -1;
        
        setcolor(GREEN);
        setfillcolor(RED);
        fillellipse(x, y, r, r);

        x1 += dx1;
        y1 += dy1;
        if (x1 < r) dx1 = 1;
        if (y1 < r) dy1 = 1;
        if (x1 >= ege::getwidth()  - r) dx1 = -1;
        if (y1 >= ege::getheight() - r) dy1 = -1;
        
        setcolor(BLUE);
        setfillcolor(YELLOW);
        fillellipse(x1, y1, r, r);

        x2 += dx2;
        y2 += dy2;
        if (x2 < r) dx2 = 1;
        if (y2 < r) dy2 = 1;
        if (x2 >= ege::getwidth()  - r) dx2 = -1;
        if (y2 >= ege::getheight() - r) dy2 = -1;
        
        setcolor(RED);
        setfillcolor(BLUE);
        fillellipse(x2, y2, r, r);

        
        x3 += dx3;
        y3 += dy3;
        if (x3 < r) dx3 = 1;
        if (y3 < r) dy3 = 1;
        if (x3 >= ege::getwidth()  - r) dx3 = -1;
        if (y3 >= ege::getheight() - r) dy3 = -1;
        
        setcolor(WHITE);
        setfillcolor(BLUE);
        fillellipse(x3, y3, r, r);
    }
   
}
}

int main(void)
{
    setinitmode(INIT_ANIMATION);
   
    initgraph(640, 480);
   
    randomize();
   
    mainloop();
   
    closegraph();
    return 0;
}
搜索更多相关主题的帖子: double 
2014-05-29 15:17
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
怎么连头文件都舍不得公布呢  

严重怀疑你这是tc下的东东  废止

DO IT YOURSELF !
2014-05-29 15:22
大柱子
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-5-29
收藏
得分:0 
呃,我以为不用复制头文件。。。第一次发帖。。见谅啊

#include <graphics.h>
#include <stdio.h>
#include <math.h>

void mainloop()
{
   
    int x = 0, y = 0, dx = 1, dy = 1, r = 30;
    int x1=0,y1=getheight()/2,dx1=1,dy1=1;
    int x2=getwidth(),y2=getheight()/2,dx2=-1,dy2=1;
    int x3=getwidth()/2,y3=getheight(),dx3=1,dy3=-1;
    int i;
    double distance1_0,distance2_1,distance2_0,distance3_0;
   
    for ( ; is_run();delay_ms(10))
    {
        cleardevice();
        distance1_0=sqrt((double)(abs((x-x1)*(x-x1)+(y-y1)*(y-y1))));
        distance2_1=sqrt((double)(abs((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))));
        distance2_0=sqrt((double)(abs((x2-x)*(x2-x)+(y2-y)*(y2-y))));
        distance3_0=sqrt((double)(abs((x-x3)*(x-x3)+(y-y3)*(y-y3))));
        if(distance1_0<=2*r)
        {
            dx=-dx;
            dy=-dy;
            dx1=-dx1;
            dy1=-dy1;
        }
        if(distance2_0<=2*r)
        {
            dx2=-dx2;
            dy2=-dy2;
            dx=-dx;
            dy=-dy;
        }
        if(distance2_1<=2*r)
        {
            dx2=-dx2;
            dy2=-dy2;
            dx1=-dx1;
            dy1=-dy1;
        }
        if(distance3_0<=2*r)
        {
            dx3=-dx3;
            dy2=-dy3;
            dx=-dx;
            dy=-dy;
        }
        {
        x += dx;
        y += dy;
        if (x < r) dx = 1;
        if (y < r) dy = 1;
        if (x >= ege::getwidth()  - r) dx = -1;
        if (y >= ege::getheight() - r) dy = -1;
        
        setcolor(GREEN);
        setfillcolor(RED);
        fillellipse(x, y, r, r);

        x1 += dx1;
        y1 += dy1;
        if (x1 < r) dx1 = 1;
        if (y1 < r) dy1 = 1;
        if (x1 >= ege::getwidth()  - r) dx1 = -1;
        if (y1 >= ege::getheight() - r) dy1 = -1;
        
        setcolor(BLUE);
        setfillcolor(YELLOW);
        fillellipse(x1, y1, r, r);

        x2 += dx2;
        y2 += dy2;
        if (x2 < r) dx2 = 1;
        if (y2 < r) dy2 = 1;
        if (x2 >= ege::getwidth()  - r) dx2 = -1;
        if (y2 >= ege::getheight() - r) dy2 = -1;
        
        setcolor(RED);
        setfillcolor(BLUE);
        fillellipse(x2, y2, r, r);

        
        x3 += dx3;
        y3 += dy3;
        if (x3 < r) dx3 = 1;
        if (y3 < r) dy3 = 1;
        if (x3 >= ege::getwidth()  - r) dx3 = -1;
        if (y3 >= ege::getheight() - r) dy3 = -1;
        
        setcolor(WHITE);
        setfillcolor(BLUE);
        fillellipse(x3, y3, r, r);
    }
   
}
}

int main(void)
{
    setinitmode(INIT_ANIMATION);
   
    initgraph(640, 480);
   
    randomize();
   
    mainloop();
   
    closegraph();
    return 0;
}
2014-05-29 15:24
快速回复:小弟刚刚入门,写了个 4 球碰撞,但是第四个球一直不能碰,求指出错误
数据加载中...
 
   



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

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