原创作者: wuhua
阅读:1138次
评论:0条
更新时间:2011-05-26
这里介绍的是Game的逻辑类,主要控制游戏的动作,以及绘制。
详细里面代码有注释
代码就是上面的,如果有什么好的建议,请评论。下面的一课,我将介绍GameThread。
详细里面代码有注释
java 代码
- /********************************************************************
- * 项目名称 :j2me学习
- *
- * Copyright 2005-2006 Wuhua. All rights reserved
- ********************************************************************/
- package org.wuhua.battleplan;
- import java.util.Stack;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import org.wuhua.game.GameCanvas;
- import org.wuhua.game.model.Fairy;
- import org.wuhua.game.util.Log;
- /**
- * 类名:Game.java
- * 编写日期: 2006-11-30
- * 程序功能描述:游戏的主体类。游戏的绘制,状态的改变都在这边。
- * Demo:
- * Bug:
- *
- * 程序变更日期 :
- * 变更作者 :
- * 变更说明 :
- *
- * @author wuhua
- */
- public class Game extends GameCanvas {
- static Log log = Log.getLog("Game");
- private Hero hero;
- private Stack balls;
- private Stack foes;
- private Stack balst;
- /**
- * 爆炸效果索引
- */
- private int balstIndex;
- /**
- * if time = 3 的时候建立一个
- */
- private int genaratBallTime;
- private int genaratFoeTime;
- Game(){
- super();
- this.setFullScreenMode(true);
- }
- void init(){
- WIDTH = getWidth();
- HEIGHT = getHeight();
- log.debug("WIDTH=" + WIDTH);
- log.debug("hegiht=" + HEIGHT);
- this.bufferImage = Image.createImage(WIDTH, HEIGHT);
- Platform.WIDTH = this.getWidth();
- Platform.HEIGHT = this.getHeight();
- hero = Hero.createHero(Platform.WIDTH/2, Platform.HEIGHT -30);
- balst = new Stack();
- }
- /**
- * 产生爆炸效果
- * @param x
- * @param y
- */
- void genaratorBalst(int x, int y){
- balst.addElement(new Fairy(Resources.BLAST[0], x, y));
- balst.addElement(new Fairy(Resources.BLAST[1], x, y));
- balst.addElement(new Fairy(Resources.BLAST[2], x, y));
- balst.addElement(new Fairy(Resources.BLAST[3], x, y));
- balst.addElement(new Fairy(Resources.BLAST[4], x, y));
- }
- /**
- * 碰撞。实在没有好的实现。 我想不出来了.
- * 逻辑是遍历所有子弹,然后再遍历所有敌机,再判断是否碰撞,如果碰撞,则产生一个爆炸实例.
- * 最后删除子弹跟飞机.
- *
- */
- void collides(){
- if(balls == null
- || foes == null)
- return ;
- for(int i = 0; i < balls.size(); i ++){
- Ball b = (Ball) balls.elementAt(i);
- for(int j =0; j < foes.size(); j ++){
- Foe f = (Foe) foes.elementAt(j);
- if(b.collidesWith(f)){
- this.genaratorBalst(f.getX(), f.getY());
- balls.removeElement(b);
- foes.removeElement(f);
- return;
- }
- }
- }
- }
- /**
- * 绘制游戏场景跟Hero
- *
- */
- void drawGame(){
- if(Platform.HEIGHT < this.getHEIGHT()){
- Platform.HEIGHT = this.getHEIGHT();
- }
- Graphics g = this.getGraphics();
- if(g == null)
- return;
- fillFullScreen(g,0x349293);
- paintHeroAndBall(g);
- paintFoe(g);
- paintBalst(g);
- this.flushGraphics();
- }
- /**
- * 绘制爆炸效果
- * @param g
- */
- private void paintBalst(Graphics g) {
- if(balst == null
- || balst.size() == 0)
- return;
- Fairy bf = (Fairy) balst.elementAt(balstIndex);
- bf.paint(g);
- if(balstIndex >= 4){
- balstIndex = 0;
- balst.removeAllElements();
- }
- balstIndex++;
- }
- /**
- * 绘制敌机
- * @param g
- */
- private void paintFoe(Graphics g) {
- if(foes == null)
- return ;
- for(int i=0; i < foes.size(); i++){
- Foe foe = (Foe) foes.elementAt(i);
- foe.paint(g);
- }
- }
- /**
- * 制造敌飞机
- *
- */
- public void genaratorFoe(){
- if(this.genaratFoeTime == 5){
- FoeManager.addFoe(FoeManager.genarator());
- FoeManager.clearFoesIsOut();
- foes = FoeManager.getFoes();
- genaratFoeTime = 0;
- }
- genaratFoeTime++;
- }
- /**
- * 敌机飞行
- *
- */
- public void foeFly(){
- if(foes == null)
- return ;
- for(int i = 0; i < foes.size(); i++){
- Foe foe = (Foe) foes.elementAt(i);
- foe.fly();
- }
- }
- private void paintHeroAndBall(Graphics g) {
- hero.paint(g);
- paintBalls(g);
- }
- /**
- * 绘制子弹
- * @param g
- */
- private void paintBalls(Graphics g) {
- if(balls == null)
- return ;
- for(int i = 0; i < balls.size(); i++){
- Ball ball = (Ball) balls.elementAt(i);
- ball.paint(g);
- }
- }
- /**
- * 子弹的飞行
- *
- */
- public void ballFly(){
- if(balls == null)
- return ;
- for(int i = 0; i < balls.size(); i++){
- Ball ball = (Ball) balls.elementAt(i);
- ball.fly();
- }
- }
- /**
- * 飞机的动作
- *
- */
- public void heroAction(){
- checkHeroIsExists();
- int keyCode = this.getKeyStates();
- switch(keyCode){
- case Platform.KEY_LEFT: hero.moveLeft(); break;
- case Platform.KEY_RIGHT: hero.moveRight(); break;
- case Platform.KEY_UP: hero.moveUp(); break;
- case Platform.KEY_DOWN: hero.moveDown(); break;
- case Platform.KEY_FIRE: genaratorBall(); break;
- }
- }
- /**
- * 创建子弹
- *
- */
- private void genaratorBall() {
- if(this.genaratBallTime == 3){
- checkHeroIsExists();
- BallManager.addBall(BallManager.genarator(hero.getX(), hero.getY()));
- BallManager.clearBallsIsOut();
- balls = BallManager.getBalls();
- genaratBallTime = 0;
- }
- genaratBallTime++;
- }
- private void checkHeroIsExists() {
- if(hero == null){
- throw new java.lang.NullPointerException("Hero is Null");
- }
- }
- /**
- * 游戏的run。控制游戏个各个方面
- *
- */
- public void run(){
- this.collides();
- this.heroAction();
- this.ballFly();
- this.genaratorFoe();
- this.foeFly();
- this.drawGame();
- this.setKeyStates(1000);
- }
- }
代码就是上面的,如果有什么好的建议,请评论。下面的一课,我将介绍GameThread。
评论 共 0 条 请登录后发表评论