/* ブロック崩し サンプルスクリプト ゲーム製作メモ http://minus273.s27.xrea.com/prog/ HSP3.0用 2005-08-27 作成開始。 2005-08-28 とりあえず完成。 */ ;マウスのみで操作するブロック崩しです。 ;----------いろいろな設定など #define VER "1.00" #define SOFTNAME "ブロック崩しのサンプル" #define TITLEBAR SOFTNAME+" "+VER #define EXENAME "block" #packopt type 0 #packopt name EXENAME #packopt runtime "hsp3c.hrt" #packopt hide 0 ;----------ウインドウの作成 screen 0,320,240 : title TITLEBAR ;----------初期化 *init ;主な変数の宣言 HSPではしなくてもいいけど ;したほうがあとから何の変数かわかりやすい dim block,8,4 ;ブロックの状態 あれば1 repeat 4 : j=cnt repeat 8 : i=cnt block(i,j)=1 loop loop ball_flag = 0 ;ボールの状態 あれば1 ball_num = 5 ;ボールの残り数 ball_x = 320 ;ボールの座標 ball_y = 240 ;最初は見えないように画面外に ball_vx = 0 ;ボールの速度 ball_vy = 0 bar_x = 0 ;バーの座標 ;----------メインループ *main await 17 ;1秒に60回くらいループする ;キー入力処理 stick ky,15 if ky&128 : end ;Escで終了 ;バーの座標はマウスの位置で決める bar_x = mousex - 24 ;ボールがあるときとないときでボールの処理は違う if ball_flag = 0 { if ky&256 { ;クリックされたら ball_flag = 1 ball_x = bar_x + 16 ball_y = 208 ball_vx = 4 ball_vy = -4 } } else { ;前回の座標を記録(ブロックとの衝突判定で使う) ball_x_before = ball_x ball_y_before = ball_y ;速度の分だけ座標に足す ball_x += ball_vx : ball_y += ball_vy ;画面の端に当たったら反射 if ball_x < 0 { ball_x = 0 ball_vx = -ball_vx } if ball_x > 304 { ball_x = 304 ball_vx = -ball_vx } if ball_y < 0 { ball_y = 0 ball_vy = -ball_vy } if ball_y > 240 { ;下に落ちたらボールを消す ball_flag = 0 ball_num-- if ball_num = -1 { pos 200,200 : color 255,255,255 mes "ゲームオーバー" stop } } ;バーに当たったら反射 if (204 < ball_y) & (ball_y < 209) { if (bar_x-16 < ball_x) & (ball_x < bar_x+48) { ball_vy = -ball_vy } } } ;ブロックとの衝突判定 参考:http://www2.pf-x.net/~shink/block.html ;衝突判定は難しいです……。どの方向から当たっても自然に跳ね返るようにするにはもっと工夫が必要。 tmp=0 ;二重のrepeat-loopから一気に抜け出すためのフラグ repeat 4 : j=cnt repeat 8 : i=cnt if block(i,j) = 1 { if (32 + i*32 - 16 < ball_x) & (ball_x < 62 + i*32) { if (32 + j*16 - 16 < ball_y) & (ball_y < 46 + j*16) { block(i,j) = 0 ;ブロックを消す ;前回のx座標がすでに範囲に入っていた場合y方向の向きを反転 if (32 + i*32 - 16 < ball_x_before) & (ball_x_before < 62 + i*32) { ball_vy = -ball_vy tmp=1 : break } ;前回のy座標がすでに範囲に入っていた場合x方向の向きを反転 if (32 + j*16 - 16 < ball_y_before) & (ball_y_before < 46 + j*16) { ball_vx = -ball_vx tmp=1 : break } ;どちらも入ってない場合は両方判定 ball_vx = -ball_vx ball_vy = -ball_vy } } } loop if tmp=1 : break loop ;いろいろ描画 redraw 0 color 0,0,0 : boxf ;白で全体を塗りつぶす ;ボールとバー pos ball_x,ball_y : color 255,255,255 : mes "●" pos bar_x,220 : mes "===" ;ブロック repeat 4 : j=cnt repeat 8 : i=cnt if block(i,j) = 1 { boxf 32 + i*32, 32 + j*16, 62 + i*32, 46 + j*16 } loop loop ;変数の内容を表示 pos 8,100 : color 191,191,191 mes "ball_flag = "+ball_flag mes "ball_num = "+ball_num mes "ball_x = "+ball_x mes "ball_y = "+ball_y mes "ball_vx = "+ball_vx mes "ball_vy = "+ball_vy mes "bar_x = "+bar_x redraw 1 goto *main