//img3.png
//img2.png
//img1.png
//tree.png
//man.png
/*
*http://blog.programfan.com/blog.asp?author=qq14923349
*程序之巢制作 @J2ME计时器@
*如果有图片
*图片存在rec目录下,记得在存之前把格式转换成png,具体方法是:
1,点击开始
2,附件
3,图画板
4,打开图片
5,另存为(可以存作PNG了,不要直接改后续名,否则运行不了)
6,其他工具,可以选用PS,或者ACDSEE等等改
*/
/*
人肉小炒(418138484) (2006-12-02 12:34:54)
没有菜单
没有帮助
没有退出游戏
没有关卡
寻路太差
可控人物跟npc没有明显区分
玩了一会居然跑到屏幕外面去了
*/
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class Saver extends MIDlet
{
private SaverCanvas savercanvas;
public void startApp(){
savercanvas=new SaverCanvas(Display.getDisplay(this));
savercanvas.start();
}
public void pauseApp(){}
public void destroyApp(boolean e){}
class SaverCanvas extends GameCanvas implements Runnable{
private Display display;
private Sprite xman;
private TiledLayer background;
private int Xbackground,Ybackground;
private int xmanX=2,xmanY=2;//主角的XY坐标,速度
private int xman_now_X,xman_now_Y;//主角现在的XY坐标
private Random rand;
private Sprite enemy1,enemy2,enemy3,enemy4;//不喜欢用数组
private int enemy_now_X,enemy_now_Y;//敌人现在的坐标
private Sprite burst_first,burst_second,burst_third;//为什么不用一个精灵?因为我手上没PS合成-!
private int lift=3,killenemy,score;
private boolean end=true;
//private Timer bursttimer;//任务
//private BurstTask bursttask;//用于爆炸
private Burst burst;
public SaverCanvas(Display d){
super(true);
display=d;
display.setCurrent(this);
}
private void start(){
try{
xman=new Sprite(Image.createImage("/man.png"),8,8);
enemy1=new Sprite(Image.createImage("/man1.png"),8,8);
enemy2=new Sprite(Image.createImage("/man1.png"),8,8);
enemy3=new Sprite(Image.createImage("/man1.png"),8,8);
enemy4=new Sprite(Image.createImage("/man1.png"),8,8);
background=new TiledLayer(15,15,Image.createImage("/tree.png"),8,8);
burst_first=new Sprite(Image.createImage("/img1.png"),8,8);
burst_second=new Sprite(Image.createImage("/img2.png"),8,8);
burst_third=new Sprite(Image.createImage("/img3.png"),16,16);
//一开始没爆炸
burst_first.setVisible(false);
burst_second.setVisible(false);
burst_third.setVisible(false);
setcell();
enemy_random_raise();
}
catch(Exception e){
}
Thread t=new Thread(this);
t.start();
}
/*
随机出现敌人
*/
private void enemy_random_raise(){//随机敌人
int enemyX,enemyY;
rand=new Random();
do{
enemyX=Math.abs(rand.nextInt())%15*8;//屏幕大概就15*8那么大吧,只要是8倍数,而且不在背景
enemyY=Math.abs(rand.nextInt())%15*8;
enemy1.setPosition(enemyX,enemyY);
}while(enemy1.collidesWith(background,false));
do{
enemyX=Math.abs(rand.nextInt())%15*8;
enemyY=Math.abs(rand.nextInt())%15*8;
enemy2.setPosition(enemyX,enemyY);
}while(enemy2.collidesWith(background,false));
do{
enemyX=Math.abs(rand.nextInt())%15*8;
enemyY=Math.abs(rand.nextInt())%15*8;
enemy3.setPosition(enemyX,enemyY);
}while(enemy3.collidesWith(background,false));
do{
enemyX=Math.abs(rand.nextInt())%15*8;
enemyY=Math.abs(rand.nextInt())%15*8;
enemy4.setPosition(enemyX,enemyY);
}while(enemy4.collidesWith(background,false));
}
/*
敌人出现不能和背景碰撞,初始化敌人出现地点
*/
/*private void firstCheckEnemy(Sprite s){
if(s.collidesWith(background,false))
enemyX=Math.abs(rand.nextInt())%15*8;
enemyY=Math.abs(rand.nextInt())%15*8;
s.setPosition(enemyX,enemyY);
}
*/
/*
为地图赋予列行
*/
private void setcell(){
int map1[]={1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
for(int i=0;i<map1.length;i++)
{
int column=i%15;//6,1
int row=(i-column)/15;//1
background.setCell(column,row,map1[i]);
}
Xbackground=2;
Ybackground=2;
xman.setPosition(8,8);
background.setPosition(0,0);
}
public void run(){
Graphics g=getGraphics();
while(end){
alive();
update();
draw(g);
try{
Thread.sleep(100);
}
catch(Exception e){}
}
}
/*
判断是否结束游戏,生命<0
*/
private void alive(){
if(lift<0){
end=false;
}
}
/*
update()核心方法
*/
private void update()
{ enemyrandommove();
xman_now_X=xman.getX();
xman_now_Y=xman.getY();
int keyState=getKeyStates();
if((keyState&FIRE_PRESSED)!=0){
burst_first.setPosition(xman_now_X,xman_now_Y);
burst_first.setVisible(true);
burst=new Burst(xman_now_X,xman_now_Y);
burst=null;
}
if((keyState&LEFT_PRESSED)!=0)
{
xman.move(-xmanX,0);
xman.nextFrame();
checkRole();
}
else if((keyState&RIGHT_PRESSED)!=0)
{
xman.move(xmanX,0);
xman.nextFrame();
checkRole();
}
if((keyState&UP_PRESSED)!=0){
xman.move(0,-xmanY);
xman.nextFrame();
checkRole();
}
if((keyState&DOWN_PRESSED)!=0){
xman.move(0,xmanY);
xman.nextFrame();
checkRole();
}
xman_knock_enemy();
burstknockenemy();
}
/*
敌人随机移动,并伴随追逐主角
*/
private void enemyrandommove(){
int enemyvalue;
rand=new Random();
enemy_now_X=enemy1.getX();
enemy_now_Y=enemy1.getY();
if(Math.abs(enemy_now_X-xman_now_X)<24 && Math.abs(enemy_now_Y-xman_now_Y)<24)//判断是否绝对值在X<24或者Y〈24就要追逐
{
enemy1.nextFrame();
if(enemy_now_X<xman_now_X)
{
enemy1.move(1,0);
checkEnemy(enemy1);
System.out.println("xSSSS");
}
if(enemy_now_X>xman_now_X)
{
enemy1.move(-1,0);
checkEnemy(enemy1);
System.out.println("xSSSS");
}
if(enemy_now_Y>xman_now_Y)
{
enemy1.move(0,-1);
checkEnemy(enemy1);
System.out.println("xSSSS");
}
if(enemy_now_Y<xman_now_Y)
{
enemy1.move(0,1);
checkEnemy(enemy1);
System.out.println("xSSSS");
}
}
//else{
enemyvalue=Math.abs(rand.nextInt())%4;
switch(enemyvalue)
{
case 0:enemy1.move(-3,0);enemy1.nextFrame();checkEnemy(enemy1);break;
case 1:enemy1.move(3,0);enemy1.nextFrame();checkEnemy(enemy1);break;
case 2:enemy1.move(0,-3);enemy1.nextFrame();checkEnemy(enemy1);break;
case 3:enemy1.move(0,3);enemy1.nextFrame();checkEnemy(enemy1);break;
default:break;
}
//}
enemy_now_X=enemy2.getX();//2
enemy_now_Y=enemy2.getY();
if(Math.abs(enemy_now_X-xman_now_X)<24 && Math.abs(enemy_now_Y-xman_now_Y)<24)
{
enemy2.nextFrame();
if(enemy_now_X<xman_now_X)
{
enemy2.move(1,0);
checkEnemy(enemy2);
System.out.println("xSSSS");
}
if(enemy_now_X>xman_now_X)
{
enemy2.move(-1,0);
checkEnemy(enemy2);
System.out.println("xSSSS");
}
if(enemy_now_Y>xman_now_Y)
{
enemy2.move(0,-1);
checkEnemy(enemy2);
System.out.println("xSSSS");
}
if(enemy_now_Y<xman_now_Y)
{
enemy2.move(0,1);
checkEnemy(enemy2);
System.out.println("xSSSS");
}
}
//else{
enemyvalue=Math.abs(rand.nextInt())%4;
switch(enemyvalue)
{
case 0:enemy2.move(-3,0);enemy2.nextFrame();checkEnemy(enemy2);break;
case 1:enemy2.move(3,0);enemy2.nextFrame();checkEnemy(enemy2);break;
case 2:enemy2.move(0,-3);enemy2.nextFrame();checkEnemy(enemy2);break;
case 3:enemy2.move(0,3);enemy2.nextFrame();checkEnemy(enemy2);break;
default:break;
}
//}
enemy_now_X=enemy3.getX();//3
enemy_now_Y=enemy3.getY();
if(Math.abs(enemy_now_X-xman_now_X)<24 && Math.abs(enemy_now_Y-xman_now_Y)<24)
{
enemy3.nextFrame();
if(enemy_now_X<xman_now_X)
{
enemy3.move(1,0);
checkEnemy(enemy3);
System.out.println("xSSSS");
}
if(enemy_now_X>xman_now_X)
{
enemy3.move(-1,0);
checkEnemy(enemy3);
System.out.println("xSSSS");
}
if(enemy_now_Y>xman_now_Y)
{
enemy3.move(0,-1);
checkEnemy(enemy3);
System.out.println("xSSSS1");
}
if(enemy_now_Y<xman_now_Y)
{
enemy3.move(0,1);
checkEnemy(enemy3);
System.out.println("xSSSS1");
}
}
//else{
enemyvalue=Math.abs(rand.nextInt())%4;
switch(enemyvalue)
{
case 0:enemy3.move(-3,0);enemy3.nextFrame();checkEnemy(enemy3);break;
case 1:enemy3.move(3,0);enemy3.nextFrame();checkEnemy(enemy3);break;
case 2:enemy3.move(0,-3);enemy3.nextFrame();checkEnemy(enemy3);break;
case 3:enemy3.move(0,3);enemy3.nextFrame();checkEnemy(enemy3);break;
default:break;
}
//}
enemy_now_X=enemy4.getX();//4
enemy_now_Y=enemy4.getY();
if(Math.abs(enemy_now_X-xman_now_X)<24 && Math.abs(enemy_now_Y-xman_now_Y)<24)
{
enemy4.nextFrame();
if(enemy_now_X<xman_now_X)
{
enemy4.move(1,0);
checkEnemy(enemy4);
System.out.println("xSSSS");
}
if(enemy_now_X>xman_now_X)
{
enemy4.move(-1,0);
checkEnemy(enemy4);
System.out.println("xSSSS");
}
if(enemy_now_Y>xman_now_Y)
{
enemy4.move(0,-1);
checkEnemy(enemy4);
System.out.println("xSSSS1");
}
if(enemy_now_Y<xman_now_Y)
{
enemy4.move(0,1);
checkEnemy(enemy4);
System.out.println("xSSSS1");
}
}
//else{
enemyvalue=Math.abs(rand.nextInt())%4;
switch(enemyvalue)
{
case 0:enemy4.move(-3,0);enemy4.nextFrame();checkEnemy(enemy4);break;
case 1:enemy4.move(3,0);enemy4.nextFrame();checkEnemy(enemy4);break;
case 2:enemy4.move(0,-3);enemy4.nextFrame();checkEnemy(enemy4);break;
case 3:enemy4.move(0,3);enemy4.nextFrame();checkEnemy(enemy4);break;
default:break;
}
//}
}
/*
敌人碰撞背景,则停止
*/
private void checkEnemy(Sprite s){
if(s.collidesWith(background,false)){
s.setPosition(enemy_now_X,enemy_now_Y);
//System.out.println("碰撞");
}
}
/*
检测主角是否到边界,或者遇到背景
*/
private void checkRole(){
if(xman.collidesWith(background,false)){
xman.setPosition(xman_now_X,xman_now_Y);
System.out.println("碰撞");
}
}
/*
主角碰到了敌人,自然后退,要判断前后碰撞和上下碰撞
#只需要把主角的左右坐标和敌人坐标比较
#左右坐标有差异的,移动左右
#上下坐标有差异的,移动上下
#实现后退
#主角上次坐标已经保留
*/
private void xman_knock_enemy(){
//if(xman.getX()%8==0)原本是为了防止碰撞后斜跳
//if(xman.getY()%8==0)原本是为了防止碰撞后斜跳
if(xman.collidesWith(enemy1,false)){
if(xman.getX()<enemy1.getX()){//敌人左边
xman.setPosition(xman_now_X-16,xman.getY());System.out.println("碰撞1");
}
if(xman.getX()>enemy1.getX()){//敌人右边
xman.setPosition(xman_now_X+16,xman.getY());
}
if(xman.getY()<enemy1.getY()){//敌人在下
xman.setPosition(xman.getX(),xman_now_Y-16);
}
if(xman.getY()>enemy1.getY()){//敌人在上
xman.setPosition(xman.getX(),xman_now_Y+16);
}
lift-=1;
//lockXmancheck();
}
if(xman.collidesWith(enemy2,false)){
if(xman.getX()<enemy2.getX()){//敌人左边
xman.setPosition(xman_now_X-16,xman.getY());System.out.println("碰撞2");
}
if(xman.getX()>enemy2.getX()){//敌人右边
xman.setPosition(xman_now_X+16,xman.getY());
}
if(xman.getY()<enemy2.getY()){//敌人在下
xman.setPosition(xman.getX(),xman_now_Y-16);
}
if(xman.getY()>enemy2.getY()){//敌人在上
xman.setPosition(xman.getX(),xman_now_Y+16);
}
lift-=1;
//lockXmancheck();
}
if(xman.collidesWith(enemy3,false)){
if(xman.getX()<enemy3.getX()){//敌人左边
xman.setPosition(xman_now_X-16,xman.getY());System.out.println("碰撞3");
}
if(xman.getX()>enemy3.getX()){//敌人右边
xman.setPosition(xman_now_X+16,xman.getY());
}
if(xman.getY()<enemy3.getY()){//敌人在下
xman.setPosition(xman.getX(),xman_now_Y-16);
}
if(xman.getY()>enemy3.getY()){//敌人在上
xman.setPosition(xman.getX(),xman_now_Y+16);
}
lift-=1;
//lockXmancheck();
}
if(xman.collidesWith(enemy4,false)){
if(xman.getX()<enemy4.getX()){//敌人左边
xman.setPosition(xman_now_X-16,xman.getY());System.out.println("碰撞4");
}
if(xman.getX()>enemy4.getX()){//敌人右边
xman.setPosition(xman_now_X+16,xman.getY());
}
if(xman.getY()<enemy4.getY()){//敌人在下
xman.setPosition(xman.getX(),xman_now_Y-16);
}
if(xman.getY()>enemy4.getY()){//敌人在上
xman.setPosition(xman.getX(),xman_now_Y+16);
}
lift-=1;
}
lockXmancheck();
}
/*
防止主角落在边界上卡住,或者在屏幕之外
*/
private void lockXmancheck(){
if(xman.getX()<8){
xman.setPosition(8,xman.getY());
}
if(xman.getX()>112){
xman.setPosition(104,xman.getY());
}
if(xman.getY()<8){
xman.setPosition(xman.getX(),8);
}
if(xman.getY()>104){
xman.setPosition(xman.getX(),96);
}
}
/*
炸弹炸到敌人,而且是第三个"炸弹"
*/
private void burstknockenemy(){
int enemyX,enemyY;
if(burst_third.collidesWith(enemy1,false)){
rand=new Random();
do{
enemyX=Math.abs(rand.nextInt())%15*8;//屏幕大概就15*8那么大吧,只要是8倍数,而且不在背景
enemyY=Math.abs(rand.nextInt())%15*8;
enemy1.setPosition(enemyX,enemyY);
}while(enemy1.collidesWith(background,false));
killenemy+=1;score+=20;
}
if(burst_third.collidesWith(enemy2,false)){
System.out.println("knock");
rand=new Random();
do{
enemyX=Math.abs(rand.nextInt())%15*8;//屏幕大概就15*8那么大吧,只要是8倍数,而且不在背景
enemyY=Math.abs(rand.nextInt())%15*8;
enemy2.setPosition(enemyX,enemyY);
}while(enemy2.collidesWith(background,false));
killenemy+=1;score+=20;
}
if(burst_third.collidesWith(enemy3,false)){
System.out.println("knock");
rand=new Random();
do{
enemyX=Math.abs(rand.nextInt())%15*8;//屏幕大概就15*8那么大吧,只要是8倍数,而且不在背景
enemyY=Math.abs(rand.nextInt())%15*8;
enemy3.setPosition(enemyX,enemyY);
}while(enemy3.collidesWith(background,false));
killenemy+=1;score+=20;
}
if(burst_third.collidesWith(enemy4,false)){
System.out.println("knock");
rand=new Random();
do{
enemyX=Math.abs(rand.nextInt())%15*8;//屏幕大概就15*8那么大吧,只要是8倍数,而且不在背景
enemyY=Math.abs(rand.nextInt())%15*8;
enemy4.setPosition(enemyX,enemyY);
}while(enemy4.collidesWith(background,false));
killenemy+=1;score+=20;
}
}
/*
绘画
*/
private void draw(Graphics g){
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
background.paint(g);
burstdrawcheck();
enemy1.paint(g);
enemy2.paint(g);
enemy3.paint(g);
enemy4.paint(g);
xman.paint(g);
liftkillscore(g,lift,killenemy,score);
flushGraphics();
}
/*
爆炸检测,有按下的话,炸弹暴
*/
private void burstdrawcheck(){
Graphics g=getGraphics();
if(burst_first.isVisible())
{burst_first.paint(g);}
if(burst_second.isVisible())
{burst_second.paint(g);}
if(burst_third.isVisible())
{burst_third.paint(g);}
}
}
/*
生命,杀敌,分数
*/
private void liftkillscore(Graphics gg,int l,int k,int s){
int lift=l;
int killenemy=k;
int score=s;
Graphics g=gg;
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL));
g.setColor(0x00ffff);
g.drawString("命:"+lift+" 杀敌:"+killenemy+" 分数: "+score,0,112,Graphics.TOP|Graphics.LEFT);
}
/*
炸弹类,做成一个任务,行不通,传值麻烦
class BurstTask implements Runnable{
private int burst_X,burst_Y;
private Sprite burst_first,burst_second,burst_third;
public BurstTask(int xman_now_X,int xman_now_Y){
burst_X=xman_now_X;
burst_Y=xman_now_Y;
try{
burst_first=new Sprite(Image.createImage("/img1.png"),8,8);
burst_second=new Sprite(Image.createImage("/img2.png"),8,8);
burst_third=new Sprite(Image.createImage("/img3.png"),16,16);
//一开始没爆炸
burst_first.setVisible(false);
burst_second.setVisible(false);
burst_third.setVisible(false);
}
catch(Exception e){}
}
public void run(){
burst_first.setPosition(burst_X,burst_Y);
burst_first.setVisible(true);
try{Thread.sleep(2000);}
catch(Exception e){}
}
}
*/
/*
爆炸类2,只是简单的变量调用
*/
class Burst implements Runnable{
private int burstX,burstY;
/*
构造器
*/
public Burst(int x,int y){
burstX=x;
burstY=y;
Thread t=new Thread(this);
t.start();
System.out.println("Thread2");
//savercanvas.xman_now_X=100;
}
/*
线程启动
*/
public void run(){
burstdraw();
}
/*
判断,并且炸弹每间隔2秒换图burst_first,burst_second,burst_third;
*/
private void burstdraw(){
if(savercanvas.burst_first.isVisible())
{
try{
Thread.sleep(1000);
}
catch(Exception e){}
savercanvas.burst_second.setPosition(burstX,burstY);
savercanvas.burst_second.setVisible(true);
savercanvas.burst_first.setVisible(false);
}
if(savercanvas.burst_second.isVisible())
{
try{
Thread.sleep(1000);
}
catch(Exception e){}
savercanvas.burst_third.setPosition(burstX,burstY);
savercanvas.burst_third.setVisible(true);
savercanvas.burst_second.setVisible(false);
}
if(savercanvas.burst_third.isVisible())
{
try{
Thread.sleep(500);
}
catch(Exception e){}
//savercanvas.burst_third.setVisible(true);
savercanvas.burst_third.setVisible(false);
}
}
}
}
/* if((keyState&FIRE_PRESSED)!=0){
// bursttimer=new Timer();
// bursttask=new BurstTask(xman_now_X,xman_now_Y);//将主角坐标传到爆炸任务里面
// bursttimer.schedule(bursttask,0);
}
*/
评论