質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

1回答

1489閲覧

javaについての質問です。

shido

総合スコア21

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

2クリップ

投稿2015/11/29 14:02

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) { } }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

次のブロックを予告する機能を実現するには、ブロック(=puyo)を2つ管理する必要があります。

今わかっているブロックを流しているという作りですが、ブロックを2つ管理するようにします。
今ソースでは puyo がブロックにあたりますが、もう一つ、puyo_next を作る形です。
ループごとに 現在の puyo_next を puyo に流し込み、新たに puyo_next を作る。リレーのようになります。

変更方法ですが、
今のソースでpuyoを2つ管理しようとすると大変だと思います。puyoをクラスとして定義しオブジェクトにしてみてはどうでしょうか。
クラスとオブジェクトを知るとても良い題材と思います。

投稿2015/11/30 10:07

編集2015/11/30 10:18
Hiroshi-Aoki

総合スコア804

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問