java初心者です。javaでとても簡単なテトリスを作ろうとしています。基盤などはできて、上から落ちてきて消えるところまではできたのですが、次に流れてくるやつを予告する機能がつけられません。ソースコードが文字数制限のため全体はつけられませんが、簡単なやり方を教えてください。(消えるシステムと、ボタン操作のシステムを除いた形で張らせていただきます。わかりにくいでしょうがよろしくお願いします。)
import java.awt.;
import java.awt.event.;
class YPanel extends XPanel {
Panel innerPanel3;
Panel innerPanel4;
Color bgcolor;
public YPanel() { // setLayout(null); innerPanel3 = new Panel(); add(innerPanel3); innerPanel3.setBackground(Color.black); innerPanel4 = new Panel(); add(innerPanel4); innerPanel4.setBackground(Color.black); bgcolor = super.getBackground(); } public void setBounds(int x, int y, int width, int height) { super.setBounds(x,y,width,height); innerPanel3.setBounds(4,height-8,width-8,8); innerPanel4.setBounds(width-8,4,8,height-8); } public Color getBackground() { return bgcolor; } public void setBackground(Color color) { bgcolor = color; super.setBackground(color); }
}
class XPanel extends Panel {
Panel innerPanel1;
Panel innerPanel2;
public XPanel() { setLayout(null); innerPanel1 = new Panel(); add(innerPanel1); innerPanel1.setBackground(Color.white); innerPanel2 = new Panel(); add(innerPanel2); innerPanel2.setBackground(Color.white); } public void setBounds(int x, int y, int width, int height) { super.setBounds(x,y,width,height); innerPanel1.setBounds(0,0,width-8,8); innerPanel2.setBounds(0,0,8,height-8); }
}
public class puyo1 {
static Frame myframe; static Panel mypanels[][]; static int puyomatrix[][]; static Color colorList[]; static int score = 0; static int puyoX = 3, puyoY = 1;
static int puyoX2 = 3;
static int puyoY2 = 2;
static int color1 = 0, color2 = 0;
static int rotate = 0;
static boolean lock = false;
static boolean lock2 = false;
static Panel rotateBtn; static Panel leftBtn; static Panel rightBtn; public static void main(String args[]) { int i,x,y;
final int puyoSize=32;
myframe = new Frame(); myframe.setLayout(null); myframe.setSize(400,600); myframe.setVisible(true); rotateBtn = new Panel(); myframe.add(rotateBtn); rotateBtn.setBounds(200,500,48,48); rotateBtn.setBackground(Color.yellow); leftBtn = new Panel(); myframe.add(leftBtn); leftBtn.setBounds(120,500,48,48); leftBtn.setBackground(Color.green); rightBtn = new Panel(); myframe.add(rightBtn); rightBtn.setBounds(280,500,48,48); rightBtn.setBackground(Color.red); mypanels = new Panel[13][8]; puyomatrix = new int[13][8]; colorList = new Color[5]; colorList[0] = Color.gray; colorList[1] = Color.white; colorList[2] = Color.yellow; colorList[3] = Color.red; colorList[4] = Color.green; for( x = 0; x < 8; x++ ) { for( y = 0; y < 13; y++ ) { Panel p = new YPanel(); mypanels[y][x] = p; myframe.add(p); p.setBounds(30+puyoSize*x,40+puyoSize*y,puyoSize,puyoSize); setpuyo(x,y,0); // no puyo is here } } for( x = 0; x < 8; x++ ) { setpuyo(x,12, 1); // ground is here } for( y = 0; y < 12; y++ ) { setpuyo(0,y,1); // wall is here setpuyo(7,y,1); // wall is here }
//基盤作り
rotateBtn.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { rotateBtnMouseReleased(e); } } ); leftBtn.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { leftBtnMouseReleased(e); } } ); rightBtn.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { rightBtnMouseReleased(e); } } ); //具体的なところは文字数制限のため、消去しました。 boolean firstPlacing = true; boolean gameIsOver = false; int fallCount = 0; while( gameIsOver != true) { lock = false; sleep(100); lock = true; while( lock2 ) { sleep(10); } if( firstPlacing ) { firstPlacing = false; color1 = (int)(Math.random()*3)+2; color2 = (int)(Math.random()*3)+2; rotate = 0; puyoX = 3; puyoY = 1; // initialPlace puyoX2 = getRotatedPositionX(puyoX,rotate); puyoY2 = getRotatedPositionY(puyoY,rotate); if( puyomatrix[puyoY][puyoX] == 0 && puyomatrix[puyoY2][puyoX2] == 0 ) { setpuyo(puyoX,puyoY,color1); setpuyo(puyoX2,puyoY2,color2); // System.out.print("next puyo is here.\n"); } else { // GAME OVER setpuyo(puyoX,puyoY,color1); setpuyo(puyoX2,puyoY2,color2); System.out.print("batan kyu-\n"); gameIsOver = true; } } if( fallCount > 5 ) { fallCount=0; // counter reset; puyoY++; // fall it for one block puyoY2++; if( (rotate == 0 || puyomatrix[puyoY][puyoX] == 0) && (rotate == 2 || puyomatrix[puyoY2][puyoX2] == 0) ) { setpuyo(puyoX,puyoY-1,0); setpuyo(puyoX2,puyoY2-1,0); setpuyo(puyoX,puyoY,color1); setpuyo(puyoX2,puyoY2,color2); } else { firstPlacing = true; // undo falling puyoY--; puyoY2--; // fall each puyo while(rotate != 0 && puyomatrix[puyoY+1][puyoX] == 0) { setpuyo(puyoX,puyoY,0); puyoY++; setpuyo(puyoX,puyoY,color1); } while(puyomatrix[puyoY2+1][puyoX2] == 0) { setpuyo(puyoX2,puyoY2,0); puyoY2++; setpuyo(puyoX2,puyoY2,color2); } while(rotate == 0 && puyomatrix[puyoY+1][puyoX] == 0) { setpuyo(puyoX,puyoY,0); puyoY++; setpuyo(puyoX,puyoY,color1); } while( areConnectedPuyosCleared() ) {
//areConnectedPuyosCleared()は消去するメソッドですが、中身は消去しました。
fallPuyos();
sleep(400);
} } } else { fallCount++; } } } public static void rotateBtnMouseReleased(MouseEvent e) { rotate(); } public static void leftBtnMouseReleased(MouseEvent e) { moveLeft(); } public static void rightBtnMouseReleased(MouseEvent e) { moveRight(); } static void setpuyo(int x, int y, int color) { puyomatrix[y][x] = color; mypanels[y][x].setBackground(colorList[color]); }
public static int getpuyo(int x, int y)
return puyomatrix[y][x];
}
static int getRotatedPositionX(int x, int r) {
int rx = 0;
switch(r) {
case 0:
rx = x; break;
case 1:
rx = x-1; break;
case 2:
rx = x; break;
case 3:
rx = x+1; break;
}
return rx;
}
// 2
// 1 o 3
// 0
static int getRotatedPositionY(int y, int r) {
int ry = 0;
switch(r) {
case 0:
ry = y+1; break;
case 1:
ry = y; break;
case 2:
ry = y-1; break;
case 3:
ry = y; break;
}
return ry;
}
static void fallPuyos() { int fallcount; do { fallcount = 0; int x,y; for( y = 10; y > 0; y-- ) { for( x = 1; x <= 6; x++ ) { if( puyomatrix[y][x] != 0 && puyomatrix[y+1][x] == 0 ) { setpuyo(x,y+1,puyomatrix[y][x]); // fall the puyo setpuyo(x,y,0); // the last position should be empty fallcount++; // count-up fallout counter } } } } while( fallcount != 0 ); } static void sleep(long msec) { try{ Thread.sleep(msec); }catch(InterruptedException ie) { } }
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。