java俄罗斯方块

做个俄罗斯方块游戏

java俄罗斯方块_第1张图片

 

 

 

 

package com.longshine.game.tetris;

import java.awt.event.KeyListener;
/**
 * 游戏驱动接口
 * @author Administrator
 *
 */
public interface GameListener extends KeyListener{
	/**
	 * 时间更新
	 *
	 */
	public void tick();
	/**
	 * 游戏开始
	 * @param scene
	 */
	public void startListen(GameController scene);
}
 

 

 

package com.longshine.game.tetris;

import java.awt.event.KeyEvent;
/**
 * 游戏驱动类
 * @author Administrator
 *
 */
public class TetrisGameListener extends Thread implements GameListener {
	private GameController controller;
	/**
	 * 时间更新
	 */
	public void tick() {
		controller.down();
	}

	public void run(){
		while(true){
			tick();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public void startListen(GameController controller){
		this.controller = controller;
		this.start();
	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			controller.left();
		}
		else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
			controller.right();
		}
		else if(e.getKeyCode()==KeyEvent.VK_DOWN){
			controller.down();
		}
		else if(e.getKeyCode()==KeyEvent.VK_UP){
			controller.trans();
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {
		
	}
}

 

 

package com.longshine.game.tetris;

import java.awt.Graphics;
/**
 * 游戏控制器
 * @author Administrator
 *
 */
public class GameController {


	protected GameListener listener;
	protected Scene scene;//
	
	protected Shape curShape;
	private int status = STATUS_STOP;
	public final static int STATUS_PLAYING = 1;
	public final static int STATUS_PAUSE = 2;
	public final static int STATUS_STOP = 3;
	
	private GameUI ui = new GameUI(this);
	public GameController(){
		scene = new Scene();
	}
	public void setStatus(int s){
		status = s;
	}
	public void left(){
		if(status != STATUS_PLAYING){
			return;
		}
		Shape s = getCurShape();
		if(!this.canGoNextStep(s, -1, 0)){
			return;
		}
		else{
			s.goLeft();
			drawAll();
		}

	}
	public void trans(){
		if(status != STATUS_PLAYING){
			return;
		}
		getCurShape().trans();
		drawAll();
	}
	public void right(){
		if(status != STATUS_PLAYING){
			return;
		}
		Shape s = getCurShape();
		if(!this.canGoNextStep(s, 1, 0)){
			return;
		}
		else{
			s.goRight();
			drawAll();
		}
	}
	public boolean canGoNextStep(Shape s,int tx,int ty){
		int [][] space = scene.getSpace();
		int [][] block = s.getBlock();
		int x = s.getLocX();
		int y = s.getLocY();
		x = x + tx;
		y = y + ty;
		if(y+s.getBottomStart()>=space.length||y<0){
			return false;
		}
		else if(x+s.getRightStart()>=space[0].length||x+s.getLeftStart()<0){
			return false;
		}
		else{
			for(int j=0;j<block.length;j++){
				for(int i=0;i<block.length;i++){
					if(block[j][i]==1&&space[y+j][x+i]==1){
						return false;
					}
				}
			}
		}
		return true;
	}
	public void down(){
		if(status != STATUS_PLAYING){
			return;
		}
		Shape s = getCurShape();
		if(!this.canGoNextStep(s, 0, 1)){
			scene.addShape(s, s.getLocX(), s.getLocY());
			this.curShape = null;
			drawAll();
			return;
		}
		else{
			s.goDown();
			drawAll();
		}
	}
	public Shape getCurShape(){
		if(curShape == null){
			curShape = new Shape();
			if(checkEnd()){
				gameOver();
			}
		}
		return curShape;
	}
	public void drawAll(){
		ui.clearPanel();
		Graphics g = ui.getGraphics();
		scene.drawMe(g);
		getCurShape().drawMe(g);
		ui.setScore(CommonUtil.getUserControl().getScore()+"");
	}

	public void addListener(GameListener listener){
		this.listener = listener;
		
		
	}
	public void gameStart(){
		ui.init();
		ui.addListener(listener);
		listener.startListen(this);
		
	}
	public void newGame(){
		scene.clearSapce();
		curShape = null;
	}
	public void gameOver(){
		curShape = null;
		status = STATUS_STOP;
		javax.swing.JOptionPane.showMessageDialog(null, "Game Over!");
	}
	public boolean checkEnd(){
		for(int j=0;j<curShape.getBlock().length;j++){
			for(int i = 0;i<curShape.getBlock()[0].length;i++){
				if(curShape.getBlock()[j][i]==1&&scene.getSpace()[curShape.getLocY()+j][curShape.getLocX()+i]==1){
					return true;
				}
			}
		}
		return false;
	}
}
 

 

你可能感兴趣的:(java)