/*
//background.png
//01.png
*/
/*
*http://blog.programfan.com/blog.asp?author=qq14923349
*程序之巢制作,移动窗口TiledLayr类例子
*图片存在rec目录下,记得在存之前把格式转换成png,具体方法是:
1,点击开始
2,附件
3,图画板
4,打开图片
5,另存为(可以存作PNG了,不要改后续名,否则运行不了)
6,其他工具,可以选用PS,或者ACDSEE等等改
*/
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Wcanvas extends MIDlet{
private Display display;
public void startApp(){
display=Display.getDisplay(this);
new wCanvas(display).start();
}
public void pauseApp(){}
public void destroyApp(boolean unkown){}
//System.out.println("试验");
class wCanvas extends GameCanvas implements Runnable
{
private Display display;
private boolean suspend=false; //暂停
private int frameDelay=100;
private Sprite role;
private TiledLayer background_1,background_2,background_3;
private int []laymap=new int[4];//地图数组
private int roleXspeed,roleYspeed,backgroundXspeed,backgroundYspeend;
private int halfwidth=getWidth();
public wCanvas(Display d){ //contractor构造涵数
super(true);
display=d;
display.setCurrent(this);
}
private void start(){
init();//初始化
toStartThread();//启动线程
}
private void init(){
try{
role=new Sprite(Image.createImage("/01.png"),22,22);
background_1=new TiledLayer(1,4,Image.createImage("/background.png"),88,40);
background_2=new TiledLayer(1,4,Image.createImage("/background.png"),88,40);
background_3=new TiledLayer(1,4,Image.createImage("/background.png"),88,40);
}
catch(Exception e){
System.out.println(e.toString());
}
int []laymap={1,2,1,2};
for(int i=0;i<laymap.length;i++){
int column=i%1;
int row=(i-column)/16;
background_1.setCell(column,row,laymap[i]);
background_1.setPosition(0,0);
role.setPosition(0,0);
}
}
private void toStartThread(){
Thread t=new Thread(this);
t.start();
}
public void run(){
while(!suspend){
Graphics g=getGraphics();
update();//更新和响应按键
judgement();//对ROLE作出判断,同时移动窗口
draw(g);//画图
try{ //频率
Thread.sleep(frameDelay);
}
catch(Exception e){System.out.println(e.toString());}
}
}
private void update(){
role.nextFrame(); //蜜蜂不停地动
int keyState=getKeyStates();
if((keyState & LEFT_PRESSED)!=0 ){
roleXspeed=-2;
role.move(roleXspeed,0);
}
else if((keyState & RIGHT_PRESSED)!=0 ){
roleXspeed=2;
role.move(roleXspeed,0);
}
if((keyState & UP_PRESSED)!=0 ){
roleYspeed=-2;
role.move(0,roleYspeed);
}
else if((keyState & DOWN_PRESSED)!=0 ){
roleYspeed=2;
role.move(0,roleYspeed);
}
}
private void judgement(){
if(role.getX()<0){
role.setPosition(0,role.getY());
}
if(role.getX()>15){
role.setPosition(15,role.getY());
if(background_1!=null)
{
background_1.move(-1,0);
}
}
if(background_1!=null)
if(background_1.getX()==-background_1.getWidth()){
background_1=null;
}
}
private void draw(Graphics g){
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
threebackgroundjudgement();
role.paint(g);
g.setColor(0x000000);
g.drawString("宽为"+getWidth()+" 高为 "+getHeight(),15,70,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
private void threebackgroundjudgement(){
Graphics g=getGraphics();
if(background_1!=null)
{
background_1.paint(g);
}
if(background_2!=null)
{
background_2.paint(g);
}
if(background_3!=null)
{
background_3.paint(g);
}
}
}
}
评论