LGame代码

/*
 * 文 件 名:  AboutScreen.java
 * 版    权:  Huawei Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  Administrator
 * 修改时间:  2012-11-6
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.dcl.xstorm;

import loon.core.graphics.Screen;
import loon.core.graphics.component.LButton;
import loon.core.graphics.component.LMessage;
import loon.core.graphics.component.LPaper;
import loon.core.graphics.opengl.GLEx;
import loon.core.graphics.opengl.LTexture;
import loon.core.input.LTouch;
import loon.core.input.LTransition;
import loon.core.timer.LTimerContext;

/**
 * 关于界面
 * 
 * @author  Administrator
 * @version  [版本号, 2012-11-6]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class AboutScreen extends Screen
{
    //    private static final String aboutString =
    //        "\n \n This showcase utilizes many features from Cocos2d-html5 engine, including: Parallax background, tilemap, actions, ease, frame animation, schedule, Labels, keyboard Dispatcher, Scene Transition. Art and audio is copyrighted by Enigmata Genus Revenge, you may not use any copyrigted material without permission. This showcase is licensed under GPL";
    //menuTitle图片纹理
    private LTexture menuTitleTexture;
    //about标题纹理,从menuTitleTexture中切割出来
    private LTexture aboutTexture;
    //about背景paper
    private LPaper aboutPaper;
    //返回按钮
    private LButton back;
    //about信息
    private LMessage aboutMsg;
    
    /**
     * 释放资源 
     **/
    
    @Override
    public void dispose()
    {
        if (null != menuTitleTexture)
        {
            menuTitleTexture.dispose();
            menuTitleTexture = null;
        }
        if (null != aboutTexture)
        {
            aboutTexture.dispose();
            aboutTexture = null;
        }
        if (null != aboutPaper)
        {
            aboutPaper.dispose();
            aboutPaper = null;
        }
        if (null != back)
        {
            back.dispose();
            back = null;
        }
        if (null != aboutMsg)
        {
            aboutMsg.dispose();
            aboutMsg = null;
        }
    }
    
    /**
     * 场景转换特效 
     **/
    
    @Override
    public LTransition onTransition()
    {
        return LTransition.newFadeIn();
    }
    
    /** 
     * 加载图片资源,初始化静态资源
     * */
    
    @Override
    public void onLoad()
    {
        //初始化纹理
        menuTitleTexture = new LTexture("assets/menuTitle.png");
        aboutTexture = menuTitleTexture.getSubTexture(0, 34, 100, 34);
        //设置背景图片
        setBackground("assets/loading.png");
        //初始化about标题背景
        aboutPaper = new LPaper(aboutTexture);
        //设置about标题位置
        aboutPaper.setLocation(getHalfWidth() - aboutPaper.getWidth() / 2, 120 - aboutPaper.getHeight() / 2);
        //将about标题加到场景中
        add(aboutPaper);
        back = new LButton("Go back", 0, 0, 30, 30)
        {
            /** 
             * 返回按钮点击操作,返回系统菜单界面
             *
             **/
            @Override
            public void doClick()
            {
                setScreen(new SysMenuScreen());
            }
        };
        back.setLocation(getHalfWidth() - back.getWidth() / 2, getHalfHeight());
        add(back);
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void alter(LTimerContext timer)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void draw(GLEx g)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDown(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDrag(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchMove(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchUp(LTouch e)
    {
    }
    
}
/*
 * 文 件 名:  GameLayerScreen.java
 * 版    权:  Huawei Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  Administrator
 * 修改时间:  2012-11-7
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.dcl.xstorm;

import loon.action.sprite.Animation;
import loon.action.sprite.Label;
import loon.action.sprite.Sprite;
import loon.action.sprite.Animation.AnimationListener;
import loon.core.graphics.LColor;
import loon.core.graphics.LFont;
import loon.core.graphics.Screen;
import loon.core.graphics.opengl.GLEx;
import loon.core.graphics.opengl.LTexture;
import loon.core.input.LTouch;
import loon.core.input.LTransition;
import loon.core.timer.LTimerContext;

/**
 * 游戏主场景
 * 
 * @author  Administrator
 * @version  [版本号, 2012-11-7]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class GameLayerScreen extends Screen
{
    //飞船纹理
    private LTexture shipTexture;
    //飞船生命纹理
    private LTexture lifeTexture;
    //飞船生命精灵
    private Sprite listSpt;
    //生命标签
    private Label lifeLb;
    //飞船精灵
    private Sprite shipSpt;
    
    /**
     * 场景转化特效 
     **/
    
    @Override
    public LTransition onTransition()
    {
        return LTransition.newFadeIn();
    }
    
    /** 
     * 初始化游戏资源
     **/
    
    @Override
    public void onLoad()
    {
        //设置背景
        setBackground("assets/bg01.jpg");
        //初始化飞船纹理
        shipTexture = new LTexture("assets/ship01.png");
        //从飞船纹理中分隔出生命纹理(其实就是一个飞船,只是大小缩小了)
        lifeTexture = shipTexture.getSubTexture(0, 0, 60, 38);
        //初始化飞船生命精灵
        listSpt = new Sprite(lifeTexture);
        //将飞船生命精灵缩小到0.6倍大小
        listSpt.setScale(0.6f, 0.6f);
        //设置飞船生命精灵的位置
        listSpt.setLocation(30 - listSpt.getWidth() / 2, 20 - listSpt.getHeight() / 2);
        //将飞船生命精灵加到场景中
        add(listSpt);
        //初始化生命标签
        lifeLb = new Label("0", 50, 30);
        //设置生命标签字体
        lifeLb.setFont("Arial", LFont.STYLE_BOLD, 20);
        lifeLb.setColor(LColor.red);
        //将生命标签加到场景中
        add(lifeLb);
        //初始化飞船精灵,用上面的生命纹理就可以了,是一样的
        shipSpt = new Sprite(lifeTexture);
        shipSpt.setLocation(getHalfWidth() - shipSpt.getWidth() / 2, 420 - shipSpt.getHeight() / 2);
        Animation animation = shipSpt.getAnimation();
        animation.addFrame(shipTexture.getSubTexture(0, 0, 60, 38), 50);
        animation.addFrame(shipTexture.getSubTexture(60, 0, 60, 38), 50);
        animation.setRunning(true);
        animation.setAnimationListener(new AnimationListener()
        {
            @Override
            public void onComplete(Animation animation)
            {
                animation.update(100);
            }
        });
        animation.start();
        add(shipSpt);
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void alter(LTimerContext timer)
    {
        if (isOnLoadComplete())
        {
            
        }
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void draw(GLEx g)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDown(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDrag(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchMove(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchUp(LTouch e)
    {
    }
    
}
package com.dcl.xstorm;

import loon.GameScene;
import loon.GameScene.LSetting;

public class MainGame
{
    
    public static void main(String[] args)
    {
        LSetting setting = new LSetting();
        setting.width = 320;
        setting.height = 480;
        setting.fps = 150;
        setting.showFPS = false;
        setting.showMemory = false;
        GameScene.register(setting, SysMenuScreen.class);
    }
    
}
/*
 * 文 件 名:  SysMenuScreen.java
 * 描    述:  <描述>
 * 修 改 人:  Administrator
 * 修改时间:  2012-11-6
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.dcl.xstorm;

import loon.action.sprite.Sprite;
import loon.core.graphics.Screen;
import loon.core.graphics.component.LButton;
import loon.core.graphics.component.LPaper;
import loon.core.graphics.opengl.GLEx;
import loon.core.graphics.opengl.LTexture;
import loon.core.input.LTouch;
import loon.core.input.LTransition;
import loon.core.timer.LTimerContext;
import loon.utils.MathUtils;

/**
 * 系统菜单界面
 * 
 * @author  duchao
 * @version  [版本号, 2012-11-6]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class SysMenuScreen extends Screen
{
    //定义三个按钮newGame,option,about
    private LButton newGameBtn, optionBtn, aboutBtn;
    //定义menu图片的纹理,用于后面切割小图片
    private LTexture menuTexture;
    //定义newGame按钮的纹理数组
    private LTexture[] newGameTexture = new LTexture[3];
    //定义option按钮的纹理数组
    private LTexture[] optionTexture = new LTexture[3];
    //定义about按钮的纹理数组
    private LTexture[] aboutTexture = new LTexture[3];
    //LOGO纹理
    private LTexture logoTexture;
    //LOGO图片
    private LPaper logoPaper;
    //飞船大图片的纹理
    private LTexture shipTexture;
    //飞船精灵的纹理,从飞船大图中分隔而来
    private LTexture shipSptTexture;
    //飞船精灵
    private Sprite shipSpt;
    //飞船x方向飞行速度
    private float xSpeed;
    //飞船y方向飞行速度
    private float ySpeed;
    
    /** 
     * 释放场景中的资源
    */
    
    @Override
    public void dispose()
    {
        if (null != newGameBtn)
        {
            newGameBtn.dispose();
            newGameBtn = null;
        }
        if (null != optionBtn)
        {
            optionBtn.dispose();
            optionBtn = null;
        }
        if (null != aboutBtn)
        {
            aboutBtn.dispose();
            aboutBtn = null;
        }
        if (null != menuTexture)
        {
            menuTexture.dispose();
            menuTexture = null;
        }
        if (null != newGameTexture && newGameTexture.length > 0)
        {
            for (int i = 0; i < newGameTexture.length; i++)
            {
                newGameTexture[i].dispose();
                newGameTexture[i] = null;
            }
        }
        if (null != optionTexture && optionTexture.length > 0)
        {
            for (int i = 0; i < optionTexture.length; i++)
            {
                optionTexture[i].dispose();
                optionTexture[i] = null;
            }
        }
        if (null != aboutTexture && aboutTexture.length > 0)
        {
            for (int i = 0; i < aboutTexture.length; i++)
            {
                aboutTexture[i].dispose();
                aboutTexture[i] = null;
            }
        }
        if (null != logoTexture)
        {
            logoTexture.dispose();
            logoTexture = null;
        }
        if (null != logoPaper)
        {
            logoPaper.dispose();
            logoPaper = null;
        }
        if (null != shipTexture)
        {
            shipTexture.dispose();
            shipTexture = null;
        }
        if (null != shipSptTexture)
        {
            shipSptTexture.dispose();
            shipSptTexture = null;
        }
        if (null != shipSpt)
        {
            shipSpt.dispose();
            shipSpt = null;
        }
    }
    
    /** 
     * 场景转换特效
     **/
    
    @Override
    public LTransition onTransition()
    {
        return LTransition.newFadeIn();
    }
    
    /** 
     * 加载图片资源
     * */
    
    @Override
    public void onLoad()
    {
        //设置背景图片
        setBackground("assets/loading.png");
        //设置LOGO
        logoTexture = new LTexture("assets/logo.png");
        logoPaper = new LPaper(logoTexture);
        logoPaper.setLocation(0, 50);
        add(logoPaper);
        //初始化按钮
        initButton();
        //初始化飞船精灵
        initShip();
    }
    
    /**
     * 初始化按钮
     * @see [类、类#方法、类#成员]
     */
    private void initButton()
    {
        //初始化按钮纹理
        menuTexture = new LTexture("assets/menu.png");
        newGameTexture[0] = menuTexture.getSubTexture(0, 0, 126, 33);
        newGameTexture[1] = menuTexture.getSubTexture(0, 33, 126, 33);
        newGameTexture[2] = menuTexture.getSubTexture(0, 33 * 2, 126, 33);
        optionTexture[0] = menuTexture.getSubTexture(126, 0, 126, 33);
        optionTexture[1] = menuTexture.getSubTexture(126, 33, 126, 33);
        optionTexture[2] = menuTexture.getSubTexture(126, 33 * 2, 126, 33);
        aboutTexture[0] = menuTexture.getSubTexture(252, 0, 126, 33);
        aboutTexture[1] = menuTexture.getSubTexture(252, 33, 126, 33);
        aboutTexture[2] = menuTexture.getSubTexture(252, 33 * 2, 126, 33);
        //初始化newGameBtn按钮
        newGameBtn = new LButton(newGameTexture, "", menuTexture.getWidth() / 3, menuTexture.getHeight() / 3, 0, 0)
        {
            /**
             * newGameBtn按钮点击执行的操作
             * @see [类、类#方法、类#成员]
             */
            @Override
            public void doClick()
            {
                //这里切换到游戏场景
                setScreen(new GameLayerScreen());
            }
            
        };
        //设定newGameBtn按钮位置,按钮位置是以按钮的左上角为起始的
        newGameBtn.setLocation(getHalfWidth() - newGameBtn.getWidth() / 2, getHalfHeight() + 40);
        //将newGameBtn按钮加入到Screen中
        add(newGameBtn);
        //初始化optionBtn按钮
        optionBtn = new LButton(optionTexture, "", menuTexture.getWidth() / 3, menuTexture.getHeight() / 3, 0, 0)
        {
            /**
             * optionBtn按钮点击执行的操作
             * @see [类、类#方法、类#成员]
             */
            @Override
            public void doClick()
            {
                //这里切换到系统设置界面
            }
        };
        //设定optionBtn按钮位置,按钮位置是以按钮的左上角为起始的
        optionBtn.setLocation(getHalfWidth() - optionBtn.getWidth() / 2, newGameBtn.getY() + newGameBtn.getHeight()
            + 10);
        //将optionBtn按钮加入到Screen中
        add(optionBtn);
        //初始化newGameBtn按钮
        aboutBtn = new LButton(aboutTexture, "", menuTexture.getWidth() / 3, menuTexture.getHeight() / 3, 0, 0)
        {
            /**
             * aboutBtn按钮点击执行的操作
             * @see [类、类#方法、类#成员]
             */
            @Override
            public void doClick()
            {
                //这里切换到关于界面
                setScreen(new AboutScreen());
            }
        };
        //设定newGameBtn按钮位置,按钮位置是以按钮的左上角为起始的
        aboutBtn.setLocation(getHalfWidth() - aboutBtn.getWidth() / 2, optionBtn.getY() + optionBtn.getHeight() + 10);
        //将newGameBtn按钮加入到Screen中
        add(aboutBtn);
        
    }
    
    /**
     * 初始化飞船精灵
     * @see [类、类#方法、类#成员]
     */
    private void initShip()
    {
        //初始化飞船大图片纹理
        shipTexture = new LTexture("assets/ship01.png");
        //初始化飞船精灵纹理
        shipSptTexture = shipTexture.getSubTexture(0, 45, 60, 38);
        //初始化飞船精灵
        shipSpt = new Sprite(shipSptTexture);
        //设置飞船起始位置
        shipSpt.setLocation(0, getHeight() - shipSpt.getHeight());
        //将飞船精灵加到场景中
        add(shipSpt);
        //初始化飞船的飞行速度
        ySpeed = MathUtils.random(0.1f, 0.4f) * 5;
        xSpeed = MathUtils.random(0.1f, 0.4f) * 5;
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void alter(LTimerContext timer)
    {
        //在运行之前需要判断资源是否已经加载完成,否则会报空指针
        if (isOnLoadComplete())
        {
            //如果飞船飞行到最上面或者最右面,设置飞船位置为初始位置
            if (shipSpt.getX() > getWidth() - shipSpt.getWidth() || shipSpt.getY() < 0)
            {
                shipSpt.setLocation(0, getHeight() - shipSpt.getHeight());
                xSpeed = MathUtils.random(0.1f, 0.4f) * 5;
                ySpeed = MathUtils.random(0.1f, 0.4f) * 5;
            }
            else
            {
                //否则按照xSpeed和ySpeed飞行
                shipSpt.setLocation(shipSpt.getX() + xSpeed, shipSpt.getY() - ySpeed);
            }
        }
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void draw(GLEx g)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDown(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchDrag(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchMove(LTouch e)
    {
    }
    
    /** {@inheritDoc} */
    
    @Override
    public void touchUp(LTouch e)
    {
    }
    
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值