下記のコードでテトリスを作ったのですがミノブロックが壁にめり込んでしまいます。考え方自体はあっていると思うのですが、どうしても内部だけで動き、積みあがる処理ができません。大変申し訳ありませんが下記のコードのどこが問題なのかご指摘いただけないでしょうか?よろしくお願いいたします。
lang
1コード
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class Tetris extends Applet implements Runnable,KeyListener {
/*縦22マス/
public static final int HEIGHT=22;
/*横13マス/
public static final int WIDTH=13;
/*ブロックの大きさ/
public static final int SIZE =30;
private int [][]field=new int[WIDTH][HEIGHT]; /**色の選択*/ public static final Color[] COLOR_SELECT={Color.WHITE,Color.ORANGE,Color.GREEN,Color.YELLOW,Color.GRAY,Color.MAGENTA,Color.BLUE,Color.RED,Color.BLACK}; /**ブロックの向き*/ public static final int DIRECTION=0; public static final int SPACE=0; public static final int WALL=1; /**中心のブロックの初期座標*/ Point point = new Point(6,0); /**テトリミノの座標*/ Point minoPoint; /**ブロックの種類*/ private Point data[][][]= //L字型ブロック {{{new Point(-1,0),new Point(0,0),new Point(1,0),new Point(-1,1)},// 初期 {new Point(0,-1),new Point(1,-1),new Point(0,0),new Point(1,1)},// 90度回転 {new Point(1,0),new Point(0,0),new Point(0,1),new Point(1,1)}, // 180度回転 {new Point(-1,-1),new Point(0,-1),new Point(0,0),new Point(0,1)},},//270度回転 //田字型ブロック {{new Point(-1,0),new Point(0,0),new Point(-1,1),new Point(0,1)}, // 初期 {new Point(-1,0),new Point(0,0),new Point(-1,1),new Point(0,1)}, // 90度回転 {new Point(-1,0),new Point(0,0),new Point(-1,1),new Point(0,1)}, // 180度回転 {new Point(-1,0),new Point(0,0),new Point(-1,1),new Point(0,1)},},// 270度回転 //I字型ブロック {{new Point(-2,0),new Point(-1,0),new Point(0,0),new Point(1,0)}, // 初期 {new Point(0,-1),new Point(0,0),new Point(0,1),new Point(0,1)}, // 90度回転 {new Point(-2,0),new Point(-1,0),new Point(0,0),new Point(1,0)}, // 180度回転 {new Point(0,-1),new Point(0,0),new Point(0,1),new Point(0,2)},},// 270度回転 //Z字型ブロック {{new Point(0,0),new Point(1,0),new Point(-1,1),new Point(0,1)}, // 初期 {new Point(-1,-1),new Point(-1,0),new Point(0,0),new Point(0,1)}, // 90度回転 {new Point(0,0),new Point(1,0),new Point(-1,1),new Point(0,1)}, // 180度回転 {new Point(-1,-1),new Point(-1,0),new Point(0,0),new Point(0,1)},}, // 270度回転 //T字型ブロック {{new Point(-1,0),new Point(0,0),new Point(1,0),new Point(0,1)}, // 初期 {new Point(0,-1),new Point(0,0),new Point(1,0),new Point(0,1)}, // 90度回転 {new Point(0,0),new Point(-1,1),new Point(0,1),new Point(1,1)}, // 180度回転 {new Point(0,-1),new Point(-1,0),new Point(0,0),new Point(0,1)},}, // 270度回転 //逆Z字型ブロック {{new Point(-1,0),new Point(0,0),new Point(0,1),new Point(1,1)}, // 初期 {new Point(1,-1),new Point(0,0),new Point(1,0),new Point(0,1)}, // 90度回転 {new Point(-1,0),new Point(0,0),new Point(0,1),new Point(1,1)}, // 180度回転 {new Point(1,-1),new Point(0,0),new Point(1,0),new Point(0,1)},}, // 270度回転 //逆L字型ブロック {{new Point(-1,0),new Point(0,0),new Point(1,0),new Point(1,1)}, // 初期 {new Point(0,-1),new Point(1,-1),new Point(0,0),new Point(0,1)}, // 90度回転 {new Point(0,0),new Point(-1,1),new Point(0,1),new Point(1,1)}, // 180度回転 {new Point(0,-1),new Point(0,0),new Point(-1,1),new Point(0,1)},}};// 270度回転 /**乱数生成*/ Random random = new Random(); /**ランダムな色のブロック表示*/ private int colorSelect =random.nextInt(7)+2; /**ランダムな形のブロック表示*/ private int blockSelect =random.nextInt(7)+2; /**初期化処理*/ @Override public void init(){ addKeyListener(this); requestFocus(); for(int y=0;y<HEIGHT;y++) { for(int x=0;x<WIDTH; x++) { field[x][y]=SPACE; } } for (int x=0;x<WIDTH;x++) { field[x][HEIGHT-1]=WALL; } for(int y=0;y<HEIGHT;y++) { field[0][y]=WALL; field[WIDTH-1][y]=WALL; } initBlock(); for(int i=0;i<4;i++){ minoPoint=data[blockSelect][DIRECTION][i]; } } /**@param e KeyEvent e*/ @Override public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if (key==KeyEvent.VK_LEFT){// 方向キー左が押されたとき if(field[(point.x-1)+minoPoint.x][point.y+minoPoint.y]==SPACE){// ブロックがSPACEのとき point.x--; repaint(); } }else if (key==KeyEvent.VK_RIGHT){// 方向キー右が押されたとき if(field[(point.x+1)+minoPoint.x][point.y+minoPoint.y]==SPACE){// ブロックがSPACEのとき point.x++; repaint(); } }else if (key==KeyEvent.VK_DOWN){// 方向キー下が押されたとき if(field[point.x+minoPoint.x][(point.y+1)+minoPoint.y]==SPACE){// ブロックがSPACEのとき point.y++; repaint(); }else{// 底辺もしくは他のブロックに接したとき initBlock(); field[point.x+minoPoint.x][point.y+minoPoint.y]=colorSelect; repaint(); } }else if(key==KeyEvent.VK_SPACE){//回転させる処理 } } @Override public void keyReleased(KeyEvent e){ } @Override public void keyTyped(KeyEvent e){ } /**スレッド開始*/ @Override public void start(){ Thread th = new Thread(this); th.start(); } /**落下メソッド*/ @Override public void run(){ while(true){ try { Thread.sleep(1000); // 1秒 } catch (InterruptedException e) { e.printStackTrace(); } if(field[minoPoint.x+point.x][minoPoint.y+(point.y+1)]==SPACE){// ブロックがSPACEのとき point.y++; repaint(); }else{//底辺もしくは他のブロックに接したとき initBlock(); field[minoPoint.x+point.x][minoPoint.y+point.y]=colorSelect; } repaint(); } } /**@param g Graphics*/ @Override public void update(Graphics g){ paint(g); } /**@param g Graphics*/ @Override public void paint(Graphics g){ Dimension size=getSize(); Image back=createImage(size.width, size.height); Graphics buffer=back.getGraphics(); // 移動ブロックの描画 if(field[point.x+minoPoint.x][point.y+minoPoint.y]==SPACE){ buffer.setColor(COLOR_SELECT[colorSelect]); buffer.fillRect((point.x+minoPoint.x)*SIZE,(point.y+minoPoint.y)*SIZE,SIZE,SIZE); } // 外壁の描画 for(int i = 0;i <WIDTH;i++){ for(int j = 0;j <HEIGHT;j++){ if(field[i][j]!=SPACE){ // fieldがSPACE以外のとき buffer.setColor(COLOR_SELECT[field[i][j]]); buffer.fillRect(i*SIZE,j*SIZE,SIZE,SIZE); } } } buffer.setColor(COLOR_SELECT[colorSelect]); for (int i = 0; i<4; i++) { buffer.fillRect(SIZE*(point.x+data[blockSelect][DIRECTION][i].x),SIZE*(point.y+data[blockSelect][DIRECTION][i].y),SIZE,SIZE); } // マス目描画 buffer.setColor(Color.black); for(int i=0;i<=HEIGHT;i++){ buffer.drawLine(0,i*SIZE,WIDTH*SIZE,i*SIZE); } for(int j=0;j<=WIDTH;j++){ buffer.drawLine(j*SIZE,0,j*SIZE,HEIGHT*SIZE); } g.drawImage(back, 0, 0, this); } /**ランダムな色で次のブロックを生成*/ public void initBlock(){ point = new Point(6, 0);// 初期配置に戻す colorSelect = random.nextInt(7)+2; blockSelect =random.nextInt(7)+2; }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2014/12/23 06:28