选择显示字体大小

游戏开发入门


gamemidlet.java

import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import javax.microedition.lcdui.display;


public class gamemidlet extends midlet {
game game;
display display;


public gamemidlet() {
game = new game(this);
display = display.getdisplay(this);
}

protected void startapp() throws midletstatechangeexception {
game.setgamestate(game.logo, 30);
game.start();
display.setcurrent(game);
}

protected void pauseapp() {
notifypaused();
}

protected void destroyapp(boolean _boolean) throws midletstatechangeexception {
notifydestroyed();
}
}

game.java

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;

/**
* <p>title: </p>
*
* <p>description: </p>
*
* <p>copyright: copyright (c) 2005</p>
*
* <p>company: </p>
*
* @author not attributable
* @version 1.0
*/
public class game extends fullcanvas implements runnable ,commandlistener&#123;

private gamemidlet gamemidlet;

private boolean rungame;//游戏是否启动
private int fps;//频率
private int gamestate = -1; //当前游戏状态
/**
* 游戏状态参数
*/

public final byte logo = 0;
public final byte menu = 1;
public final byte about = 2;
public final byte help = 3;
public final byte wait = 4;
public final byte loginform = 5;
public final byte result = 6;
public final byte loading = 7;
public final byte regist = 8;
public final byte game = 9;


private image bufferedimage;//双缓冲图片


private form form;
private command cmdreg, cmdback; // 注册、登陆、返回、修改按钮对象
private textfield tfaccount, tfpassword; // 昵称、密码,textfield对象
/**
* 默认构造方法
*/
public game(gamemidlet midlet) &#123;
gamemidlet = midlet;
int width = 128;
int height = 128;

// create image buffer:
bufferedimage = image.createimage(width, height);
&#125;


protected void keypressed(int keycode) &#123;
if (keycode == -5) &#123;
setgamestate(loginform, 30);
&#125;
&#125;

/**
*设置当前状态
* @param bgamestate byte 状态值
* @param newfps byte 当前状态下的频率
*/

public void setgamestate(int bgamestate,int newfps)&#123;
this.release();
this.gamestate = bgamestate;
this.fps = newfps;
this.init();
&#125;
/**
* 设置当前状态
* @param bgamestate int 状态值
*/
public void setgamestate(int bgamestate)&#123;
this.release();
this.gamestate = bgamestate;
this.fps = 30;
this.init();
&#125;
/**
* 得到当前状态
* @return int
*/
public int getgamestate()&#123;
return this.gamestate;
&#125;

/**
* 游戏线程启动
*/

public void start() &#123;
rungame = true;
thread t = new thread(this);
t.start();
&#125;

/**
* 游戏线程停止
*/
public void stop() &#123;
rungame = false;
&#125;
/**
* 实现runnable接口
*/
public void run() &#123;
graphics g = bufferedimage.getgraphics();//得到graphics绘图对象
long starttime;
int interval = 1000 / this.fps;
byte count = 2;//垃圾回收次数控制
byte n = count;
thread currentthread = thread.currentthread();
while (rungame && currentthread == thread.currentthread()) &#123;
interval = 1000 / this.fps;
starttime = system.currenttimemillis();

this.input();

this.work();

drawscreen(g);
if (gamestate != loginform) &#123;
repaint();//请求刷新屏幕
servicerepaints();
&#125;

waitsomething(starttime, interval);
if (n == 0) &#123;
runtime.getruntime().gc();
n = count;
&#125;
n--;
&#125;
&#125;
/**
* 线程休眠一段时间
* @param loopstarttime long
* @param interval long
*/
protected void waitsomething(long loopstarttime, long interval) &#123;
long time = system.currenttimemillis() - loopstarttime;
if (time < interval) &#123;
try &#123;
thread.sleep(interval - time);
&#125; catch (interruptedexception e) &#123;&#125;
&#125; else &#123;
thread.yield();
&#125;
&#125;

public void paint(graphics g) &#123;
g.drawimage(this.bufferedimage, 0, 0, graphics.top graphics.left);
&#125;
/**
* 当前状态初始化
*/
protected void init() &#123;
switch (getgamestate()) &#123;
case loginform:
initloginform();
break;
default:
break;
&#125;
&#125;


/**
* 当前状态释放资源
*/
protected void release() &#123;
switch (getgamestate()) &#123;
case loginform:
releaseloginform();
break;
default:
break;
&#125;
runtime.getruntime().gc();
&#125;
/**
* 当前状态下的用户输入
*/
protected void input() &#123;
&#125;
/**
* 当前状态下的逻辑处理
*/
protected void work() &#123;
&#125;
/**
* 当前状态下的绘制
* @param g graphics
*/
protected void drawscreen(graphics g) &#123;
switch (getgamestate()) &#123;
case log
g.drawstring("aaa",10,50,0);
break;
default:
break;
&#125;
system.out.println(runtime.getruntime().freememory());
&#125;

private void initloginform()&#123;
form = new form("用户登陆");
tfaccount = new textfield("帐号:", "", 9, textfield.any);
// tfpassword = new textfield("密码:", "", 9, textfield.password);


cmdreg = new command("注册", command.screen, 2);
cmdback = new command("后退", command.back, 3);

form.append(tfaccount);
// form.append(tfpassword);

form.addcommand(cmdreg);
form.addcommand(cmdback);

form.setcommandlistener(this);
gamemidlet.display.setcurrent(form);
&#125;

public void commandaction(command command, displayable displayable) &#123;
if (command == cmdreg) &#123;
setgamestate(logo, 30);
gamemidlet.display.setcurrent(this);
&#125;
&#125;

private void releaseloginform()&#123;
for (int i = form.size() - 1; i >= 0; i --)&#123;
form.delete(i);
&#125;
tfaccount = null;
// tfpassword = null;
cmdback = null;
cmdreg = null;
form = null;
&#125;
&#125;


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons