gamerender.java
来自「学习j2me写的,虽然在碰撞上处理的不好!但其中的技巧还是可以学习的1」· Java 代码 · 共 209 行
JAVA
209 行
package Snowball;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import Creature.Creature;
public class GameRender
{
private final static int GameWidth = 176; //游戏屏幕宽度
private final static int GameHeight = 208; //游戏屏幕高度
private int Width; //屏幕宽度
private int Height; //屏幕高度
private Graphics g; //图形对象
private GameStatus gs; //游戏状态数据
private GameCanvas gc; //游戏画板
private GameImage gi; //游戏图片资源
private ObjectManage om; //游戏对象管理器
/** Creates a new instance of GameRender */
public GameRender(Graphics g, GameCanvas gc, GameStatus gs, GameImage gi, ObjectManage om)
{
this.g = g;
this.gc = gc;
this.gs = gs;
this.gi = gi;
this.om = om;
}
/**==============================**
** 整体绘画更新:Process()
**==============================**/
public void Process()
{
if(gs.drawState==gs.LOGO) //LOGO
logoPage(g);
if(gs.drawState==gs.INDEX) //首页
indexPage(g);
if(gs.drawState == gs.GAME) //游戏
drawGame();
if(gs.drawState == gs.NEW_LEVEL){ //关卡
levelPage(g);
gs.Wait(1500);
}
if(gs.drawState == gs.HELP) //帮助
helpPage(g);
if(gs.drawState == gs.ABOUT) //关于
aboutPage(g);
if(gs.drawState==gs.GAME_OVER) { //GameOver
GameOver(g);
gs.Wait(1500);
}
}
/**==============================**
** 绘画开始LOGO画面
**==============================**/
private void logoPage(Graphics g) {
if(gs.logoState>=0 && gs.logoState < 16){
if(gi.yblogoImg!=null && gi.ybgameImg!=null){
g.setColor(255, 255, 255);
g.fillRect(0, 0, Width, Height);
g.drawImage(gi.yblogoImg,60,48, Graphics.TOP | Graphics.LEFT);
g.setClip(0, 152, 176/10*gs.logoState+5, 56);
g.drawImage(gi.ybgameImg, 0, 152, Graphics.TOP | Graphics.LEFT);
g.setClip(0,0, 176,208);
}
gs.logoState++;
}
gc.flushGraphics();
}
/**==============================**
** 绘制首页:indexPage
**==============================**/
private void indexPage(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD , Font.SIZE_LARGE));
g.setColor(30,153,189);
g.fillRect(0,0,Width,Height);
g.drawImage(gi.IndexImg, 0, 0, Graphics.TOP | Graphics.LEFT);
g.drawString(gs.menuStr1[gs.idxTmp],
103,
168,
Graphics.HCENTER | Graphics.TOP );
gc.flushGraphics();
}
/**==============================**
** 绘制首页:levelPage
**==============================**/
private void levelPage(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD | Font.STYLE_ITALIC , Font.SIZE_LARGE));
g.setColor(30,153,189);
g.fillRect(0,0,Width,Height);
g.drawImage(gi.LevelImg, 0, 0, Graphics.TOP | Graphics.LEFT);
g.drawString(""+gs.gl.getGreatLevel(),73,84,Graphics.HCENTER | Graphics.TOP );
g.drawString(""+gs.gl.getShareLevel(),73,125,Graphics.HCENTER | Graphics.TOP );
gc.flushGraphics();
}
/**==============================**
** 绘制首页:helpPage
**==============================**/
private void helpPage(Graphics g)
{
g.setColor(0,0,255);
g.fillRect(0,0,Width,Height);
g.drawImage(gi.HelpImg,gs.HelpX, gs.HelpY, Graphics.TOP | Graphics.LEFT);
gc.flushGraphics();
}
/**==============================**
** 绘制首页:helpPage
**==============================**/
private void aboutPage(Graphics g)
{
g.setColor(0,0,255);
g.fillRect(0,0,Width,Height);
g.drawImage(gi.AboutImg, 0, 0, Graphics.TOP | Graphics.LEFT);
gc.flushGraphics();
}
/**==============================**
** 绘画游戏:drawGoods()
**==============================**/
private void drawGoods(Graphics g)
{
if(1 == gs.gp.GoodsType || 2 == gs.gp.GoodsType || 3 == gs.gp.GoodsType){
g.drawImage(gi.GoodsImg[gs.gp.GoodsType - 1], gs.gp.GoodsX , gs.gp.GoodsY , Graphics.TOP | Graphics.LEFT);
}
gc.flushGraphics();
}
/**==============================**
** 绘画游戏:GameOver()
**==============================**/
private void GameOver(Graphics g){
g.drawImage(gi.GameOverImg, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(0xdd1100);
g.drawString("您的得分: "+gs.gp.Scores,48,180,Graphics.TOP | Graphics.LEFT);
gc.flushGraphics();
}
/**==============================**
** 绘画游戏:drawGame
**==============================**/
private void drawGame()
{
Creature co = null;
//清空屏幕
g.setColor(0, 0, 255);
g.fillRect(0, 0, Width, Height);
//绘制背景
g.drawImage(gi.Backgroud[gs.gl.getGreatLevel() - 1][gs.gl.getShareLevel() - 1], 0, 0, Graphics.TOP | Graphics.LEFT);
//显示桢数
g.setColor(255, 0, 0);
// g.drawString(String.valueOf(gs.TickCount), 0, 0, Graphics.LEFT | Graphics.TOP);
g.drawString(String.valueOf(gs.gp.Scores), 0, 0, Graphics.LEFT | Graphics.TOP);
//生命显示
g.drawImage(gi.LifeImg, 160, 2, Graphics.TOP | Graphics.RIGHT);
g.drawString(String.valueOf(gs.gp.LifeNum), 168,2, Graphics.RIGHT | Graphics.TOP);
//绘制对象管理器里的对象
co = (Creature)om.Search(co);
while(co != null)
{
//判断方向
if(co.DrawState){
switch(co.WalkDir)
{
case 0:
co.ImageL.setPosition(co.x, co.y - co.ImageL.getHeight());
co.ImageL.setFrame(co.Action);
co.ImageL.paint(g);
break;
case 1:
co.ImageR.setPosition(co.x, co.y - co.ImageR.getHeight());
co.ImageR.setFrame(co.Action);
co.ImageR.paint(g);
break;
}
}
//检索下一个对象
co = (Creature)om.Search(co);
}
//物品显示
if(gs.gp.GoodsState){
drawGoods(g);
}
//测试
//g.drawString(String.valueOf(gs.gp.Action), gs.gp.x, gs.gp.y, Graphics.LEFT | Graphics.TOP);
gc.flushGraphics();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?