2011-01 << 2011-02 >> 2011-03

2011-02-15 (火)

* よりにもよって,友達を自動で放り投げ続けるプログラム

昨日書いたやつ.eldeshが書けというので仕事が忙しい中,40分かけて書いたプログラムです.

初期のスクリーンショット http://twitpic.com/3zp4cd

#include <iostream>
#include "../cppfl/all.h"
#include "../cppfl/imagetool/binarize.h"

int main() {
    BitmapWindow win("よりにもよって,友達を自動で放り投げるプログラム",400,300);
    Screen screen;
    DIBitmap dib(200,200);
    DIBitmap dib2(200,200);

    int opos = 0;
    int spos = 0, start_time = 0;

    while (win.isexist()) {
        // マウス周辺をキャプチャして二値化
        Point p = Mouse::point();
        screen.capture(dib, p.x, p.y);
        imagetool::thresholdRGB(dib2,dib,60);

        // トラック探す
        int px = 0 , py = 0;
        for (int y=0;y<195;y++) {
            for (int x=0;x<195;x++) {
                if (dib2[Point(x,y)].b == 0 && dib2[Point(x+1,y+1)].b == 0 &&
                        dib2[Point(x+1,y+2)].b == 0 && dib2[Point(x+2,y+1)].b == 0) {
                    dib2[Point(x,y)].r = 255;
                    px = x; py = y + 1;
                    break;
                }
            }
        }

        // 残り距離
        int pos = 0;
        for (int x = px-1 ; x>0 ; x--) {
            if (dib2[Point(x,py)].b == 0) {
                pos = px - x - 50; // 落下位置は左端から50pxの位置
                break;
            }
        }
        
        // pixel per sec.
        int speed = 0;
        if (opos>0 && pos>0 && spos>pos) {
            speed = (spos-pos)*1000 / (GetTickCount()-start_time);
        }

        // 行けそうなら投げる 数値は適当に調整
        if ( speed>0 && speed<55 && pos*1000/speed-1750 <= 0 && pos*1000/speed-1750 > -80 ) {
            Mouse::lclick(p.x,p.y);
        }
        

        if (pos>opos) {
            spos = pos;
            start_time = GetTickCount();
        }
        opos = pos;

        // 表示
        win.clear();
        win.draw(Point(0,0),dib2.getdc(),Rect(0,0,200,200));
        win.color(Color::green);
        win.circle(px,py,10);
        if (speed > 0) {
            win.print(STR "pos:"+ pos + " speed:" + speed + " p:" + pos*1000/speed);
        } else {
            win.print(STR "マウスで右下のあれの場所を");
        }

        Application.wait(20);
    }

    return 0;
}
2011-01 << 2011-02 >> 2011-03