Ich bin dabei tetris zu Programmieren. Habe 2 Klassen angelegt.
Java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class tetris_main extends Applet{
private stone stone = new stone();
private static final long serialVersionUID = 1L;
private int[][] field = stone.getNewStone();
public tetris_main(){
super();
}
private int [][]feld = {{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,0,0,0,0,0,0,0,0,0,0,2,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2}};
public void init(){
this.setSize(300,600);
addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent e){
};
public void keyTyped(KeyEvent e){
};
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP){
field = stone.rotate(field);
repaint();
}
};
});
}
public void paint(Graphics g){
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
if(field[i][j]!=0){
g.setColor(Color.RED);
}
else{
g.setColor(Color.white);
}
g.fillRect(i*20, j*20, 19, 19);
}
}
for(int i = 0;i<14;i++){
for(int j = 0;j<22;j++){
if(feld[j][i]==1){
g.setColor(Color.RED);
}
else{
g.setColor(Color.white);
}
g.fillRect(i*20, j*20, 19, 19);
g.setColor(Color.black);
g.drawRect(40, 0, 200, 400);
g.setColor(Color.white);
}
}
Alles anzeigen
und der spielesteinklasse
Code
[B][size=10][COLOR=#7f0055][align=left]public[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]class[/B][/COLOR][/SIZE][size=10] stone {
[/SIZE][B][size=10][COLOR=#7f0055]private[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][][] [/SIZE][size=10][COLOR=#0000c0]stones[/COLOR][/SIZE][size=10] = {{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//I[/align]
[/COLOR][/SIZE][size=10][align=left]{{0,0,0,0},{1,1,0,0},{0,1,1,0},{0,0,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//Z[/align]
[/COLOR][/SIZE][size=10][align=left]{{0,0,0,0},{0,1,1,0},{0,1,1,0},{0,0,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//Würfel[/align]
[/COLOR][/SIZE][size=10][align=left]{{0,0,0,0},{1,1,1,0},{0,1,0,0},{0,0,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//T[/align]
[/COLOR][/SIZE][size=10][align=left]{{0,0,0,0},{0,1,1,0},{1,1,0,0},{0,0,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//Umgedr. Z[/align]
[/COLOR][/SIZE][size=10][align=left]{{0,1,0,0},{0,1,0,0},{1,1,0,0},{0,0,0,0}}, [/SIZE][size=10][COLOR=#3f7f5f]//L[/align]
[/COLOR][/SIZE][size=10][align=left]{{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}}};[/SIZE][size=10][COLOR=#3f7f5f]//umgedr.L[/align]
[/COLOR][/SIZE][size=10][align=left]
[/SIZE][B][size=10][COLOR=#7f0055]public[/B][/COLOR][/SIZE][size=10] stone(){
}
[/SIZE][B][size=10][COLOR=#7f0055]public[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][] getNewStone(){
[/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][] newStone=[/SIZE][B][size=10][COLOR=#7f0055]new[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][4][4];
[/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10] steinNr = ([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10])(Math.[I]round[/I](Math.[I]random[/I]()*6));
[/SIZE][B][size=10][COLOR=#7f0055]for[/B][/COLOR][/SIZE][size=10]([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10] i = 0; i<4;i++){
[/SIZE][B][size=10][COLOR=#7f0055]for[/B][/COLOR][/SIZE][size=10]([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10] j = 0; j<4; j++){
newStone[i][j] = [/SIZE][size=10][COLOR=#0000c0]stones[/COLOR][/SIZE][size=10][steinNr][i][j];
}
}
[/SIZE][B][size=10][COLOR=#7f0055]return[/B][/COLOR][/SIZE][size=10] newStone;
}
[/SIZE][B][size=10][COLOR=#7f0055]public[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][] rotate([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][] oldStone){
[/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][][] newStone = [/SIZE][B][size=10][COLOR=#7f0055]new[/B][/COLOR][/SIZE][size=10] [/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10][4][4];
[/SIZE][B][size=10][COLOR=#7f0055]for[/B][/COLOR][/SIZE][size=10]([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10] i=0;i<4;i++){ [/SIZE][size=10][COLOR=#3f7f5f]//Zeile[/align]
[/COLOR][/SIZE][size=10][align=left][/SIZE][B][size=10][COLOR=#7f0055]for[/B][/COLOR][/SIZE][size=10]([/SIZE][B][size=10][COLOR=#7f0055]int[/B][/COLOR][/SIZE][size=10] j =0;j <4; j++){ [/SIZE][size=10][COLOR=#3f7f5f]//Spalte[/align]
[/COLOR][/SIZE][size=10][align=left]newStone[j][3-i]=oldStone[i][j];
}
}
[/SIZE][B][size=10][COLOR=#7f0055]return[/B][/COLOR][/SIZE][size=10] newStone;
} [/align]
}
[/SIZE]
Alles anzeigen
2 Fragen:
1. Wie kann ich die Spielsteine in dem Spielfeld verwenden??
2. Wie ist es möglich, das die Steine fallen und liegen bleiben ---> eine volle reihe verschwindet?
Ich habe mir überegt, dass man dies mit einer abfrage anstellen könne.. aber bin auch nicht weiter gekommen...
bitte um hilfe