The holiday is okay. I wrote 90 lines of Tetris, which is implemented in Java.

Source: Internet
Author: User

 

Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import javax. swing. *; <br/> public class Terris extends jframe implements runnable, keylistener {<br/> private short isplaying = 0, xoffset = 2, yoffset = 0, blocktype = (short) math. round (math. random () * 6), blockrotation = 0, blockcolor = (short) math. round (math. random () * 5); <br/> private short matrix [] [] = new short [21] [10]; // <br/> private short Block [] [] [] [] = {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0 }},/* l */{0, 0, 0}, {1, 1, 1 }, {0, 0, 0, 0}, {0, 0, 0, 0 }},/*-*/{0, 0, 0, 0 }, {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0},/* z */{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0 }}}, /* z | */{0, 0, 0, 0}, {0, 1, 1, 0}, {1, 1, 0 }, {0, 0, 0 }},/* xz */{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0 }}}, /* xz | */{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1 }, {0, 0, 0, 0 }},/** [] */{0, 1, 1, 0}, {0, 1, 0, 0, 0 },{ 0, 1, 0, 0 },{ 0, 0, 0, 0 }},{ 0, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0 }},{ 0, 1, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0 }},{ {1, 0, 0, 0 }, {1, 1, 1, 0}, {0, 0, 0}, {0, 0, 0, 0 }},/* f */{1, 1, 0, 0}, {0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0 }},{ {0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0 }},{ 0, 1, 0, 0}, {0, 1, 0 }, {0, 1, 1, 0}, {0, 0, 0, 0 }},{ {0, 0, 0}, {1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0 }},/* XF */{0, 1, 0, 0 },{ 1, 1, 1, 0 },{ 0, 0, 0, 0}, {0, 0, 0, 0 }},{ 0, 1, 0, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0 }}, {0, 0, 0}, {1, 1, 0 }, {0, 1, 0, 0}, {0, 0, 0, 0 }},{ {0, 1, 0, 0}, {1, 1, 0, 0 },{ 0, 1, 0, 0}, {0, 0, 0 }}}}; /* T */<br/> private image [] images = {New imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/red.gif "). getimage (), new imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/blue.gif "). getimage (), new imageicon ("D:/Java/Eclipse/workspace/ Terris/src/img/pink.gif ")). getimage (), new imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/bblue.gif ")). getimage (), new imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/orange.gif ")). getimage (), new imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/green.gif ")). getimage (), new imageicon ("D:/Java/Eclipse/workspace/Terris/src/img/red.gif "). getimage () }; <br/> Public Terris () {<br/> setsize (160, 335); <br/> setvisible (true); <br/> createbufferstrategy (2); <br/> setdefaclocloseoperation (jframe. exit_on_close); <br/> addkeylistener (this); <br/>}< br/> Public void paint (Graphics g) {<br/> graphics Tg = This. getbufferstrategy (). getdrawgraphics (); <br/> TG. fillrect (5, 30,150,340); <br/> for (INT I = 0; I <21; I ++) <br/> for (Int J = 0; j <10; j ++) {<br/> If (Matrix [I] [J]! = 0) <br/> TG. drawimage (images [matrix [I] [J], J * 15 + 5, I * 15 + 15, null ); <br/> If (I <4 & J <4 & Block [blocktype] [blockrotation] [I] [J]! = 0) <br/> TG. drawimage (images [blockcolor + 1], (J + xoffset) * 15) + 5, (I + yoffset) * 15) + 15, null ); <br/>}< br/> This. getbufferstrategy (). show (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> New thread (New Terris ()). start (); <br/>}< br/> Public void run () {<br/> while (isplaying = 0) <br/> try {<br/> If (check (0, 0, 0, 1) <br/> yoffset + = 1; <br/> else {<br/> If (yoffset = 0) {<br/> I Splaying = 1; <br/> continue; <br/>}< br/> freezeandnew (); <br/>}< br/> repaint (); <br/> thread. sleep (600); <br/>} catch (interruptedexception e) {<br/>}< br/> private Boolean check (INT left, int right, int up, int down) {<br/> for (INT I = 0; I <4; I ++) <br/> for (Int J = 0; j <4; j ++) <br/> If (xoffset + J-left + right <0 | xoffset + J-left + Right> = 10) & Block [blocktype] [(blockrot Ation + up)> = block [blocktype]. length? 0: (blockrotation + up)] [I] [J]! = 0) | (yoffset + I + down> = 21) & Block [blocktype] [(blockrotation + up)> = block [blocktype]. length? 0: (blockrotation + up)] [I] [J]! = 0) | (Block [blocktype] [(blockrotation + up)> = block [blocktype]. length? 0: (blockrotation + up)] [I] [J]! = 0 & matrix [yoffset + I + down] [xoffset + J-left + right]! = 0) <br/> return false; <br/> return true; <br/>}< br/> private void freezeandnew () {<br/> Boolean [] Clear = new Boolean [4]; <br/> for (INT I = 0; I <4; I ++) {<br/> for (Int J = 0; j <4; j ++) <br/> If (Block [blocktype] [blockrotation] [I] [J]! = 0) <br/> matrix [I + yoffset] [J + xoffset] = (short) (blockcolor + 1 ); <br/> clear [I] = I + yoffset> = matrix. length? False: (Matrix [I + yoffset] [0]! = 0 & matrix [I + yoffset] [1]! = 0 & matrix [I + yoffset] [2]! = 0 & matrix [I + yoffset] [3]! = 0 & matrix [I + yoffset] [4]! = 0 & matrix [I + yoffset] [5]! = 0 & matrix [I + yoffset] [6]! = 0 & matrix [I + yoffset] [7]! = 0 & matrix [I + yoffset] [8]! = 0 & matrix [I + yoffset] [9]! = 0); <br/>}< br/> for (INT I = 0; I <clear. length; I ++) <br/> If (clear [I]) <br/> for (Int J = yoffset + I; j> 0; j --) <br/> matrix [J] = matrix [J-1]; <br/> yoffset = blockrotation = 0; <br/> xoffset = 2; <br/> blocktype = (short) math. round (math. random () * 6); <br/> blockrotation = (short) math. round (math. random () * (Block [blocktype]. length-1); <br/> blockcolor = (short) math. round (math. random () * 5); <br/>}< br/> publi C void keypressed (keyevent e) {// 38-Top 40-bottom 37-left 39-Right <br/> If (E. getkeycode () = 65 | E. getkeycode () = 37) & check (1, 0, 0, 0) & isplaying = 0) {// left <br/> xoffset --; <br/>} else if (E. getkeycode () = 68 | E. getkeycode () = 39) & check (0, 1, 0, 0) & isplaying = 0) {// right <br/> xoffset ++; <br/>} else if (E. getkeycode () = 87 | E. getkeycode () = 38) & check (0, 0, 1, 0) & isplaying = 0) {// up <Br/> blockrotation = (short) (blockrotation + 1)> = block [blocktype]. length? 0: (blockrotation + 1); <br/>} else if (E. getkeycode () = 83 | E. getkeycode () = 40) & check (0, 0, 0, 1) & isplaying = 0) {// down <br/> yoffset + = 1; <br/>} else if (E. getkeycode () = 83 | E. getkeycode () = 40) & isplaying = 0) <br/> freezeandnew (); <br/> repaint (); <br/>}< br/> Public void keyreleased (keyevent arg0) {<br/>}< br/> Public void keytyped (keyevent arg0) {<br/>}< br/> // by: http://wireless.javaeye.com/blog/595321

Reprinted please indicate the source http://wireless.javaeye.com/blog/595321

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.