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

/*目次へ戻る*/ #include "DxLib.h" #define BALL_SPEED 5 #define BALL_SIZE 5 #define BAR_SPEED 5 #define BAR_WIDTH 50 #define BAR_HEIGHT 10 #define FIELD_WIDTH 300 #define FIELD_HEIGHT 480 #define X_POSI 170 void my_init_variable(void); void my_move_ball(void); void my_move_bar(void); void my_cd_check(void); void my_draw_field(void); int my_get_key(void); int bar_x; int bar_y; int ball_x; int ball_y; int x_direction; int y_direction; int key[256]; int Color_White; int Color_Red; int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){ ChangeWindowMode(TRUE); DxLib_Init(); SetDrawScreen(DX_SCREEN_BACK); my_init_variable(); while (ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && my_get_key()==0){ my_move_ball(); my_move_bar(); my_cd_check(); my_draw_field(); } DxLib_End(); return 0; } void my_init_variable(){ bar_x = 0; bar_y = FIELD_HEIGHT - BAR_HEIGHT; ball_x = BAR_WIDTH / 2; ball_y = (FIELD_HEIGHT - BAR_HEIGHT) - BALL_SIZE; x_direction = 0; y_direction = 0; Color_White = GetColor(255, 255, 255); Color_Red = GetColor(255, 0, 0); } void my_move_ball(){ if(x_direction == 0){ ball_x = ball_x + BALL_SPEED; } else{ ball_x = ball_x - BALL_SPEED; } if(y_direction == 0){ ball_y = ball_y + BALL_SPEED; } else{ ball_y = ball_y - BALL_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_cd_check(){ if(ball_x >= FIELD_WIDTH){ x_direction = 1; } if(ball_x <= 0){ x_direction = 0; } if(ball_y <= 0){ y_direction = 0; } if (ball_x > bar_x && ball_x < (bar_x + BAR_WIDTH) && ball_y >= bar_y){ y_direction = 1; } } void my_draw_field(){ DrawBox(X_POSI + bar_x, bar_y,X_POSI + bar_x + BAR_WIDTH, bar_y + BAR_HEIGHT, Color_Red, true); DrawCircle(X_POSI + ball_x, ball_y, BALL_SIZE, Color_White, false, 1); 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*/