) Release a simple Java-written Tetris source code algorithm (300 lines) with detailed comments

Source: Internet
Author: User

From: http://topic.csdn.net/u/20100612/03/A8D7B257-4385-4BB8-82FF-4A51AC3BD810.html

 

Import Java. AWT. *; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. AWT. event. keyevent; <br/> Import Java. AWT. event. keylistener; <br/> Import javax. swing. *; <br/> Import javax. swing. timer; </P> <p> public class Tetris extends jframe {<br/> Public Tetris () {<br/> tetrisblok A = new tetrisblok (); <br/> addkeylistener (a); <br/> Add (a); <br/>}</P> <p> Publ IC static void main (string [] ARGs) {<br/> Tetris frame = new Tetris (); <br/> jmenubar menu = new jmenubar (); <br/> frame. setjmenubar (menu); <br/> jmenu game = new jmenu ("game"); <br/> jmenuitem newgame = game. add ("New Game"); <br/> jmenuitem pause = game. add ("Suspend"); <br/> jmenuitem goon = game. add ("continue"); <br/> jmenuitem exit = game. add ("exit"); <br/> jmenu help = new jmenu ("help"); <br/> jmenuitem about = He LP. add ("about"); <br/> menu. add (game); <br/> menu. add (HELP); <br/> frame. setlocationrelativeto (null); <br/> frame. setdefaclocloseoperation (jframe. exit_on_close); <br/> frame. setsize (220,275); <br/> frame. settitle (" RIS beta version"); <br/> // frame. setundecorated (true); <br/> frame. setvisible (true); <br/> frame. setresizable (false ); </P> <p >}< br/>}</P> <p> // create a Tetris class <br/> class tetrisblok extends jpanel im Plements keylistener {</P> <p> // blocktype indicates the block type <br/> // turnstate indicates the block status <br/> private int blocktype; <br/> private int score = 0; </P> <p> private int turnstate; </P> <p> private int X; </P> <p> private int y; </P> <p> private int I = 0; </P> <p> Int J = 0; <br/> int flag = 0; <br/> // defines the placed blocks x = 0-11, y = 0-21; <br/> int [] [] map = new int [13] [23]; </P> <p> // the first group of squares indicates the block type S, Z, L, J, I, O, and T. The second group indicates the number of times the third group is rotated. is Square Matrix <br/> private final int shapes [] [] [] = new int [] [] {<br/> // I <br/> {0, 0, 0, 0, 1, 1, 1, 1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br/> // L <br/> {1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, <br/> {1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, <br/> {1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0}, <br/> {0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }}, <br/> // T <br/> {0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },< br/> {0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },< br/> {1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },< br/> {0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0 }}; </P> <p> // method for generating new blocks <br/> Public void newblock () {<br/> blocktype = (INT) (math. random () * 1000) % 7; <br/> turnstate = (INT) (math. random () * 1000) % 4; <br/> X = 4; <br/> Y = 0; <br/> If (gameover (x, y) = 1) {</P> <p> NEWMAP (); <br/> drawwall (); <br/> score = 0; <br/> joptionpane. showmessagedialog (null, "Game over"); <br/>}</P> <p> // draw a wall <br/> Public void drawwall () {<br/> for (I = 0; I <12; I ++) {<br/> map [I] [21] = 2; <br/>}< br/> for (j = 0; j <22; j ++) {<br/> map [11] [J] = 2; <br/> map [0] [J] = 2; <br/>}</P> <p> // initialize the map. <br/> Public void NEWMAP () {<br/> for (I = 0; I <12; I ++) {<br/> for (j = 0; j <22; j ++) {<br/> map [I] [J] = 0; <br/>}</P> <p> // initialization constructor <br/> tetrisblok () {<br/> newblock (); <br/> NEWMAP (); <br/> drawwall (); <br/> timer = new timer (1000, new timerlistener (); <br/> timer. start (); <br/>}</P> <p> // method of rotation <br/> Public void turn () {<br/> int tempturnstate = turnstate; <br/> turnstate = (turnstate + 1) % 4; <br/> If (blow (X, Y, blocktype, turnstate) = 1) {<br/>}< br/> If (blow (X, Y, blocktype, turnstate) = 0) {<br/> turnstate = tempturnstate; <br/>}< br/> repaint (); <br/>}</P> <p> // left shifting method <br/> Public void left () {<br/> If (blow (X-1, y, blocktype, turnstate) = 1) {<br/> X = x-1; <br/>}< br/>;< br/> repaint (); <br/>}</P> <p> // right shifting method <br/> Public void right () {<br/> If (blow (x + 1, y, blocktype, turnst Ate) = 1) {<br/> X = x + 1; <br/>}< br/> <br/> repaint (); <br/>}</P> <p> // method of dropping <br/> Public void down () {<br/> If (blow (X, Y + 1, blocktype, turnstate) = 1) {<br/> Y = Y + 1; <br/> delline (); <br/>}< br/>; <br/> If (blow (X, Y + 1, blocktype, turnstate) = 0) {<br/> Add (X, Y, blocktype, turnstate); <br/> newblock (); <br/> delline (); <br/>}< br/>; <br/> repaint (); <br/>}</P> <p> // Yes Valid method <br/> Public int blow (int x, int y, int blocktype, int turnstate) {<br/> for (int A = 0; A <4; A ++) {<br/> for (INT B = 0; B <4; B ++) {<br/> If (SHAPES [blocktype] [turnstate] [A * 4 + B] = 1) & (Map [x <br/> + B + 1] [Y + A] = 1 )) <br/> | (SHAPES [blocktype] [turnstate] [A * 4 + B] = 1) & (Map [x <br/> + B + 1] [Y + A] = 2) {</P> <p> return 0; <br/>}< br/> return 1; <BR/>}</P> <p> // Method for deleting rows <br/> Public void delline () {<br/> int C = 0; <br/> for (INT B = 0; B <22; B ++) {<br/> for (int A = 0; A <12; A ++) {<br/> If (Map [a] [B] = 1) {</P> <p> C = C + 1; <br/> If (C = 10) {<br/> score + = 10; <br/> for (INT d = B; D> 0; d --) {<br/> for (int e = 0; e <11; E ++) {<br/> map [E] [d] = map [E] [d-1]; </P> <p >}< br/> C = 0; <br/>}< B R/>}</P> <p> // determine the method you are using. <br/> Public int gameover (int x, int y) {<br/> If (blow (X, Y, blocktype, turnstate) = 0) {<br/> return 1; <br/>}< br/> return 0; <br/>}</P> <p> // Add the current Map <br/> Public void add (int x, int y, int blocktype, int turnstate) <br/> Int J = 0; <br/> for (int A = 0; A <4; A ++) {<br/> for (INT B = 0; B <4; B ++) {<br/> If (Map [x + B + 1] [Y + A] = 0) {<br/> map [x + B + 1] [Y + A] = shapes [blocktype] [turnstate] [J]; <br/>}< br/>;< br/> J ++; <br/>}</P> <p> // Method for drawing blocks <br/> Public void paintcomponent (Graphics g) {<br/> super. paintcomponent (g); <br/> // draw the current square <br/> for (j = 0; j <16; j ++) {<br/> If (SHAPES [blocktype] [turnstate] [J] = 1) {<br/> G. fillrect (J % 4 + x + 1) * 10, (J/4 + y) * 10, 10, 10 ); <br/>}< br/> // draw fixed blocks <br/> (J = 0; j <22; j ++) {<br/> for (I = 0; I <12; I ++) {<br/> If (Map [I] [J] = 1) {<br/> G. fillrect (I * 10, J * 10, 10, 10); </P> <p >}< br/> If (Map [I] [J] = 2) {<br/> G. drawrect (I * 10, J * 10, 10, 10); </P> <p >}< br/> G. drawstring ("score =" + score, 125, 10); <br/> G. drawstring ("Resist poor games,", 125, 50); <br/> G. drawstring ("reject pirated games. ", 125, 70); <br/> G. drawstring (" Pay attention to self-protection, ", 125, 90); <br/> G. drawstring (" Beware of fraud. ", 125,110); <br/> G. drawstring (" moderate game benefits brain, ", 125,130); <br/> G. drawstring (" indulge in game injury. ", 125,150); <br/> G. drawstring (" reasonably scheduled time, ", 125,170); <br/> G. drawstring (" enjoy a healthy life. ", 125,190); <br/>}</P> <p> // keyboard listener <br/> Public void keypressed (keyevent E) {<br/> switch (E. getkeycode () {<br/> case keyevent. vk_down: <br/> down (); <br/> break; <br/> case keyevent. vk_up: <br/> turn (); <br/> break; <br/> case keyevent. vk_right: <br/> right (); <br/> break; <br/> case keyevent. vk_left: <br/> left (); <br/> break; <br/>}</P> <p> // useless <br/> Public void keyreleased (keyevent E) {<br/>}</P> <p> // useless <br/> Public void keytyped (keyevent E) {<br/>}</P> <p> // timer listener <br/> class timerlistener implements actionlistener {<br/> Public void actionreceivmed (actionevent E) {</P> <p> repaint (); <br/> If (blow (X, Y + 1, blocktype, turnstate) = 1) {<br/> Y = Y + 1; <br/> delline (); <br/>}< br/>; <br/> If (blow (X, Y + 1, blocktype, turnstate) = 0) {</P> <p> If (flag = 1) {<br/> Add (X, Y, blocktype, turnstate); <br/> delline (); <br/> newblock (); <br/> flag = 0; <br/>}< br/> flag = 1; <br/>}< br/>; <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.