package tank.midp.core;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class tankgamecanvas extends gamecanvas implements runnable {
// 控制方向:
private static int index_of_up = 0;
private static int index_of_down = 1;
private static int index_of_left = 3;
private static int index_of_right = 2;
private boolean isplay; // game loop runs when isplay is true
private long delay; // to give thread consistency
private int currentx, currenty; // to hold current position of the 'x'
private int width; // to hold screen width
private int height; // to hold screen height
private sprite spritetank; // our sprite!
// constructor and initialization
public tankgamecanvas() {
super(true);
width = getwidth();
height = getheight();
currentx = width / 2;
currenty = height / 2;
delay = 20;
// init sprite:
try {
image image = image.createimage("/res/img/player1.png"); // 注意路径
spritetank = new sprite(image, 16, 16); // 大小是16x16
}
catch(exception e) { e.printstacktrace(); }
}
// automatically start thread for game loop
public void start() {
isplay = true;
new thread(this).start();
}
public void stop() { isplay = false; }
// main game loop
public void run() {
graphics g = getgraphics();
while (isplay) {
input();
drawscreen(g);
try {
thread.sleep(delay);
}
catch (interruptedexception ie) {}
}
}
// method to handle user inputs
private void input() {
int keystates = getkeystates();
// left
if ((keystates & left_pressed) != 0) {
currentx = math.max(0, currentx - 1);
spritetank.setframe(index_of_left);
}
// right
if ((keystates & right_pressed) !=0 ) {
if ( currentx + 5 < width)
currentx = math.min(width, currentx + 1);
spritetank.setframe(index_of_right);
}
// up
if ((keystates & up_pressed) != 0) {
currenty = math.max(0, currenty - 1);
spritetank.setframe(index_of_up);
}
// down
if ((keystates & down_pressed) !=0) {
if ( currenty + 10 < height)
currenty = math.min(height, currenty + 1);
spritetank.setframe(index_of_down);
}
}
// method to display graphics
private void drawscreen(graphics g) {
g.setcolor(0); // black
g.fillrect(0, 0, getwidth(), getheight());
// 画一个sprite非常简单:
spritetank.setposition(currentx, currenty);
spritetank.paint(g);
flushgraphics();
}
}
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 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器