Java language-Tetris Java source case

Source: Internet
Author: User

Only provide reference source code

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 xblocks=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,0,0); int Score;short emptyline;int objectx, objecty;int objectx;short objecttype;short objectcolor;int objectrotation;int 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,0, -1,0, 0,-1, -1,-1,//Four square,  Normal state 0,0, -1,0, 0, 1, -1,1,//rotated 90 degrees 0,0, 1, 0, 0, 1, 1,1,0,0, 1, 0, 0,-1, 1,-1,0,0, 0,-1, 0,-2, 0,-3,0,0, -1,0, -2,0, -3,0,0,0,  0,1, 0, 2, 0,3,0,0, 1, 0, 2, 0, 3,0,0,0, 1, 0, 0,-1, -1,-1,0,0, 0,-1, -1,0, -1,1,0,0, -1,0, 0, 1, 1,1,0,0, 0, 1, 1, 0, 1,-1,0,0, -1,0, 0,-1, 1,-1,0,0, 0, 1, -1,0, -1,-1,0,0, 1, 0, 0, 1, -1,1,0,0, 0,-1, 1, 0, 1,1,0,0, 1, 0, -1,0, 0,-1,0,0, 0, 1, 0,-1, -1,0, 0,0, 0, 1, -1,0, 1,0,0,0, 1, 0, 0,-1, 0,1,0,0, 0,-1, 1,-1, 0,1,0,0, -1,0, -1,-1, 1,0,0,0, -1,1, 0, 1, 0,-1,0,0, -1,0, 1, 0, 1, 1,0,0, 0,1, 0,-1, -1,-1,0,0, 1,0, -1,0, -1,1,0,0, 0,-1, 0,1, 1,1,0,0, -1,0, 1,0, 1,-1,};int checks[]={-1,1, 0, 1, -1,1, 0,1 , -1,2, 0, 2, -1,2, 0,2,0,2, 1, 2, 0, 2, 1,2,0,1, 1, 1, 0, 1, 1,1,0,1, 0, 1, 0, 1, 0,1,0,1, -1,1, -2,1, -3,1,0,4, 0, 4, 0, 4, 0,4,0,  1, 1, 1, 2, 1, 3,1,0,1, -1,0, 1, 1, 0,1,0,1, -1,2, 0, 1, -1,2,0,2, 1, 2, -1,2, 0,2,0,2, 1, 1, 0, 2, 1,1,-1,1, 0, 1, 1, 0, 1,0,-1,1, 0,2, 0, 2, -1,1,-1,2, 0, 2, 1,1, 1,1,0,1, 1, 2, 0, 1, 1,2,-1,1, 0, 1, 1, 1, 1,1,-1,1, 0, 2, 0, 2, -1,1,-1,1, 0, 2, 1, 1, 1,1,0,2, 1, 1, 0, 2, 1,1,0,2, 1, 0, 1, 0, 0 , 2,-1,1, 0, 1, 1, 1, 1,1,-1,2, 0, 2, 0, 2, -1,2,-1,1, 0, 1, 1, 2, 1,2,-1,0, 0, 2, 0, 2, -1,0,-1,2, 0, 1, 1, 1, 1,1,0,2, 1, 2, 1, 2, 0, 2,-1,1, 0,1, ublic, and,};p string Getappletinfo () {return ("blocks-by Ali");} Initialize appletpublic void init () {Short i;screendata=new short[xblocks][yblocks];blocks=new color[maxcolors+1];// Set the background color blocks[0]=background;//set the block color Blocks[1]=new color (255,0,0); Blocks[2]=new color (0,255,0); Blocks[3]=new Color ( 0,0,255); Blocks[4]=new color (255,255,0); Blocks[5]=new color (255,0,255); Blocks[6]=new color (0,255,255); Graphics g;resize (width+2*barwidth,height+30);d =size (); SetBackground (background); G=getgraphics (); G.setfont ( Smallfont); Fmsmall=g.getfontmetrics (); G.setfont (Largefont); Fmlarge=g.getfontmetrics (); GameInit ();} Initialize game 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;objectptr= (short) (Objecttype*itemlen); Checkptr= ( Short) (Mat.random () *maxcolors+1); if (objectcolor>maxcolors) objectcolor=maxcolors;objectrotation=0;count= maxcount;//determine if 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;}}} Determines whether the keyboard is pressed and handles 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;} Handle Keyboard Lift Event public Boolean keyUp (event e, int key) {if (Key==event.down) {fast=false;} return true;} Paint () method 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 PlayGame () method PlayGame (); else//Otherwise, display additional information//display initial information Showintro ();//Show Score Showscore (); G.drawimage (ii, 0, 0, this);} Implement game public void PlayGame () {boolean Bottomreached=false;boolean stillscrolling=false;if (emptyline<0) { Bottomreached=drawobject ();} Else{scrolldown (); stillscrolling=true;} Drawbars ();d rawblocks (); if (Stillscrolling| | bottomreached) {checkfull ();}} Displays the initial information public void Showintro () {String s;drawbars ();d rawblocks (), 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= "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 movement"; 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.White); Goff.drawstring (s,barwidth+ (Width-fmsmall.stringwidth (s))/2,height/2+10) s= "Use 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 () {I;boolean bottomreached=false;int x,y,checkx,checky;//clear Old graphics for (i=0;i<4;i++) {x= Objectx+items[objectptr+i*2+objectrotation*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 graphic 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 void CheckrOtation () {int dummyx;int dummyrot;int x,y;short i;boolean cando=true;dummyrot= (objectrotation+objectrotationd)%4; dummyx=objectx+objectdx;//confirm that the section 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,blocksize-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); starttime+=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, download number: 0)

Detailed description: http://java.662p.com/thread-47-1-1.html

Java language-Tetris Java source case

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.