e-fs.info

移動範囲設定


キャラ移動制限

画像を表示させ動き回るとウィンドウからはみ出してしまう。
なので、移動制限を設ける。

これ以外にも方法はあるので、考えてみる事をお勧めします。

実行サンプル


プログラム

▽ 全体表示
static int x=20,y=50;
static const int width = 48;    // sekitoriの幅
static const int height= 48;    // sekitoriの高さ
static const int speed = 5;
static Graphic sekitori,background;

bool Start(HDC hdc){

    // 画像を読み込む
    sekitori.Create(hdc,"Graphic/sekitori1.bmp");
    background.Create(hdc,"Graphic/back.bmp");
    return true;
}

// メインループ
void Mainloop(HDC hdc,const int frame){
    if(GetKeyState(VK_UP   ) & 0x80){
        y -= speed;
        if(y < 0)y = 0;
    }else 
    if(GetKeyState(VK_DOWN ) & 0x80){
        y += speed;
        if(y+height > WINY)y = WINY - height;
    }

    if(GetKeyState(VK_RIGHT) & 0x80){
        x += speed;
        if(x+width > WINX)x = WINX - width;
    }else 
    if(GetKeyState(VK_LEFT ) & 0x80){
        x -= speed;
        if(x < 0)x = 0;
    }


    background.Draw(hdc,0,0,320,240);

    // RGB(255,255,255)を透明にして描画
    sekitori.TrDraw(hdc,x,y,width,height,0,0,RGB(255,255,255));

    // FPSを表示
    FPS_Show(hdc,0,0);
}

bool End(HDC hdc){
    return true;
}