hallo leute.
ich muss das alt bekannte spiel tetris programmieren. hab mittlerweile auch schon so einiges an code. jedoch weiß ich nicht wie ich es schaffen soll, dass der stein der im preview-Fenster angezeigt wird im Spielfeld erscheint und runterfällt.
Bitte um Hilfestellung.
Hier mal ein paar Klassen zur Veranschaulichung:
ich muss das alt bekannte spiel tetris programmieren. hab mittlerweile auch schon so einiges an code. jedoch weiß ich nicht wie ich es schaffen soll, dass der stein der im preview-Fenster angezeigt wird im Spielfeld erscheint und runterfällt.
Bitte um Hilfestellung.
Hier mal ein paar Klassen zur Veranschaulichung:
Code:
public class Stones {
private int rotation = 0; // current rotation of shape
private int width = 0; // current width of shape
private int height = 0; // current height of shape
private int sType = 0; // current shape
private int[][] stoneArray = new int[4][4]; //array for the stones
private Timer timer;
private int boardWidth = 250;
private int boardHeight = 300;
private int currentX=5;
private int currentY=0;
private int[][] fallingPosition = new int[currentX][currentY];
private boolean isPlaying; //eventuell wieder löschen.. diese Variable sollte ich eig woanders herbekommen
private TetrisPanel board;
private int xPos = 0;
private int yPos = 0;
private int orientation = 0;
//Constructor
public Stones(int type){
rotation = 0;
sType = type;
switch (sType) {
case 1: // bar
bar0(stoneArray);
break;
case 2: //square
square(stoneArray);
break;
case 3: //triangle
triangle0(stoneArray);
break;
case 4: //rightL
rightL0(stoneArray);
break;
case 5: //leftL
leftL0(stoneArray);
break;
}
}
// every shape has an own number to choose a color
public void bar0(int[][] array){
resetShapeArray(array);
array[0][1]=1;
array[1][1]=1;
array[2][1]=1;
array[3][1]=1;
width = 4;
height = 1;
}
public void bar1(int[][] array){
resetShapeArray(array);
array[1][0]=1;
array[1][1]=1;
array[1][2]=1;
array[1][3]=1;
width = 1;
height = 4;
}
public void rightL0(int[][] array){
resetShapeArray(array);
array[0][1]=2;
array[1][1]=2;
array[2][1]=2;
array[2][2]=2;
width = 2;
height = 3;
}
public void rightL1(int[][] array){
resetShapeArray(array);
array[1][1]=2;
array[1][2]=2;
array[1][3]=2;
array[2][1]=2;
width = 3;
height = 2;
}
public void rightL2(int[][] array){
resetShapeArray(array);
array[1][1]=2;
array[1][2]=2;
array[2][2]=2;
array[3][2]=2;
width = 2;
height = 3;
}
public void rightL3(int[][] array){
resetShapeArray(array);
array[2][0]=2;
array[2][1]=2;
array[2][2]=2;
array[1][2]=2;
width = 3;
height = 2;
}
public void leftL0(int[][] array){
resetShapeArray(array);
array[0][2]=3;
array[1][2]=3;
array[2][2]=3;
array[2][1]=3;
width = 2;
height = 3;
}
public void leftL1(int[][] array){
resetShapeArray(array);
array[1][1]=3;
array[2][1]=3;
array[2][2]=3;
array[2][3]=3;
width = 3;
height = 2;
}
public void leftL2(int[][] array){
resetShapeArray(array);
array[1][1]=3;
array[1][2]=3;
array[2][1]=3;
array[3][1]=3;
width = 2;
height = 3;
}
public void leftL3(int[][] array){
resetShapeArray(array);
array[1][0]=3;
array[1][1]=3;
array[1][2]=3;
array[2][2]=3;
width = 3;
height = 2;
}
public void square(int[][] array){
resetShapeArray(array);
array[1][1]=4;
array[1][2]=4;
array[2][1]=4;
array[2][2]=4;
width = 2;
height = 2;
}
public void triangle0(int[][] array){
resetShapeArray(array);
array[1][0]=5;
array[1][1]=5;
array[1][2]=5;
array[2][1]=5;
width = 3;
height = 2;
}
public void triangle1(int[][] array){
resetShapeArray(array);
array[1][2]=5;
array[2][2]=5;
array[3][2]=5;
array[2][1]=5;
width = 2;
height = 3;
}
public void triangle2(int[][] array){
resetShapeArray(array);
array[2][0]=5;
array[2][1]=5;
array[2][2]=5;
array[1][1]=5;
width = 3;
height = 2;
}
public void triangle3(int[][] array){
resetShapeArray(array);
array[1][1]=5;
array[2][1]=5;
array[3][1]=5;
array[2][2]=5;
width = 2;
height = 3;
}
public void resetShapeArray(int[][] array){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
array[i][j] = 0;
}
}
}
public void drawShapes(){
switch (sType) {
case 1:
if(rotation == 0 || rotation == 2){
bar0(stoneArray);
}
else{
bar1(stoneArray);
}
break;
case 2:
if(rotation == 0 || rotation == 1 || rotation ==2 || rotation ==3){
square(stoneArray);
}
break;
case 3:
if(rotation == 0){
triangle0(stoneArray);
}
if(rotation == 1){
triangle1(stoneArray);
}
if(rotation == 2){
triangle2(stoneArray);
}
else{
triangle3(stoneArray);
}
break;
case 4:
if(rotation == 0){
rightL0(stoneArray);
}
if(rotation == 1){
rightL1(stoneArray);
}
if(rotation == 2){
rightL2(stoneArray);
}
else {
rightL3(stoneArray);
}
break;
case 5:
if(rotation == 0){
leftL0(stoneArray);
}
if(rotation == 1){
leftL1(stoneArray);
}
if(rotation == 2){
leftL2(stoneArray);
}
else{
leftL3(stoneArray);
}
break;
}
}
public void turnShape() {
if (rotation == 0) {
rotation = 1;
}
if (rotation == 1) {
rotation = 2;
}
if (rotation == 2) {
rotation = 3;
}
else {
rotation = 0;
}
drawShapes();
}
//Getters and Setters
public int getRotation() {
return rotation;
}
public void setRotation(int rotation) {
this.rotation = rotation;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getShape() {
return sType;
}
public void setShape(int shape) {
this.sType = shape;
}
public int[][] getShapeArray() {
return stoneArray;
}
public void setShapeArray(int[][] shapeArray) {
this.stoneArray = shapeArray;
}
public void accelerate() {
int y = yPos;
while(canBeMoved (xPos, y+1, orientation)){
y++;}
}
public void moveLeft() {
if(canBeMoved(xPos-1, yPos, orientation)) {
xPos--;
}
}
public void moveRight() {
if(canBeMoved(xPos+1, yPos, orientation)){
xPos++;
}
}
public int[][] getStoneArray() {
return stoneArray;
}
public void setStoneArray(int[][] stoneArray) {
this.stoneArray = stoneArray;
}
private void copyArray(int[][] source, int[][] target) {
for (int i = 0; i < source.length; i++)
for (int j = 0; j < source[i].length; j++)
target[i][j] = source[i][j];
}
private boolean canBeMoved(int newX, int newY, int newOrientation) {
int x;
int y;
return isPlaying;
}
}
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class PreviewPanel extends JPanel {
private static final long serialVersionUID = 1L;
int type = 0;
Stones myStone;
/**
* This is the constructor
*/
public PreviewPanel(Stones preStone) {
super();
initialize();
myStone = preStone;
}
private void initialize() {
this.setSize(75,75);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i< myStone.getStoneArray().length; i++){
for(int j = 0; j<myStone.getStoneArray().length; j++){
switch(myStone.getStoneArray()[i][j]){
case 0: //empty
g.setColor(new Color(190,190,190));
break; //with break you jump out of the switch case
case 1: //bar
g.setColor(new Color(255,150,0));
break;
case 2: //square
g.setColor(new Color(240,110,220));
break;
case 3: //triangle
g.setColor(new Color(150,0,50));
break;
case 4: //right L
g.setColor(new Color(0,100,150));
break;
case 5: //left L
g.setColor(new Color(10,150,0));
break;
}
if(myStone.getStoneArray()[i][j] != 0){
g.fill3DRect(20*i, 20*j+10, 20, 20, true);
}else{
g.fillRect(20*i, 20*j+10, 20, 20);
}
}
}
}
}
Code:
public class TetrisApplication {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="10,10"
private JPanel controlPanel = null;
private JButton startButton = null;
private JButton stopButton = null;
private JPanel buttons = null;
private JLabel scoreLabel = null;
private JLabel highScoreLabel = null;
private PreviewPanel preview = null;
private JLabel hScoreNameLabel = null;
private JLabel scoreNameLabel = null;
private JPanel previewWindow = null;
public Stones test(){
Random rnd = new Random(); // @jve:decl-index=0:
int type = rnd.nextInt(4)+1;
Stones preStone = new Stones(type); // @jve:decl-index=0:
Stones boardStone = preStone;
return preStone;
}
private JPanel playPanel = null;
private MainGame mainGame;
private int[][] playArray = new int[20][30];
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(402, 486);
jFrame.setContentPane(getControlPanel());
jFrame.setTitle("Tetris");
}
return jFrame;
}
/**
* This method initializes controlPanel
*
* @return javax.swing.JPanel
*/
private JPanel getControlPanel() {
if (controlPanel == null) {
controlPanel = new JPanel();
//controlPanel = new TetrisPanel(boardStone, this, null);
controlPanel.setLayout(null);
controlPanel.setBackground(Color.white);
controlPanel.add(getButtons(), null);
controlPanel.add(getPlayPanel(), null);
}
return controlPanel;
}
/**
* This method initializes startButton
*
* @return javax.swing.JButton
*/
private JButton getStartButton() {
if (startButton == null) {
startButton = new JButton();
startButton.setPreferredSize(new Dimension(70, 30));
startButton.setForeground(Color.white);
startButton.setBackground(new Color(153, 153, 255));
startButton.setFont(new Font("Berlin Sans FB", Font.BOLD, 12));
startButton.setBounds(new Rectangle(15, 89, 70, 25));
startButton.setText("START");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return startButton;
}
/**
* This method initializes stopButton
*
* @return javax.swing.JButton
*/
private JButton getStopButton() {
if (stopButton == null) {
stopButton = new JButton();
stopButton.setPreferredSize(new Dimension(70, 30));
stopButton.setForeground(Color.white);
stopButton.setFont(new Font("Berlin Sans FB Demi", Font.BOLD, 12));
stopButton.setBackground(new Color(102, 204, 0));
stopButton.setBounds(new Rectangle(15, 119, 69, 25));
stopButton.setText("STOP");
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JOptionPane.showMessageDialog(getJFrame(),
"You have stopped the game!", "PAUSE",
JOptionPane.PLAIN_MESSAGE);
}
});
}
return stopButton;
}
/**
* This method initializes buttons
*
* @return javax.swing.JPanel
*/
private JPanel getButtons() {
if (buttons == null) {
scoreNameLabel = new JLabel();
scoreNameLabel.setBounds(new Rectangle(16, 159, 68, 16));
scoreNameLabel.setText("Score:");
hScoreNameLabel = new JLabel();
hScoreNameLabel.setBounds(new Rectangle(17, 207, 66, 16));
hScoreNameLabel.setText("Highscore:");
highScoreLabel = new JLabel();
highScoreLabel.setBounds(new Rectangle(15, 226, 69, 21));
//highScoreLabel.setText(toString(Highscore.highscore[1])); //damit i den highscore ausgeben kann, aber irgwie funzt des net.. hm...ich brauch da noch irgwie die richtigen Variable
highScoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
highScoreLabel.setHorizontalTextPosition(SwingConstants.CENTER);
scoreLabel = new JLabel();
scoreLabel.setBounds(new Rectangle(15, 177, 70, 19));
//scoreLabel.setText(toString(Highscore.newScore)); //da muss ich noch die Variable für den aktuellen Score eingeben, aber die gibts irgwie noch nicht :)
scoreLabel.setHorizontalTextPosition(SwingConstants.CENTER);
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
buttons = new JPanel();
buttons.setLayout(null);
buttons.setBounds(new Rectangle(293, 15, 96, 274));
buttons.add(getStartButton(), null);
buttons.add(getStopButton(), null);
buttons.add(scoreLabel, null);
buttons.add(highScoreLabel, null);
buttons.add(hScoreNameLabel, null);
buttons.add(scoreNameLabel, null);
buttons.add(getPreviewWindow(), null);
}
return buttons;
}
/**
* This method initializes previewWindow
*
* @return javax.swing.JPanel
*/
private JPanel getPreviewWindow() {
if (previewWindow == null) {
previewWindow = new PreviewPanel(test());
previewWindow.setLayout(new GridBagLayout());
previewWindow.setBounds(new Rectangle(6, -1, 80, 80));
}
return previewWindow;
}
/**
* This method initializes playPanel
*
* @return javax.swing.JPanel
*/
private JPanel getPlayPanel() {
if (playPanel == null) {
playPanel = new TetrisPanel(playArray , this, mainGame);
playPanel.setLayout(new GridBagLayout());
playPanel.setBounds(new Rectangle(16, 19, 255, 430));
}
return playPanel;
}
/**
* Launches this application
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TetrisApplication application = new TetrisApplication();
application.getJFrame().setVisible(true);
}
});
}
}