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

📄 gamecharmovable.java

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

import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class GameCharMovable
    extends GameChar {

  private GameControlKeySet keySet;

  private int deslocamento = 5;

  public GameCharMovable(String name, int x, int y, int width, int height) {
    super(name, x, y, width, height);
    keySet = new GameControlKeySet();
  }

  public void keyPressed(int key) {

    if (keySet.keyPressed(this, key)) {
      repaint();
    }

  }

  public boolean left() {
    return moveTo(getLocation().x - deslocamento, getLocation().y);
  }

  public boolean right() {
    return moveTo(getLocation().x + deslocamento, getLocation().y);
  }

  public boolean down() {
    return moveTo(getLocation().x, getLocation().y + deslocamento);
  }

  public boolean up() {
    return moveTo(getLocation().x, getLocation().y - deslocamento);
  }

  public boolean moveTo(int x, int y) {
    boolean canMove = true;

    if (x >= 0 && y >= 0 && (x + getWidth() <= getParent().getWidth())
        && (y + getHeight() <= getParent().getHeight())) {

      Rectangle toTest = getBounds();
      toTest.x = x;
      toTest.y = y;
      boolean repaint = false;

      Component[] brothers = getParent().getComponents();
      for (int i = 0; i < brothers.length; i++) {

        // not the same and implents GameChar
        if (!brothers[i].equals(this)
            && brothers[i] instanceof GameChar) {

          if (toTest.intersects(brothers[i].getBounds())) {

            canMove = false;
            repaint = ( (GameChar) brothers[i]).colideTo(this);
            repaint = repaint || colideTo( (GameChar) brothers[i]);

            if (repaint) {
              getParent().repaint();
            }

            break;
          }

        }
      }
      if (canMove) {
        setLocation(x, y);
      }
    }
    else {
      hitWall();
      canMove = false;
    }

    return canMove;
  }

  public void hitWall() {
    message("sai da parede !");
  }

  public int getDeslocamento() {
    return deslocamento;
  }

  public void setDeslocamento(int deslocamento) {
    this.deslocamento = deslocamento;
  }

  public GameControlKeySet getKeySet() {
    return keySet;
  }

  public void setKeySet(GameControlKeySet keySet) {
    this.keySet = keySet;
  }

}

⌨️ 快捷键说明

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