⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetris.java

📁 俄罗斯方块java源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			repaint(blockPos[getY(i)][getX(i)].x, blockPos[getY(i)][getX(i)].y,
				 		gridSize, gridSize);			
			
		}
		return true;
	}
	
	public void change(){		
		tmpStatus = currentStatus + direction;
		if(tmpStatus >= currentShape.length)
			tmpStatus = 0;
		else if(tmpStatus < 0)
			tmpStatus = currentShape.length - 1;
			
	
		int i;
		for(i = 0; i < currentShape[currentStatus].length; i++)
			if(getTmpX(i) < 0 || getTmpX(i) > XMAX || getTmpY(i) < 0 || getTmpY(i) > YMAX
				|| table[getTmpY(i)][getTmpX(i)]){	
				break;
			}
		if(i >= currentShape[currentStatus].length){			
			int t = currentStatus;
			currentStatus = tmpStatus;
			tmpStatus = t;
			for(i = 0; i < currentShape[currentStatus].length; i++){
				repaint(blockPos[getY(i)][getX(i)].x, blockPos[getY(i)][getX(i)].y,
				 			gridSize, gridSize);
				repaint(blockPos[getTmpY(i)][getTmpX(i)].x, 
						blockPos[getTmpY(i)][getTmpX(i)].y,
							gridSize, gridSize);
			}
		}else
			canMoveDown = true;
		
	}
	
	public boolean checkEat(){
		boolean eat = false;
		int step = 1;
		points = 0;
		for(int i = 0; i <= YMAX;){
			int j;
			for(j = 0; j <= XMAX; j++)
				if(table[i][j] == false)
					break;
			if(j > XMAX){
				for(j = i; j < YMAX; j++){
					System.arraycopy(table[j + 1], 0, table[j], 0, 
						table[j].length);
				}
				eat = true;	
				points += 10 * step++;				
			}else
				i++;
		}
		return eat;
	}
	
	public int getSleepTime(){
		switch(speed){
			case 1:
				return 600;
			case 2:
				return 400;
			case 3:
				return 330;
			case 4:
				return 260;
			case 5:
				return 200;
			case 6:
				return 165;
			case 7:
				return 120;
			case 8:
				return 70;
			case 9:			
				return 40;
		}
		return 1;
	}
	
	public void run(){
		while(true){
			if(gameStatus == PLAY){				
				if(canMoveDown && !moveDown()){
					for(int i = 0; i < currentShape[currentStatus].length; i++){
						table[getY(i)][getX(i)] = true;
					}
					if(!getNextBlock()){
						gameStatus = OVER;
						repaint();
						continue;
					}
					repaint();
					
				}
				if(checkEat()){
					score += points;
					int tmp = score / 500;
					speed = tmp + beginSpeed;			
					if(speed > 9) speed = 9;
					repaint();
				}
				try{
					int sleepTime = getSleepTime();
					Thread.sleep(sleepTime);
				}catch(Exception exc){}
			}else{
				try{				
					Thread.sleep(100);
				}catch(Exception exc){}
			}
		}
	}
	
	public int getX(int i){
		return currentX + currentShape[currentStatus][i][0];
	}
	public int getY(int i){
		return currentY + currentShape[currentStatus][i][1];
	}
	
	public int getTmpX(int i){
		return currentX + currentShape[tmpStatus][i][0];
	}
	public int getTmpY(int i){
		return currentY + currentShape[tmpStatus][i][1];
	}
	
	public void paint(Graphics g){
		super.paint(g);
		if(STOP != gameStatus){
			changeStatus();
			
			//the instruction
			g.setColor(Color.LIGHT_GRAY);
			g.fillRect(gridSize * horizontalGameBlocks + 2, 52, 
					gridSize * horizontalStauesBlocks, gridSize * verticalBlocks);
					
			g.setColor(Color.red);		
			g.drawString("→: right", gridSize * horizontalGameBlocks + 5,
					gridSize * (verticalBlocks - 12) + 72);
			g.drawString("←: left", gridSize * horizontalGameBlocks + 5,
					gridSize * (verticalBlocks - 11) + 72);
			g.drawString("↓: down", gridSize * horizontalGameBlocks + 5,
					gridSize * (verticalBlocks - 10) + 72);
			g.drawString("↑: circumrotate", gridSize * horizontalGameBlocks + 5,
					gridSize * (verticalBlocks - 9) + 72);
			g.drawString("space:fall down", gridSize * horizontalGameBlocks + 5,
					gridSize * (verticalBlocks - 8) + 72);				
			
			//score and speed				
			Font font = new Font("SansSerif", Font.BOLD, 16);
			g.setFont(font);
			g.drawString("Score: " + score, gridSize * horizontalGameBlocks + 5, 
						gridSize * (verticalBlocks - 4) + 72);
			g.drawString("Speed: " + speed, gridSize * horizontalGameBlocks + 5, 
						gridSize * (verticalBlocks - 2) + 72);
			
			//the game block
			g.setColor(Color.yellow);
			for(int i = 0; i < verticalBlocks; i++)
				for(int j = 0; j < horizontalGameBlocks; j++){
					if(table[i][j])
						g.fill3DRect(blockPos[i][j].x, blockPos[i][j].y, 
							gridSize - 1, gridSize - 1, true);
				}
			
			for(int i = 0; i < currentShape[currentStatus].length; i++){		
				g.fill3DRect(blockPos[getY(i)][getX(i)].x, 	blockPos[getY(i)][getX(i)].y,
					gridSize - 1, gridSize - 1, true);
			}
			
			//pre-block paint
			g.setColor(Color.red);
			for(int i = 0; i < nextShape[nextStatus].length; i++)
				g.fill3DRect(gridSize * (horizontalGameBlocks + nextShape[nextStatus][i][0] + 1) + 5, 
							gridSize * (-nextShape[nextStatus][i][1] + verticalBlocks - 19) + 72,
							gridSize - 1, gridSize - 1, true);
		}
	}
	
	public void changeStatus(){
		if(PLAY == gameStatus){			
			statusLabel.setText(" Press Esc to pause...");
		}else if(PAUSE == gameStatus){			
			statusLabel.setText(" Pause! Press any key to continue...");
		}else if(TOBEGIN == gameStatus){			
			statusLabel.setText(" Press any key to begin...");
		}else if(STOP == gameStatus){			
			statusLabel.setText(" Get ready to start !");
		}else{			
			statusLabel.setText(" You lost! You got " + score + " points!");
		}				
	}
	
	//KeyEvent
	public void keyPressed(KeyEvent event){
		if(PAUSE == gameStatus || TOBEGIN == gameStatus){
			gameStatus = PLAY;
			changeStatus();
		}else if(PLAY == gameStatus){			
			if(event.getKeyCode() == KeyEvent.VK_ESCAPE){				
				gameStatus = PAUSE;
				changeStatus();						
			}else if(PLAY == gameStatus){		
				switch(event.getKeyCode()){
					case KeyEvent.VK_DOWN:
						moveDown();
						break;
					case KeyEvent.VK_RIGHT:
						canMoveDown = false;
						moveRight();
						break;
					case KeyEvent.VK_LEFT:
						canMoveDown = false;
						moveLeft();
						break;
					case KeyEvent.VK_UP:
						canMoveDown = false;						
						change();
						break;
					case KeyEvent.VK_SPACE:
						while(moveDown());
						break;
				}
			}
		}	
	}
	public void keyReleased(KeyEvent event){		
		canMoveDown = true;		
	}
	public void keyTyped(KeyEvent event){}		
	
	//main
	public static void main(String args[]){
		Tetris game = new Tetris();		
	}
}


class BlockShape{
	//the shape matrix
	public static final int blockShape[][][][] = {		
		{
			{
				{0, 0}, {1, 0}, {0, -1}, {1, -1}
			}
		},			
		{
			{
				{0, 0}, {1, 0}, {2, 0}, {2, -1}
			},	
			{
				{2, 0}, {2, -1}, {2, 1}, {1, -1}
			},	
			{
				{0, 0}, {0, -1}, {1, -1}, {2, -1}
			},			
			{
				{1, 1}, {2, 1}, {1, 0}, {1, -1}
			}	
		},	
		{
			{
				{0, 0}, {1, 0}, {2, 0}, {3, 0}
			},	
			{
				{1, 1}, {1, 0}, {1, -1}, {1, -2}
			},	
		},	
		{
			{
				{0, -1}, {0, 0}, {1, 0}, {2, 0}
			},	
			{
				{0, 1}, {1, 1}, {1, 0}, {1, -1}
			},	
			{
				{1, 0}, {1, -1}, {0, -1}, {-1, -1}
			},	
			{
				{0, 0}, {0, 1}, {0, -1}, {1, -1}
			}
		},	
		{
			{
				{0, 0}, {1, 0}, {1, -1}, {2, -1}
			},	
			{
				{2, 1}, {2, 0}, {1, 0}, {1, -1}
			}
		},	
		{
			{
				{0, 0}, {0, -1}, {1, -1}, {1, -2}
			},	
			{
				{1, -1}, {1, -2}, {0, -2}, {2, -1}
			}
		},	
		{
			{
				{1, 0}, {0, -1}, {1, -1}, {2, -1}
			},	
			{
				{1, 0}, {1, -1}, {2, -1}, {1, -2}
			},	
			{
				{1, -1}, {2, -1}, {1, -2}, {0, -1}
			},	
			{
				{1, -1}, {1, -2}, {0, -1}, {1, 0}
			}
		}
	};
	public BlockShape(){}	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -