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

📄 gameelement.java

📁 一个简单的游戏程序
💻 JAVA
字号:
package pushbook.core;

import java.awt.*;
import pushbook.core.*;
import java.awt.image.ImageObserver;
import java.util.Vector;

/**
 * <p>Title: 推箱子</p>
 * <p>Description: 游戏元素</p>
 * <p>Copyright: 无版权</p>
 * <p>Company: 吉林省前郭尔罗斯蒙古族自治县第五高级中学</p>
 * @author 郑雪
 * @version 1.0
 */
public class GameElement
{
    //位置坐标
    protected int x = 0;
    protected int y = 0;

    //步伐堆栈
    private Vector stepStack = null;

    //图片
    protected String img = "";
    protected Image trueimg = null;

    //可移动的对象
    protected boolean canMove;

    //可覆盖的对象
    protected boolean canOvercast;

    //主动移动对象,直接处理键盘消息
    protected boolean active;

    //目的地对象
    protected boolean isDestination;

    //初始化时参照的模板名称
    protected String templetName = "";

    /**
     * 默认的构造函数
     */
    public GameElement()
    {
        stepStack = new Vector();
    }

    /**
     * 构造函数
     * @param ix int 初始化坐标X
     * @param iy int 初始化坐标Y
     * @param iimg Image 使用的图片
     */
    public GameElement(int ix,int iy,String iimg)
    {
        stepStack = new Vector();
        x = ix;
        y = iy;
        stepPushStack();
        img = iimg;
    }

    /**
     * 初始化
     * @param ix int 初始化坐标X
     * @param iy int 初始化坐标Y
     * @param iimg Image 使用的图片
     */
    public void init(int ix,int iy,String iimg)
    {
        stepStack = new Vector();
        x = ix;
        y = iy;
        stepPushStack();
        img = iimg;
    }



    /**
     * 判断指定对象是否是可以移动的
     * @return boolean
     */
    public boolean canMove()
    {
        return canMove;
    }

    /**
     * 判断是否可以覆盖
     * @return boolean
     */
    public boolean canOvercast()
    {
        return canOvercast;
    }

    /**
     * 判断是否可以主动移动
     * @return boolean
     */
    public boolean canActive()
    {
        return active;
    }

    /**
     * 取得当前所在的位置
     * @return Point
     */
    public Point getLocation()
    {
        Point loc = new Point(x,y);
        return loc;
    }

    /**
     * 绘制对象
     * @param g Graphics
     * @param imgobs ImageObserver
     */
    public void drawMe(Graphics g ,ImageObserver imgobs)
    {
        if(img != null)
        {
            if (trueimg == null)
            {
                trueimg =
                    ( (Frame) imgobs).getToolkit().getImage(getClass().getResource(img));
            }
            g.drawImage(trueimg, x, y, Invariable.unitSize, Invariable.unitSize, imgobs);
        }
    }


    /**
     * 向上移动一个单位
     * @return boolean 如果成功则返回true,否则返回false
     */
    public boolean moveUp()
    {
        if(canMove)
        {
            if (y - Invariable.unitSize > Invariable.gameAreaY)
            {
                y -= Invariable.unitSize;
                return true;
            }
            else
                return false;
        }
        else
        {
            return false;
        }
    }

    /**
     * 向下移动一个单位
     * @return boolean
     */
    public boolean moveDown()
    {
        if(canMove)
        {
            if (y + Invariable.unitSize <
                (Invariable.gameAreaY + Invariable.gameAreaHeight) - Invariable.unitSize)
            {
                y += Invariable.unitSize;
                return true;
            }
            return false;
        }
        else
        {
            return false;
        }
    }


    /**
     * 向左移动一个单位
     * @return boolean 如果成功则返回true,否则返回false
     */
    public boolean moveLeft()
    {
        if(canMove)
        {
            if (x - Invariable.unitSize > Invariable.gameAreaX)
            {
                x -= Invariable.unitSize;
                return true;
            }
            return false;
        }
        else
        {
            return false;
        }
    }

    /**
     * 向右移动一个单位
     * @return boolean 如果成功则返回true,否则返回false
     */
    public boolean moveRight()
    {
        if(canMove)
        {
            if (x + Invariable.unitSize <
                (Invariable.gameAreaX + Invariable.gameAreaWidth) - Invariable.unitSize)
            {
                x += Invariable.unitSize;
                return true;
            }
            return false;
        }
        else
        {
            return false;
        }
    }


    /**
     * 当前位置入栈
     */
    public void stepPushStack()
    {
        stepStack.add(0,new Point(x,y));
    }

    /**
     * 回退
     * @param step int
     */
    public boolean breakStep()
    {
        if(stepStack.size() == 0) return false;
        Point op = (Point)stepStack.get(0);
        x = op.x;
        y = op.y;
        stepStack.remove(0);
        return true;
    }
}

⌨️ 快捷键说明

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