graphics矩形检测

作者: admin
标签: 平面几何
更新: 8/9/2019, 11:46:45 AM
#include <graphics.h>
#include <cmath>
void control(int& x1,int& y1,int w1,int h1,int x2,int y2,int w2,int h2){
    if(kbhit()){
        char ch = getch();
        if(ch == 'a') x1-=5;
        if(ch == 'd') x1+=5;
        if(ch == 'w') y1-=5;
        if(ch == 's') y1+=5;
        if((abs((x1+w1/2)-(x2+w2/2))<(w1/2+w2/2)&&abs((y1+h1/2)-(y2+h2/2))<(h1/2+h2/2))){
            xyprintf(100,100,"碰撞了");
        }
    }
}
int main(){
    initgraph(640,480);
    int w1 = 100, h1=40;
    int w2 = 100, h2=60;
    int x1=20,y1=20,x2=200,y2=200;
    while(is_run()){
        rectangle(x1,y1,x1+w1,y1+h1);
        rectangle(x2,y2,x2+w2,y2+h2);
        control(x1,y1,w1,h1,x2,y2,w2,h2);
        delay_fps(60);
        cleardevice();
    }
    return 0;
} 
删除
修改

asdfgh

当时的首都首都华盛顿1对方的1

点击登陆评论