ブロック崩し完成
Posted: 2010年6月03日(木) 21:54
旧ハナブトオオトカゲです。
名前を変えてUltimateにしました。
これからも、少しでもUltimate(究極)に近付くよう頑張りたいと思います。
ところで、本題ですが、ついにhttp://www.play21.jp/board/formz.cgi?ac ... &rln=53238でもらった回答を参考に、自力でブロック表示と当たり判定などをやってブロック崩しができました。
次は、中級編に進むべきでしょうか?
それとも初級編までの知識を使って自分で何かを作って練習した方がいいでしょうか?
ちなみに、ブロック崩しのソースは以下のとおりです。
よかったらやってみて感想を教えてください。(多分1分もあれば終わります。)
特殊効果は
Iキーでバーが長くなる。
XとY同時押しでボール復活。
はじめてのDXライブラリの作品です。(初めて約1週間半と少しくらい)
#include "DxLib.h"
typedef struct
{
int x; //ボールX座標
int y; //ボールY座標
int dx; //Xスピード
int dy; //Yスピード
int r; //円の直半径
int flag; //発射フラグ
}ch_t;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ChangeWindowMode(TRUE);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
ch_t ball;
ball.dx = 0;
ball.dy = 0;
ball.r = 5;
ball.flag = 0;
int blocksum=0; //壊したブロック合計
int block[4][8],blockflag[4][8],blockcolor[4][8];//ブロック表示のための配列
int bx = 270; //バーX座標
int by = 460; //バーY座標
int blong=100; //バーの長さ
int color = GetColor(255, 255, 255); //バー、ボールの色
char key[256]; //キー入力の配列
int ballmax=0,wcount=0,scount=0; //ボール発射関係
ball.x = 320; //ボールX座標
ball.y = 455; //ボールY座標
int i,j; //for文用
//ブロックの色設定
for(i=0;i<4;i++){
for(j=0;j<8;j++){
blockcolor[j]=GetColor(GetRand(255),GetRand(255),GetRand(255));
}
}
//ブロック初期化 表示フラグを1に
for(i=0;i<4;i++){
for(j=0;j<8;j++){
blockflag[j]=1;
}
}
while(ProcessMessage() != -1)
{
ClearDrawScreen();
GetHitKeyStateAll(key);
SetDrawBright( 255 , 255 , 255 );
if(key[KEY_INPUT_ESCAPE])
{
break;
}
// 発射
if(key[KEY_INPUT_SPACE]&&scount==0)
{
ball.flag=1;
ball.dx = 7;
ball.dy = -7;
scount=1;
wcount=0;
}
// バー移動と発射前ボール移動
if(key[KEY_INPUT_RIGHT])
{
if(ball.flag==0) ball.x += 8;
bx += 8;
}
if(key[KEY_INPUT_LEFT])
{
if(ball.flag==0) ball.x -= 8;
bx -= 8;
}
// バー移動範囲と発射前ボール範囲
if(bx > 640-blong)
{
if(ball.flag==0) ball.x = 640-blong/2;
bx = 640-blong;
}
if(bx < 0)
{
if(ball.flag==0) ball.x = blong/2;
bx = 0;
}
//特殊効果 Iキーでバーの長さを伸ばす
if(key[KEY_INPUT_I]==1) blong=200;
//特殊効果 XとY同時押しでボール数リセット
if(key[KEY_INPUT_X]==1&&key[KEY_INPUT_Y]==1){
ballmax=0;
SetDrawBright( 255 , 255 , 0 );
}
// ボールの移動 壁との当たり判定
if(ball.flag==1)
{
// 移動距離計算
ball.x += ball.dx;
ball.y += ball.dy;
// 移動範囲
if(ball.x < 5 || ball.x > 640 - 5)
{
ball.dx *= -1;
}
if(ball.y < 0)
{
ball.dy *= -1;
}
if(by <= ball.y && by + 10 >= ball.y && bx <= ball.x && bx + blong >= ball.x)
{
ball.dy *= -1;
}
}
//ブロックとの当たり判定
for(i=0;i<4;i++){
for(j=0;j<8;j++){
if(blockflag[j]==1&&ball.y-5<=i*30+30&&ball.y-5>=i*30&&ball.x>=j*82&&ball.x<=j*82+64){
blockflag[j]=0;
ball.dy *= -1;
blocksum+=1;
}
}
}
//ボールが下に落ちた時
if(ball.y>480&&wcount==0&&ballmax<3){
ballmax += 1;
wcount = 1;
ball.flag = 0;
ball.y = 455;
ball.x = bx+blong/2;
scount=0;
}
if(ball.y>480&&wcount==0&&ballmax==3) ballmax+=1;
//ブロック絵画
for(i=0;i<4;i++){
for(j=0;j<8;j++){
if(blockflag[j]==1){
DrawBox(j*82+5,i*30+10,j*82+64,i*30+30,blockcolor[j],TRUE);
}
}
}
//その他絵画
DrawBox(bx, by, bx + blong, by + 10, color, TRUE); // バー絵画
DrawCircle(ball.x, ball.y, ball.r, color, TRUE); // ボール絵画
if(ballmax<=3)DrawFormatString(0,460,color,"ボール残り%d",3-ballmax);//ボール残り数表示
if(ballmax==4&&blocksum!=32)DrawString(0,460,"GAME OVER",color,TRUE);
if(blocksum==32){
ClearDrawScreen();
DrawString(250,230,"ゲームクリアー",color,TRUE);
}
ScreenFlip();
}
DxLib_End();
return 0;
}

名前を変えてUltimateにしました。
これからも、少しでもUltimate(究極)に近付くよう頑張りたいと思います。
ところで、本題ですが、ついにhttp://www.play21.jp/board/formz.cgi?ac ... &rln=53238でもらった回答を参考に、自力でブロック表示と当たり判定などをやってブロック崩しができました。
次は、中級編に進むべきでしょうか?
それとも初級編までの知識を使って自分で何かを作って練習した方がいいでしょうか?
ちなみに、ブロック崩しのソースは以下のとおりです。
よかったらやってみて感想を教えてください。(多分1分もあれば終わります。)
特殊効果は
Iキーでバーが長くなる。
XとY同時押しでボール復活。
はじめてのDXライブラリの作品です。(初めて約1週間半と少しくらい)
#include "DxLib.h"
typedef struct
{
int x; //ボールX座標
int y; //ボールY座標
int dx; //Xスピード
int dy; //Yスピード
int r; //円の直半径
int flag; //発射フラグ
}ch_t;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ChangeWindowMode(TRUE);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
ch_t ball;
ball.dx = 0;
ball.dy = 0;
ball.r = 5;
ball.flag = 0;
int blocksum=0; //壊したブロック合計
int block[4][8],blockflag[4][8],blockcolor[4][8];//ブロック表示のための配列
int bx = 270; //バーX座標
int by = 460; //バーY座標
int blong=100; //バーの長さ
int color = GetColor(255, 255, 255); //バー、ボールの色
char key[256]; //キー入力の配列
int ballmax=0,wcount=0,scount=0; //ボール発射関係
ball.x = 320; //ボールX座標
ball.y = 455; //ボールY座標
int i,j; //for文用
//ブロックの色設定
for(i=0;i<4;i++){
for(j=0;j<8;j++){
blockcolor[j]=GetColor(GetRand(255),GetRand(255),GetRand(255));
}
}
//ブロック初期化 表示フラグを1に
for(i=0;i<4;i++){
for(j=0;j<8;j++){
blockflag[j]=1;
}
}
while(ProcessMessage() != -1)
{
ClearDrawScreen();
GetHitKeyStateAll(key);
SetDrawBright( 255 , 255 , 255 );
if(key[KEY_INPUT_ESCAPE])
{
break;
}
// 発射
if(key[KEY_INPUT_SPACE]&&scount==0)
{
ball.flag=1;
ball.dx = 7;
ball.dy = -7;
scount=1;
wcount=0;
}
// バー移動と発射前ボール移動
if(key[KEY_INPUT_RIGHT])
{
if(ball.flag==0) ball.x += 8;
bx += 8;
}
if(key[KEY_INPUT_LEFT])
{
if(ball.flag==0) ball.x -= 8;
bx -= 8;
}
// バー移動範囲と発射前ボール範囲
if(bx > 640-blong)
{
if(ball.flag==0) ball.x = 640-blong/2;
bx = 640-blong;
}
if(bx < 0)
{
if(ball.flag==0) ball.x = blong/2;
bx = 0;
}
//特殊効果 Iキーでバーの長さを伸ばす
if(key[KEY_INPUT_I]==1) blong=200;
//特殊効果 XとY同時押しでボール数リセット
if(key[KEY_INPUT_X]==1&&key[KEY_INPUT_Y]==1){
ballmax=0;
SetDrawBright( 255 , 255 , 0 );
}
// ボールの移動 壁との当たり判定
if(ball.flag==1)
{
// 移動距離計算
ball.x += ball.dx;
ball.y += ball.dy;
// 移動範囲
if(ball.x < 5 || ball.x > 640 - 5)
{
ball.dx *= -1;
}
if(ball.y < 0)
{
ball.dy *= -1;
}
if(by <= ball.y && by + 10 >= ball.y && bx <= ball.x && bx + blong >= ball.x)
{
ball.dy *= -1;
}
}
//ブロックとの当たり判定
for(i=0;i<4;i++){
for(j=0;j<8;j++){
if(blockflag[j]==1&&ball.y-5<=i*30+30&&ball.y-5>=i*30&&ball.x>=j*82&&ball.x<=j*82+64){
blockflag[j]=0;
ball.dy *= -1;
blocksum+=1;
}
}
}
//ボールが下に落ちた時
if(ball.y>480&&wcount==0&&ballmax<3){
ballmax += 1;
wcount = 1;
ball.flag = 0;
ball.y = 455;
ball.x = bx+blong/2;
scount=0;
}
if(ball.y>480&&wcount==0&&ballmax==3) ballmax+=1;
//ブロック絵画
for(i=0;i<4;i++){
for(j=0;j<8;j++){
if(blockflag[j]==1){
DrawBox(j*82+5,i*30+10,j*82+64,i*30+30,blockcolor[j],TRUE);
}
}
}
//その他絵画
DrawBox(bx, by, bx + blong, by + 10, color, TRUE); // バー絵画
DrawCircle(ball.x, ball.y, ball.r, color, TRUE); // ボール絵画
if(ballmax<=3)DrawFormatString(0,460,color,"ボール残り%d",3-ballmax);//ボール残り数表示
if(ballmax==4&&blocksum!=32)DrawString(0,460,"GAME OVER",color,TRUE);
if(blocksum==32){
ClearDrawScreen();
DrawString(250,230,"ゲームクリアー",color,TRUE);
}
ScreenFlip();
}
DxLib_End();
return 0;
}
