?? a.java
字號:
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import java.util.Random;
public class a extends Canvas implements Runnable
{
private int state;//游戲狀態
private final int AAA=10;
private final int LOGO=14;
private final int Menu=11;
private final int Play=12;
private final int Puase=13;
private final int GameOver = 15;
private int init;//初始化狀態
private final int Logo=20;
private final int menu=21;
private final int play=22;
private Image image[];
private long firstTime;
Graphics g0;//雙緩沖畫筆
Image myback;
private int k;
main a;
Thread t;
private int f_m;//菜單選擇幀
/*************人物***************/
private int cx;
private int cy;
private int f_n_x;
private int f_n_y;
private int n_npcstate;//未碰撞人物狀態
private int y_npcstate;//碰撞人物狀態
private final int s=30;
private final int j=31;
private final int l_w=32;
private final int r_w=33;
private final int G=1;
private int speed;
private int speed_w;
/*************箱子***************/
private int box_state;
private int box[][];
private int point;//得分
Random r;
public a(main a)
{
setFullScreenMode(true);
this.a=a;
image=new Image[4];
state=AAA;
init = Logo;
t=new Thread(this);
t.start();
r=new Random();
box = new int[60][4];
}
public void aaa()//初始化游戲資源
{
myback = Image.createImage(getWidth(), getHeight());
g0 = myback.getGraphics();
switch(init)
{
case Logo:
image[0]=loadImage("/cmcc.png");
image[1] = loadImage("/bbox50.png");
state=LOGO;
break;
case menu:
clear();
f_m=0;
image[0] = loadImage("/stroe.png");//背景
image[1] = loadImage("/zi.png");//標題
image[2] = loadImage("/mainword.png");//菜單
state = Menu;
break;
case play:
clear();
image[0] = loadImage("/man.png");//人物
image[1] = loadImage("/box.png");//箱子
image[2] = loadImage("/boxPO.png");//破箱子
image[3] = loadImage("/GameOver.png");//結束
cx=(getWidth()-image[0].getWidth()/4)/2;
cy=getHeight()-image[0].getHeight()/5;
for(int i=0;i<box.length;i++)
{
box[i][0] = 180;//0表示box的X坐標
box[i][1] = 78;//1表示box的Y坐標
box[i][2] = 0;//0表示不出現,1表示出現
}
f_n_x=3;
f_n_y=3;
speed = -8;
speed_w = 3;
box_state = 1;
n_npcstate=s;
state=Play;
break;
}
}
/************進入游戲***********/
public void logo1()
{
Clcd();
g0.drawImage(image[0],(getWidth() - image[0].getWidth()) / 2, (getHeight() - image[0].getHeight()) / 2,0);
repaint();
sleep(1000);
}
public void logo2()
{
Clcd();
g0.drawImage(image[1],(getWidth() - image[1].getWidth()) / 2, (getHeight() - image[1].getHeight()) / 2,0);
repaint();
sleep(1000);
}
public void drawmenu()//菜單繪制
{
Clcd();
g0.drawImage(image[0], 0, 0, 0);
g0.drawImage(image[1],(getWidth()-image[1].getWidth())/2,image[1].getHeight(),0);
g0.setColor(255,255,255);
g0.drawRect((getWidth() - image[2].getWidth()) / 2 -6, (getHeight() - image[2].getHeight()) / 2 - 6, image[2].getWidth() + 9, image[2].getHeight() + 9);
g0.setColor(0x00225599);
g0.fillRect((getWidth()-image[2].getWidth())/2-5,(getHeight()-image[2].getHeight())/2-5,image[2].getWidth()+8,image[2].getHeight()+8);
g0.drawImage(image[2], image[2].getWidth(), image[2].getHeight(),0);
g0.setColor(255,0,0);
g0.drawRect(image[2].getWidth()-1, (image[2].getHeight()-1)+f_m*(image[2].getHeight()/5), image[2].getWidth()+2, (image[2].getHeight()+2)/5);
repaint();
}
public void ctrMenu()//菜單控制
{
if(k==0)
return;
if(up())
{
if(f_m==0)
{
f_m=4;
}
else
{
f_m--;
}
}
else if(down())
{
if(f_m==4)
{
f_m=0;
}
else
{
f_m++;
}
}
else if(fire())
{
if(f_m==0)
{
init=play;
state=AAA;
}else if(f_m==4)
{
a.destroyApp(true);
}
}
k=0;
}
/************游戲界面***********/
public void drawplay()
{
Clcd();
g0.setColor(0,0,255);
g0.fillRect(0,0,getWidth(),78);
g0.setColor(0x88AAAAAA);
g0.fillRect(0,78,getWidth(),142);
g0.setClip(cx,cy,image[0].getWidth()/4,image[0].getHeight()/5);
g0.drawImage(image[0],cx-image[0].getWidth()/4*f_n_x,cy-image[0].getHeight()/5*f_n_y,0);
g0.setClip(0,0,getWidth(),getHeight());
for (int i = 0; i < box.length; i++)
{
if (box[i][2] == 1)
{
g0.drawImage(image[1], box[i][0], box[i][1], 0);
}
}
repaint();
}
public void drawOver()
{
g0.setColor(0);
g0.fillRect(0,0,getWidth(),getHeight());
g0.drawImage(image[3],(getWidth()-image[3].getWidth())/2,(getHeight()-image[3].getHeight())/2,0);
}
public void boxMove()//箱子運動狀態
{
int Rad;
long lastTime=System.currentTimeMillis();
if(lastTime-firstTime>3000)//每固定時間出現一個箱子
{
Rad = (r.nextInt() >>> 1) % 162;
firstTime=System.currentTimeMillis();
for (int i = 0; i < box.length; i++)
{
if (box[i][2] == 0)
{
box[i][2] = 1;
box[i][3] = Rad;
break;
}
}
}
for (int i = 0; i < box.length; i++)//遍厲數組
{
if (box[i][2] == 1)//尋找出現狀態的箱子
{
if (box[i][0] > -18 && box[i][0] != box[i][3])
{
box[i][0]--;
}
if (box[i][0] == box[i][3])
{
if (box[i][1] < getHeight() - 18)
{
box[i][1] += 2;
}
}
repaint();
}
}
}
public void n_npc_state()//未碰撞游戲角色狀態
{
if (speed > -8)
{
n_npcstate = j;
}
switch(n_npcstate)
{
case s:///////////////////站立
f_n_x=3;
f_n_y=3;
break;
case j:///////////////////跳躍
if (speed>-8&&cy == getHeight() - image[0].getHeight() / 5)
{
speed = -8;
n_npcstate = s;
}
else
{
speed += G;
cy += speed;
if (speed <= 0)
{
f_n_x = 0;
}
else if (speed > 0)
{
f_n_x = 1;
}
}
break;
case l_w://///////////////左走
if (k == 0)
{
n_npcstate = s;
return;
}
f_n_y = 0;
if (f_n_x == 3)
{
f_n_x = 0;
}
else
{
f_n_x++;
}
if (cx > 0)
{
cx -= speed_w;
}
break;
case r_w://///////////////右走
if (k == 0)
{
n_npcstate = s;
return;
}
f_n_y = 1;
if (f_n_x == 3)
{
f_n_x = 0;
}
else
{
f_n_x++;
}
if (cx < getWidth() - image[0].getWidth()/4)
{
cx += speed_w;
}
break;
}
}
public void n_ctrplay()//未碰撞控制
{
if(k==0)
return;
if(up())
{
n_npcstate = j;
}
else if (left())
{
if (speed > -8)
{
f_n_y = 2;
if (cx > 0)
{
cx -= speed_w;
}
}
else
{
n_npcstate = l_w;
}
}
else if (right())
{
if (speed > -8)
{
f_n_y = 4;
if (cx < getWidth() - image[0].getWidth() / 4)
{
cx += speed_w;
}
}
else
{
n_npcstate = r_w;
}
}
}
public void y_npc_state()//碰撞狀態
{
switch (y_npcstate)
{
case 1://箱子頂爛狀態
break;
}
}
public void ccc()//碰撞判斷
{
for (int i = 0; i < 60; i++)
{
if(cx<box[i][0])
{
if (top1_isIntersectingLine(cx, cy, image[0].getWidth() / 4,box[i][0], box[i][1] + 18, 18 ))
{
if (speed > -8 && speed < 0)
{
box[i][2] = 0;
y_npcstate = 1;
y_npc_state();
}
else
{
state = GameOver;
step();
}
}
}
else if (cx >= box[i][0])
{
if (top2_isIntersectingLine(cx, cy, image[0].getWidth() / 4,box[i][0], box[i][1] + 18, 18 ))
{
if (speed > -8 && speed < 0)
{
box[i][2] = 0;
y_npcstate = 1;
y_npc_state();
}
else
{
state = GameOver;
step();
}
}
}
if (l1_isIntersectingLine(cx, cy, image[0].getHeight() / 5, box[i][0], box[i][1], 18))
{
if (box[i][0] > 0)
{
speed_w = 2;
y_npcstate = 2;
}
else
{
speed_w = 0;
}
}
//if (top_isIntersectingLine(box[i][0], box[i][1], 18,box[i][0], box[i][1], 18))//箱子和箱子
//{ }
}
}
public void step()//游戲狀態
{
switch(state)
{
case AAA:
aaa();
break;
case LOGO:
logo1();
logo2();
init=menu;
state = AAA;
break;
case Menu:
ctrMenu();
drawmenu();
break;
case Play:
ccc();
boxMove();
n_ctrplay();
n_npc_state();
drawplay();
break;
case Puase:
break;
case GameOver:
for (int i = 0; i <= getWidth(); i ++)
{
g0.setClip(0, 0, i, getHeight());
drawOver();
repaint();
}
break;
}
}
public void run()//線程
{
while(true)
{
step();
try
{
Thread.sleep(60);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/*********將緩沖圖片繪制在桌面********/
public void paint(Graphics g)
{
g.drawImage(myback, 0, 0, 0);
}
/***************工具***************/
protected void keyPressed(int keyCode)
{
k = keyCode;
}
protected void keyReleased(int keyCode)
{
k = 0;
}
public boolean left()
{
if(LEFT==getGameAction(k))
return true;
return false;
}
public boolean right()
{
if(RIGHT==getGameAction(k))
return true;
return false;
}
public boolean fire()
{
if(FIRE==getGameAction(k))
return true;
return false;
}
public boolean up()
{
if(UP==getGameAction(k))
return true;
return false;
}
public boolean down()
{
if(DOWN==getGameAction(k))
return true;
return false;
}
public void Clcd()//清屏
{
g0.setColor(255, 255, 255);
g0.fillRect(0,0,getWidth(),getHeight());
}
/*************線程睡眠**************/
protected void sleep(long l)
{
if (l <= 0)
return;
try
{
Thread.sleep(l);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*************導入圖片**************/
protected Image loadImage(String str)
{
Image image = null;
try
{
image = Image.createImage(str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return image;
}
}
public void clear()//數組清空
{
for (int i = 0; i < image.length; i++)
{
if (image[i] != null)
{
image[i] = null;
}
}
System.gc();
}
/*************碰撞檢測**************/
public static final boolean top1_isIntersectingLine(int ax, int ay, int aw, int bx, int by, int bw)//底與上碰撞,a在b前
{
if (((bx+bw)-ax)<34 && Math.abs(ay - by) < 2)
return true;
return false;
}
public static final boolean top2_isIntersectingLine(int ax, int ay, int aw, int bx, int by, int bw)//底與上碰撞,a在b后
{
if (((ax + aw) - bx) < 34 && Math.abs(ay - by) < 2)
return true;
return false;
}
public static final boolean l1_isIntersectingLine(int ax, int ay, int ah, int bx, int by, int bh)//邊碰撞,a在b后
{
if (by-(ay+ah)< 34 && Math.abs(ax - bx) < 2)
return true;
return false;
}
public static final boolean l2_isIntersectingLine(int ax, int ay, int ah, int bx, int by, int bh)//邊碰撞,a在b前
{
if (ay - (by + bh) < 34 && Math.abs(bx - ax) < 2)
return true;
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -