⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 手机拼图游戏:J2ME游戏
💻 JAVA
字号:
/*
 *author:wangfeng

 *date:2007-02-23
 */
import util.ParseXML;
import util.RandomTiledLayerCellPos;
import util.PlayMedia;
import util.GraphicDraw;

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;

public class Game extends GameCanvas implements Runnable
{
    public static int currentRow =0;
    public static int currentCol =0;
    public static int oldRow = 0;
    public static int oldCol = 0;
    public static Command comdCancel = new Command("退出",Command.CANCEL,1);
    public static Command comdContinue = new Command("下一关",Command.SCREEN,2);
    public static Command comdEnd = new Command("通关",Command.SCREEN,2);
    public static int startPicIndex = 0;
    public static int gameLevelNum = 0;
    public static String GameLevelName = "";

    public PlayMedia song = new PlayMedia();
    public Player player = null;
    public boolean conti = true;

    private int sleepTime = 300;
    private Graphics g = null;
    private TiledLayer tiledLayer = null;
    private LayerManager lm = null;
	private Random random = null;
	private RandomTiledLayerCellPos randomCellPosTiled = null;
    private boolean changeOldPoint = true;
    private String passGateSongPath = "resource:/song/test-wav.wav";

    /* value from xml file */
	private String imagePath = "";
	private int col = 0;
	private int row = 0;
	private int tileWidth = 0;
	private int tileHeight = 0;

    private boolean randomPace = false;
    private int maxPicNum = 0;

    public Game() throws Exception
	{
		super(true);
        this.addCommand(comdCancel);
        lm = new LayerManager();
        try
        {
            player = song.createPlayer(passGateSongPath);
            drawGraphic();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

    /* 线程执行函数*/
	public void run()
	{
		while(conti)
		{
			try
			{
				input();
				Thread.sleep(sleepTime);
			}catch(InterruptedException e)
			{
				System.out.println(e);
			}catch(Exception e)
            {
                e.printStackTrace();
            }

        }
	}
	
	/* 线程结束*/
	public void exit()
    {
               conti = false ;
    }

	public void drawGraphic() throws Exception
	{
		try
		{
            if(null != tiledLayer)
                lm.remove(tiledLayer);

            this.parseXml();

            if("".equals(imagePath))
				throw new Exception("Load spell picture error!");
			if(0==col)
				throw new Exception("TiledLayer col is 0");
			if(0==row)
				throw new Exception("TiledLayer row is 0");
			if(0==tileWidth)
				throw new Exception("TiledLayer tileWidth is 0");
			if(0==tileHeight)
			throw new Exception("TiledLayer tileHeight is 0");
			
			Image image = Image.createImage(imagePath);
            if(GameLevelName.equals(SelectLevelMenu.LEVEL_EASY_NAME))
            {
                tiledLayer = new TiledLayer(col,row,image,tileWidth,tileHeight);
            }else if(GameLevelName.equals(SelectLevelMenu.LEVEL_NORMAL_NAME))
            {
                util.ExtendMath math = new util.ExtendMath();
                int ch = math.getGreatestCommonDivisor(row,tileHeight);
                row = row*ch;
                tileHeight = tileHeight/ch;
                int rw = math.gcd(col,tileWidth);
                col = col*rw;
                tileWidth = tileWidth/rw;

                tiledLayer = new TiledLayer(col,row,image,tileWidth,tileHeight);
            }else if(GameLevelName.equals(SelectLevelMenu.LEVEL_DIFFICULTY_NAME))
            {
                util.ExtendMath math = new util.ExtendMath();
                int ch = math.getGreatestCommonDivisor(row*2,tileHeight*2);
                row = row*ch;
                tileHeight = tileHeight/(ch);
                int rw = math.gcd(col,tileWidth);
                col = col*rw;
                tileWidth = tileWidth/(rw);

                tiledLayer = new TiledLayer(col,row,image,tileWidth,tileHeight);
            }

            lm.append(tiledLayer);
            g = getGraphics();
		    randomCellPosTiled = new RandomTiledLayerCellPos(tiledLayer,col,row);
			randomCellPosTiled.setPos(randomCellPosTiled.getRandomPosArray());

            lm.paint(g,0,0);

            this.drawFrame(0,0,this.tileWidth,this.tileHeight);
            paint(g);
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private void input() throws Exception
	{
		int keystate = getKeyStates();
		OnKeySelect(keystate);
	}

    private void analysePoint() throws Exception
    {
//        System.out.println("oldRow:"+oldRow+";oldCol:"+oldCol+";currentRow:"+currentRow+";currentCol:"+currentCol);
        int flag = this.randomCellPosTiled.reDraw(oldRow,oldCol,currentRow,currentCol);

        if(flag==0)
        {/* 调换 */
            changeOldPoint=true;
            lm.paint(g,0,0);
            drawFrame(currentCol * this.tileWidth,currentRow* this.tileHeight,tileWidth,tileHeight);
            flushGraphics();
        }else if(flag == 1)
        {/* 两个点都不可动 */
             changeOldPoint=true;
        }else if(flag == 2)
        {/* 前一个点不可动 */
             changeOldPoint=true;
        }else if(flag == 3)
        {/* 后一个点不可动 */
            changeOldPoint = false;
        }
        if(randomCellPosTiled.compareArray(randomCellPosTiled.getInitPosArray(),randomCellPosTiled.getRandomPosArray()))
        {
            if(startPicIndex < maxPicNum -1)
            {
                lm.paint(g,0,0);
                flushGraphics();

                addCommand(comdContinue);
                playSong();
            }else
            {//通关啦

                lm.paint(g,0,0);
                flushGraphics();

                removeCommand(comdContinue);
                addCommand(comdEnd);
                playSong();
            }
        }
    }
    private void playSong()
    {
        try
        {
            player.start();
        }catch(Exception e)
        {
            try
            {
                player = song.createPlayer(passGateSongPath);
                player.start();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }

    }
    /* analyse key */
	private void OnKeySelect(int keystate)  throws Exception
	{
        if(changeOldPoint)
        {
            oldRow = currentRow;
            oldCol = currentCol;
        }
        /* 按下上键*/
		if((keystate & UP_PRESSED)!=0)
		{
            song.playTone();
            if(currentRow>0)
            {
               currentRow--;
               analysePoint();
            }
        }
		/* 按下下键 */
		if((keystate & DOWN_PRESSED)!=0)
		{
           song.playTone();
            if(currentRow<row-1)
            {
               currentRow++;
                analysePoint();
            }
        }
		/* 按下左键*/
		if((keystate & LEFT_PRESSED)!=0)
		{
            song.playTone();
             if(currentCol>0)
             {
                currentCol--;
                 analysePoint();
             }
        }
		/* 按下右键*/
		if((keystate & RIGHT_PRESSED)!=0)
		{
            song.playTone();
             if(currentCol<col-1)
             {
                currentCol++;
                 analysePoint();
             }
        }
		/* 按下中间键*/
		if((keystate & FIRE_PRESSED)!=0)
		{
			
		}
    }
	
	/* 线程开始*/
	public void start()
	{
		Thread t = new Thread(this);
		t.start();
	}

	/* get paramer value from xml file */
	public void parseXml()
	{
		try
		{
            
            InputStream inputStream = this.getClass().getResourceAsStream("/Picture.xml");
			ParseXML parse = new ParseXML(inputStream);
            maxPicNum = Integer.parseInt((String)((Hashtable)parse.get("PicList")).get("MaxPicNum"));
            tileWidth = Integer.parseInt((String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("TileWidth"));
			tileHeight = Integer.parseInt((String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("TileHeight"));
			/* get current pic index info */
			imagePath = (String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("Image");
			col = Integer.parseInt((String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("Col"));
			row = Integer.parseInt((String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("Row")); 
			if("true".equals((String)((Hashtable)((Hashtable)parse.get("PicList")).get("Pic_"+startPicIndex)).get("Random")))
				randomPace = true;
		}catch(IOException e)
	 	{
	 		System.out.println(e);
	 	}catch(Exception e)
	 	{
	 		e.printStackTrace();
	 	}
	}

    public void drawFrame(int x,int y,int w,int h)
    {
        GraphicDraw draw = new GraphicDraw();
        draw.drawLingRectangle(g,x,y,w,h);
        draw.drawLingRectangle(g,x+1,y+1,w-2,h-2);
        draw.drawLingRectangle(g,x+2,y+2,w-4,h-4);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -