图片:
现在我已经完全被手机游戏开发所迷住了.
本身就对写游戏充满着兴趣,没想到J2ME本身就是专门为游戏而开发的
她比J2SE下开发游戏简单的不知有几倍!!!
基本上几个重要的方法她都帮你写好了,比如双缓冲技术,层,砖块,冲撞检测,精灵...等等,真的太帮了!!!
有了J2ME使我如虎添翼,再也不要在J2SE下写底层的东西了:P上面几个技术就几个方法痛痛搞定.实在佩服SUN公司的伟大杰作.
对了告诉大家一个坏消息,在文化学J2ME的时候我看到大家都买了QD手机
QD手机他只支持MIDP 1.0 !!!
所以双缓冲技术,层,砖块,冲撞检测,精灵,如果要实现起来就要自己写方法了
QD不支持MIDP 2.0 ~~~~
-------------------------------------
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class onegame extends MIDlet implements CommandListener
{
private oneCanvas canvas;
public void startApp()
{
if(canvas==null)
{
canvas=new oneCanvas(Display.getDisplay(this));
Command exitCommand=new Command("EXIT",Command.EXIT,0);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
}
canvas.start();
}
public void commandAction(Command c,Displayable d)
{
if(c.getCommandType()==Command.EXIT)
{
destroyApp(true);
notifyDestroyed();
}
}
public void destroyApp(boolean b)
{
canvas.stop();
}
public void pauseApp()
{
}
}
-------------------------------------
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
public class oneCanvas extends GameCanvas implements Runnable
{
private Display display;
private int frameWidth,frameHeight;
private int SpriteWidth,SpriteHeight;
private int Score;
private int life;
private Image back,head;
private Sprite mySprite;
private Sprite[] other=new Sprite[4];
private int[] otherSpeed=new int[4];
private long delay;
private boolean deal,sleep;
private Thread t;
public oneCanvas(Display d)
{
super(true);
display=d;
delay=33;
sleep=false;
deal=false;
}
public void start()
{
display.setCurrent(this);
try
{
back=Image.createImage("/back.png"); //读入图片
head=Image.createImage("/me.png");
mySprite=new Sprite(Image.createImage("/m.png"),16,20);
mySprite.setPosition(5,100);
other[0]=new Sprite(Image.createImage("/a1.png"));
other[0].setPosition(32,50);
otherSpeed[0]=4;
other[1]=new Sprite(Image.createImage("/a2.png"));
other[1].setPosition(80,150);
otherSpeed[1]=2;
other[2]=new Sprite(Image.createImage("/a3.png"));
other[2].setPosition(120,180);
otherSpeed[2]=-1;
other[3]=new Sprite(Image.createImage("/a4.png"));
other[3].setPosition(160,10);
otherSpeed[3]=-4;
}
catch(IOException e)
{
e.printStackTrace();
}
life=3;
Score=0;
frameWidth=getWidth();
frameHeight=getHeight();
t=new Thread(this);
t.start();
}
public void run()
{
Graphics g=getGraphics();
while(!sleep)
{
updraw();
draw(g);
try
{
Thread.sleep(delay);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void stop()
{
sleep=true;
}
public void updraw()
{
if(deal)
{
int keyState=getKeyStates();
if((keyState & FIRE_PRESSED) != 0)
{
mySprite.setPosition(5,100);
deal=false;
Score=0;
life=3;
}
return;
}
int key=getKeyStates();
if((key & LEFT_PRESSED) != 0)
{
mySprite.move(-1,0);
mySprite.nextFrame();
}
else if((key & RIGHT_PRESSED) != 0)
{
mySprite.move(1,0);
mySprite.nextFrame();
}
else if((key & UP_PRESSED) != 0)
{
mySprite.move(0,-1);
mySprite.nextFrame();
}
else if((key & DOWN_PRESSED) != 0)
{
mySprite.move(0,1);
mySprite.nextFrame();
}
bound(mySprite,false);
if(mySprite.getX()>220)
{
Score+=100;
mySprite.setPosition(5,100);
}
for(int i=0;i<4;i++)
{
other.move(0,otherSpeed);
bound(other,true);
if(mySprite.collidesWith(other,true))
{
--life;
if(life==0)
{
deal=true;
}
else
mySprite.setPosition(5,100);
}
}
}
public void bound(Sprite s,boolean tf)
{
if(tf) //敵人的
{
if(s.getY() >= frameHeight)
s.setPosition(s.getX(),-s.getHeight());
else if(s.getY() <= -s.getHeight())
s.setPosition(s.getX(),frameHeight);
}
else //自己的
{
if(s.getX() <= 0)
s.setPosition(0,s.getY());
if(s.getY() <= 0)
s.setPosition(s.getX(),0);
else if(s.getY()+s.getHeight() >= frameHeight)
s.setPosition(s.getX(),frameHeight-s.getHeight());
}
}
public void draw(Graphics g)
{
g.drawImage(back,0,0,Graphics.TOP | Graphics.LEFT);
mySprite.paint(g);
for(int i=0;i<4;i++)
{
other.paint(g);
}
for(int i=1;i<=life;i++)
{
g.drawImage(head,225,200+i*20,Graphics.TOP | Graphics.LEFT);
}
if(deal)
{
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString("游戏结束",120,100,Graphics.TOP | Graphics.HCENTER);
g.drawString("你的得分是:"+Score,120,150,Graphics.TOP | Graphics.HCENTER);
g.drawString("作者:上海黑暗之龙 2005/10/5",120,180,Graphics.TOP | Graphics.HCENTER);
}
flushGraphics();
}
}
-------------------------------------
希望大家看了回贴,你们的回贴就是对我最大的鼓励!
期待下次游戏课程-----<<急速赛车>>