Tetris6.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Tetris6 extends Applet implements Runnable, KeyListener {
    private int x,y;
    private int startXX=50,startYY=20;
    private int LOWSPEED=1000,HIGHSPEED=100;
    private int SPEED=LOWSPEED;
    private int block=10;
    private int width=100,height=200;
    private int margin=10;
    private int xx,yy;
    private int pattern,turn;
    private int ran=0;
    
    private boolean[][] Status;
    private int[][] colorStatus;
    
    private Graphics offG;
    private Image img;
    private Thread th;
    
    public void init() {
        setSize(200,270);
        setSize(200,270);
        setLayout(null);
        img = createImage(width,height);
        offG = img.getGraphics();
        
        addKeyListener(this);
        requestFocus();
        
        x=width/block;
        y=height/block;
        
        Status = new boolean[x][y+1];
        colorStatus = new int[x][y];
        
        for(int i=0;i<x;i++) {
            for(int j=0;j<y;j++) {
                Status[i][j] = true;
            }
        }
        
        xx=startXX;
    }
    
    public void start() {
        if(th==null) {
            th = new Thread(this);
            th.start();
        }
    }
       
    public void paint(Graphics G) {
        offG.clearRect(0,0,width,height);
        offG.setColor(Color.gray);
        offG.fillRect(0,0,width,height);
        
        Block.polygon(xx,yy,block,offG,pattern,ran);
        
        for(int i=0;i<x;i++) {
            for(int j=0;j<y;j++) {
                if(Status[i][j]==false) {
                    offG.setColor(Clr.clr[colorStatus[i][j]]);
                    offG.fillRect(i*block,j*block,block,block);
                    offG.setColor(Color.black);
                    offG.drawRect(i*block,j*block,block,block);
                }
            }
        }
        
        G.drawImage(img,margin,margin,this);
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    
    public void run() {
        while(th == Thread.currentThread()) {
            try {
                Thread.sleep(SPEED);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
            yy += block;
            
            if(Seigyo.downCheck(xx,yy,block,Status,colorStatus,pattern,ran)) {
                yy=startYY;
                ran = (int)(Math.random()*10)%2;
            }
        }
    }
    
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT) {
            if(Seigyo.leftCheck(xx,yy,width,block,Status,pattern,ran)) {
                xx-=block;
            }
            repaint();
        }
        
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if(Seigyo.rightCheck(xx,yy,width,block,Status,pattern,ran)) {
                xx+=block;
            }
            repaint();
        }
        
        if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            SPEED = HIGHSPEED;
        }
    }
    
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            SPEED = LOWSPEED;
        }
        
        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            turn++;
            pattern = turn%4;
            repaint();
        }
    }
    
    public void keyTyped(KeyEvent e) {
    }
}
		

Clr.java
import java.awt.*;

public class Clr {
    	static Color[] clr = {Color.red, Color.yellow, Color.orange, Color.pink, Color.magenta, Color.blue, Color.green, Color.black};
}
		

Block.java
import java.awt.*;

public class Block {
    public static void polygon(int xx,int yy,int block,Graphics offG,int pattern,int ran) {
        switch(ran) {
            case 0:
                if(pattern==0) {
                    offG.setColor(Clr.clr[ran]);
                    offG.fillRect(xx,yy+block,block,block);
                    offG.fillRect(xx,yy,block,block);
                    offG.fillRect(xx,yy-block,block,block);
                    offG.fillRect(xx,yy-block*2,block,block);
                    
                    offG.setColor(Clr.clr[7]);
                    offG.drawRect(xx,yy+block,block,block);
                    offG.drawRect(xx,yy,block,block);
                    offG.drawRect(xx,yy-block,block,block);
                    offG.drawRect(xx,yy-block*2,block,block);
                }
                
                if(pattern==1) {
                    offG.setColor(Clr.clr[ran]);
                    offG.fillRect(xx-block,yy,block,block);
                    offG.fillRect(xx,yy,block,block);
                    offG.fillRect(xx+block,yy,block,block);
                    offG.fillRect(xx+block*2,yy,block,block);
                    
                    offG.setColor(Clr.clr[7]);
                    offG.drawRect(xx-block,yy,block,block);
                    offG.drawRect(xx,yy,block,block);
                    offG.drawRect(xx+block,yy,block,block);
                    offG.drawRect(xx+block*2,yy,block,block);
                }
                
                if(pattern==2) {
                    offG.setColor(Clr.clr[ran]);
                    offG.fillRect(xx,yy-block,block,block);
                    offG.fillRect(xx,yy,block,block);
                    offG.fillRect(xx,yy+block,block,block);
                    offG.fillRect(xx,yy+block*2,block,block);
                    
                    offG.setColor(Clr.clr[7]);
                    offG.drawRect(xx,yy-block,block,block);
                    offG.drawRect(xx,yy,block,block);
                    offG.drawRect(xx,yy+block,block,block);
                    offG.drawRect(xx,yy+block*2,block,block);
                }
                
                if(pattern==3) {
                    offG.setColor(Clr.clr[ran]);
                    offG.fillRect(xx+block,yy,block,block);
                    offG.fillRect(xx,yy,block,block);
                    offG.fillRect(xx-block,yy,block,block);
                    offG.fillRect(xx-block*2,yy,block,block);
                    
                    offG.setColor(Clr.clr[7]);
                    offG.drawRect(xx+block,yy,block,block);
                    offG.drawRect(xx,yy,block,block);
                    offG.drawRect(xx-block,yy,block,block);
                    offG.drawRect(xx-block*2,yy,block,block);
                }
                break;
                
            case 1:
                offG.setColor(Clr.clr[ran]);
                offG.fillRect(xx,yy,block,block);
                offG.fillRect(xx+block,yy,block,block);
                offG.fillRect(xx,yy-block,block,block);
                offG.fillRect(xx+block,yy-block,block,block);
                
                offG.setColor(Clr.clr[7]);
                offG.drawRect(xx,yy,block,block);
                offG.drawRect(xx+block,yy,block,block);
                offG.drawRect(xx,yy-block,block,block);
                offG.drawRect(xx+block,yy-block,block,block);
                break;
        }
    }
}
		

Seigoy.java
public class Seigyo {
    private static boolean bool;
    
    public static boolean downCheck(int xx,int yy,int block,boolean[][] Status,int[][] colorStatus,int pattern,int ran) {
        
        bool=false;
        
        switch(ran) {
            case 0:
                if(pattern==0) {
                    if(Status[xx/block][yy/block+2]==false) {
                        Status[xx/block][yy/block+1]=false;
                        Status[xx/block][yy/block]=false;
                        Status[xx/block][yy/block-1]=false;
                        Status[xx/block][yy/block-2]=false;
                        bool=true;
                    }
                }
                
                if(pattern==1) {
                    if(!Status[xx/block+2][yy/block+1] || !Status[xx/block+1][yy/block+1] || !Status[xx/block][yy/block+1] || !Status[xx/block-1][yy/block+1]) {
                        Status[xx/block+2][yy/block]=false;
                        Status[xx/block+1][yy/block]=false;
                        Status[xx/block][yy/block]=false;
                        Status[xx/block-1][yy/block]=false;
                        bool=true;
                    }
                }
                
                if(pattern==2) {
                    if(Status[xx/block][yy/block+3]==false) {
                        Status[xx/block][yy/block-1]=false;
                        Status[xx/block][yy/block]=false;
                        Status[xx/block][yy/block+1]=false;
                        Status[xx/block][yy/block+2]=false;
                        bool=true;
                    }
                }
                
                if(pattern==3) {
                    if(!Status[xx/block-2][yy/block+1] || !Status[xx/block-1][yy/block+1] || !Status[xx/block][yy/block+1] || !Status[xx/block+1][yy/block+1]) {
                        Status[xx/block-2][yy/block]=false;
                        Status[xx/block-1][yy/block]=false;
                        Status[xx/block][yy/block]=false;
                        Status[xx/block+1][yy/block]=false;
                        bool=true;
                    }
                }
                break;
                
            case 1:
                if(!Status[xx/block][yy/block+1] || !Status[xx/block+1][yy/block+1]) {
                    Status[xx/block][yy/block]=false;
                    Status[xx/block+1][yy/block]=false;
                    Status[xx/block][yy/block-1]=false;
                    Status[xx/block+1][yy/block-1]=false;
                    
                    colorStatus[xx/block][yy/block]=ran;
                    colorStatus[xx/block+1][yy/block]=ran;
                    colorStatus[xx/block][yy/block-1]=ran;
                    colorStatus[xx/block+1][yy/block-1]=ran;
                    bool=true;
                }
                break;
        }
        
        return bool;
    }
    
    public static boolean leftCheck(int xx,int yy,int width,int block,boolean[][] Status,int pattern,int ran) {
        
        bool=false;
        
        switch(ran) {
            case 0:
                if(pattern==0) {
                    if(xx>=block && Status[xx/block-1][yy/block+1] && Status[xx/block-1][yy/block] && Status[xx/block-1][yy/block-1] && Status[xx/block-1][yy/block-2]) {
                        bool=true;
                    }
                }
                
                if(pattern==1) {
                    if(xx>=block*2 && Status[xx/block-2][yy/block]) {
                        bool=true;
                    }
                }
                
                if(pattern==2) {
                    if(xx>=block && Status[xx/block-1][yy/block-1] && Status[xx/block-1][yy/block] && Status[xx/block-1][yy/block+1] && Status[xx/block-1][yy/block+2]) {
                        bool=true;
                    }
                }
                
                if(pattern==3) {
                    if(xx>=block*3 && Status[xx/block-3][yy/block]) {
                        bool=true;
                    }
                }
                break;
            
            case 1:
                if(Status[xx/block-1][yy/block] && Status[xx/block-1][yy/block-1]) {
                    bool=true;
                }
                break;
        }
        
        return bool;
    }
    
    public static boolean rightCheck(int xx,int yy,int width,int block,boolean[][] Status,int pattern,int ran) {
        bool=false;
        
        switch(ran) {
            case 0:
                if(pattern==0) {
                    if(xx<width-block && Status[xx/block+1][yy/block+1] && Status[xx/block+1][yy/block] && Status[xx/block+1][yy/block-1] && Status[xx/block+1][yy/block-2]) {
                        bool=true;
                    }
                }
                
                if(pattern==1) {
                    if(xx<width-block*3 && Status[xx/block+3][yy/block]) {
                        bool=true;
                    }
                }
                
                if(pattern==2) {
                    if(xx<width-block && Status[xx/block+1][yy/block-1] && Status[xx/block+1][yy/block] && Status[xx/block+1][yy/block+1] && Status[xx/block+1][yy/block+2]) {
                        bool=true;
                    }
                }
                
                if(pattern==3) {
                    if(xx<width-block*2 && Status[xx/block+2][yy/block]) {
                        bool=true;
                    }
                }
                break;
                
            case 1:
                if(Status[xx/block+2][yy/block] && Status[xx/block+2][yy/block-1]) {
                    bool=true;
                }
                break;
        }
        return bool;
    }
}

		

top

6918