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

📄 fallingbox2.java

📁 游戏基础
💻 JAVA
字号:
package net.java.gamebase.sample.boxes.v4;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import net.java.gamebase.core.GameCharMovable;
import net.java.gamebase.sample.boxes.v2.Box;

public class FallingBox2 extends GameCharMovable {

	private static Random random = new Random();

	public static final Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN,
			Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
			Color.MAGENTA, Color.RED, Color.PINK, Color.YELLOW };

	private int color;

	private int position;

	private boolean falling;

	private int verticalSpace;

	private BoxesGame4 base;

	/**
	 * this class is responsible for the random color
	 * 
	 * @param x
	 * @param y
	 * @param base
	 */
	public FallingBox2(int x, int y, BoxesGame4 base) {
		super("", x, y, 32, 32);

		this.color = random.nextInt(Box.colors.length - 1);
		this.position = 0;
		this.falling = true;
		this.base = base;
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(colors[color]);
		g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
	}

	public void tick() {

		// if its falling
		if (falling) {

			// check if its at the last lane
			if (!base.isLastLane(this)) {
				position++;
				int step = getHeight() + verticalSpace;
				setY((position * step) + verticalSpace);
			}
		}

	}

	public void stopFalling() {
		falling = false;
	}

	public int getPosition() {
		return position;
	}

	public void setHorizontalSpace(int horizontalSpace) {
		this.verticalSpace = horizontalSpace;
	}

	/**
	 * This is the baset way to compare if 2 objects are equals
	 * 
	 */
	public boolean equals(Object obj) {
		if (obj != null) {
			if (obj instanceof FallingBox2) {
				return ((FallingBox2) obj).color == this.color;
			}
		}
		return false;
	}

	public int getColor() {
		return color;
	}

}

⌨️ 快捷键说明

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