/*広告*/

/*ブロックパズルの作り方 完成ソース*/

/*目次へ戻る*/

#include "DxLib.h" #include <stdlib.h> #include <time.h> #define DRAW_POSITION 140 #define NEXT_BLOCK_X 500 #define NEXT_BLOCK_Y 20 #define HOLD_BLOCK_X 60 #define HOLD_BLOCK_Y 20 #define BLOCK_HEIGHT 4 #define BLOCK_WIDTH 4 #define STAGE_HEIGHT 23 #define STAGE_WIDTH 18 #define DRAW_BLOCK_WIDTH 20 void my_init_var(void); void my_init_var2(void); void my_op(void); void my_ed(void); void my_make_block(void); void my_hold_block(void); void my_gameover(void); void my_get_key(void); void my_turn_right(void); void my_turn_left(void); void my_move_block(void); void my_collision_left(void); void my_collision_right(void); void my_collision_bottom(void); void my_collision_center(void); void my_collision_turn(void); void my_fix_block(void); void my_search_line(void); void my_clear_line(void); void my_save_block(void); void my_draw_back(void); void my_draw_variable(void); void my_draw_block(void); void my_draw_stage(void); void my_fall_block(void); void my_change_lv(void); int block[BLOCK_HEIGHT][BLOCK_WIDTH]; int turn_block[BLOCK_HEIGHT][BLOCK_WIDTH]; int next_block[BLOCK_HEIGHT][BLOCK_WIDTH]; int hold_block[BLOCK_HEIGHT][BLOCK_WIDTH]; int stage[STAGE_HEIGHT][STAGE_WIDTH]; int blocks[BLOCK_HEIGHT * 6][BLOCK_WIDTH * 4] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0}, {0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,2,2,0,0,0,2,0,0,2,0,0,0,2,2,0}, {0,0,2,0,0,2,2,0,0,2,2,0,0,2,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,3,3,0,0,3,3,0,0,0,3,0,0,3,0,0}, {0,3,0,0,0,0,3,0,0,3,3,0,0,3,3,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,4,0,4,0,0,4,4,0,0,0,0,0,4,4,0}, {0,4,4,4,0,0,4,0,0,4,4,4,0,0,4,0}, {0,0,0,0,0,0,4,4,0,4,0,4,0,4,4,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,5,0,0,0,0,0,0,0,5,0,0,0,5,0,0}, {0,5,5,0,5,5,5,0,5,5,0,0,5,5,5,0}, {0,5,0,0,0,5,0,0,0,5,0,0,0,0,0,0}, {0,6,0,0,0,0,0,0,0,6,0,0,0,0,0,0}, {0,6,0,0,6,6,6,6,0,6,0,0,6,6,6,6}, {0,6,0,0,0,0,0,0,0,6,0,0,0,0,0,0}, {0,6,0,0,0,0,0,0,0,6,0,0,0,0,0,0} }; int clear_line_point[STAGE_HEIGHT - 3]; int game_state; int block_x; int block_y; float block_y_count; float block_speed; int collision_flag; int gameover_flag; int first_block_flag; int make_block_flag; int hold_block_flag; int clear_flag; int block_id; int next_block_id; int hold_block_id; int clear_count; int turn_point; int hold_turn_point; int block_y_point; int fix_count; int lv; int score; int Color_Red; int Color_Black; int Color_Green; int Color_Blue; int Color_Yellow; int Color_Fuchsia; int Color_Aqua; int back_img1; int key[256]; int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){ ChangeWindowMode(TRUE); DxLib_Init(); SetDrawScreen(DX_SCREEN_BACK); my_init_var(); while(ProcessMessage() == 0){ ClearDrawScreen(); switch(game_state){ case 0: my_get_key(); my_draw_back(); my_draw_variable(); my_draw_stage(); my_op(); if(key[KEY_INPUT_RETURN] == 1){ game_state = 5; } break; case 5: if(clear_flag == 0){ my_make_block(); my_gameover(); my_get_key(); my_move_block(); my_draw_back(); my_draw_variable(); my_draw_block(); my_draw_stage(); my_fix_block(); my_fall_block(); } else{ my_clear_line(); my_draw_back(); my_draw_variable(); my_draw_stage(); } if(gameover_flag == 1){ game_state = 10; } break; case 10: my_draw_back(); my_draw_variable(); my_draw_block(); my_draw_stage(); my_ed(); break; default: break; } ScreenFlip(); } DxLib_End(); return 0; } void my_init_var(){ int i,j; for(i=0;i<STAGE_HEIGHT;i++){ for(j=0;j<STAGE_WIDTH;j++){ stage[i][0] = 9; stage[i][1] = 9; stage[i][2] = 9; stage[20][j] = 9; stage[21][j] = 9; stage[22][j] = 9; stage[i][15] = 9; stage[i][16] = 9; stage[i][17] = 9; } } game_state = 0; block_x = 7; block_y = 0; block_y_count = 0; block_speed = 0.5f; collision_flag = 0; gameover_flag = 0; first_block_flag = 1; make_block_flag = 1; hold_block_flag = 0; block_id = 0; next_block_id = 0; hold_block_id = 0; clear_count = 0; turn_point = 0; hold_turn_point = 0; block_y_point = 0; fix_count = 0; lv = 1; score = 0; Color_Red = GetColor(255,0,0); Color_Black = GetColor(0,0,0); Color_Green = GetColor(0,255,0); Color_Blue = GetColor(0,0,255); Color_Yellow = GetColor(255,255,0); Color_Fuchsia = GetColor(255,0,255); Color_Aqua = GetColor(0,255,255); back_img1 = LoadGraph("./back_img1.jpg"); srand((unsigned)time(NULL)); } void my_init_var2(){ block_x = 7; block_y = 0; block_y_count = 0; make_block_flag = 1; turn_point = 0; block_y_point = 0; fix_count = 0; } void my_op(){ DrawFormatString(260,220,Color_Black,"BLOCK PUZZLE"); DrawFormatString(210,240,Color_Black,"press enter key to start"); } void my_ed(){ DrawFormatString(280,220,Color_Black,"GAME OVER"); } /*ブロック生成を2段階に*/ /*先に生成したブロックを「next_block_id」に保存*/ /*すぐに「block_id」に保存しなおして再び*/ /*ブロック生成処理を行い「next_block_id」に保存する*/ /*2回目以降はこれを繰り返す*/ void my_make_block(){ int x,y; if(first_block_flag == 1){ next_block_id = (rand() % 6); for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ next_block[y][x] = blocks[(next_block_id * BLOCK_HEIGHT) + y][x]; } } first_block_flag = 0; } if(make_block_flag == 1){ block_id = next_block_id; next_block_id = (rand() % 6); for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ block[y][x] = next_block[y][x]; next_block[y][x] = blocks[(next_block_id * BLOCK_HEIGHT) + y][x]; } } make_block_flag = 0; } } /*ブロックホールド処理*/ /*初回ホールド時はそのまま「hold_block[4][4]」に*/ /*その時の「block_id」など共に保存、初期化の「my_init_var2()」を行う*/ /*2回目以降は先ほどの「hold_block[4][4]」との入れ替え処理を行い*/ /*そのままブロックの位置だけを元に戻す*/ void my_hold_block(){ int x,y; int tmp_block[4][4] = {0}; int tmp_block_id; int tmp_turn_point; if(hold_block_flag == 0){ for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ hold_block[y][x] = block[y][x]; } } hold_block_id = block_id; hold_turn_point = turn_point; my_init_var2(); my_make_block(); hold_block_flag = 1; } else{ for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ tmp_block[y][x] = block[y][x]; block[y][x] = hold_block[y][x]; hold_block[y][x] = tmp_block[y][x]; } } tmp_block_id = block_id; tmp_turn_point = turn_point; block_id = hold_block_id; turn_point = hold_turn_point; hold_block_id = tmp_block_id; hold_turn_point = tmp_turn_point; block_x = 7; block_y = 0; block_y_count = 0; } } void my_gameover(){ my_collision_center(); if(collision_flag != 0){ gameover_flag = 1; } } void my_move_block(){ if(key[KEY_INPUT_LEFT] % 5 == 1){ my_collision_left(); if(collision_flag == 0){ block_x--; } } if(key[KEY_INPUT_RIGHT] % 5 == 1){ my_collision_right(); if(collision_flag == 0){ block_x++; } } if(key[KEY_INPUT_DOWN] % 5 == 1){ my_collision_bottom(); if(collision_flag == 0){ block_y++; block_y_count = block_y * DRAW_BLOCK_WIDTH; } } if(key[KEY_INPUT_UP] == 1){ my_turn_right(); } if(key[KEY_INPUT_Z] == 1){ my_turn_left(); } if(key[KEY_INPUT_H] == 1){ my_hold_block(); } } void my_turn_right(){ int x,y; turn_point++; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ turn_block[y][x] = blocks[(block_id * BLOCK_HEIGHT) + y][(turn_point % 4 * BLOCK_WIDTH) + x]; } } my_collision_turn(); if(collision_flag == 0){ for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ block[y][x] = turn_block[y][x]; } } } else{ turn_point--; } } void my_turn_left(){ int x,y; turn_point += 3; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ turn_block[y][x] = blocks[(block_id * BLOCK_HEIGHT) + y][(turn_point % 4 * BLOCK_WIDTH) + x]; } } my_collision_turn(); if(collision_flag == 0){ for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ block[y][x] = turn_block[y][x]; } } } else{ turn_point -= 3; } } void my_collision_left(){ int x,y; collision_flag = 0; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] != 0){ if(stage[block_y + y][block_x + (x - 1)] != 0){ collision_flag = 1; } else if((int)(block_y_count - (block_y * DRAW_BLOCK_WIDTH)) > 0){ if(stage[block_y + (y + 1)][block_x + (x - 1)] != 0){ collision_flag = 1; } } } } } } void my_collision_right(){ int x,y; collision_flag = 0; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] != 0){ if(stage[block_y + y][block_x + (x + 1)] != 0){ collision_flag = 1; } else if((int)(block_y_count - (block_y * DRAW_BLOCK_WIDTH)) > 0){ if(stage[block_y + (y + 1)][block_x + (x + 1)] != 0){ collision_flag = 1; } } } } } } void my_collision_bottom(){ int x,y; collision_flag = 0; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] != 0){ if(stage[block_y + (y + 1)][block_x + x] != 0){ collision_flag = 1; } } } } } void my_collision_center(){ int x,y; collision_flag = 0; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] != 0){ if(stage[block_y + y][block_x + x] != 0){ collision_flag = 1; } } } } } void my_collision_turn(){ int x,y; collision_flag = 0; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(turn_block[y][x] != 0){ if(stage[block_y + y][block_x + x] != 0){ collision_flag = 1; } } } } } /*ブロック固定を2段階に*/ /*最初は固定せずにその時のブロックのy座標を保存*/ /*固定までのカウントを始める*/ /*指定したカウント後も同じy座標にブロックがあるようなら固定*/ void my_fix_block(){ my_collision_bottom(); if(collision_flag != 0){ if(block_y_point == block_y && fix_count > 10){ my_save_block(); my_search_line(); if(clear_flag == 0){ my_init_var2(); } } else{ if(block_y_point == block_y){ fix_count++; } else{ fix_count = 0; } block_y_point = block_y; block_y_count -= block_speed; } } } void my_search_line(){ int i,j; for(i=0;i<STAGE_HEIGHT - 3;i++){ clear_line_point[i] = 0; } for(i=0;i<STAGE_HEIGHT - 3;i++){ for(j=3;j<STAGE_WIDTH - 3;j++){ if(stage[i][j] == 0){ clear_line_point[i] = 1; break; } } } for(i=0;i<STAGE_HEIGHT - 3;i++){ if(clear_line_point[i] == 0){ clear_flag = 1; score++; } } } void my_clear_line(){ int i,j; int remain_line_point[20] = {0}; int remain_line_index = 0; if(clear_count < 12){ for(i=0;i<STAGE_HEIGHT - 3;i++){ if(clear_line_point[i] == 0){ stage[i][clear_count + 3] = 0; } } clear_count++; } else{ for(i=STAGE_HEIGHT - 4;i >= 0;i--){ if(clear_line_point[i] != 0){ remain_line_point[remain_line_index] = i; remain_line_index++; } } remain_line_index = 0; for(i=STAGE_HEIGHT - 4;i >= 0;i--){ for(j=3;j<STAGE_WIDTH - 3;j++){ stage[i][j] = stage[remain_line_point[remain_line_index]][j]; } remain_line_index++; } clear_flag = 0; clear_count = 0; my_init_var2(); my_change_lv(); } } void my_save_block(){ int x,y; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ stage[block_y + y][block_x + x] += block[y][x]; } } } void my_draw_back(){ DrawGraph(0,0,back_img1,TRUE); } /*スコア・ネクストブロック・ホールドブロック表示部分追加*/ void my_draw_variable(){ int x,y; DrawBox(470,0,610,160,Color_Black,FALSE); DrawBox(30,0,170,160,Color_Black,FALSE); DrawFormatString(520, 0, Color_Black, "NEXT"); DrawFormatString(80, 0, Color_Black, "HOLD"); DrawFormatString(500, 100, Color_Black, "SCORE %d",score * 10); DrawFormatString(500, 120, Color_Black, "LINE %d",score); DrawFormatString(500, 140, Color_Black, "LV %d",lv); for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(next_block[y][x] == 1)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Red, "■"); else if(next_block[y][x] == 2)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Green, "■"); else if(next_block[y][x] == 3)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Blue, "■"); else if(next_block[y][x] == 4)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Yellow, "■"); else if(next_block[y][x] == 5)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Fuchsia, "■"); else if(next_block[y][x] == 6)DrawFormatString(NEXT_BLOCK_X + x * 20 ,NEXT_BLOCK_Y + y * 20, Color_Aqua, "■"); } } for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(hold_block[y][x] == 1)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Red, "■"); else if(hold_block[y][x] == 2)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Green, "■"); else if(hold_block[y][x] == 3)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Blue, "■"); else if(hold_block[y][x] == 4)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Yellow, "■"); else if(hold_block[y][x] == 5)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Fuchsia, "■"); else if(hold_block[y][x] == 6)DrawFormatString(HOLD_BLOCK_X + x * 20 ,HOLD_BLOCK_Y + y * 20, Color_Aqua, "■"); } } } void my_draw_block(){ int x,y; for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] == 1)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Red,"■"); else if(block[y][x] == 2)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Green,"■"); else if(block[y][x] == 3)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Blue,"■"); else if(block[y][x] == 4)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Yellow,"■"); else if(block[y][x] == 5)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Fuchsia,"■"); else if(block[y][x] == 6)DrawFormatString(DRAW_POSITION + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, block_y_count + y * DRAW_BLOCK_WIDTH,Color_Aqua,"■"); } } } void my_draw_stage(){ int x,y; for(y=0;y<STAGE_HEIGHT - 2;y++){ for(x=2;x<STAGE_WIDTH - 2;x++){ if(stage[y][x] == 1)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Red,"■"); else if(stage[y][x] == 2)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Green,"■"); else if(stage[y][x] == 3)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Blue,"■"); else if(stage[y][x] == 4)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Yellow,"■"); else if(stage[y][x] == 5)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Fuchsia,"■"); else if(stage[y][x] == 6)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Aqua,"■"); else if(stage[y][x] == 9)DrawFormatString(DRAW_POSITION + x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Black,"■"); } } } void my_fall_block(){ if(make_block_flag == 0){ block_y_count += block_speed; block_y = block_y_count / DRAW_BLOCK_WIDTH; } } void my_get_key(){ int i; char keys[256]; GetHitKeyStateAll(keys); for(i=0;i<256;i++){ if(keys[i] != 0){ key[i]++; } else{ key[i] = 0; } } } void my_change_lv(){ if(score < 10){ lv = 1; block_speed = 0.5f; } else if(score < 20){ lv = 2; block_speed = 1.0f; } else if(score < 30){ lv = 3; block_speed = 2.0f; } else if(score < 40){ lv = 4; block_speed = 5.0f; } else{ lv = 5; block_speed = 10.0f; } }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

/*Copyright 2016 K.N/petitetech.com*/

/*広告*/