Java language-Case Study of Tetris Java source code, java Tetris
Only reference source code is provided
Import java. awt. *; import java. applet. applet; public class Blocks extends Applet implements Runnable {Dimension d; Font largefont = new Font ("Helvetica", Font. BOLD, 28); Font smallfont = new Font ("Helvetica", Font. BOLD, 10); Color textcolor1 = new Color (96,128,255); Color textcolor2 = new Color (255,160, 64); FontMetrics fmsmall, fmlarge; Graphics goff; Image ii; Threadthethread; boolean ingame = false; final short xbloc Ks = 10; final short yblocks = 20; final int blocksize = 16; final int width = xblocks * blocksize; final int height = yblocks * blocksize; short [] [] screendata; final short maxcolors = 6; Color [] blocks; final int barwidth = 8; final Color barcolor = new Color (128,255, 64); final Color background = new Color, 0); int score; short emptyline; int objectx, objecty; int objectx; short objecttype; short objectcolor; int objectrotation; in T objectrotationd = 0; short objectptr; short checkptr; final short itemcount = 7; final short itemrotlen = 8; final short itemlen = itemrotlen * 4; short count; final short maxcount = 5; short curcount; boolean fast = false; final short screendelay = 40; short screencount = 40; boolean showtitle = true; int items [] = {0,-1, 0,-1, -1,-1, // square, normal status: 0,-, 0,-, // 90 degrees 0, 0, 0, 0,-1, 1,-1, 0, 0,-1, 0,-2, 0,-, 0,, 0,-1,-1,-, 0, 0,-1,-, 0,, 1,-, 0,-, 0,-1,-1, 1,-, 0,-,-1,-,-, 0, ,-, 0, 0,-1,-, 0,-,-, 0, 0,-1, ,-, 0,-1, 0,-1, 1,-1, 0, -1,-1,-, 0,-, 0, 0 ,-,, 0, 0,-1,-1,-, 0, 0,-, 0,-1, 0, 0,-, 1,-1,}; int checks [] =,,, ,-,-, ,,, 1, 1,}; public string getAppletInfo () {return ("Blocks-by Ali");} // initialize appletpublic void init () {short I; screendata = new short [xblocks] [yblocks]; Blocks = new Color [maxcolors + 1]; // sets the background Color blocks [0] = background; // sets the block Color blocks [1] = new Color (, 0, 0 ); blocks [2] = new Color (0,255, 0); blocks [3] = new Color (255, 255,255); blocks [4] = new Color (, 0 ); blocks [5] = new Color (255, 0,255,255); blocks [6] = new Color (); Graphics g; resize (width + 2 * barwidth, height + 30 ); d = size (); setBackground (background); g = getGraphics (); g. setFont (smallfont); fmsmall = g. getFontMetrics (); g. setFon T (largefont); fmlarge = g. getFontMetrics (); gameInit () ;}// initialize the public boid gameInit () {short I, j; for (I = 0; I <xblocks; I ++) {for (j = 0; j <yblocks; j ++) {screendata [I] [j] = 0 ;}} score = 0; emptyline =-1; newObject (); fast = false; curcount = maxcount;} public void newObject () {short I; int y; objectx = xblocks/2-1; objectdx = 0; objecty = 0; objecttype = (short) (Math. random () * itemcount); if (objecttype> = itemcount) objecttype = itemcount-1; object Ptr = (short) (objecttype * itemlen); checkptr = (short) (Mat. random () * maxcolors + 1); if (objectcolor> maxcolors) objectcolor = maxcolors; objectrotation = 0; count = maxcount; // determine whether the game is over for (I = 0; I <4; I ++) {y = items [objectptr + I * 2 + 1]; if (y> = 0 & screendata [objectx + items [objectptr + I * 2] [y]! = 0) {ingame = false; showtitle = true ;}}// checks whether the keyboard is pressed and processes the Event public boolean keyDown (Event e, int key) {if (ingame) {if (Key = Event. LEFT) {objectdx =-1;} else if (Key = Event. RIGHT) {objectdx = 1;} else if (Key = Event. UP) {objectrotationd = 1;} else if (Key = Event. DOWN) {fast = true;} else if (Key = Event. ESCAPE) {ingame = false ;}} else {if (Key = 's' | Key = 's') {ingame = true; gameInit ();}} return true;} // process the keyboard lift event public boolean keyUp (Even T e, int key) {if (Key = Event. DOWN) {fast = false;} return true;} // The public void paint (Graphics g) {Graphics gg; if (goff = null & d. width> 0 & d. height> 0) {ii = createImage (d. width, d. height); goff = ii. getGraphics () ;}if (goff = null | ii = null) return; // set the background color goff. setColor (background); goff. fillRect (0, 0, d. width, d. height); // if the game has started if (ingame) // call the playGame () method playGame (); else // otherwise, show other information // display the initial information showIntro (); // display the score ShowScore (); g. drawImage (ii, 0, 0, this) ;}// implement the public void playGame () {boolean bottomreached = false; boolean stillscrolling = false; if (emptyline <0) {bottomreached = drawObject () ;}else {scrollDown (); stillscrolling = true ;} drawBars (); drawBlocks (); if (stillscrolling | bottomreached) {checkFull () ;}}// display the initial information public void showIntro () {String s; drawBars (); drawBlocks (); if (showtitle) {goff. setFont (largefont); s = "Tetris" goff. SetColor (textcolor1); goff. drawString (s, barwidth + (width-fmlarge.stringWidth (s)/2-2, height/2-22); goff. setColor (Color. white); goff. drawString (s, barwidth + (width-fmlarge.stringWidth (s)/2, height/2-20);} else {goff. setFont (smallfont); s = "please press the 's' key to start the game"; goff. setColor (textcolor1); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2-1, height/2-31); goff. setColor (Color. white); goff. drawString (s, barwidth + (Width-fmsmall.stringWidth (s)/2, height/2-30); s = "use left and right arrow keys to control motion"; goff. setColor (textcolor2); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2-1, height/2-11); goff. setColor (Color. white); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2, height/2-10); s = "rotate with the downward arrow key"; goff. setColor (textcolor2); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2-1, height/2 + 9); goff. setColor (Color. w Hite); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2, height/2 + 10); s = "Use the downward arrow key to achieve descent"; goff. setColor (textcolor2); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2-1, height/2 + 29); goff. setColor (Color. white); goff. drawString (s, barwidth + (width-fmsmall.stringWidth (s)/2, height/2 + 30);} screencount --; if (screencount <= 0) {screencount = screendelay; showtitle =! Showtitle ;}} public void drawBars () {goff. setColor (barcolor); goff. fillRect (0, 0, barwidth, blocksize * yblocks); goff. fillRect (barwidth + blocksize * xblocks, 0, barwidth, blocksize * yblocks); goff. fillRect (0, blocksize * yblocks, xblocks * blocksize + 2 * barwidth, barwidth);} public boolean drawObject () {short I; boolean bottomreached = false; int x, y, checkx, checky; // clear the old image for (I = 0; I <4; I ++) {x = objectx + items [objectptr + I * 2 + objectrota Tion * itemrotlen]; y = objecty + items [objectptr + I * 2 + objectrotation * itemrotlen + 1]; checkx = objectx + checks [objectptr + I * 2 + objectrotation * itemrotlen]; checky = objecty + checks [objectptr + I * 2 + objectrotation * itemrotlen + 1]; if (y> = 0) screendata [x] [y] = 0; if (screendata [checks] [checky]! = 0) bottomreached = true;} if (! Bottomreached) {count --; if (count <= 0 | fast) {objecty ++; count = curcount;} checkRotation (); objectdx = 0; objectrotationd = 0 ;} // draw a new image for (I = 0; I <4; I ++) {x = objectx + items [objectptr + I * 2 + objectrotation * itemrotlen]; y = objecty + items [objectptr + I * 2 + objectrotation * itemrotlen + 1]; if (y> = 0) screendata [x] [y] = objectcolor; if (y> = (yblocks-1) bottomreached = true;} if (bottomreached) {score ++; newObject () ;}return bottomreached;} public voi D checkRotation () {int dummyx; int dummyrot; int x, y; short I; boolean cando = true; dummyrot = (objectrotation + objectrotationd) % 4; dummyx = objectx + objectdx; // confirm that this part does not rotate the screen for (I = 0; I <4; I ++) {x = dummyx + items [objectptr + I * 2 + dummyrot * itemrotlen]; if (x> = xblocks) dummyx-= (x-xblocks + 1 ); else if (x <0) dummyx-= x ;}for (I = 0; (I <4 & cando); I ++) {x = dummyx + items [objectptr + I * 2 + dummyrot * itemrotlen]; y = objecty + items [objectptr + I * 2 + dummyrot * Itemrotlen + 1]; if (y> = 0) cando = cando & (screendata [x] [y] = 0 ); if (y> = yblocks | x <0 | x> = xblocks) cando = false;} if (cando) {objectrotation = dummyrot; objectx = dummyx ;}} public void drawBlocks () {short x, y; for (x = 0; x <xblocks; x ++) {for (y = 0; y <yblocks; y ++) {goff. setColor (blocks [screendata [x] [y]); goff. drawRect (x * blocksize + barwidth, y * blocksize, blocksize-1, blocksize-1); goff. fillRect (x * blocksize + barwidth + 3, y * blocksize + 3, bloc Ksize-6, blocksize-6) ;}} public void checkFull () {short x, y; boolean found = false; for (y = yblocks-1; (y> = 0 &&! Found); y --) {found = true; for (x = 0; x <xblocks; x ++) {if (screendata [x] [y] = 0) found = false;} if (found) {score + = 10; // when the user scores a lot, increase the game speed if (score> 800) curcount = 1; else if (score> 600) curcount = 2; else if (score> 400) curcount = 3; else if (score> 200) curcount = 4; for (x = 0; x <xblocks; x ++) {screendata [x] [y] = 0;} emptyline = y ;}} public void scrollDown () {short x, y; for (y = emptyline; y> 0; y --) {for (x = 0; x <xblocks; x ++) {screendata [x] [y] = screendata [x] [Y-1];} for (x = 0; x <xblocks; x ++) {screendata [x] [0] = 0;} emptyline =-1 ;} public void showScore () {String s; goff. setFont (smallfont); goff. setColor (Color. white); s = "score:" + score; goff. drawString (s, width/2-40, (yblocks + 1) * blocksize + 10);} public void run () {long starttime; Graphics g; Thread. currentThread (). setPriorit (Thread. MAX_PRIORITY); g = getGraphics (); while (true) {starttime = System. currentTimeMillis (); try {paint (g); st Arttime + = 60; Thread. sleep (Math. max (0, starttime-System.currentTimeMillis ();} catch (InterruptedException e) {break ;}} public void start () {if (thethread = null) {thethread = new Thread (this); thethread. start () ;}} public void stop () {if (thethread! = Null) {thethread. stop (); thethread = null ;}}}
<Ignore_js_op>
<Ignore_js_op>
-
2. jpg (15.48 KB, downloads: 0)
Http://java.662p.com/thread-47-1-1.html