/**
* @(#)MaliCanvas.java
* Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
* @version 1.0, 10/05/2004
* @author 饶荣庆
* @author 余煜辉
*/
package com.wuhua.mali;
import java.util.Timer;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class MaliCanvas extends GameCanvas implements CommandListener
{
//---------------------------------------------------------
//属性字段
/**
*定义一个屏幕对象用于显示游戏窗体
*/
public Display display;
/**
*游戏菜单类
*/
public WelcomeToGame welcomeToGame;
/**
*玛莉类
*/
public Mali mali = null;
/**
*游戏线程类
*/
public GameThread game;
/**
*栏杆类
*/
public Parapet parapet = null;
/**
*A Timer
*/
private Timer timer;
private MillisecondTask mt;
/**
*启动与暂停变量,来控制时间
*/
public static boolean isStart;
private static boolean isPause;
/**
*定义游戏3个软键用来控制游戏
*/
private Command goCommand = null;
private Command pauseCommand = null;
private Command stopCommand = null;
private Command backCommand = null;
/**
*定义屏幕宽度变量
*/
public int DISP_WIDTH = getWidth();
/**
*定义屏幕高度变量
*/
public int DISP_HEIGHT = getHeight();
/**
*画布的水平位置
*/
public static int CANVAS_X;
/**
*画布的垂直位置
*/
public static int CANVAS_Y;
/**
*定义图象玛莉前进贴的顺序
*/
private int order;
/**
*定义玛莉跳跃贴的顺序
*/
private int jumpOrder = 1;
/**
*定义判断玛莉是否跳过
*/
private boolean isJump;
/**
*玛莉跑的路程
*/
private int distance;
/*时间格式字符串*/
private String theTime;
//----------------------------------------------------------
// 初始化
/**
* standard constructor.
*/
public MaliCanvas()
{
super(true);
try
{
this.mali = new Mali(10, (DISP_HEIGHT/4) * 3 - 24);
}
catch(Exception e)
{}
try
{
this.parapet = new Parapet(DISP_WIDTH - 10, (DISP_HEIGHT/4) * 3);
}
catch(Exception e)
{
}
/*用来计算玛莉赛跑用掉的时间*/
this.timer = new Timer();
this.mt = new MillisecondTask();
/*初始化时间格式*/
this.theTime = "00:00";
this.game= new GameThread(this);
this.drawScreen(this.getGraphics()); //初始化游戏界面
this.goCommand = new Command("Go", Command.OK, 2);
this.pauseCommand = new Command("Pause", Command.OK, 2);
this.stopCommand = new Command("Stop", Command.OK, 2);
this.backCommand = new Command("Back", Command.BACK, 2);
this.addCommand(goCommand);
this.addCommand(backCommand);
this.setCommandListener(this);
//this.maliManager = new MaliManager(0,0, DISP_WIDTH, DISP_HEIGHT);
}
//----------------------------------------------------------
// 方法
public void showForm(Display display, WelcomeToGame welcomeToGame)// GameMenu gameMenu) //显示窗体
{
this.welcomeToGame = welcomeToGame;
this.display = display;
this.display.setCurrent(this);
}
/**
*返回图形给线程用的方法
*/
public Graphics getGraph()
{
return this.getGraphics();
}
/**
*处理用户按件游戏事件的方法
*/
public void input()
{
/*获得按键代号*/
int keyStates = getKeyStates();
// Left
if ((keyStates & RIGHT_PRESSED) != 0)
{
this.down();
this.maliAction();
this.parapetAction();
//this.checkCollision();
}
// Right
if ((keyStates & LEFT_PRESSED) !=0 )
{
this.maliLeftDecelerate(); //减速
}
// Up
if ((keyStates & UP_PRESSED) != 0)
{
this.jump();
}
if (this.distance == 110) //等于110米游戏结束
{
this.game.requestStop(); //停止游戏
this.removeCommand(pauseCommand);
this.addCommand(goCommand);
}
}
/**
*用来描述玛莉的运动状态
*/
public void maliAction()
{
mali.setFrame(order);
/*检查有没有跟栏杆碰撞*/
//if (this.parapet.getRefPixelX() > DISP_WIDTH/3)
//{
this.checkCollision();
//}
/*玛莉加速到一定位置时保持哪个位置*/
if (mali.getRefPixelX() < DISP_WIDTH/3)
{
mali.advance();
}
if (game.delay > 30)
{
game.delay = game.delay - 3; //延迟减少来显示加速
}
/*假如栏杆出了屏幕则创建一个新的,并显示出来*/
if (parapet.getRefPixelX() < 0)
{
/*假如一个栏杆消失了则玛莉跑的路程加10*/
this.distance = this.distance + 10;
try
{
this.parapet = new Parapet(DISP_WIDTH, (DISP_HEIGHT/4) * 3);
}
catch(Exception e)
{
}
/*当一个栏杆消失后,玛莉的速度加快,为了保持玛莉与栏杆的相对速度,所以栏杆的速度加快*/
this.parapetAction(); //保持玛莉与栏杆的相对运动
}
order++; //动画的贴号
if (order>3)
{
order=0;
}
}
/**
*栏杆的移动速度方法
*/
public void parapetAction()
{
this.parapet.advance();
this.parapet.accelerate(); //加速
}
/**
*玛莉遇到障碍时减速
*/
public void maliDecelerate()
{
this.game.delay = 60; //用延迟来显示玛莉减速
this.parapet.advance();
this.parapet.decelerate(); //减速
}
/**
*按向左键时候玛莉减速
*/
public void maliLeftDecelerate()
{
mali.setFrame(order);
if (game.delay < 80)
{
game.delay = game.delay + 5; //延迟减少来显示加速
}
order++; //动画的贴号
if (order >= 3)
{
order = 2; //玛莉停下来的时候固定的贴号
}
this.parapet.advance(); //栏杆前进
this.parapet.decelerate(); //减速
}
/**
*在画布里实现玛莉跳跃的方法
*/
public void jump()
{
if (!isJump)
{
this.mali.setFrame(0);
this.parapetAction(); //给栏杆运动使玛莉产生跳跃的效果
this.mali.jump();
}
isJump = true;
this.checkCollision();
}
/**
*在画布实现玛莉下降的方法
*/
public void down()
{
if (isJump)
{
this.parapetAction(); //给栏杆运动产生跳跃的效果
this.parapetAction();
/*用来延迟运行*/
for (int i = 0; i < 5001; i++ )
{
if (i == 5000 && isJump)
{
this.mali.down();
this.isJump = false;
}
}
}
}
/**
*碰撞算法
*/
public void checkCollision()
{
if (this.isJump == true)
{
}
else if (this.parapet.getRefPixelX() > DISP_WIDTH/3 && this.isJump == false )
{
if(this.mali.checkCollision(this.parapet))
{
this.maliDecelerate();//玛莉减速
this.parapet.setFrame(1); //假如被玛莉碰到,则栏杆倒下
}
}
/*if (this.)
{
}*/
}
/*实现记时器的方法*/
public void clock()
{
timer.schedule(mt, 1, 1); //锁定时间进度为0.001秒,并放置这个时间隔要运行的程序。
}
/**
*格式化时间的方法
*/
public void formatTime(int l)
{
int sec = l / 60;
int millisec = l % 60;
if (sec < 10)
{
theTime = "0" + sec + ":";
}
else
{
theTime = sec + ":";
}
if (millisec < 10)
{
theTime = theTime + "0" + millisec ;
}
else
{
theTime = theTime + millisec ;
}
}
/**
*此方法是用来画出玛莉与栏杆
*/
public void drawScreen(Graphics g)
{
/*背景颜色*/
// g.setColor(255, 255,255); // black
g.fillRect(0, 0, DISP_WIDTH, DISP_HEIGHT );
g.setColor(0);
g.drawString("Time:", DISP_WIDTH/2, 50, Graphics.LEFT | Graphics.TOP);
g.drawString("Distance: " + " meter", DISP_WIDTH/2 - 100, 50, Graphics.LEFT | Graphics.TOP);
/*显示时间与路程*/
this.formatTime(mt.getTime());
g.setColor(255);
g.drawString(" " + theTime, DISP_WIDTH/2, 50, Graphics.LEFT | Graphics.TOP);
g.drawString(" " + distance , DISP_WIDTH/2 - 100, 50, Graphics.LEFT | Graphics.TOP);
/*跑道的
评论0