ShowGame.java
上传用户:public
上传日期:2010-01-27
资源大小:484k
文件大小:10k
- package GameDisplay;
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import Draw.*;
- import java.io.*;
- import java.util.*;
- public class ShowGame
- extends GameCanvas
- implements Runnable,CommandListener{
- private int currentx = 85;
- private int currenty = 50; /////////////////玩家的初始位置坐标
- private int screenw;
- private int screenh; ///////////////////////屏幕宽,高
- private int imgh; ///////////////////////////玩家的宽,高
- private int xback;
- private int yback = 0; ////*******************
- private int SleepTimer = 70; /////////////////初始刷新时间
- private int states = 0; /////////////////////设置变量,用于人物图片动画
- private int direct = 1; /////////////////////设置玩家运动方向
- private int changelife = 0;/////////////////减血量
- private int setColor=30;
- private int vx=4;
- private int vy=10; //////////////////玩家左右运动速度
- public int mapNum;
- private int mapLength=1200;/////////每关地图大小
- private String str = ""; /////////////////将所在的层数显示在屏幕上
- public String playerName;
- public int vyback =3; //////////yback的向上速度
- public int floor = 1; /////////////////////所下的层数
- public boolean alive = false; /////////////////////设置条件判断线程是否进行
- public boolean isPause=false;
- Command pauseCmd;
- Command goOnCmd;
- Command exitCmd;
- Command reStartCmd;
- Random i;
- public Image zhangaiImg; /////////////////////障碍物图片
- private Image personImg; ///////////////////玩家图片
- private Image RunningImg;
- private Image RunningImg1;///////////////左右传输带图片
- private Image pauseImg; ///////////////暂停图片
- private Image AlertImg;
- private Image overImg;
- Sprite sp;
- LayerManager mL;
- DrawLife life;
- DrawOther drawOther;
- MapBack back;
- FileTool ft;
- BackSprite run_right;
- BackSprite run_left;
- public ShowGame() {
- super(true);
- try {
- jbInit();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void jbInit() throws Exception {
- try {
- personImg = Image.createImage("/res/man.png");
- zhangaiImg = Image.createImage("/res/zhangai.png");
- RunningImg = Image.createImage("/res/right_running.png");
- RunningImg1 = Image.createImage("/res/left_running.png");
- pauseImg = Image.createImage("/res/pause.png");
- overImg=Image.createImage("/res/over.png");
- }
- catch (Exception ex) {
- }
- i=new Random();
- mapNum=1+i.nextInt(2);
- screenw = this.getWidth();
- screenh = this.getHeight();
- imgh = personImg.getHeight();
- pauseCmd =new Command("暂停",Command.OK,0);
- goOnCmd =new Command("继续",Command.OK,0);
- exitCmd =new Command("退出",Command.EXIT,0);
- reStartCmd=new Command("重新开局",Command.OK,0);
- addCommand(pauseCmd);
- addCommand(exitCmd);
- InitBack();
- life = new DrawLife();
- drawOther = new DrawOther();
- run_right = new BackSprite(RunningImg, 54, 10); //运输带图片
- run_left = new BackSprite(RunningImg1, 54, 10); //运输带图片
- sp = new Sprite(personImg, 20, 18); //玩家图片
- run_right.setPosition(40, 685); ////////////////////////////////两种传输带的位置
- run_left.setPosition(45, 285);
- mL = new LayerManager();
- mL.append(sp);
- mL.append(run_left);
- mL.append(run_right);
- mL.append(back);
- setCommandListener(this);
- }
- public void Initgame() { /////////////////////////加开关以控制游戏开始
- this.alive = true;
- Thread t = new Thread(this);
- t.start();
- }
- public void run() {
- Graphics g = this.getGraphics();
- while (alive) {
- g.setColor(0,0,setColor);
- g.fillRect(0, 0, screenw, screenh);
- run_right.Move();
- run_left.Move();
- Input(g);
- mL.paint(g, 0, 0);
- mL.setViewWindow(xback, yback, screenw, screenh);
- drawOther.DrawSelf(g, screenw, screenh);
- life.DrawLife(g, 0, 4, 50, 8, changelife); ///////////////画玩家的生命值
- MoveMap(g); ///////////////////////////////////////地图轮回
- DrawFooler(g);
- hit(); //////////////////////////////////////////////检测与上钉板的碰撞
- DrawPause(g);
- DrawOver(g);
- flushGraphics();
- //System.out.println("yback:::"+yback);
- try {
- Thread.sleep(SleepTimer);
- }
- catch (InterruptedException ex) {
- System.out.println("error");
- }
- }
- }
- private void InitBack() throws Exception {//////读文件加载地图
- ft = new FileTool("/res/zhangai_"+mapNum+".txt");
- int cx = ft.getCellx();
- int cy = ft.getCelly();
- int maps[] = ft.getMaps();
- back = new MapBack(cx, cy, zhangaiImg, 18, 10, maps);
- }
- private Font CustomFont() { //定制字体//////////设置为下滑线,大字号
- return Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED,
- Font.SIZE_LARGE);
- }
- private void DrawFooler(Graphics g){
- g.setColor(237, 255, 0); ////////////////////////////////画层数所用的字体颜色
- g.setFont(CustomFont()); //设置字体为“大”
- str = "第" + str.valueOf(floor) + "层";
- g.drawString(str,screenw,screenh-16, Graphics.TOP | Graphics.RIGHT); //画所在的层
- }
- private void Input(Graphics g) { ///////////////////////////////////按键加检测
- int gk = this.getKeyStates(); /////////////////只保留左右判断(4,6)
- int speedx = 0;
- if (!isPause) {
- // +暂停键(5),,暂时没想好,再改
- // if((gk&GameCanvas.FIRE_PRESSED)!=0)
- // DrawPause(g);
- if ( (gk & GameCanvas.LEFT_PRESSED) != 0) {
- speedx = -vx;
- direct = 2;
- }
- if ( (gk & GameCanvas.RIGHT_PRESSED) != 0) {
- speedx = vx;
- direct = 4;
- }
- if (gk == 0)
- direct = 0;
- currentx += speedx;
- sp.setPosition(currentx, currenty);
- if (sp.collidesWith(back, true))
- currentx -= speedx;
- if (currentx < 0)
- currentx = -vx;
- if (currentx > screenw - sp.getWidth())
- currentx = screenw - sp.getWidth() + 4;
- currenty += vy;
- sp.setPosition(currentx, currenty);
- if (sp.collidesWith(back, true)) {
- int x1 = sp.getX() / 18;
- int y1 = (sp.getY() + sp.getHeight()) / 10; /////////得到x,y的数组位置
- int pitx = back.getCell(x1, y1); ////////////////////判断碰撞的TiledLayer的具体编号
- if (direct == 0)
- this.states = 0;
- if (pitx == 2) {
- changelife +=6 ; ///////////////////////////////////生命值减6
- }
- currenty -= vy;
- }
- sp.setPosition(currentx, currenty);
- sp.setFrame(direct +(states++) % 2 ); ////////////////设置相应的按键及运动图片
- }
- }
- public void hit() { ///////////////////////////////只能用上边界来检测,图片检测不会用
- if (currenty - yback <= 20) {
- changelife +=10; ///////////////撞到上钉板,生命值减5,玩家自动向下推动超过一个挡板位置
- currenty += 30;
- }
- if (sp.collidesWith(run_right, true)) {
- currenty -= vy;
- currentx += vx-3;
- }
- if (sp.collidesWith(run_left, true)) {
- currenty -=vy;
- currentx += -vx+3;
- }
- sp.setPosition(currentx, currenty); ////////碰到运输带的位置如果解决(解决了,给初始位置加5就好了)
- }
- private void MoveMap(Graphics g) { //////////////////改变地图位置
- yback +=vyback; ////////////////////////////如果变速的话,一定要注意yback是地图高的整数倍之一
- yback = yback % mapLength; ///////////////////////////////地图的续用
- if (yback % mapLength == 0) {
- currenty = 30; ////////////////////////////////它也要注意,应该是玩家向下运动的速度的整数倍
- // if(setColor<240)
- // setColor+=30;//////////////////////////////////每过一关(五层)改变背景颜色
- if (SleepTimer >= 35)
- SleepTimer -= 5; //////////////////////////////用于每变一关(五层),速度变快
- }
- if (yback % (mapLength/5) == 0)
- floor++; ///////////////////////////////////每过150像素,层数加1
- if(yback % (mapLength/40) == 0&&changelife>=2)//每过30个像素,自动加血
- changelife-=2;
- }
- private void DrawPause(Graphics g) {
- if(isPause)
- g.drawImage(pauseImg,60,80,0);
- }
- public void DrawOver(Graphics g){//////////////////////////////////////失败界面
- if (life.isDead() == false ||
- currenty - yback >= 177 - sp.getHeight() / 2) {
- // 四周画直线
- // for(int i=0;i<(screenw-overImg.getWidth())/2;i+=2){
- // g.setColor(255,255,255);
- // g.drawRect(i,i+20,screenw-2*i,screenh-2*i);
- // }
- g.drawImage(overImg,(screenw-overImg.getWidth())/2,
- (screenh-overImg.getHeight())/2+5,Graphics.LEFT|Graphics.TOP);
- this.removeCommand(pauseCmd);
- alive = false;
- addCommand(reStartCmd);
- }
- }
- public void commandAction(Command c, Displayable d)
- {
- if(c==pauseCmd)
- {
- Control.pause();
- removeCommand(pauseCmd);
- addCommand(goOnCmd);
- }
- else if(c==exitCmd)
- {
- alive=false;
- Control.showNameRecord();
- }
- else if(c==goOnCmd)
- {
- removeCommand(goOnCmd);
- addCommand(pauseCmd);
- Control.goOn();
- }
- else if( c==reStartCmd)
- {
- Control.highrecord.matches("Loser",floor);
- Control.quitApp();
- }
- }
- public void setVx(int vx) {
- this.vx = vx;
- }
- public void setVy(int vy) {
- this.vy = vy;
- }
- public void setVyback(int vyback) {
- this.vyback = vyback;
- }
- public void setIsPause(boolean isPause) {
- this.isPause = isPause;
- }
- public void setPlayerName(String playerName) {
- this.playerName = playerName;
- }
- }