import java.awt.*; import java.applet.Applet;//pop out window public class tetris extends Applet implements Runnable { Dimension d; Font largefont = new Font("Helvetica", Font.BOLD, 17); Font smallfont = new Font("Helvetica", Font.BOLD, 12); // size and font for title and score FontMetrics fmsmall, fmlarge; Graphics grafix; Image ii; Thread thethread; boolean startgame=false; final short xblocks=15;//number of blocks we have horizontally final short yblocks=30; //number of blocks we have vertically final int blocksize=20;//size of the blocks final int width=xblocks*blocksize;//width final int height=yblocks*blocksize;//height short[][] screendata; final short maxcolors=1;//1 colors for block Color[] blocks; final int blockwidth=8; //final Color barcolor=new Color(128,255,64); final Color background=new Color(138,146,133);//color for backgroud int score; short emptyline; int objectx, objecty; int horizontalrotation; short objecttype; short objectcolor; int objectrotation; int objectrotationd=0; short objectptr; final short itemcount=7; final short itemrotlen=8; final short itemlen=itemrotlen*4; boolean fast=false; boolean showtitle=true; int items[]={ 0,0, -1,0, 0,-1, -1,-1, // cube, normal 0,0, -1,0, 0,1, -1,1, // rotated 90 degrees 0,0, 1,0, 0,1, 1,1, // rotated 180 degrees 0,0, 1,0, 0,-1, 1,-1, // rotated 270 degrees 0,0, 0,-1, 0,-2, 0,-3, // straight line 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, 1,1, 1,1 }; public void init() { short i; screendata=new short[xblocks][yblocks]; //the coordinates blocks=new Color[maxcolors+1]; // have two color blocks[0]=background; blocks[1]=new Color(196,99,201);//shape color Graphics g; resize(width+20,height+30); d = size(); setBackground(background); g=getGraphics(); g.setFont(smallfont); fmsmall = g.getFontMetrics(); g.setFont(largefont); fmlarge = g.getFontMetrics(); gameInit(); } public void gameInit() { score=0; newObject(); } public void newObject() { short i; int y; objectx=xblocks/2-1; horizontalrotation=0; objecty=0; objecttype=(short)(Math.random()*itemcount); if (objecttype>=itemcount) objecttype=itemcount-1; objectptr=(short)(objecttype*itemlen); objectcolor=1; objectrotation=0; for (i=0; i<4; i++) { y=items[objectptr+i*2+1]; if (y>=0 && screendata[objectx+items[objectptr+i*2]][y]!=0) { startgame=false; showtitle=true; } } } public boolean keyDown(Event e,int key) { if (startgame) { if (key == Event.LEFT) { horizontalrotation=-1; } else if (key == Event.RIGHT) { horizontalrotation=1; } else if (key == Event.UP) { objectrotationd=1; } else if (key == Event.DOWN) { fast=true; } else if (key == Event.ESCAPE) { startgame=false; } } else { if (key == ' ') { startgame=true; gameInit(); } } return true; } public boolean keyUp(Event e, int key) { if (key == Event.DOWN) { fast=false; } return true; } public void paint(Graphics g) { Graphics gg; if (grafix==null && d.width>0 && d.height>0) { ii = createImage(d.width, d.height); grafix = ii.getGraphics(); } if (grafix==null || ii==null) return; grafix.setColor(background);//dont screw with this grafix.fillRect(0, 0, d.width, d.height);//dont change anymore if (startgame) playGame(); else showIntro(); showScore(); g.drawImage(ii, 0, 0, this); } public void playGame() { boolean bottomreached=false; if (emptyline<0) { bottomreached=drawObject(); } else { scrollDown(); } drawBlocks(); if ( bottomreached) { checkFull(); } } public void showIntro() { String s; drawBlocks(); if (showtitle) { grafix.setFont(largefont); s="Tetris"; grafix.setColor(Color.white); grafix.drawString(s,blockwidth+(width-fmlarge.stringWidth(s)) / 2, height/2 - 20); grafix.setFont(largefont); s=" press space to start game"; grafix.setColor(new Color(96,128,255)); grafix.drawString(s,blockwidth+(width-fmsmall.stringWidth(s))/2 -100,height/2 - 60); } } public boolean drawObject() { short i; boolean bottomreached=false; int x,y,checkx,checky; 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)//print print print screendata[x][y]=0; if (screendata[checkx][checky]!=0) bottomreached=true; } if (!bottomreached) { if ( fast) { objecty++;//move down + change y } checkRotation(); horizontalrotation=0; objectrotationd=0; } // draw new 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) { newObject(); } return bottomreached; } public void checkRotation() { int dummyx; int dummyrot; int x,y; short i; boolean cando=true; dummyrot=(objectrotation+objectrotationd)%4; dummyx=objectx+horizontalrotation; // make sure the part doesn't rotate of the playscreen 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=0; y--) { found=true; for (x=0; x0; y--) { for (x=0; x