这里介绍的是Game的逻辑类,主要控制游戏的动作,以及绘制。
详细里面代码有注释
java 代码
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
- 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();
-
- }
-
-
-
-
-
-
- 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;
- }
-
- }
- }
- }
-
-
-
-
-
- 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();
- }
-
-
-
-
-
- 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++;
- }
-
-
-
-
-
- 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);
- }
-
-
-
-
-
- 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");
- }
- }
-
-
-
-
-
- public void run(){
- this.collides();
- this.heroAction();
- this.ballFly();
- this.genaratorFoe();
- this.foeFly();
-
- this.drawGame();
- this.setKeyStates(1000);
- }
- }
代码就是上面的,如果有什么好的建议,请评论。下面的一课,我将介绍GameThread。 |