搜索
您的当前位置:首页正文

超级玛丽

来源:易榕旅网
山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

实验十二 手机游戏编程

一、实验目的及任务

1、掌握图层类,Sprite类,TiledLayer类 2、RMS管理

二、实验环境

1、J2ME Wireless Tookit

2、任意一个java源代码开发工具,如Eclipse

三、实验步骤

制作超级玛丽手机版。至少一关。有游戏进入界面。有相应的得分显示。 *************源代码************* package ch2;

import java.io.IOException; import java.io.InputStream;

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*;

import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.game.Sprite;

public class MenuGame extends MIDlet implements CommandListener{ private Display dis; private GamemenuDemo gmd; private Command cmdOK,cmdstop,cmdgo; private Command cmdExit; private MaLiGameDemo mgd; Player player; public MenuGame() { mgd = new MaLiGameDemo();

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

}

gmd=new GamemenuDemo();

cmdOK=new Command(\"开始\cmdExit=new Command(\"退出\cmdstop=new Command(\"暂停\

protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub }

protected void pauseApp() { // TODO Auto-generated method stub }

protected void startApp() throws MIDletStateChangeException { dis=Display.getDisplay(this); gmd.addCommand(cmdOK); gmd.addCommand(cmdExit); mgd.addCommand(cmdstop); mgd.setCommandListener(this); gmd.setCommandListener(this); dis.setCurrent(gmd); Thread th=new Thread(gmd); th.start(); }

class GamemenuDemo extends GameCanvas implements Runnable{ Image game_menu01,game_menu02; Graphics gra; boolean RUN=true; Sprite sp_game01,sp_game02; protected GamemenuDemo() { super(true); try { game_menu01=Image.createImage(\"/cai01.png\"); game_menu02=Image.createImage(\"/cai02.png\"); } catch (IOException e) { // TODO Auto-generated catch block

e.printStackTrace();

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

}

}

gra=this.getGraphics();

sp_game01=new Sprite(game_menu01); sp_game01.setPosition(90, 80);

sp_game02=new Sprite(game_menu02); sp_game02.setPosition(30, 200);

public void run() { playMIDI(); repaint(); while(RUN){ gra.setColor(0, 255, 255); gra.fillRect(0, 0, this.getWidth(), this.getHeight()); sp_game01.paint(gra); sp_game02.paint(gra); this.flushGraphics(); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

}

public void playMIDI(){ try { InputStream is=getClass().getResourceAsStream(\"/menu.mid\"); player=Manager.createPlayer(is,\"audio/midi\"); player.realize(); player.prefetch(); player.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MediaException e) { // TODO Auto-generated catch block e.printStackTrace(); }

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

} } public void commandAction(Command cmd, Displayable dis) { if(cmd==cmdOK){ Display dis1=Display.getDisplay(this); dis1.setCurrent(mgd); Thread t=new Thread(mgd); t.start(); try { player.stop(); } catch (MediaException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(cmd==cmdstop){ try { destroyApp(false); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.notifyDestroyed(); } if(cmd==cmdExit){ try { destroyApp(false); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.notifyDestroyed(); } } } }

package ch2;

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

import java.io.IOException; import java.io.InputStream;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.game.LayerManager; import javax.microedition.lcdui.game.Sprite; import javax.microedition.lcdui.game.TiledLayer; import javax.microedition.media.Manager;

import javax.microedition.media.MediaException; import javax.microedition.media.Player;

class MaLiGameDemo extends GameCanvas implements Runnable{ boolean RUN = true; boolean running=true; Graphics gra; TiledLayer til_caodi,til_qianxz,til_zhuan,til_shitou; Image img_caodi,img_malizou,img_qianxz,img_yun; Image img_zhuan,img_shitou; int cells_map[][],cells_peng[][]; Sprite sp_malizou,sp_yun,sp_yun1; int sp_malizou_X=5,sp_malizou_Y=200; LayerManager lm; protected MaLiGameDemo() { super(true); try { img_caodi = Image.createImage(\"/1.jpg\"); img_shitou=Image.createImage(\"/2.jpg\"); img_qianxz=Image.createImage(\"/3.jpg\"); img_malizou=Image.createImage(\"/6.png\"); img_yun=Image.createImage(\"/7.png\"); img_zhuan=Image.createImage(\"/8.jpg\"); } catch (IOException e) { e.printStackTrace(); }

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

gra = this.getGraphics(); lm=new LayerManager(); til_caodi = new TiledLayer(50, 15, img_caodi, 20,20); til_shitou=new TiledLayer(50,15,img_shitou,20,20); til_qianxz=new TiledLayer(50,12,img_qianxz,20,20); til_zhuan=new TiledLayer(50,12,img_zhuan,20,20); sp_malizou=new Sprite(img_malizou,30,30); sp_malizou.setPosition(sp_malizou_X, sp_malizou_Y); sp_yun=new Sprite(img_yun); sp_yun.setPosition(30, 30); sp_yun1=new Sprite(img_yun); sp_yun1.setPosition(200, 60); int[] OK = { 0 }; sp_malizou.setFrameSequence(OK); cells_map = new int[][] { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 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ell(nRow, nCol, cells_map[nCol][nRow]); if (cells_map[nCol][nRow] == 2) til_shitou.setCell(nRow, nCol, cells_map[nCol][nRow]); } } for (int nRow = 0; nRow < 50; nRow++) {

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

for (int nCol = 0; nCol < 12; nCol++) { if (cells_peng[nCol][nRow] == 1) til_qianxz.setCell(nRow, nCol, cells_peng[nCol][nRow]); else til_zhuan.setCell(nRow, nCol, cells_peng[nCol][nRow]); } } } public void run() { playMIDI(); while (RUN) { gra.setColor(0, 140, 255); gra.fillRect(0, 0, this.getWidth(), this.getHeight()); //检测碰撞 if(sp_malizou.collidesWith(til_caodi, true)||sp_malizou.collidesWith(til_shitou, true)||sp_malizou.collidesWith(til_qianxz, true)||sp_malizou.collidesWith(til_zhuan, true)){ running=false; } if(running){ sp_malizou.move(0, 3); }else{ sp_malizou.move(0, -3); } if(sp_malizou.collidesWith(til_caodi, false)||sp_malizou.collidesWith(til_shitou, false)||sp_malizou.collidesWith(til_qianxz, false)||sp_malizou.collidesWith(til_zhuan, false)){ running=true; }if(running){ sp_malizou.move(0, 3); }else{ sp_malizou.move(0, -3); } input(); lm.append(sp_yun); lm.append(sp_yun1); lm.append(til_zhuan); lm.append(til_caodi); lm.append(til_qianxz); lm.append(sp_malizou); lm.append(til_shitou);

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

}

lm.paint(gra, 0, 0); setViewWindow(); this.flushGraphics(); sp_malizou.nextFrame(); try { Thread.sleep(100);

} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }

}

private void input() { int state = getKeyStates(); if ((state & LEFT_PRESSED) != 0) { sp_malizou.move(-6, 0); int[] left = { 0, 1}; sp_malizou.setFrameSequence(left);

}

if ((state & RIGHT_PRESSED) != 0) { sp_malizou.move(+6 ,0); int[] right = { 0, 1}; sp_malizou.setFrameSequence(right); }

if ((state & UP_PRESSED) != 0) { if(running){ sp_malizou.move(0, -24); } } }

//设置可视窗口

public void setViewWindow(){ if(lm==null) return; int nX=sp_malizou.getRefPixelX()-getWidth()/2; if(nX<0) nX=0;

山东建筑大学 计算机学院 实验报告

班级:软件102 姓名:杨大鑫 学号:2010111171 实验成绩: 课程:javaME 同组者: 实验日期:

lm.setViewWindow(nX, 0, getWidth(), getHeight()); } public void playMIDI(){ try { InputStream is=getClass().getResourceAsStream(\"/gameback01.mid\"); Player player=Manager.createPlayer(is,\"audio/midi\"); player.realize(); player.prefetch(); player.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MediaException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

******************图片****************

在这说明下,由于音乐没法复制,说以音乐就没弄上来,所以您们在运行的时候可以把音乐部分删除了,这是我做的一个小小的课题实验,供大家参考学习,谢谢~~~希望能对你有帮助,大家有什么疑问可以给我的QQ1074156299发临时会话

因篇幅问题不能全部显示,请点此查看更多更全内容

Top