/*広告*/

/*ブロックパズルの作り方 中間ソース5*/

/*目次へ戻る*/

#include "DxLib.h" #include <stdlib.h> #include <time.h> #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_ed(void); void my_make_block(void); void my_gameover(void); void my_get_key(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_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); int 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 make_block_flag; int clear_flag; int block_id; int clear_count; 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: 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_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; make_block_flag = 1; block_id = 0; clear_count = 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; } void my_ed(){ DrawFormatString(400,400,Color_Black,"GAME OVER"); } void my_make_block(){ int x,y; if(make_block_flag == 1){ block_id = (rand() % 6); for(y=0;y<BLOCK_HEIGHT;y++){ for(x=0;x<BLOCK_WIDTH;x++){ block[y][x] = blocks[(block_id * BLOCK_HEIGHT) + y][x]; } } make_block_flag = 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; } } } 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_fix_block(){ my_collision_bottom(); if(collision_flag != 0){ my_save_block(); my_search_line(); if(clear_flag == 0){ my_init_var2(); } } } 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; break; } } } 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(); } } 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(){ DrawFormatString(400,400,Color_Black,"block_x = %d",block_x); DrawFormatString(400,420,Color_Black,"block_y = %d",block_y); DrawFormatString(400,440,Color_Black,"block_y_count = %f",block_y_count); } 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(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(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(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(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(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(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(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Red,"■"); else if(stage[y][x] == 2)DrawFormatString(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Green,"■"); else if(stage[y][x] == 3)DrawFormatString(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Blue,"■"); else if(stage[y][x] == 4)DrawFormatString(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Yellow,"■"); else if(stage[y][x] == 5)DrawFormatString(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Fuchsia,"■"); else if(stage[y][x] == 6)DrawFormatString(x * DRAW_BLOCK_WIDTH, y * DRAW_BLOCK_WIDTH,Color_Aqua,"■"); else if(stage[y][x] == 9)DrawFormatString(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; } } }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

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

/*広告*/