/* * TetriX.java * * Created on 4 février 2002, 14:49 */ package TetriX; import java.awt.*; /** * @author Olivier Zitvogel - http://www.zitvogel.com - All rights reserved */ public class TetriX extends java.applet.Applet { //each block is a 16 bit short where each 4 bits represents a 4 cols row private short[] blocks = {0x0660,0x4460,0x2260,0x04E0,0x4444,0x4620,0x2640,(short)(Math.random()*100)}; //18 rows of 10 cols, we will use only the 10 first bits of the 16 bit short private short[] board = new short[21]; //the current block private short current=blocks[1]; //the next block private short next; //the current block position (upper left corner) private byte XOffset=6; private byte YOffset=0; private long sleepms = 500; private Thread runner; //double buff private Image offImg; private Graphics offG; private Graphics screenG; //ccw rotation (newX = oldY, newY = width-1-oldX, on a short where each 4 bits sequence represents a row, the position is calculated like this X=i%4 and Y=i/4) private short rotate(short block) { short newBlock = 0; for(int i=15;i>=0;i--) newBlock = (short)(newBlock|(block>>>i&0x0001)<<(((3-i%4)<<2)+(i>>>2))); return newBlock; } /* moves * 0 = down, 1 = right, 2 = left , 3 rotate returns false when the piece cannot move anymore or rotate*/ private boolean move(int direction) { byte newX=XOffset, newY=YOffset; short block = current; switch(direction) { case 0: newY++; break; case 1: newX--; break; case 2: newX++; break; case 3: block = rotate(current); break; } deleteCurrentBlock(); if(fits(newX,newY,block)) { YOffset = newY; XOffset = newX; current = block; insertCurrentBlock(); return true; } else { insertCurrentBlock(); return false; } } private void insertCurrentBlock() { board[YOffset+3] |= (short)((current&0x000F)<>>4&0x000F)<>>8&0x000F)<>>12&0x000F)<>>4&0x000F)<>>8&0x000F)<>>12&0x000F)<>>4&0x000F)<>>8&0x000F)<>>12&0x000F)<=1;) { if(board[i]==(short)0xFFFF) { count++; for(int j=i;j>=1;j--) board[j]=board[j-1]; } else i--; } //System.out.println(count +" line(s)"); render(); } private void render() { Dimension d = getSize(); offG.setColor(getBackground()); offG.fillRect(0,0,d.width,d.height); offG.setColor(getForeground()); if(runner==null) { offG.setColor(Color.red); offG.fillRect(0,0,150,50); offG.setColor(Color.white); offG.drawString("PERDU GLANDU!!!",10,20); } else { for(int y=0;y<18;y++) for(int x=3;x<13;x++) if((board[y]<