📄 game.java
字号:
/********************************************************************
* 项目名称 :<b>j2me学习</b> <br/>
*
* 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;
/**
* <b>类名:Game.java</b> </br>
* 编写日期: 2006-11-30 <br/>
* 程序功能描述:游戏的主体类。游戏的绘制,状态的改变都在这边。 <br/>
* Demo: <br/>
* Bug: <br/>
*
* 程序变更日期 :<br/>
* 变更作者 :<br/>
* 变更说明 :<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
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();
}
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();
}
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);
}
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -