/*広告*/

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

/*目次へ戻る*/

import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.animation.AnimationTimer; import javafx.scene.image.Image; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.paint.Color; import javafx.scene.input.KeyEvent; import javafx.event.EventHandler; import java.util.Random; public class B_PMain extends Application{ private B_PThread b_pthread; private Key key; public static void main(String args[]){ launch(args); } @Override public void start(Stage stage){ stage.setTitle("BLOCK PUZZLE"); Pane pane = new Pane(); Scene scene = new Scene(pane); stage.setScene(scene); Canvas canvas = new Canvas(640,480); GraphicsContext gc = canvas.getGraphicsContext2D(); pane.getChildren().add(canvas); key = new Key(); scene.setOnKeyPressed( new EventHandler<KeyEvent>(){ public void handle(KeyEvent e){ key.key_pressed(e); } } ); scene.setOnKeyReleased( new EventHandler<KeyEvent>(){ public void handle(KeyEvent e){ key.key_released(e); } } ); b_pthread = new B_PThread(gc,key); b_pthread.start(); stage.show(); } } class B_PThread extends AnimationTimer{ private GraphicsContext gc; private Font ft; private Key key; private Random rnd; private final int DRAW_POSITION_X = 140; private final int DRAW_POSITION_Y = 20; private final int NEXT_BLOCK_X = 500; private final int NEXT_BLOCK_Y = 40; private final int HOLD_BLOCK_X = 60; private final int HOLD_BLOCK_Y = 40; private final int BLOCK_HEIGHT = 4; private final int BLOCK_WIDTH = 4; private final int STAGE_HEIGHT = 23; private final int STAGE_WIDTH = 18; private final int DRAW_BLOCK_WIDTH = 20; private int block[][] = new int[BLOCK_HEIGHT][BLOCK_WIDTH]; private int turn_block[][] = new int[BLOCK_HEIGHT][BLOCK_WIDTH]; private int next_block[][] = new int[BLOCK_HEIGHT][BLOCK_WIDTH]; private int hold_block[][] = new int[BLOCK_HEIGHT][BLOCK_WIDTH]; private int stage[][] = new int[STAGE_HEIGHT][STAGE_WIDTH]; private int blocks[][] = { {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} }; private int clear_line_point[] = new int[STAGE_HEIGHT - 3]; private int game_state; private int block_x; private int block_y; private float block_y_count; private float block_speed; private int collision_flag; private int gameover_flag; private int first_block_flag; private int make_block_flag; private int hold_block_flag; private int clear_flag; private int block_id; private int next_block_id; private int hold_block_id; private int clear_count; private int turn_point; private int hold_turn_point; private int block_y_point; private int fix_count; private int lv; private int score; private Image img; B_PThread(GraphicsContext gc,Key key){ this.gc = gc; this.key = key; rnd = new Random(); ft = Font.font("Serif",FontWeight.BOLD,20); gc.setFont(ft); my_init_var(); } @Override public void handle(long time){ gc.clearRect(0,0,640,480); key.calc_key_count(); switch(game_state){ case 0: my_draw_back(); my_draw_variable(); my_draw_stage(); my_op(); if(key.get_enter() == 1){ game_state = 5; } break; case 5: if(clear_flag == 0){ my_make_block(); my_gameover(); my_move_block(); my_draw_back(); my_draw_variable(); my_draw_block(); my_draw_block2(); my_draw_stage(); my_fix_block(); my_fall_block(); } else{ my_clear_line(); my_draw_back(); my_draw_variable(); my_draw_block2(); 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; } } void my_init_var(){ for(int i=0;i<STAGE_HEIGHT;i++){ for(int 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; img = new Image("back_img.jpg"); } 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(){ ft = Font.font("Serif",FontWeight.BOLD,20); gc.setFont(ft); gc.setFill(Color.WHITE); gc.fillText("BLOCK PUZZLE",240,220); gc.fillText("press enter key to start",220,240); } void my_ed(){ ft = Font.font("Serif",FontWeight.BOLD,20); gc.setFont(ft); gc.setFill(Color.WHITE); gc.fillText("GAME OVER",260,220); } /*ブロック生成を2段階に*/ /*先に生成したブロックを「next_block_id」に保存*/ /*すぐに「block_id」に保存しなおして再び*/ /*ブロック生成処理を行い「next_block_id」に保存する*/ /*2回目以降はこれを繰り返す*/ void my_make_block(){ if(first_block_flag == 1){ next_block_id = rnd.nextInt(6); for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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 = rnd.nextInt(6); for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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 tmp_block[][] = new int[4][4]; int tmp_block_id; int tmp_turn_point; if(hold_block_flag == 0){ for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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.get_left() % 5 == 1){ my_collision_left(); if(collision_flag == 0){ block_x--; } } if(key.get_right() % 5 == 1){ my_collision_right(); if(collision_flag == 0){ block_x++; } } if(key.get_down() % 5 == 1){ my_collision_bottom(); if(collision_flag == 0){ block_y++; block_y_count = block_y * DRAW_BLOCK_WIDTH; } } if(key.get_up() == 1){ my_turn_right(); } if(key.get_z() == 1){ my_turn_left(); } if(key.get_h() == 1){ my_hold_block(); } } void my_turn_right(){ turn_point++; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ block[y][x] = turn_block[y][x]; } } } else{ turn_point--; } } void my_turn_left(){ turn_point += 3; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ block[y][x] = turn_block[y][x]; } } } else{ turn_point -= 3; } } void my_collision_left(){ collision_flag = 0; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(){ collision_flag = 0; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(){ collision_flag = 0; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(){ collision_flag = 0; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(){ collision_flag = 0; for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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(){ for(int i=0;i<STAGE_HEIGHT - 3;i++){ clear_line_point[i] = 0; } for(int i=0;i<STAGE_HEIGHT - 3;i++){ for(int j=3;j<STAGE_WIDTH - 3;j++){ if(stage[i][j] == 0){ clear_line_point[i] = 1; break; } } } for(int i=0;i<STAGE_HEIGHT - 3;i++){ if(clear_line_point[i] == 0){ clear_flag = 1; score++; } } } void my_clear_line(){ int remain_line_point[] = new int[20]; int remain_line_index = 0; if(clear_count < 12){ for(int i=0;i<STAGE_HEIGHT - 3;i++){ if(clear_line_point[i] == 0){ stage[i][clear_count + 3] = 0; } } clear_count++; } else{ for(int 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(int i=STAGE_HEIGHT - 4;i >= 0;i--){ for(int 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(){ for(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ stage[block_y + y][block_x + x] += block[y][x]; } } } void my_draw_back(){ gc.drawImage(img,0,0); } void my_draw_variable(){ ft = Font.font("Serif",FontWeight.BOLD,20); gc.setFont(ft); gc.setFill(Color.WHITE); gc.strokeRect(470,0,140,180); gc.strokeRect(30,0,140,180); gc.fillText("NEXT",510,20); gc.fillText("HOLD",70,20); gc.fillText("SCORE",490,130); gc.fillText("LINE",490,150); gc.fillText("LV",490,170); gc.fillText("" + score * 10,570,130); gc.fillText("" + score,570,150); gc.fillText("" + lv,570,170); } void my_draw_block(){ ft = Font.font("Serif",FontWeight.BOLD,25); gc.setFont(ft); for(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ if(block[y][x] == 1){gc.setFill(Color.RED); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} else if(block[y][x] == 2){gc.setFill(Color.GREEN); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} else if(block[y][x] == 3){gc.setFill(Color.BLUE); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} else if(block[y][x] == 4){gc.setFill(Color.YELLOW); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} else if(block[y][x] == 5){gc.setFill(Color.FUCHSIA); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} else if(block[y][x] == 6){gc.setFill(Color.AQUA); gc.fillText("■",DRAW_POSITION_X + block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} } } } void my_draw_block2(){ ft = Font.font("Serif",FontWeight.BOLD,25); gc.setFont(ft); for(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ if(next_block[y][x] == 1){gc.setFill(Color.RED); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} else if(next_block[y][x] == 2){gc.setFill(Color.GREEN); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} else if(next_block[y][x] == 3){gc.setFill(Color.BLUE); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} else if(next_block[y][x] == 4){gc.setFill(Color.YELLOW); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} else if(next_block[y][x] == 5){gc.setFill(Color.FUCHSIA); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} else if(next_block[y][x] == 6){gc.setFill(Color.AQUA); gc.fillText("■",NEXT_BLOCK_X + x * 20,NEXT_BLOCK_Y + y * 20);} } } for(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ if(hold_block[y][x] == 1){gc.setFill(Color.RED); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} else if(hold_block[y][x] == 2){gc.setFill(Color.GREEN); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} else if(hold_block[y][x] == 3){gc.setFill(Color.BLUE); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} else if(hold_block[y][x] == 4){gc.setFill(Color.YELLOW); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} else if(hold_block[y][x] == 5){gc.setFill(Color.FUCHSIA); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} else if(hold_block[y][x] == 6){gc.setFill(Color.AQUA); gc.fillText("■",HOLD_BLOCK_X + x * 20,HOLD_BLOCK_Y + y * 20);} } } } void my_draw_stage(){ ft = Font.font("Serif",FontWeight.BOLD,25); gc.setFont(ft); for(int y=0;y<STAGE_HEIGHT - 2;y++){ for(int x=2;x<STAGE_WIDTH - 2;x++){ if(stage[y][x] == 1){gc.setFill(Color.RED); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 2){gc.setFill(Color.GREEN); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 3){gc.setFill(Color.BLUE); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 4){gc.setFill(Color.YELLOW); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 5){gc.setFill(Color.FUCHSIA); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 6){gc.setFill(Color.AQUA); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 9){gc.setFill(Color.WHITE); gc.fillText("■",DRAW_POSITION_X + x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} } } } void my_fall_block(){ if(make_block_flag == 0){ block_y_count += block_speed; block_y = (int)(block_y_count / DRAW_BLOCK_WIDTH); } } 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; } } } class Key{ private boolean left,right,down,up,z,h,enter; private int left_count,right_count,down_count,up_count,z_count,h_count,enter_count; Key(){ left = false; right = false; down = false; up = false; z = false; h = false; enter = false; left_count = 0; right_count = 0; down_count = 0; up_count = 0; z_count = 0; h_count = 0; enter_count = 0; } void key_pressed(KeyEvent e){ switch(e.getCode()){ case LEFT: left = true; break; case RIGHT: right = true; break; case DOWN: down = true; break; case UP: up = true; break; case Z: z = true; break; case H: h = true; break; case ENTER: enter = true; break; default: break; } } void key_released(KeyEvent e){ switch(e.getCode()){ case LEFT: left = false; break; case RIGHT: right = false; break; case DOWN: down = false; break; case UP: up = false; break; case Z: z = false; break; case H: h = false; break; case ENTER: enter = false; break; default: break; } } void calc_key_count(){ if(left)left_count++; else left_count = 0; if(right)right_count++; else right_count = 0; if(down)down_count++; else down_count = 0; if(up)up_count++; else up_count = 0; if(z)z_count++; else z_count = 0; if(h)h_count++; else h_count = 0; if(enter)enter_count++; else enter_count = 0; } int get_left(){ return left_count; } int get_right(){ return right_count; } int get_down(){ return down_count; } int get_up(){ return up_count; } int get_z(){ return z_count; } int get_h(){ return h_count; } int get_enter(){ return enter_count; } }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

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

/*広告*/