000
28.05.2004, 10:10 Uhr
~Salfador12
Gast
|
Hallo
Hallo zusammen
ich hab erst vor kurzen angefangen mit Java und wollte mal Tetris nachprogrammieren. Das Problem wo ich habe ist das Zuweisen von Daten in ein interger Array und Boolean Array, die in der Supperclasse deklariert, sind von einer unteren Classe aus. Der Code der Zuweisung sieht so aus:
Code: |
for(int sx=0;sx<4;sx++){ x=xPos+Tetris.kBreite*shapeX[sx]; y=yPos*Tetris.kBreite*shapeY[sx]; Tetris.BlockPosition [x] [y]=true; Tetris.BlockFarbe[x][y] = type; }
|
leider hängt sich das Programm bei der Ersten Zuweisung auf, bzw. überspring dies. Wieso weis ich nicht. Ich wäre dankbar wenn mir jemand die richtige Vorgehensweise darstellt. Vollständigkeitshalber habe ich den ganzen Code hinzugefügt.
Code: |
package Tetris;
import java.awt.*; import java.awt.image.*; import java.applet.Applet;
import java.lang.*;
/** * * @author Salfador */ public class Tetris extends java.applet.Applet implements Runnable{ public static final int x=10; //linke obere Ecke public static final int y=100; public static final int kBreite=25; //Kästchenbreite public static final int Kx=10; //Anzahl der Kästchen in x Richtung public static final int Ky=22; //Anzahl der Kästchen in y Richtung public static final Color HintergrundFarbe = Color.black; public static int BlockFarbe[] [] = new int[Kx][Ky]; public static boolean BlockPosition[] [] = new boolean[Kx][Ky]; protected int i=1; FeldCanvas myFeldCanvas; FigureCanvas myFigureCanvas; Thread status = null; ScoreCanvas myScoreCanvas; public void init() { setLayout(new BorderLayout()); Panel grid = new Panel(); grid.setLayout(new GridLayout(0, 2)); add("Center", grid); myFeldCanvas = new FeldCanvas(); grid.add(myFeldCanvas); block.xPos=FeldCanvas.BalkenBreite+Kx*kBreite/2+x-kBreite; for(int i=0; i<Kx; i++){ for(int j=0; j<Ky; j++){ BlockFarbe [ i ][j] = 8; BlockPosition [ i ][j] = false; } } } public void start() { if(status == null) { status = new Thread(this); status.start(); } }
public void stop() { status = null; }
public void run() { while (status !=null){ try { Thread.sleep(1000); } catch (InterruptedException e){} block.yPos =+ kBreite*i; myFeldCanvas.repaint(); i++; } status = null; } }
class FeldCanvas extends Canvas implements ImageObserver{ public static final int BalkenBreite = 8; public static final Color BalkenFarbe = Color.blue; //----------Farbenconstanten definieren------------------ public static final int SQUARE_COLOR = 1; public static final int LINE_COLOR = 2; public static final int S_COLOR = 3; public static final int Z_COLOR = 4; public static final int RIGHT_ANGLE_COLOR = 5; public static final int LEFT_ANGLE_COLOR = 6; public static final int TRIANGLE_COLOR = 7; protected Color FigureColor; protected block FallingShape = new block(); public void FigColor(int color){ initializeColor(color); } private void initializeColor(int color){ switch (color) { case SQUARE_COLOR : FigureColor = Color.yellow; break; case LINE_COLOR : FigureColor = Color.GREEN; break; case S_COLOR : FigureColor = Color.CYAN; break; case Z_COLOR : FigureColor = Color.MAGENTA; break; case RIGHT_ANGLE_COLOR : FigureColor = Color.RED; break; case LEFT_ANGLE_COLOR : FigureColor = Color.BLUE; break; case TRIANGLE_COLOR : FigureColor = Color.ORANGE; break; default : FigureColor = Color.BLACK; } } public void paint(Graphics g) { DrawFallingShape(); DrawPlayField(g); //DrawFallingShape(g); }
public void DrawFallingShape() { FallingShape.Display(1,1); }
public void DrawPlayField(Graphics g) { int x, y; g.setColor(BalkenFarbe); g.fillRect (Tetris.x, 0, BalkenBreite, Tetris.kBreite * Tetris.Ky); //linker Balken g.fillRect (Tetris.kBreite * Tetris.Kx + BalkenBreite + Tetris.x, 0, BalkenBreite, Tetris.kBreite * Tetris.Ky);//Rechter Balken g.fillRect (Tetris.x, Tetris.kBreite * Tetris.Ky, Tetris.kBreite * Tetris.Kx + BalkenBreite * 2, BalkenBreite);//Unterer Balken for (x = 0; x < Tetris.Kx; x++) { for (y = 0; y < Tetris.Ky; y++) { FigColor(Tetris.BlockFarbe [x][y]); g.setColor(FigureColor); g.fillRect((BalkenBreite +Tetris.x) + x * Tetris.kBreite + 1, y * Tetris.kBreite + 1, Tetris.kBreite - 2, Tetris.kBreite - 2); g.setColor(Color.white); g.drawRect((BalkenBreite +Tetris.x) + x * Tetris.kBreite, y * Tetris.kBreite, Tetris.kBreite - 1, Tetris.kBreite - 1); } } }
}
class block{ public static int xPos; public static int yPos; //---------Geometrieconstanten definieren---------------- public static final int SQUARE_FIGURE = 1; public static final int LINE_FIGURE = 2; public static final int S_FIGURE = 3; public static final int Z_FIGURE = 4; public static final int RIGHT_ANGLE_FIGURE = 5; public static final int LEFT_ANGLE_FIGURE = 6; public static final int TRIANGLE_FIGURE = 7; private int maxOrientation = 4; private int[] shapeX = new int[4]; private int[] shapeY = new int[4]; public void Figure(int type, int color){ initializeGeo(type); } private void initializeGeo(int type) throws IllegalArgumentException { switch (type) { case SQUARE_FIGURE : maxOrientation = 1; shapeX[0] = -1; shapeY[0] = 0; shapeX[1] = 0; shapeY[1] = 0; shapeX[2] = -1; shapeY[2] = 1; shapeX[3] = 0; shapeY[3] = 1; break; case LINE_FIGURE : maxOrientation = 2; shapeX[0] = -2; shapeY[0] = 0; shapeX[1] = -1; shapeY[1] = 0; shapeX[2] = 0; shapeY[2] = 0; shapeX[3] = 1; shapeY[3] = 0; break; case S_FIGURE : maxOrientation = 2; shapeX[0] = 0; shapeY[0] = 0; shapeX[1] = 1; shapeY[1] = 0; shapeX[2] = -1; shapeY[2] = 1; shapeX[3] = 0; shapeY[3] = 1; break; case Z_FIGURE : maxOrientation = 2; shapeX[0] = -1; shapeY[0] = 0; shapeX[1] = 0; shapeY[1] = 0; shapeX[2] = 0; shapeY[2] = 1; shapeX[3] = 1; shapeY[3] = 1; break; case RIGHT_ANGLE_FIGURE : maxOrientation = 4; shapeX[0] = -1; shapeY[0] = 0; shapeX[1] = 0; shapeY[1] = 0; shapeX[2] = 1; shapeY[2] = 0; shapeX[3] = 1; shapeY[3] = 1; break; case LEFT_ANGLE_FIGURE : maxOrientation = 4; shapeX[0] = -1; shapeY[0] = 0; shapeX[1] = 0; shapeY[1] = 0; shapeX[2] = 1; shapeY[2] = 0; shapeX[3] = -1; shapeY[3] = 1; break; case TRIANGLE_FIGURE : maxOrientation = 4; shapeX[0] = -1; shapeY[0] = 0; shapeX[1] = 0; shapeY[1] = 0; shapeX[2] = 1; shapeY[2] = 0; shapeX[3] = 0; shapeY[3] = 1; break; default : throw new IllegalArgumentException("No figure constant: " + type); } } public void Display (int type, int color) { int x, y; //init Figure(type, color); for(int sx=0;sx<4;sx++){ x=xPos+Tetris.kBreite*shapeX[sx]; y=yPos*Tetris.kBreite*shapeY[sx]; Tetris.BlockPosition [x] [y]=true; Tetris.BlockFarbe[x][y] = type; } } }
|
Bearbeitung von typecast: |
Array-Zugriffe mit i so verändert, dass keine Kursivschrift auftritt
|
Dieser Post wurde am 28.05.2004 um 13:51 Uhr von typecast editiert. |