Die Beispiele in diesem Artikel teilen Ihnen den spezifischen Code der Java -Implementierung Tetris für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Speichern Sie die Karte des Spiels mit einem 2D -Array:
// Game Map Grid, jedes Netz speichert einen Platz, Array -Aufzeichnung des Status des Block Private Status Map [] [] = New Status [Zeilen] [Spalten];
Initialisieren Sie alle Gitter in der Karte, um vor dem Spiel zu leeren:
/* Initialisieren Sie alle Blöcke auf leer*/for (int i = 0; i <map.length; i ++) {für (int j = 0; j <map [i] .Length; j ++) {map [i] [j] = state.empty; }}Während des Spiels können wir die Blöcke auf der Schnittstelle sehen, damit wir alle Blöcke in der Karte zeichnen müssen. Natürlich müssen die Spielpunkte und das Ende des Spiels zusätzlich zum Zeichnen der Blöcke auch bei Bedarf gezeichnet werden:
/*** Zeichnen Sie den Inhalt des Formulars, einschließlich Spielblöcke, Spielpunkte oder End -String*/@oversidepublic void Paint (Graphics g) {Super.Paint (g); für (int i = 0; i <Zeilen; i ++) {für (int j = 0; j <spalten; j ++) {if (map [i] [j] == STATE.Active) {// Zeichnen Sie den aktiven Block G.SetColor (ActiveColor); G.FillRoundRect (j * block_size, i * block_size + 25, block_size - 1, block_size - 1, block_size / 5, block_size / 5); } else if (map [i] [j] == state.stoped) {// einen statischen Block G.SetColor (stopedColor) zeichnen; G.FillRoundRect (j * block_size, i * block_size + 25, block_size - 1, block_size - 1, block_size / 5, block_size / 5); }}} /* Print Score* / G.SetColor (ScoreColor); G.Setfont (neue Schriftart ("Times New Roman", Font.bold, 30)); G.Drawstring ("Score:" + Totalscore, 5, 70); // Das Spiel endet, drucken Sie die End -String if (! Isgoingon) {g.setColor (color.red); G.SetFont (neue Schriftart ("Times New Roman", Font.bold, 40)); G.Drawstring ("Game Over!", this.getWidth () / 2 - 140, this.getheight () / 2); }}Verschiedene Arten von Grafiken, die aus Quadraten bestehen, werden durch Zufallszahlen generiert. Im Allgemeinen sieben Arten von Grafiken: Balken, Feld, reguläre 7, Reverse 7, T-förmige, z-förmige und umgekehrte Z-förmige, z. B. Balken erzeugen:
Karte [0] [Randpos] = MAP [0] [Randpos - 1] = MAP [0] [Randpos + 1] = MAP [0] [Randpos + 2] = Zustand.Active;
Nach der Erzeugung der Grafiken werden der Aufenthaltsort implementiert. Wenn Sie auf Hindernisse stoßen, können Sie nicht weiter fallen:
isfall = wahr; // Whether it can fall// Check from the current line, stop the fall if an obstacle is encountered for (int i = 0; i < blockRows; i++) { for (int j = 0; j < columns; j++) { // If the block in the line is an active block, the block is a static block, the block is encountered if (map[rowIndex - i][j] == State.ACTIVE && map[rowIndex - i + 1] [j] == STATE.Stoped) {isFall = false; // Stoppen Sie die Herbstpause; }} if (! isfall) break;}Wenn kein Hindernis auftritt, bewegt sich das Blockdiagramm eine Linie insgesamt nach unten:
// The graph falls a row for (int i = 0; i < blockRows; i++) { for (int j = 0; j < columns; j++) { if (map[rowIndex - i][j] == State.ACTIVE) { // The active block moves down one row map[rowIndex - i][j] = State.EMPTY; // Der ursprüngliche aktive Block wird zu einer leeren Blockkarte [rowIndex - i + 1] [j] = Status.Active; // Die nächste Blocklinie wird zu einem aktiven Block}}}Eine ähnliche Operation ist bei der Bewegung nach links und rechts:
/*** Gehen Sie nach links*/private void links () {// Markieren Sie, ob ein Hindernis auf der linken booleschen Hasblock = false; /* Check whether there is an obstacle on the left*/ for (int i = 0; i < blockRows; i++) { if (map[rowIndex - i][0] == State.ACTIVE) { // Check whether the left is a wall hasBlock = true; brechen; // There is an obstacle, do not need to loop to judge the row} else { for (int j = 1; j < columns; j++) { // Check whether there are other blocks on the left if (map[rowIndex - i][j] == State.ACTIVE && map[rowIndex - i][j - 1] == State.STOPED) { hasBlock = true; brechen; // Es gibt ein Hindernis, keine Notwendigkeit, die Beurteilungsspalte zu recyceln}} Wenn (HasBlock) Break; // There is an obstacle, no need to recycle the judgment row} } /* There is no obstacle on the left, move the figure to the left by the distance of a block*/ if (!hasBlock) { for (int i = 0; i < blockRows; i++) { for (int j = 1; j < columns; j++) { if (map[rowIndex - i][j] = State.EMPTY; map[rowIndex - i] [j - 1] = state.active;}}} // Repaint Repaint ();Wenn Sie sich nach unten bewegen, reduziert es das Zeitintervall für jeden normalen Zustand:
/*** Gehen Sie direkt nach unten*/private void Down () {// Markierungen können den Fall sofort beschleunigen = true;}Wie man die Richtung des Diagramms ändert, wird hier nur eine sehr einfache Methode verwendet, um eine Richttransformation zu erreichen. Natürlich kann es einen besseren Algorithmus geben, um Richttransformationsvorgänge zu implementieren. Sie können es selbst studieren:
/*** Drehen Sie die quadratische Form*/private void rotate () {try {if (form == 4) {// quadratisch, die gleiche Form wird vor und nach der Rotation zurückgegeben; } else if (form == 0) {// Streifen // Temporäres Array, platzieren Sie die Abbildung nach Rotationszustand [] [] tmp = neuer Zustand [4] [4]; int startColumn = 0; // Find the first square position at the beginning of the figure for (int i = 0; i < columns; i++) { if (map[rowIndex][i] == State.ACTIVE) { startColumn = i; brechen; }} // Finden Sie heraus, ob nach der Rotation ein Hindernis vorhanden ist. If there is an obstacle, do not rotate for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (map[rowIndex - 3 + i][j + startColumn] == State.STOPED) { return; } } } } if (map[rowIndex][startColumn + 1] == State.ACTIVE) { // Horizontal bar, transform into a vertical bar for (int i = 0; i < 4; i++) { tmp[i][0] = State.ACTIVE; für (int j = 1; j <4; j ++) {tmp [i] [j] = STAAT.EMPTY; }} blockrows = 4; } else {// vertikale Balken, verwandeln Sie sich in horizontale Balken für (int j = 0; j <4; j ++) {tmp [3] [j] = state.active; für (int i = 0; i <3; i ++) {tmp [i] [j] = State.Empty; }} blockrows = 1; } // Modify the graph in the original map to a transformed graph for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { map[rowIndex - 3 + i][startColumn + j] = tmp[i][j]; }}} else {// Temporäres Array, platzieren Sie die Rotation des Figurenzustands [] [] tmp = neuer Zustand [3] [3]; int startColumn = Spalten; // Find the first block position at the beginning of the figure for (int j = 0; j < 3; j++) { for (int i = 0; i < columns; i++) { if (map[rowIndex - j][i] == State.ACTIVE) { startColumn = i < startColumn ? I: StartColumn; } } } // Determine whether there will be obstacles after transformation for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (map[rowIndex - 2 + j][startColumn + 2 - i] == State.STOPED) return; }} // transformieren für (int i = 0; i <3; i ++) {für (int j = 0; j <3; j ++) {tmp [2 - j] [i] = map [rowIndex - 2+i] [startColumn+j]; } } // Modify the graph in the original map to the transformed graph for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { map[rowIndex - 2 + i][startColumn + j] = tmp[i][j]; }} // Repaint Repaint (); // Remove the line pointer for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (map[rowIndex - i][startColumn + j] != null || map[rowIndex - i][startColumn + j] != State.EMPTY) { rowIndex = rowIndex - i; BlockRows = 3; zurückkehren; } } } } } } catch (Exception e) { // If an array subscript is encountered, it means that the shape of the graph cannot be changed and no processing is done}}When the figure falls and stops when it encounters obstacles, we need to determine whether there is a certain line or several lines that can be eliminated. Zu diesem Zeitpunkt können wir zuerst die Anzahl der Quadrate in jeder Zeile erhalten und dann ein Urteil fällen:
int [] blocksCount = new int [Zeilen]; // Notieren Sie die Anzahl der Spalten mit Blöcken pro Zeile int eliminaterows = 0; // Anzahl der Zeilen eliminiert/* Berechnen Sie die Anzahl der Blöcke pro Zeile*/for (int i = 0; i <Zeilen; i ++) {blocksCount [i] = 0; für (int j = 0; j <columns; j ++) {if (map [i] [j] == STATE.stoped) blocksCount [i] ++; }}Wenn es eine vollständige Reihe von Blöcken gibt, wird die Blöckenreihe beseitigt:
/* Implementieren Sie Blockausscheidungsoperation mit vollständigen Zeilen*/for (int i = 0; i <Zeilen; i ++) {if (blocksCount [i] == Spalten) {// eine Zeile für (int m = i; m> = 0; m--) {für (int n = 0; n <spalten; n ++) {map [n] = (m = (m = () (m = (M = () (m = (m = () (m = (m = () (M = (m = () (m = (m = () (m = (m = () (m = (m = () (m = (m = () (m = (m = () (m = (m = () () () ()afes State.eMpty: map [m - 1] [n]; }} eliminaterows ++; // Notieren Sie die Anzahl der eliminierten Zeilen}}Schließlich können wir die Punkte neu streichen und sie anzeigen.
Wiederholen Sie die oben genannten Operationen der Erzeugung von Grafiken, fallenden Grafiken, der links und rechts bewegenden und der Eliminierungslinie, und ein einfacher Tetris wird abgeschlossen.
Laufeffekt:
Vollständiger Beispielcode: Tetris
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.