Tetris

xiaoxiao2021-03-05  24

A Russian square written earlier, I don't know if there is a bug.

/ * * Russian square (basic function implementation only 400 lines of code) * Program main frame is: Gameinit () * Run () -----> Paint () & update () * | * | new curblock < | NEED new block? * | | * Klistener (), Moveright () * MoveDown (), MoveroTate ()) * * Display the game interface Paint (), update () * initializer parameters, create a game thread to enter the game cycle {* Response User Operation Klistener * /

Source code:

TETRICS.JAVA

Import java.awt. *; import java.awt.event. *; import javax.swing. *;

public class Tetrics {JFrame mainF; TetricsCanvas tCanvas = new TetricsCanvas (); public Tetrics () {mainF = new JFrame (); mainF.getContentPane () add (tCanvas);. showFrame ();} private void showFrame () {mainF .SETTILE ("TETRICS"); Mainf.setsize (320, 550); mainf.setresizable (false); mainf.setlocation (300, 100); mainf.setvisible;} public static void main (string [] args) {new TETRICS ();

Tetricscanvas.java

Import java.awt. *; import java.awt.event. *; import javax.swing. *;

public class TetricsCanvas extends Canvas implements Runnable {boolean bGameOver = false; int UNITSIZE = 15, ROWS = 32, COLUMNS = 18; int [] [] gameBoard = new int [ROWS 1] [COLUMNS]; int [] [] blockBoard = New Int [ROWS 1] [Column]; Block [] curblock = new block [4]; boolean bnewblock = true;

Image bgimage; graphics bg;

Public tetricscanvas () {setBackground (color.white); addKeylistener (new klistener ()); gameinit ();} public void gameinit () {for (int i = 0; i

G.drawImage (bgimage, 0, 0, this);} public void run () {while (! bgameover) {try {thread.sleep (100);} catch (exception e) {E.PrintStackTrace ();} (bnewBlock) {newBlock (); checkGameOver (); bnewBlock = false;} else {bnewBlock = moveDown ();!} repaint ();} gameOverDlg ();} public void gameOverDlg () {JPanel msgP = new JPanel () Msgp.setLayout (New GridLayout (3, 1)); Msgp.Add (New Jlabel ("Author: Pluto")); Msgp.Add (New Jlabel ("Date: 2005.4")); msgp.add (New Jlabel ("Site: bjtu15 #"); JOPTIONPANE.SHOWMESSAGEDIALOG (NULL, MSGP, "INFO", JOPTIONPANE.YES_OPTION; PUBLIC VOID CheckgameOver () {for (INT i = 0; i <4; i ) IF (Gameboard [curblock [i] .ROW] [curblock [i] .column] == 1) {bgameOver = true; system.out.println (bgameover);}}

Class Block {Int Row, Column; Public Block (Int row, int column) {this.row = row; this.column = column;}} public void newblock ()} public void newblock ()} public void newblock ()} public void newblock ()} public void newblock ()} public void newblock ()} public void newblock () {int UROW = 0; int UCOLUMN = Column / 2; Switch ((int) (math.random () * 6)) {case 0: / * @@@@@ / curblock [0] = new block (urow, ucolumn - 1); curblock [1] = new block ( Urow, Ucolumn - 2); Curblock [2] = New Block (urow, ucolumn); curblock [3] = new block (urow, ucolumn 1); break; case 1: / * @@@@ / curblock [ 0] = new block (urow 1, ucolumn); curblock [1] = new block (urow, ucolumn); curblock [2] = new block (urow 1, ucolumn - 1); curblock [3] = new block (urow 1, ucolumn 1); break; case 2: / * @@ @@ * / curblock [0] = new block (urow 1, ucolumn); curblock [1] = new block (urow, ucolumn 1); curblock [2] = new block (urow, ucolumn); curblock [3] = new block (urow 1, ucolumn 1); break; casse 3: / * @@ @@ * / curblock [0] = New Block (urow 1, ucolumn); Curblock [ 1] = new block (urow, ucolumn 1); curblock [2] = new block (urow, ucolumn); curblock [3] = new block (urow 1, ucolumn - 1); Break; Case 4: / * @@ @@ * / curblock [0] = new block (urow, ucolumn - 1); curblock [1] = new block (urow, ucolumn); curblock [2] = new block (urow 1, ucolumn - 1) Curblock [3] = new block (urow 1, ucolumn); break; case 5: / * @@@ @ / curblock [0] = new block (urow, ucolumn); curblock [1] = new block ( Urow, Ucolumn - 1);

Curblock [2] = new block (urow, ucolumn 1); curblock [3] = new block (urow 1, ucolumn 1); break; case 6: / * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ade New block (urow, ucolumn); curblock [1] = new block (urow, ucolumn 1); curblock [2] = new block (urow, ucolumn - 1); curblock [3] = new block (urow 1, Ucolumn - 1); Break;}} public void for (INT i = 0; i <4; i ) {IF (Curblock [i] .COLUMN - 1 <0 || Gameboard [Curblock [i]. Row] [Curblock [i] .COLUMN - 1] == 1) Return;} for (int i = 0; i <4; i ) {blockboard [curblock [i] .row] [curblock [i] .column] = 0;} for (int i = 0; i <4; i ) {blockboard [curblock [i] .ROW] [curblock [i] .column - 1] = 1;} for (int i = 0; i < 4; i ) {curblock [i] = new block (curblock [i] .row, curblock [i] .COLUMN - 1);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} ) {IF (Curblock [i] .COLUMN 1> Column - 1 || Gameboard [curblock [i] .ROW] [Curblock [i] .COLUMN 1] == 1) Return;} for (int i = 0 i <4 i ) {Blockboard [curblock [i] .row] [curblock [i] .column] = 0;} for (int i = 0; i <4; i ) {blockboard [curblock [i] .row] [Curblock [i] .COLUMN 1] = 1;} for (INT i = 0; i <4; i ) {curblock [i] = new block (curblock [i] .row, curblock [i] .column 1) }}

Public Boolean MoveDown () {for (INT i = 0; i <4; i ) {if (curblock [i] .row == Rows - 1 || Gameboard [curblock [i] .row 1] [Curblock [i ] .COLUMN] == 1) {copyData (); return false;}} for (int i = 0; i <4; i ) {blockboard [curblock [i] .row] [curblock [i] .column] = 0;} for (int i = 0; i <4; i ) {blockboard [curblock [i] .row 1] [curblock [i] .column] = 1;} for (INT i = 0; i <4 ; i ) {curblock [i] = new block (curblock [i] .row 1, curblock [i] .column);} return true;} public void copyData () {for (int i = 0; i <4 ; i ) {Gameboard [curblock [i] .row] [curblock [i] .COLUMN] = 1;} checkfullrow ();} public void checkfullrow ()} public void checkfullrow ()} public void checkfullrow ()} public void checkfullrow ()} public void checkfullrow ()} public void checkfullrow ()} public void checkfullrow () I> = 0; I - 1; J> = 0; J - GameBoard [i] [j] == 1) Num ;} if (Num == Columns) {for (int R = i; r> 0; r -) for (int C = columns - 1; c> = 0; c -) {gameboard [r] [c] = Gameboard [r - 1 ] [c]; blockboard [r] [c] = blockboard [r - 1] [c]; For (int top = color; TOP ---) {gameboard [0] [TOP] = 0; blockboard [0] [top] = 0;} i ;} Num = 0;}}

Public boolean brotate (block [] tempblock, int i) {if (tempblock [i] .row <0 || Tempblock [i] .row> Rows - 1 || TempBlock [i] .column <0 || Tempblock [i ] .Column> Column - 1) Return False; if (Gameboard [Tempblock [i] .row] [Tempblock [i] .COLUMN] == 1) Return False; Return True;} public void Moverotate () {int [] [] Offset = new int [4] [2]; for (int i = 0; i <4; i ) {offset [i] [0] = curblock [i] .column - curblock [0] .column; offset [i] [1] = curblock [i] .row - curblock [0] .row;} block [] tempblock = new block [4]; for (int i = 0; i <4; i ) {tempblock [i ] = new block (curblock [i] .row, curblock [i] .column);} int tempx, tempy;

For (int i = 0; i <4; i ) {if (Offset [i] [0]> 0 && offset [i] [1] == 0) {TEMPX = Offset [i] [0]; TEMPY = Offset [I] [1]; Tempblock [i] .COLUMN = 0 Tempblock [0] .COLUMN; TEMPBLOCK [i] .row = Tempx Tempblock [0] .row; if (! brotate (Tempblock, i)) {Return;}} else if (Offset [i] [0]> 0 && offet [i] [1]> 0) {TEMPX = Offset [i] [0]; Tempy = Offset [i] [1]; Tempblock [i] .Column = -Tempx Tempblock [0] .Column; tempblock [i] .row = Tempy Tempblock [0] .row; if (! brotate (temblock, i)) {return;}} else Offset [i] [0] == 0 && Offset [i] [1]> 0) {TEMPX = Offset [i] [0]; Tempy = Offset [i] [1]; Tempblock [i] .column = - Tempy Tempblock [0] .COLUMN; Tempblock [i] .row = 0 Tempblock [0] .Row; if (! Brotate (Tempblock, i)) {Return;}} else if (Offset [i] [0] <0 && Offset [I] [1]> 0) {TEMPX = Offset [I] [0]; Tempy = Offset [I] [1]; Tempblock [i] .Column = Tempx Tempblock [0] .column; Tempblock [i] .row = -Tempy Tempblock [0] .Row; if (! Brotate (TempBlock, i)) {return;}} else if (Offset [i] [0] <0 && offset [i] [1] == 0) { Tempx = Offset [i] [0]; Tempy = Offset [i] [1]; Tempblock [i] .COLUMN = 0 Tempblock [0] .COLUMN; TEMPBLOCK [i] .Row = Tempx Tempblock [0]. Row; if (! Brotate (Tempblock, I)) {return;}} else if (Offset [i] [0] <0 && offset [i] [1] <0) {tempx = offset [i] [0] ; Tempy = offset [i] [1]; Tempblock [i] .column = -Tempx

转载请注明原文地址:https://www.9cbs.com/read-35154.html

New Post(0)