/*広告*/

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

/*目次へ戻る*/

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; 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 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,1,1,0}, {0,1,1,0}, {0,0,0,0} }; private int block_x; private int block_y; private float block_y_count; private float block_speed; private int collision_flag; private Image img; B_PThread(GraphicsContext gc,Key key){ this.gc = gc; this.key = key; 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(); my_make_block(); my_move_block(); my_draw_back(); my_draw_variable(); my_draw_block(); my_draw_stage(); my_fall_block(); if(block_y_count > DRAW_BLOCK_WIDTH * 17){ block_y_count = 0; block_y = 0; } } 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; } } block_x = 7; block_y = 0; block_y_count = 0; block_speed = 0.5f; collision_flag = 0; img = new Image("back_img.jpg"); } void my_make_block(){ for(int y=0;y<BLOCK_HEIGHT;y++){ for(int x=0;x<BLOCK_WIDTH;x++){ block[y][x] = blocks[y][x]; } } } 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++; } } } 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_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);} } } } 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] == 9){gc.setFill(Color.WHITE); gc.fillText("■",x * DRAW_BLOCK_WIDTH,DRAW_POSITION_Y + y * DRAW_BLOCK_WIDTH);} } } } void my_fall_block(){ block_y_count += block_speed; block_y = (int)(block_y_count / DRAW_BLOCK_WIDTH); } } class Key{ private boolean left,right; private int left_count,right_count; Key(){ left = false; right = false; left_count = 0; right_count = 0; } void key_pressed(KeyEvent e){ switch(e.getCode()){ case LEFT: left = true; break; case RIGHT: right = true; break; default: break; } } void key_released(KeyEvent e){ switch(e.getCode()){ case LEFT: left = false; break; case RIGHT: right = false; break; default: break; } } void calc_key_count(){ if(left)left_count++; else left_count = 0; if(right)right_count++; else right_count = 0; } int get_left(){ return left_count; } int get_right(){ return right_count; } }

/*ページの先頭へ*/

/*目次へ戻る*/

/*HOME*/

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

/*広告*/