#include <graph.h>
main()
{
HBITMAP hbmp_ball;
int b_xx, //ボールの位置
bold_xx; //ボールの旧位置
unsigned long
tick;
gl_openwin(-1, -1, 640, 480, 0);
hbmp_ball = gl_loadbitmap("ball.bmp");
if(hbmp_ball == NULL){
printf("ビットマップファイルが見つかりません.\n");
exit(0);
}
bold_xx = -1;
b_xx = 320;
tick = GetTickCount();
for(;;){
if(GetTickCount() >= tick && GetTickCount() < tick + 50)
continue;
tick = GetTickCount();
if(kbhit()){
switch(getch()){
case 7: //キー入力は左カーソル
b_xx -= 10;
//ボールが画面からはみ出さないようにする処理
if(b_xx < 0)
b_xx = 0;
break;
case 8: //キー入力は右カーソル
b_xx += 10;
//ボールが画面からはみ出さないようにする処理
if(b_xx > 640 - 16)
b_xx = 640 - 16;
break;
}
}
if(bold_xx != b_xx){ //ボールの旧位置から移動していれば再描画
//旧位置(bold_xx)のボールを消去、bold_xxが-1の時(起動直後)は何もしない
if(bold_xx != -1){
gl_fillrect(bold_xx, 240, bold_xx + 16, 240 + 16, RGB(0, 0, 0));
}
//新しい位置にボールを描く
gl_drawbitmap(hbmp_ball, b_xx, 240, 16, 16, 0, 0);
//旧位置(bold_xx)を新しいボールの位置に置き換える
bold_xx = b_xx;
}
gl_refresh();
}
}
|