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

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

一,俄罗斯方块。
话不多说。先看源码。这是一个最基础的Javaweb工程编写的。有何不对的多多指教。。。
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.。。。Model 


import java.util.ArrayList;

public class Modle {

	private ArrayList<Node> modle = new ArrayList<Node>();
	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<Node> 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<Node> getModle() {
		return modle;
	}

	public void setModle(ArrayList<Node> 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<y ,次数>
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		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<Entry<Integer, Integer>> 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<Integer> yy) {
		ArrayList<Node> nodes = m.getModel();
		Iterator<Integer> i = yy.iterator();
		while (i.hasNext()) {
			int y = i.next();
			Iterator<Node> 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俄罗斯就实现了,多多指点。。。

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值