http://iruril.cafe24.com/iruril/Tetris/Tetris_SM.html


~cpp 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Tetris extends Applet implements Runnable {
	
	Thread clock;
	Image mem;
	Graphics memG;

	Random r;
	
	boolean [][]board;
	Color [][]colorMap;
	Color []colorType;
	
	int blockType;
	int []blockX;
	int []blockY;
	int blockDirection;
	
	int delayTime;
	boolean runGame;

	public void init() {
		
		mem=createImage(181,316);
		memG = mem.getGraphics();
		memG.setColor(Color.white);
		memG.fillRect(0,0,192,192);
		
		board = new boolean[12][21];
		
		blockX = new int[4];
		blockY = new int[4];
		blockDirection = 0;
		
		r = new Random();
		blockType = Math.abs(r.nextInt() % 7);
		setBlock(blockType);
		
		drawBlock();
		drawMap();
		drawGrid();
		
		delayTime=1000;
		runGame=true;
		
		this.requestFocus();
		addKeyListener(new MyKeyHandler());
		
	}
	
	private void drawMap() {

		for(int i=0;i<12;i++) {
			for(int j=0;j<21;j++) {
				if(board[i][j]) {
					memG.setColor(Color.blue);
					memG.fillRect(i*15,j*15,15,15);
				}
				else {
					memG.setColor(Color.white);
					memG.fillRect(i*15,j*15,15,15);
				}
				
			}
		}

	}

	private void drawBlock() {

		for(int i=0;i<4;i++) {
			board[blockX[i]][blockY[i]] = true;
		}
	
	}

	private void drawGrid() {
		memG.setColor(new Color(190,190,190));
		
		for(int i=0;i<12;i++) {
			for(int j=0;j<21;j++) {
				memG.drawRect(i*15,j*15,15,15);
			}
		}
		
	}

	private void dropBlock() {
		removeBlock();

		if(checkDrop()) {
			
			for(int i=0;i<4;i++) {
	//			blockX[i]+=1;
				blockY[i]+=1;
			}
		}
		else {
			drawBlock();
			nextBlock();
		}
		
	}
	
	private void nextBlock() {
		blockType=Math.abs(r.nextInt() % 7);
		blockDirection=0;
		delLine();
		setBlock(blockType);
		
	}
	
	private void removeBlock() {
		for(int i=0;i<4;i++) {
			board[blockX[i]][blockY[i]] = false;
			
		}
		
	}
	
	public void delLine()
	{
		boolean delOk;
		
		for(int row=20; row>=0; row--) {
			delOk=true;
			for(int col=0;col<12;col++) {
				if(!board[col][row])
					delOk=false;
			}
			
			if(delOk) {
				for(int delRow=row;delRow>0;delRow--) {
					for(int delCol=0;delCol<12;delCol++) {
						board[delCol][delRow] = board[delCol][delRow-1];
					}
				}
				for(int i=0;i<12;i++) {
					board[0][i]=false;
				}
				row++;
			}
		}
	}

	private boolean checkDrop() {
		boolean dropOk=true;
		
		for(int i=0;i<4;i++) {
			if( (blockY[i]+1)!=21) {
				if(board[blockX[i]] [blockY[i]+1] )
					dropOk = false;
			}
			else {
				dropOk=false;
			}
		}
		return dropOk;
	}
	
	
	private void setBlock(int bType) {
		switch(bType) {
		case 0:
			blockX[0]=5; blockY[0]=0;
			blockX[1]=5; blockY[1]=1;
			blockX[2]=5; blockY[2]=2;
			blockX[3]=6; blockY[3]=2;
			break;
		case 1:
			blockX[0]=5; blockY[0]=0;
			blockX[1]=5; blockY[1]=1;
			blockX[2]=6; blockY[2]=0;
			blockX[3]=6; blockY[3]=1;
			break;
		case 2:
			blockX[0]=6; blockY[0]=0;
			blockX[1]=5; blockY[1]=0;
			blockX[2]=5; blockY[2]=1;
			blockX[3]=5; blockY[3]=2;
			break;
		case 3:
			blockX[0]=5; blockY[0]=0;
			blockX[1]=5; blockY[1]=1;
			blockX[2]=4; blockY[2]=1;
			blockX[3]=6; blockY[3]=1;
			break;
		case 4:
			blockX[0]=5; blockY[0]=0;
			blockX[1]=5; blockY[1]=1;
			blockX[2]=5; blockY[2]=2;
			blockX[3]=5; blockY[3]=3;
			break;
		case 5:
			blockX[0]=4; blockY[0]=0;
			blockX[1]=5; blockY[1]=0;
			blockX[2]=5; blockY[2]=1;
			blockX[3]=6; blockY[3]=1;
			break;
		case 6:
			blockX[0]=6; blockY[0]=0;
			blockX[1]=5; blockY[1]=0;
			blockX[2]=5; blockY[2]=1;
			blockX[3]=4; blockY[3]=1;
			break;	

		}
		
		
	}

	public void start() {
		if(clock==null) {
			clock = new Thread(this); 
			clock.start(); // 쓰레드 시작
		}

		this.setSize(200,333);
	}
	
	public void stop() {
		if((clock!=null) && (clock.isAlive())) {
			clock=null; // 시계 정지
		}
	}
	
	public void paint(Graphics g) {
		g.drawImage(mem, 0, 0, this); //
	}
	
	public void update(Graphics g) {
		paint(g);
	}
	
	public void run() {
		while(true)
		{
			try {
				clock.sleep(delayTime);
			}catch(InterruptedException ie) { }
			
			dropBlock();
			
			if(runGame) {
				drawBlock();
				drawMap();
				drawGrid();
			} else {
				
			}
			
			repaint();
		}
		
	}
	public boolean checkTurn() {
		boolean turnOk = true;
		
		for(int i=0;i<4;i++) {
			if( blockX[i]>=0 && blockX[i]<12 && blockY[i]>=0 && blockY[i]<21 ) {
				if(board[blockX[i]][blockY[i]])
					turnOk=false;
			}
			else {
				turnOk= false;
			}
		}

		return turnOk;		
	}
	public boolean checkMove(int dir)
	{
		boolean moveOk=true;
		
		removeBlock();
		
		for(int i=0; i<4; i++) {
			if( (blockX[i]+dir)>=0 && (blockX[i]+dir)<12 ) {
				if( board[blockX[i]+dir][ blockY[i]])
					moveOk = false;
			}
			else moveOk=false;
		}
		
		if(!moveOk) drawBlock();

		return moveOk;		
	}
	public void turnBlock()
	{
		switch(blockType) {
			case 1:
				switch(blockDirection) {
					case 0:
						blockX[3]-=1; blockY[3]-=1;
						break;
					case 1:
						blockX[1]+=1; blockY[1]-=1;
						blockX[2]+=1; blockY[2]-=1;
						blockY[3]-=1;
						break;
					case 2:
						blockX[0]+=1;
						blockY[1]+=1;
						blockY[2]+=1;
						blockY[3]+=1;
						break;
					case 3:
						blockX[1]-=1;
						blockX[2]-=1;
						blockX[3]+=1; blockY[3]-=1;
						break;
				}
				break;
			case 2:
				switch(blockDirection) {
				case 0:
					blockX[0]-=2;
					blockX[1]+=1; blockY[1]-=1;
					blockX[3]-=1; blockY[3]+=1;
					break;
				case 1:
					blockX[2]+=1; blockY[2]-=1;
					blockX[3]-=1; blockY[3]-=1;
					break;
				case 2:
					blockX[0]+=1;
					blockY[1]+=1;
					blockX[1]-=1;blockY[2]+=2;
					blockX[3]+=2;blockY[3]+=1;
					break;
				case 3:
					blockX[0]+=1;
					blockX[1]-=1;
					blockY[2]-=1;
					blockY[3]-=1;
					break;
				}
				break;
			case 4:
				switch(blockDirection) {
				case 0:
				case 2:
					blockX[1]+=1; blockY[1]-=1;
					blockX[2]+=2; blockY[2]-=2;
					blockX[3]+=3; blockY[3]-=3;
					break;
				case 1:
				case 3:
					blockX[1]-=1; blockY[1]+=1;
					blockX[2]-=2; blockY[2]+=2;
					blockX[3]-=3; blockY[3]+=3;
					break;
				}
				break;
				
		}
	}
	

	class MyKeyHandler extends KeyAdapter
	{
		public void keyPressed(KeyEvent e) {
			String keyCode =  KeyEvent.getKeyText(e.getKeyCode());
			
			if( keyCode == "Left" ) {
//				if(blockX[0]!=0 && blockX[1]!=0 && blockX[2]!=0 && blockX[3]!=0) {
				if(checkMove(-1)) {
					blockX[0] -= 1;
					blockX[1] -= 1;
					blockX[2] -= 1;
					blockX[3] -= 1;
				}
			}
			if( keyCode == "Right" ) {
//				if(blockX[0]!=11 && blockX[1]!=11 && blockX[2]!=11 && blockX[3]!=11) {
				if(checkMove(1)) {
					blockX[0] += 1;
					blockX[1] += 1;
					blockX[2] += 1;
					blockX[3] += 1;
				}
			}
			if( keyCode == "Down" ) {
				removeBlock();
				
				if(checkDrop()) {
					blockY[0] += 1;
					blockY[1] += 1;
					blockY[2] += 1;
					blockY[3] += 1;
				}
				else {
					drawBlock();
				}
			}
			if( keyCode == "Up" ) {
				int []tempX= new int[4];
				int []tempY= new int[4];
				
				for(int i=0;i<4;i++) {
					tempX[i]=blockX[i];
					tempY[i]=blockY[i];
				}
				
				removeBlock();
				
				turnBlock();
				
				if(checkTurn()) {
					if(blockDirection<4) {
						blockDirection++;
					}
					else {
						blockDirection=0;
					}
				}
				else {
					for(int i=0; i<4; i++) {

						blockX[i]=tempX[i];
						blockY[i]=tempY[i];

						board[blockX[i]][blockY[i]]=true;

					}
				}
			}
			
			drawBlock();
			drawMap();
			drawGrid();
			repaint();
			
		}
	}

}



Retrieved from http://wiki.zeropage.org/wiki.php/TheJavaMan/테트리스
last modified 2021-02-07 05:28:12