一个javaweb基础的小游戏。。俄罗斯方块。。。

java编写简单的小游戏(纯粹:练习基础)

一,俄罗斯方块。
话不多说。先看源码。这是一个最基础的Javaweb工程编写的。有何不对的多多指教。。。
一个javaweb基础的小游戏。。俄罗斯方块。。。_第1张图片

一个javaweb基础的小游戏。。俄罗斯方块。。。_第2张图片
一个javaweb基础的小游戏。。俄罗斯方块。。。_第3张图片

1.。。。Model 


import java.util.ArrayList;

public class Modle {

	private ArrayList modle = new ArrayList();
	private int dir = DOWN;
	private int type = 0; // 每种模型的可变换类型的标记
	private char style; // 模式共7种
	private boolean isMoved = true; // 是否可以移动
	/*
	 * ooo o o oo o o type=0 type=1 ...
	 */

	public static final int UP = -1;
	public static final int DOWN = 1;
	public static final int LEFT = -10;
	public static final int RIGHT = 10;
	// 格子的宽度
	public static final int WIDTH = 20;

	public Modle() {
		super();
	}

	// 构建6种形式的模型
	public Modle(int type) {
		switch (type) {
			case 0:
				// oooo
				getModle().add(new Node(-2 * WIDTH, 0));
				getModle().add(new Node(-WIDTH, 0));
				getModle().add(new Node(0, 0));
				getModle().add(new Node(WIDTH, 0));
				moveCenter();
				// type = 0;
				style = '-';
				break;
			case 1:
				// ooo
				// o
				getModle().add(new Node(-WIDTH, -WIDTH));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(WIDTH, -WIDTH));
				getModle().add(new Node(0, 0));
				moveCenter();
				style = 'T';
				break;
			case 2:
				// oo
				// oo
				getModle().add(new Node(-WIDTH, -WIDTH));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(-WIDTH, 0));
				getModle().add(new Node(0, 0));
				moveCenter();
				style = '口';
				break;
			case 3:
				// oo
				// oo
				getModle().add(new Node(-WIDTH, -WIDTH));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(0, 0));
				getModle().add(new Node(WIDTH, 0));
				moveCenter();
				style = 'Z';
				break;
			case 4:
				// oo
				// o
				// o
				getModle().add(new Node(WIDTH, -2 * WIDTH));
				getModle().add(new Node(0, -2 * WIDTH));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(0, 0));
				moveCenter();
				style = 'F';
				break;
			case 5:
				// oo
				// o
				// o
				getModle().add(new Node(-WIDTH, -2 * WIDTH));
				getModle().add(new Node(0, -2 * WIDTH));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(0, 0));
				moveCenter();
				style = '7';
				break;
			case 6:
				// oo
				// oo
				getModle().add(new Node(-WIDTH, 0));
				getModle().add(new Node(0, 0));
				getModle().add(new Node(0, -WIDTH));
				getModle().add(new Node(WIDTH, -WIDTH));
				moveCenter();
				style = 'S';
				break;
			default:
				break;
		}
	}

	// 将构造的Modle移动到面板中间
	private void moveCenter() {

		for (int i = 0; i < getModle().size(); i++) {
			Node n = getModle().get(i);
			n.setX(n.getX() + TetrisDemo.WIDTH / 2);
			n.setY(n.getY() - 10);
		}
	}

	// 将模型移动到指定位置
	public void moveCenter(Node node) {

		for (int i = 0; i < getModle().size(); i++) {
			Node n = getModle().get(i);
			// System.out.println("1 " + n);
			n.setX(n.getX() + node.getX() - TetrisDemo.WIDTH / 2);
			n.setY(n.getY() + node.getY() + 10);
			// System.out.println("2  " + n);
		}
	}

	public void moveBack(Node node) {

		for (int i = 0; i < getModle().size(); i++) {
			Node n = getModle().get(i);
			// System.out.println("1 " + n);
			n.setX(n.getX() - node.getX() + TetrisDemo.WIDTH / 2);
			n.setY(n.getY() - node.getY() - 10);
			// System.out.println("2  " + n);
		}
	}

	public ArrayList getModel() {
		return getModle();
	}

	// 按默认方向走一步
	public synchronized void step() {
		for (Node n : getModle()) {
			n.setX(n.getX() + dir / 10 * WIDTH);
			n.setY(n.getY() + dir % 10 * WIDTH);
		}
		this.dir = DOWN;
	}

	// 按dir方向走一步
	public synchronized void step(int dir, Modle fixedModle) {
		if (getModle() == null) {
			return;
		}
		if (fixedModle == null) {
			for (Node n : getModle()) {
				int x = n.getX() + dir / 10 * WIDTH;
				int y = n.getY() + dir % 10 * WIDTH;
				if (x >= TetrisDemo.WIDTH - TetrisDemo.WIDTH / 4
						|| x < TetrisDemo.WIDTH / 4 || y < -50) {
					this.dir = DOWN;
					return;
				}
				if (y > TetrisDemo.HIGHT - 50) {
					setMoved(false);
					this.dir = DOWN;
					return;
				}
			}
		}
		for (Node n : getModle()) {
			int x = n.getX() + dir / 10 * WIDTH;
			int y = n.getY() + dir % 10 * WIDTH;
			if (fixedModle.contains(new Node(x, y))) {
				if (dir != DOWN) {
					return;
				}
				setMoved(false);
				return;
			}
			if (x >= TetrisDemo.WIDTH - TetrisDemo.WIDTH / 4
					|| x < TetrisDemo.WIDTH / 4 || y < -50) {
				this.dir = DOWN;
				return;
			}
			if (y > TetrisDemo.HIGHT - 50) {
				setMoved(false);
				this.dir = DOWN;
				return;
			}
		}
		this.dir = dir;
		step();
	}

	// 将不能移动的modle添加到fixedNoes
	public void add(Modle modle) {
		if (modle == null) {
			return;
		}
		for (Node m : modle.getModel()) {
			getModel().add(m);
		}
	}

	// 当前modle是否包含节点node
	public boolean contains(Node node) {
		if (node == null) {
			return false;
		}
		for (Node n : getModle()) {
			if (n.equals(node)) {
				return true;
			}
		}
		return false;
	}

	// 检查节点是否在Modle内,是否在面板内
	public boolean checked(Node node) {
		int x = node.getX();
		int y = node.getY();
		if (x >= TetrisDemo.WIDTH - TetrisDemo.WIDTH / 4
				|| x < TetrisDemo.WIDTH / 4 || y < -50
				|| y > TetrisDemo.HIGHT - 50) {
			return false;
		}
		if (contains(node)) {
			return false;
		}
		return true;
	}

	// 模型的转换
	public synchronized void turn(Modle fixedNode) {
		Modle m = fixedNode;
		if (modle.size() != 4) {
			this.dir = 0;
			return;
		}
		int x2 = modle.get(1).getX();
		int x3 = modle.get(2).getX();
		int x4 = modle.get(3).getX();
		int y2 = modle.get(1).getY();
		int y3 = modle.get(2).getY();
		int y4 = modle.get(3).getY();

		if (style == ('-')) {
			switch (type) {
				case 0:
					// System.out.println("1  " + modle);
					if (m.checked(new Node(x3, y3 - (2 * WIDTH)))
							&& m.checked(new Node(x3, y3 - WIDTH))
							&& m.checked(new Node(x3, y3 + WIDTH))) {
						modle.set(0, new Node(x3, y3 - (2 * WIDTH)));
						modle.set(1, new Node(x3, y3 - WIDTH));
						modle.set(3, new Node(x3, y3 + WIDTH));
						this.type = 1;
					}
					// System.out.println("2  " + modle);
					break;
				case 1:
					// System.out.println("3  " + modle);
					if (m.checked(new Node(x3 - 2 * WIDTH, y3))
							&& m.checked(new Node(x3 - WIDTH, y3))
							&& m.checked(new Node(x3 + WIDTH, y3))) {
						modle.set(0, new Node(x3 - 2 * WIDTH, y3));
						modle.set(1, new Node(x3 - WIDTH, y3));
						modle.set(3, new Node(x3 + WIDTH, y3));
						this.type = 0;
					}
					// System.out.println("4  " + modle);
					break;
				default:
					break;
			}
		}
		if (style == ('T')) {
			switch (type) {
				case 0:
					if (m.checked(new Node(x2, y2 - WIDTH))) {
						modle.set(0, new Node(x2, y2 - WIDTH));
						this.type = 1;
					}
					break;
				case 1:
					if (m.checked(new Node(x2 - WIDTH, y2))) {
						modle.set(3, new Node(x2 - WIDTH, y2));
						type = 2;
					}
					break;
				case 2:
					if (m.checked(new Node(x2, y2 + WIDTH))) {
						modle.set(2, new Node(x2, y2 + WIDTH));
						type = 3;
					}
					break;
				case 3:
					if (m.checked(new Node(x4, y4))
							&& m.checked(new Node(x2 + WIDTH, y2))
							&& m.checked(new Node(x3, y3))) {
						modle.set(0, new Node(x4, y4));
						modle.set(2, new Node(x2 + WIDTH, y2));
						modle.set(3, new Node(x3, y3));
						type = 0;
					}
					break;
				default:
					break;
			}
		}
		if (style == ('口')) {
			this.dir = 0;
			return;
		}
		if (style == ('Z')) {
			switch (type) {
				case 0:
					if (m.checked(new Node(x4, y4 - 2 * WIDTH))
							&& m.checked(new Node(x4, y4 - WIDTH))
							&& m.checked(new Node(x4, y4 - WIDTH))) {
						modle.set(0, new Node(x4, y4 - 2 * WIDTH));
						modle.set(3, new Node(x4, y4 - WIDTH));
						type = 1;
					}
					break;
				case 1:
					if (m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x4, y4 + WIDTH))) {
						modle.set(0, new Node(x2 - WIDTH, y2));
						modle.set(3, new Node(x4, y4 + WIDTH));
						type = 0;
					}
					break;
				default:
					break;
			}
		}
		if (style == 'F') {
			switch (type) {
				case 0:
					if (m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2 + WIDTH, y2))
							&& m.checked(new Node(x2 - WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2 - WIDTH, y2));
						modle.set(2, new Node(x2 + WIDTH, y2));
						modle.set(3, new Node(x2 - WIDTH, y2 - WIDTH));
						type = 1;
					}
					break;
				case 1:
					if (m.checked(new Node(x2, y2 + WIDTH))
							&& m.checked(new Node(x2, y2 - WIDTH))
							&& m.checked(new Node(x2 - WIDTH, y2 + WIDTH))) {
						modle.set(0, new Node(x2, y2 + WIDTH));
						modle.set(2, new Node(x2, y2 - WIDTH));
						modle.set(3, new Node(x2 - WIDTH, y2 + WIDTH));
						type = 2;
					}
					break;
				case 2:
					if (m.checked(new Node(x2 + WIDTH, y2))
							&& m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2 + WIDTH, y2 + WIDTH))) {
						modle.set(0, new Node(x2 + WIDTH, y2));
						modle.set(2, new Node(x2 - WIDTH, y2));
						modle.set(3, new Node(x2 + WIDTH, y2 + WIDTH));
						type = 3;
					}
					break;
				case 3:
					if (m.checked(new Node(x2, y2 - WIDTH))
							&& m.checked(new Node(x2, y2 + WIDTH))
							&& m.checked(new Node(x2 + WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2, y2 - WIDTH));
						modle.set(2, new Node(x2, y2 + WIDTH));
						modle.set(3, new Node(x2 + WIDTH, y2 - WIDTH));
						type = 0;
					}
					break;
				default:
					break;
			}
		}
		if (style == '7') {
			switch (type) {
				case 0:
					if (m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2 + WIDTH, y2))
							&& m.checked(new Node(x2 - WIDTH, y2 + WIDTH))) {
						modle.set(0, new Node(x2 - WIDTH, y2));
						modle.set(2, new Node(x2 + WIDTH, y2));
						modle.set(3, new Node(x2 - WIDTH, y2 + WIDTH));
						type = 1;
					}
					break;
				case 1:
					if (m.checked(new Node(x2, y2 + WIDTH))
							&& m.checked(new Node(x2, y2 - WIDTH))
							&& m.checked(new Node(x2 + WIDTH, y2 + WIDTH))) {
						modle.set(0, new Node(x2, y2 + WIDTH));
						modle.set(2, new Node(x2, y2 - WIDTH));
						modle.set(3, new Node(x2 + WIDTH, y2 + WIDTH));
						type = 2;
					}
					break;
				case 2:
					if (m.checked(new Node(x2 + WIDTH, y2))
							&& m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2 + WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2 + WIDTH, y2));
						modle.set(2, new Node(x2 - WIDTH, y2));
						modle.set(3, new Node(x2 + WIDTH, y2 - WIDTH));
						type = 3;
					}
					break;
				case 3:
					if (m.checked(new Node(x2, y2 - WIDTH))
							&& m.checked(new Node(x2, y2 + WIDTH))
							&& m.checked(new Node(x2 - WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2, y2 - WIDTH));
						modle.set(2, new Node(x2, y2 + WIDTH));
						modle.set(3, new Node(x2 - WIDTH, y2 - WIDTH));
						type = 0;
					}
					break;
				default:
					break;
			}
		}
		if (style == 'S') {
			switch (type) {
				case 0:
					if (m.checked(new Node(x2, y2 + WIDTH))
							&& m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2 - WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2, y2 + WIDTH));
						modle.set(2, new Node(x2 - WIDTH, y2));
						modle.set(3, new Node(x2 - WIDTH, y2 - WIDTH));
						type = 1;
					}
					break;
				case 1:
					if (m.checked(new Node(x2 - WIDTH, y2))
							&& m.checked(new Node(x2, y2 - WIDTH))
							&& m.checked(new Node(x2 + WIDTH, y2 - WIDTH))) {
						modle.set(0, new Node(x2 - WIDTH, y2));
						modle.set(2, new Node(x2, y2 - WIDTH));
						modle.set(3, new Node(x2 + WIDTH, y2 - WIDTH));
						type = 0;
					}
					break;
				default:
					break;
			}
		}
		this.dir = 0;
		return;
	}

	public boolean isMoved() {
		return isMoved;
	}

	public void setMoved(boolean isMoved) {
		this.isMoved = isMoved;
	}

	public int getDir() {
		return dir;
	}

	public void setDir(int dir) {
		this.dir = dir;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public char getStyle() {
		return style;
	}

	public void setStyle(char style) {
		this.style = style;
	}

	@Override
	public String toString() {
		return "Modle [model=" + getModle().toString() + ", dir=" + dir
				+ ", isMoved=" + isMoved + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + dir;
		result = prime * result + (isMoved ? 1231 : 1237);
		result = prime * result
				+ ((getModle() == null) ? 0 : getModle().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Modle other = (Modle) obj;
		if (dir != other.dir)
			return false;
		if (isMoved != other.isMoved)
			return false;
		if (getModle() == null) {
			if (other.getModle() != null)
				return false;
		} else if (!getModle().equals(other.getModle()))
			return false;
		return true;
	}

	public ArrayList getModle() {
		return modle;
	}

	public void setModle(ArrayList modle) {
		this.modle = modle;
	}
}

2.。。。。Node

/*
 * 节点:用于组成方块的位置
 * x:横坐标
 * y:纵坐标
 *
 */
public class Node {
	private int x;
	private int y;

	public Node() {
	}

	public Node(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public String toString() {
		return "Node [x=" + x + ", y=" + y + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Node other = (Node) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}


}

3.。。。TetrisDemo



import javax.swing.JFrame;

public class TetrisDemo extends JFrame{
	private TetrisPanel panel;
	public static final int WIDTH = 800;
	public static final int HIGHT = 600;

	public TetrisDemo() {
		panel = new TetrisPanel();
		setTitle("俄罗斯方块");
		add(panel);
		setContentPane(panel);
		setSize(WIDTH, HIGHT);
		setVisible(true);
		setResizable(false);
		panel.requestFocus();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		TetrisDemo frame = new TetrisDemo();
	}
}

4.。。。TetrisPanel


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import javax.swing.JPanel;

public class TetrisPanel extends JPanel {
	private Modle modle;
	private Modle fixedNode = new Modle();
	private int score = 0;
	private MoveListener m = new MoveListener();
	private Service s = new Service();
	Random random = new Random();
	private Modle next;

	public TetrisPanel() {
		modle = new Modle(random.nextInt(7));
		// modle = new Modle(0);
		addKeyListener(m);
		fixedNode.setMoved(false);
		s.start();
	}

	public void paint(Graphics g) {
		super.paint(g);

		// 黑边
		g.setColor(Color.black);
		g.fill3DRect(0, 0, TetrisDemo.WIDTH / 4, TetrisDemo.HIGHT, false);
		g.fill3DRect(TetrisDemo.WIDTH - TetrisDemo.WIDTH / 4, 0,
				TetrisDemo.WIDTH / 4, TetrisDemo.HIGHT, false);
		// 画提示线
		g.setColor(Color.pink);
		g.drawLine(TetrisDemo.WIDTH / 4 + 1, 0, TetrisDemo.WIDTH / 4 + 1,
				TetrisDemo.HIGHT);
		for (int i = TetrisDemo.WIDTH / 4; i <= TetrisDemo.WIDTH * 3 / 4; i++) {
			if (i % Modle.WIDTH == 0) {
				g.drawLine(i, 0, i, TetrisDemo.HIGHT);
			}
		}

		// 分数
		g.setColor(Color.blue);
		g.setFont(new Font(TOOL_TIP_TEXT_KEY, ERROR, 20));
		g.drawString("您的分数是:", 50, 180);
		g.clearRect(50, 200, TetrisDemo.WIDTH / 8, 50);
		g.setColor(Color.red);
		score = getScore();
		// System.out.println("分数 == " + score);
		g.drawString("" + score, 70, 230);

		// 下一个上一个
		// 下一个
		g.setColor(Color.blue);
		g.setFont(new Font(TOOL_TIP_TEXT_KEY, ERROR, 20));
		g.drawString("下一个:", TetrisDemo.WIDTH * 3 / 4 + 50,
				TetrisDemo.HIGHT / 2 - 200);
		g.clearRect(TetrisDemo.WIDTH * 3 / 4 + 60 - Modle.WIDTH,
				130 - Modle.WIDTH, TetrisDemo.WIDTH / 8 + 2 * Modle.WIDTH,
				100 + 2 * Modle.WIDTH);

		// 不能移动的模型放到集合fixedNode中
		if (!modle.isMoved()) {
			fixedNode.add(modle);
			modle = clone(next);
			next = new Modle(random.nextInt(7));
			// next = new Modle(0);
			next.setMoved(false);
			next.moveCenter(new Node(TetrisDemo.WIDTH * 3 / 4 + 100, 185));
			// System.out.println("next  " + next);
			// 随机产生一个模型

		}
		if (next == null) {
			next = new Modle(random.nextInt(7));
			// next = new Modle(0);
			next.moveCenter(new Node(TetrisDemo.WIDTH * 3 / 4 + 100, 185));
			next.setMoved(false);
		}

		g.setColor(Color.orange);
		paintModle(next, g);

		// 当前
		g.setColor(Color.red);
		g.drawString("当  前:", TetrisDemo.WIDTH * 3 / 4 + 50,
				TetrisDemo.HIGHT / 2);
		g.clearRect(TetrisDemo.WIDTH * 3 / 4 + 50 - Modle.WIDTH,
				200 + 130 - Modle.WIDTH,
				TetrisDemo.WIDTH / 8 + 2 * Modle.WIDTH, 100 + 2 * Modle.WIDTH);
		// 构造当前模型
		Modle current = clone(modle);
		// System.out.println("current  == " + current);
		// System.out.println("node = " + new Node(TetrisDemo.WIDTH * 3 / 4 +
		// 50, 200 + 130));
		current.moveCenter(new Node(TetrisDemo.WIDTH * 3 / 4 + 90, 200 + 185));
		g.setColor(Color.green);
		paintModle(current, g);

		// 画可移动模型
		// System.out.println("将要画的modle = " + modle);
//		g.setColor(Color.green);
//		for (Node n : modle.getModel()) {
//			if (fixedNode.contains(n)) {
//				s.stop();
//				paintEnd(g);
//				return;
//			}
		paintModle(modle, g);
//		}
		// 画不可移动模型
		// System.out.println("将要画的fixedNode的长度 = " +
		// fixedNode.getModel().size());
		g.setColor(Color.cyan);
		paintModle(fixedNode, g);
	}

	// 结束界面
	public void paintEnd(Graphics g) {
		g.setColor(Color.red);
		g.setFont(new Font(TOOL_TIP_TEXT_KEY, ERROR, 30));
		g.clearRect(0, 150, 550, 100);
		g.setColor(Color.BLUE);
		g.fillRect(0, 150, 550, 100);
		g.clearRect(150, 180, 200, 40);
		g.setColor(Color.red);
		g.drawString(" Game  Over!", 150, 210);
		Service.interrupted();
		// removeKeyListener(listener);
	}

	// 复制模型m
	public Modle clone(Modle m) {
		if (m == null) {
			return null;
		}
		Modle current = new Modle();
		char[] chars = new char[] { '-', 'T', '口', 'Z', 'F', '7', 'S' };
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if (m.getStyle() == c) {
				current = new Modle(i);
				break;
			}
		}
		return current;
	}

	// 计算分数
	public int getScore() {
		// 填充满的行数
		int line = 0;

		if (fixedNode == null) {
			return score;
		}
		// 将fixNode中Node的y统计出现的次数map
		Map map = new HashMap();
		for (Node n : fixedNode.getModel()) {
			int y = n.getY();
			int count = 1;
			if (map.keySet().contains(y)) {
				count = map.get(y) + 1;
				map.put(y, count);
				continue;
			}
			map.put(y, 1);
		}
		// 迭代y 删除每行不够20个格子
		Iterator> ite = map.entrySet().iterator();
		while (ite.hasNext()) {
			int key = ite.next().getKey();
			int count = map.get(key);
			// System.out.println("key : value = " + key + ":" + count);
			if (count == 20) {
				line++;
				map.put(key, 0);
				continue;
			}
			ite.remove();
		}

		// 迭代fixedNode 并修改需要移动行数count
		deleteLine(fixedNode, map.keySet());

		TetrisPanel.this.repaint();
		return line * 10 + score;
	}

	public void deleteLine(Modle m, Set yy) {
		ArrayList nodes = m.getModel();
		Iterator i = yy.iterator();
		while (i.hasNext()) {
			int y = i.next();
			Iterator nod = nodes.iterator();
			while (nod.hasNext()) {
				Node n = nod.next();
				if (y == n.getY()) {
					nod.remove();
					continue;
				}
				if (n.getY() < y) {
					n.setY(n.getY() + Modle.WIDTH);
				}
			}

		}

	}

	// 画模型
	public void paintModle(Modle m, Graphics g) {
		if (m == null) {
			return;
		}
		// g.setColor(Color.cyan);
		for (Node n : m.getModel()) {
			g.fill3DRect(n.getX(), n.getY(), Modle.WIDTH, Modle.WIDTH, false);
		}
	}

	// 监听器
	class MoveListener extends KeyAdapter {

		int dir;

		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
				case KeyEvent.VK_UP:
					// Modle mod = modle;
					modle.setDir(0);
					// System.out.println("start  " + modle);
					modle.turn(fixedNode);
					// System.out.println("end   " + modle);
					modle.setDir(0);
					break;
				case KeyEvent.VK_DOWN:
					dir = Modle.DOWN;
					break;
				case KeyEvent.VK_LEFT:
					dir = Modle.LEFT;
					break;
				case KeyEvent.VK_RIGHT:
					dir = Modle.RIGHT;
					break;
				default:
					break;
			}
			// System.out.println("你按下的是:  " + e.getKeyChar());
			// System.out.println("dir = " + dir);
			// System.out.println("当前模型 = " + modle);
			// System.out.println("判断 " + modle);
			if (modle.isMoved() && modle.getDir() != 0) {
				modle.step(dir, fixedNode);
				// System.out.println("移动后的模型 = " + modle);
			}
			modle.setDir(Modle.DOWN);
			// System.out.println("重绘之前  " + modle);
			TetrisPanel.this.repaint();
		}
	}

	// 移动线程
	class Service extends Thread {

		public void run() {
			while (true) {
				try {
					Thread.sleep(500);
					if (modle.isMoved() && modle.getDir() != 0) {
						modle.step(Modle.DOWN, fixedNode);
					}
					// System.out.println(modle.getModel());
					TetrisPanel.this.repaint();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Modle modle() {
		return modle;
	}

}

最简单的一个Java俄罗斯就实现了,多多指点。。。

你可能感兴趣的:(一个javaweb基础的小游戏。。俄罗斯方块。。。)