/*広告*/

/*ブロック崩しの作り方 中間ソース4*/

/*目次へ戻る*/

#include "DxLib.h" #define BALL_SIZE 5 #define BALL_SPEED 5 #define BAR_SPEED 5 #define BAR_WIDTH 50 #define BAR_HEIGHT 10 #define BLOCK_WIDTH 50 #define BLOCK_HEIGHT 10 #define FIELD_WIDTH 300 #define FIELD_HEIGHT 480 #define X_POSI 170 void my_init_variable(void); void my_init_blocks(void); void my_gameover(void); void my_move_bar(void); void my_move_ball(void); void my_collision_detection(void); void my_draw_ball(void); void my_draw_bar(void); void my_draw_block(void); void my_draw_field(void); int my_get_key(void); struct BLOCKS{ int x; int y; int flag; int color; }; struct BLOCKS block; int game_state; int bar_x; int bar_y; int ball_x; int ball_y; int x_speed; int y_speed; int Color_White; int Color_Red; int Color_Green; int key[256]; int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){ ChangeWindowMode(TRUE); DxLib_Init(); SetDrawScreen(DX_SCREEN_BACK); my_init_variable(); my_init_blocks(); while (ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && my_get_key()==0){ switch(game_state){ case 0: game_state = 5; break; case 5: my_move_bar(); my_move_ball(); my_collision_detection(); my_draw_ball(); my_draw_bar(); my_draw_block(); my_draw_field(); break; case 10: my_draw_bar(); my_draw_block(); my_draw_field(); my_gameover(); break; default: break; } } DxLib_End(); return 0; } void my_init_variable(){ game_state = 0; bar_x = 0; bar_y = FIELD_HEIGHT - BAR_HEIGHT; ball_x = BAR_WIDTH / 2; ball_y = (FIELD_HEIGHT - BAR_HEIGHT) - BALL_SIZE; x_speed = BALL_SPEED; y_speed = BALL_SPEED; Color_White = GetColor(255,255,255); Color_Red = GetColor(255,0,0); Color_Green = GetColor(0,255,0); } void my_init_blocks(){ block.x = 100; block.y = 30; block.flag = 1; block.color = Color_Green; } void my_gameover(){ DrawFormatString(280,240,Color_White,"GAME OVER"); } void my_move_ball(){ ball_x += x_speed; ball_y += y_speed; } void my_move_bar(){ if (key[KEY_INPUT_RIGHT] > 0 && (bar_x + BAR_WIDTH) < FIELD_WIDTH){ bar_x += BAR_SPEED; } if (key[KEY_INPUT_LEFT] > 0 && bar_x > 0){ bar_x -= BAR_SPEED; } } void my_collision_detection(){ if(ball_x >= FIELD_WIDTH){ ball_x = FIELD_WIDTH - BALL_SIZE; x_speed *= -1; } if(ball_x <= 0){ ball_x = BALL_SIZE; x_speed *= -1; } if(ball_y <= 0){ ball_y = BALL_SIZE; y_speed *= -1; } if(ball_y >= FIELD_HEIGHT){ game_state = 10; } if (ball_x > bar_x && ball_x < (bar_x + BAR_WIDTH) && ball_y >= bar_y){ ball_y = bar_y - BALL_SIZE; y_speed *= -1; } if(block.flag == 1){ if (ball_x >= block.x && ball_x <= block.x + BLOCK_WIDTH){ if (ball_y >= block.y && ball_y <= block.y + 3){ ball_y = block.y; y_speed *= -1; block.flag = 0; } else if (ball_y <= block.y + BLOCK_HEIGHT && ball_y >= block.y + BLOCK_HEIGHT - 3){ ball_y = block.y + BLOCK_HEIGHT; y_speed *= -1; block.flag = 0; } } else if (ball_y >= block.y && ball_y <= block.y + BLOCK_HEIGHT){ if (ball_x >= block.x && ball_x <= block.x + 3){ ball_x = block.x; x_speed *= -1; block.flag = 0; } else if (ball_x <= block.x + BLOCK_WIDTH && ball_x >= block.x + BLOCK_WIDTH - 3){ ball_x = block.x + BLOCK_WIDTH; x_speed *= -1; block.flag = 0; } } } } void my_draw_ball(){ DrawCircle(X_POSI + ball_x,ball_y,BALL_SIZE,Color_White,false); } void my_draw_bar(){ DrawBox(X_POSI + bar_x,bar_y,X_POSI + bar_x + BAR_WIDTH,bar_y + BAR_HEIGHT,Color_Red,true); } void my_draw_block(){ if(block.flag == 1){ DrawBox(X_POSI + block.x,block.y,X_POSI + block.x + BLOCK_WIDTH,block.y + BLOCK_HEIGHT,block.color,true); } } void my_draw_field(){ DrawBox(X_POSI + 0,0,X_POSI + FIELD_WIDTH,FIELD_HEIGHT,Color_White,false); } int my_get_key(){ char keys[256]; GetHitKeyStateAll(keys); for (int i = 0; i < 256; i++){ if (keys[i] != 0){ key[i]++; } else{ key[i] = 0; } } return 0; }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

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

/*広告*/