Make your own free website on Tripod.com

This Java code is converted into HTML using JAVA_Colorizator v1.0 (c) 2001 by Alexander Yanuar Koentjara (lexzeus@hotmail.com)
/* Created by : Alexander Yanuar KOENTJARA - LexZEUS, 21-Jun-2000 E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com Source : lexTetris.java Class : lexTetris.class To work properly, this class needs tetrisContainer.class and block.class in its folder/directory. As a open source program, you can use my "lexTetris v1.0" in your web site, modify, or distribute it as your wish, but you must include my name as the author in the source code. Also, I make the code with a nice indents, so even a beginner can learn to know how my code works. You know, it's a bad habbit for some of programmers to be so selfish to write their codes within very awful indents, it won't help other people to debug or modify if there's something error inside, unless their code is for private used. Any comment or question, just contact me via my e-mail ... */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; public class lexTetris extends Applet implements Runnable, KeyListener { private Font dummyFont; private int wd,hg,TH_ctr,level; private boolean TH_end,gameOver,restartGame; private tetrisContainer myTC; private block myBlock; Thread myTH; Image myBuff; int isSpin, isMove, score; public void init() { dummyFont = new Font("Halvetica", Font.BOLD, 10 ); gameOver=false; restartGame=false; level=0; score=0; wd=10; hg=20; TH_ctr=0; TH_end=false; isSpin=0; isMove=0; addKeyListener(this); myBuff = createImage(wd*15+50,hg*15+100); myBlock = new block(Math.round(wd/2-2),0,hg,wd); myTC = new tetrisContainer(wd,hg,myBlock,this); myTH = new Thread(this); myTH.start(); } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_R) restartGame=true; if (e.getKeyCode()==KeyEvent.VK_DOWN) isMove=5; if (e.getKeyCode()==KeyEvent.VK_RIGHT) isMove=1; if (e.getKeyCode()==KeyEvent.VK_LEFT) isMove=-1; if (e.getKeyCode()==KeyEvent.VK_SPACE) isMove=10; if (e.getKeyCode()==KeyEvent.VK_X) isSpin=1; if (e.getKeyCode()==KeyEvent.VK_Z) isSpin=-1; } void setGameOver(boolean x) { gameOver = x; } public void run() { for(;;) { TH_ctr++; if (restartGame) { gameOver=false; restartGame=false; TH_ctr=0; score=0; level=0; isSpin=0; isMove=0; myTC.clearAll(); myBlock.reNew(); repaint(); } level=(int) score/1000; if (level>7) level=7; if (TH_end) return; if ( ( (gameOver) && (TH_ctr%20==0) ) // if gameover, don't repaint too often ||( (TH_ctr%500<500) && (TH_ctr%500>100) ) ) repaint(); if (!gameOver) { if (isMove!=0) { myTC.moveBlock(isMove); repaint(); isMove=0; } if (isSpin==1) { myBlock.spin(true); repaint(); isSpin=0;} if (isSpin==-1) { myBlock.spin(false); repaint(); isSpin=0;} if (TH_ctr%(50-level*5)==0) { myTC.down(false); repaint(); } } if (TH_ctr>10000) TH_ctr=0; try { myTH.sleep(25); } catch (Exception e) { } } } public void start() { setBackground(Color.white); } public void stop() { } public void destroy() { } public void update(Graphics scGR) { paint(scGR); } public String getAppletInfo() { String t = " ************************************* \n" + " \n" + " LexZEUS Tetris Ver 1.0 \n" + " \n" + " also known as \n" + " \n" + " lexTetris Ver 1.0 \n" + " \n" + " ------------------------------------- \n" + " \n" + " (C) by Alexander Yanuar Koentjara \n" + " by LexZEUS, 2000 \n" + " \n" + " Term 'LexZEUS' refers to my alias \n" + " name which is also used in every \n" + " orher applications I made. \n" + " \n" + " ************************************* \n"; return t; } void LexZEUS(Graphics Gr) { int x=(TH_ctr % 500)-50; if (x>100) x=100; Gr.setColor(new Color(250, 250-(5*(x-50)), 250-(5*(x-50))) ); Gr.setFont(new Font("Halvetica", Font.BOLD, (int) Math.round((x-50)/2) ) ); Gr.drawString("LexZEUS",((TH_ctr%500)-100),50); } void addScore(int adder) { score+=adder; } public void paint(Graphics scGR) { Graphics Buff = myBuff.getGraphics(); Buff.setColor(Color.white); Buff.fillRect(0,0,wd*15+50,hg*15+100); if ((TH_ctr%500<500)&&(TH_ctr%500>100)) LexZEUS(Buff); myTC.reRect(Buff); myTC.reFill(Buff); Buff.setFont(dummyFont); Buff.setColor(Color.red); Buff.drawString("(C) LexZEUS, 21/Jun/2000", 1, hg*16 + 10); Buff.drawString("(C) Alexander Yanuar Koentjara", 1, hg*16 + 25); Buff.setColor(Color.black); if (!gameOver) { Buff.drawString("Level : " + (level + 1), 1, hg*16 + 45); Buff.drawString("Score : " + score, 1, hg*16 + 60); } else { Buff.drawString("GAME OVER !!!", 1, hg*16 + 45); Buff.drawString("Press 'R' to restart.", 1, hg*16 + 60); } scGR.drawImage(myBuff,25,25,this); } } /* Created by : Alexander Yanuar KOENTJARA - LexZEUS, 21-Jun-2000 E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com Source : block.java Class : block.class */ class block { static int template[][][] = { { {0, 0, 1, 0 }, {0, 0, 1, 0 }, {0, 0, 1, 0 }, {0, 0, 1, 0 } }, { {1, 1, 0, 0 }, {1, 0, 0, 0 }, {1, 0, 0, 0 }, {0, 0, 0, 0 } }, { {1, 1, 0, 0 }, {0, 1, 0, 0 }, {0, 1, 0, 0 }, {0, 0, 0, 0 } }, { {0, 1, 0, 0 }, {1, 1, 0, 0 }, {1, 0, 0, 0 }, {0, 0, 0, 0 } }, { {1, 0, 0, 0 }, {1, 1, 0, 0 }, {0, 1, 0, 0 }, {0, 0, 0, 0 } }, { {1, 1, 0, 0 }, {1, 1, 0, 0 }, {0, 0, 0, 0 }, {0, 0, 0, 0 } } }; private int posX,posY,which,startX,startY; private int col; private int body[][] = new int[4][4]; private int temp[][] = new int[4][4]; block(int sx,int sy) { new java.util.Random(); which=0; col=0; startX=sx; startY=sy; reNew(); } private void setTemplate() { for (int y=0;y<4;y++) for (int x=0;x<4;x++) body[x][y]=template[which][y][x]; } void setColor() { col++; if (col>4) this.col=1; } int getColor() { return col; } int getValue(int x, int y) { return body[x][y]; } void gotoXY(int x,int y) { posX = x; posY = y; } int getY() { return posY; } int getX() { return posX; } void setY(int y) { posY = y; } void setX(int x) { posX = x; } void spin(boolean toRight) { if (which==5) return; int x,y; int max=3; if (which==0) max=4; if (toRight) for (y=0;y<max;y++) for (x=0;x<max;x++) temp[max-1-y][x]=body[x][y]; else for (y=0;y<max;y++) for (x=0;x<max;x++) temp[y][max-1-x]=body[x][y]; boolean bounch=false; for (y=0;y<4;y++) for (x=0;x<4;x++) if (!bounch && ((posX+x<0)|| (posX+3>width)|| (posY+3>height)) && (temp[x][y]>0) ) bounch=true; if (!bounch) for (y=0;y<max;y++) for (x=0;x<max;x++) body[x][y]=temp[x][y]; } void reNew() { int x=(int) (Math.random()*110); if (x<25) which = 3; else if (x<50) which = 4; else if (x<70) which = 1; else if (x<90) which = 2; else if (x<100) which = 5; else which = 0; col++; setTemplate(); gotoXY(startX,startY); if (col>4) col=1; } } /* Created by : Alexander Yanuar KOENTJARA - LexZEUS, 21-Jun-2000 E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com Source : tetrisContainer.java Class : tetrisContainer.class */ import java.awt.Graphics; import java.awt.Color; class tetrisContainer { private lexTetris LexT; private int tile[][]; private int width, height; private block BLK; private Color myCol[] = { Color.green, Color.red, Color.blue, Color.green, Color.magenta }; tetrisContainer(int wd,int hg,block bl2,lexTetris lt) { tile = new int[wd][hg]; width=wd; height=hg; clearAll(); BLK = bl2; LexT = lt; } void clearAll() { // clear all logical gridelines (tile) for (int y=0;y<height;y++) for (int x=0;x<width;x++) tile[x][y]=0; } void reRect(Graphics gr) { // to draw gridlines gr.setColor(Color.lightGray); for (int i=0;i<=height;i++) gr.drawLine(0,15*i,width*15,15*i); for (int i=0;i<=width;i++) gr.drawLine(15*i,0,15*i,height*15) ; } private void littleSquare(Graphics gr,int x,int y) { // to draw little square gr.fillRect(x*15+1,y*15+1,15-1,15-1); } private void drawMainBlock(Graphics gr) { // to draw the movable block int i,j; int x = BLK.getX(), y = BLK.getY(); gr.setColor(myCol[BLK.getColor()]); for (j=0;j<4;j++) for (i=0;i<4;i++) if (BLK.getValue(i,j)>0) littleSquare(gr,x+i,y+j); } private boolean bounch(int theX,int theY) { // will the block bounch ?? boolean flag=false; for (int y=0;y<4;y++) for (int x=0;x<4;x++) if ( ( (!flag) && (BLK.getValue(x,y)>0) ) && ( (theX+x>=width) || (theX+x<0) || (theY+y>=height) || (theY+y<0) || (tile[x+theX][y+theY]>0) ) ) flag=true; return flag; } void moveBlock(int dir) { // to move the movable block // move block to -1 : left // 1 : right // 5 : down // 10 : fall down if (dir<10) { if (dir==1) { if (!bounch(BLK.getX()+1,BLK.getY())) BLK.setX(BLK.getX()+1); } else if (dir==-1) { if (!bounch(BLK.getX()-1,BLK.getY())) BLK.setX(BLK.getX()-1); } else down(false); } else down(true); } private void checkBlock() { // see if there's a full sequenced blocks int sum = 0, x, y = height-1, i, j, ctr; boolean thisdone; while(y>=0) { ctr=0; thisdone=true; for(x=0;x<width;x++) if(tile[x][y]>0) ctr++; if (ctr==width) { thisdone=false; sum++; for(j=y;j>0;j--) for(i=0;i<width;i++) tile[i][j]=tile[i][j-1]; } if (thisdone) y--; } LexT.addScore(50*sum); } private void markContainer() { // put dead block to logical gridline (tile) for(int y=0;y<4;y++) for(int x=0;x<4;x++) if (BLK.getValue(x,y)!=0) tile[BLK.getX()+x][BLK.getY()+y]=BLK.getColor(); LexT.addScore(5); checkBlock(); } private boolean checkGameOver() { // is game over ? boolean t=false; for (int y=0;y<4;y++) for (int x=0;x<4;x++) if ((!t)&&(BLK.getValue(x,y)>0)) if (tile[BLK.getX()+x][BLK.getY()+y]>0) t=true; return t; } void down(boolean fall) { // move down 1 block or fall to the bottom if (!fall) { if (!bounch(BLK.getX(),BLK.getY()+1)) BLK.setY(BLK.getY()+1); else { markContainer(); BLK.reNew(); if (checkGameOver()) LexT.setGameOver(true); } } else { while (!bounch(BLK.getX(),BLK.getY()+1)) BLK.setY(BLK.getY()+1); markContainer(); BLK.reNew(); if (checkGameOver()) LexT.setGameOver(true); } } private void drawAnotherBlock(Graphics gr) { // construct the dead blocks for (int y=0; y<height; y++) for (int x=0; x<width; x++) if (tile[x][y]>0) { gr.setColor(myCol[tile[x][y]]); littleSquare(gr,x,y); } } void reFill(Graphics gr) { // show all dead blocks and movable blocks drawMainBlock(gr); drawAnotherBlock(gr); } }