import java.applet.*; import java.awt.*; public class Tetris extends java.applet.Applet implements Runnable{ int height,width; long score=0; long Delay=700; long OldDelay=700; long update; int s_ofsx,s_ofsy,s_width,s_height; int b_ofsx,b_ofsy; int b_size; int b_rot,b_width,b_height,b_color,b_absx,b_absy; boolean b_occupied,b_stop; int p_next,p_current,p_x,p_y,p_rot; int[][] blocks; Thread go = null; //---------------------- init() ---------------------------- public void init(){ int i,j; Dimension d = size(); height=d.height; width=d.width; Delay=700; // Blocks info update=255; b_size=(int)width/12; if( height/25 < b_size)b_size=(int)height/25; b_width=10; b_height=22; b_ofsx=1; b_ofsy=1; blocks=new int[b_width+2][b_height+2]; for(i=1;i<=b_width;i++) for(j=1;j<=b_height;j++)blocks[i][j]=64; // Score info s_ofsx=b_size*(b_width+3); s_ofsy=4*b_size; s_width=width-s_ofsx+20; s_height=b_size; score=0; // Pieces info p_next=Randomize(7); p_current=Randomize(7); p_x=5;p_y=3; p_rot=0; repaint(); } // ------------------- start() ----------------- public void start() { if(go == null) { go = new Thread(this); go.start(); } } // ---------------------- stop() ------------------- public void stop(){ go=null; } // --------------------- run() ---------------------- public void run(){ while(go != null){ try{ Thread.sleep(Delay);} catch(InterruptedException e){} Descent(); } go=null; } public void Update_Score(Graphics g) { g.setColor(Color.darkGray); g.clearRect(s_ofsx,s_ofsy-s_height,s_width,s_height); g.drawString("Your score : "+score,s_ofsx,s_ofsy); } public boolean keyDown(Event e, int y){ // System.out.println("key pressed : "+y+" ("+(char)y+")"); v_color_piece(0); // left if(((y==1000)||(y==55)||(y=='q'))&&(move_piece(p_x-1,p_y,p_rot))){ clear_Piece(); p_x--; v_color_piece(1); update=65; }else // right if(((y==1002)||(y==57)||(y=='e'))&&(move_piece(p_x+1,p_y,p_rot))){ clear_Piece(); p_x++; v_color_piece(1); update=65; }else // turn if(((y==1004)||(y==56)||(y=='w'))&&(move_piece(p_x,p_y,(p_rot+1)&3))){ clear_Piece(); p_rot=(p_rot+1)&3; v_color_piece(1); update=65; }else if((y=='r')||(y=='R')){init();start();update=255;}else if((y=='f')||(y=='F')){Delay=300;}else if((y=='s')||(y=='S')){Delay=700;}else if(y==' '){if(Delay!=50){OldDelay=Delay;Delay=50;}}else{v_color_piece(1);} repaint(); return false; } public void paint(Graphics g){ int i,j; // if((update & 2) == 0){ // g.drawRect(0,0,height-1,width-1); // } g.drawRect(b_ofsx+b_size,b_ofsy+b_size,b_width*b_size,b_height*b_size); for(i=1;i<=b_width;i++) for(j=1;j<=b_height;j++)PaintBlock(g,i,j); update=255; Update_Score(g); } // ------------------------ update() ------------------------ public void update(Graphics g){ int i,j; // if((update & 2) == 0){ // g.drawRect(0,0,height-1,width-1); // } // g.drawRect(b_ofsx+b_size,b_ofsy+b_size,b_width*b_size,b_height*b_size); for(i=1;i<=b_width;i++) for(j=1;j<=b_height;j++)PaintBlock(g,i,j); update=255; Update_Score(g); } public boolean move_piece(int x,int y,int rot) { b_color=32; b_absx=x; b_absy=y; b_rot=rot; b_occupied=false; PutPiece(p_current); return(!b_occupied); } public void v_color_piece(int color){ b_color=color; b_absx=p_x; b_absy=p_y; b_rot=p_rot; PutPiece(p_current); } public boolean is_line(int j){ int i; boolean a=(blocks[1][j] & 63)!=0; for(i=2;i<=b_width;i++)a=a && ((blocks[i][j] & 63)!=0); return(a); } public void cancel_line(int j){ int i,k; // System.out.println("c"+j); for(i=1;i<=b_width;i++) for(k=j;k>1;k--){blocks[i][k]=blocks[i][k-1];if(blocks[i][k]==0)blocks[i][k]=64;} for(i=1;i<=b_width;i++)blocks[i][1]=64; update=255; score+=80; } public void cancel_lines(){ int i; for(i=b_height;i>=1;){if(is_line(i)){cancel_line(i);}else{ i--;} }; update=255; repaint(); } public void clear_Piece() { v_color_piece(64); update=255; } // -------------------- Descent() ---------------------------------- public void Descent() { v_color_piece(0); if(move_piece(p_x,p_y+1,p_rot)){ clear_Piece(); // Putting new piece p_y++; v_color_piece(1); update=255; repaint(); }else{ // (get a new piece) v_color_piece(1); score+=6; cancel_lines(); // reset Delay Delay=OldDelay; p_x=b_width/2; p_y=3; p_rot=Randomize(4); p_current=Randomize(7); move_piece(p_x,p_y,p_rot); if(b_occupied){ // Game over go=null; } v_color_piece(1); update=255; repaint(); } } private int Randomize( int range ) { double rawResult; rawResult = Math.random(); return (int) (rawResult * range); } void PaintBlock(Graphics g,int x,int y){ int color=blocks[x][y]; if((color & update)!=0){ if((color&64) ==0){ g.setColor(Color.blue); g.drawRect(b_ofsx+x*b_size+1,b_ofsy+y*b_size+1,b_size-2,b_size-2); g.setColor(Color.red); g.fillRect(b_ofsx+x*b_size+2,b_ofsy+y*b_size+2,b_size-4,b_size-4); }else{ g.clearRect(b_ofsx+x*b_size+1,b_ofsy+y*b_size+1,b_size-1,b_size-1); //g.setColor(Color.darkGray); //g.fillOval(b_ofsx+x*b_size+b_size/2,b_ofsy+y*b_size+b_size/2,1,1); blocks[x][y]=0; } } } public void PutBlock(int relx,int rely){ int x,y; if((b_rot & 1)==0){ x=b_absx+(1-(b_rot & 2))*relx; y=b_absy+(1-(b_rot & 2))*rely; }else { x=b_absx+((b_rot & 2)-1)*rely; y=b_absy+(1-(b_rot & 2))*relx; } if((b_color & 32)==0){ if((x>0)&&(y>0)&&(x<=b_width)&&(y<=b_height))blocks[x][y]=b_color;} else{ if((x>0)&&(y>0)&&(x<=b_width)&&(y<=b_height))b_occupied=b_occupied || ((blocks[x][y] & 63) !=0); else b_occupied=true; } } public void PutPiece(int type) { if(type==0){ PutBlock(0,0); PutBlock(0,-1); PutBlock(1,-1); PutBlock(-1,-1); } if(type==1){ PutBlock(0,0); PutBlock(0,1); PutBlock(1,0); PutBlock(1,-1); } if(type==2){ PutBlock(0,0); PutBlock(1,0); PutBlock(0,-1); PutBlock(1,1); } if(type==3){ PutBlock(0,0); PutBlock(0,1); PutBlock(0,-1); PutBlock(0,-2); } if(type==4){ PutBlock(0,0); PutBlock(1,0); PutBlock(0,-1); PutBlock(1,-1); } if(type==5){ PutBlock(0,0); PutBlock(0,1); PutBlock(0,-1); PutBlock(-1,-1); } if(type==6){ PutBlock(0,0); PutBlock(0,1); PutBlock(0,-1); PutBlock(1,-1); } if(type==7){ PutBlock(1,1); PutBlock(-1,1); PutBlock(-1,-1); PutBlock(1,-1); } } }