/*広告*/
/*ブロックパズルの作り方 中間ソース5*/
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 Key key; private Random rnd; private final int DRAW_POSITION_X = 0; private final int DRAW_POSITION_Y = 20; 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 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 make_block_flag; private int clear_flag; private int block_id; private int clear_count; private Image img; B_PThread(GraphicsContext gc,Key key){ this.gc = gc; this.key = key; rnd = new Random(); Font theFont = Font.font("Serif",FontWeight.BOLD,25); gc.setFont(theFont); my_init_var(); } @Override public void handle(long time){ gc.clearRect(0,0,640,480); key.calc_key_count(); switch(game_state){ case 0: 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_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; } } 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; make_block_flag = 1; block_id = 0; clear_count = 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; } void my_ed(){ gc.setFill(Color.WHITE); gc.fillText("GAME OVER",400,400); } void my_make_block(){ if(make_block_flag == 1){ block_id = rnd.nextInt(6); for(int y=0;y<BLOCK_HEIGHT;y++){ for(int 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.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; } } } 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_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(){ 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; break; } } } 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(); } } 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(){ gc.setFill(Color.WHITE); gc.fillText("block_x = " + block_x,400,400); gc.fillText("block_y = " + block_y,400,420); gc.fillText("block_y_count = " + block_y_count,400,440); } void my_draw_block(){ 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("■",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("■",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("■",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("■",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("■",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("■",block_x * DRAW_BLOCK_WIDTH + x * DRAW_BLOCK_WIDTH, DRAW_POSITION_Y + block_y_count + y * DRAW_BLOCK_WIDTH);} } } } void my_draw_stage(){ 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("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 2){gc.setFill(Color.GREEN); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 3){gc.setFill(Color.BLUE); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 4){gc.setFill(Color.YELLOW); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 5){gc.setFill(Color.FUCHSIA); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 6){gc.setFill(Color.AQUA); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} else if(stage[y][x] == 9){gc.setFill(Color.WHITE); gc.fillText("■",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); } } } class Key{ private boolean left,right,down; private int left_count,right_count,down_count; Key(){ left = false; right = false; down = false; left_count = 0; right_count = 0; down_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; 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; 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; } int get_left(){ return left_count; } int get_right(){ return right_count; } int get_down(){ return down_count; } }/*Copyright 2016 K.N/petitetech.com*/
/*広告*/