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

📄 linezcanvas.java

📁 用J2ME写的一种五子连线的手机游戏。ColorLinez是一款由玩家通过功能键移动各色小球
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package game;

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;

public class LinezCanvas extends Canvas
{
	//Display对象
	Display display;
	//屏幕的宽度与高度
	private int scrWidth = 0;
	private int scrHeight = 0;

	//声明次画面
	private Graphics offG = null;
	private Image image = null;

	private int grid[][] = new int[9][9];

	private int position[][] = new int[81][2];
	
	//格子与顶部、左端的距离
	private int spaceTop = 0;
	private int spaceLeft = 0;
	
	//格子的宽度与高度
	private int gridWidth = 0;
	private int gridHeight = 0;

	//格子的x、y坐标
	private int gridX = 0;
	private int gridY = 0;

	//已经选择标记
	private boolean clickFlag = false;

	//选择框初始位置
	private int initRow = 4;
	private int initCol = 4;

	//球被选择的位置
	private int choseRow = 0;
	private int choseCol = 0;
	
	//声明图片对象
	private Image img[] = new Image[3+(ColorLinezMIDlet.gameGrade-1)*2];

	//声明随机数对象
	private Random random = null;

	//球的下标
	private int chSphere = 0;
	
	//球的颜色数
	private int colorTotal = 0;
	
	//消失球数
	private int delNum = 5;

	//球的总数
	private int sphereTotal = 0;

	//新球位置
	private int newPos = 0;
	
	//存储球位置一维下标
	private int posRow = 0;
	
	//游戏状态 0代表进行中 1游戏暂停 2游戏结束
	private int gameFlag = 0;
	
	//分数
	private int score = 0;
	
	//时间分钟数
	private int minute = 0;
	//秒数
	private int second = 0;
	
	//定时器
	private Timer timer = null;
	
	//球消失时闪烁的次数
	private int disCount = 0;
	
	//球是否正在消失
	private boolean deleting = false;
	
	//球色相同所在的行数
	private int rows[] = new int[6];
    //球色相同所在的列数
	private int cols[] = new int[6];
    //球色相同所在的行数
	private int same[] = new int[6];
	
    //球消失的方向
	private int delDir[] = { 0, 1, 2, 2, 3, 3 };
    private boolean isUper[] = new boolean[6];
	
    //出现球的球数
    private int count = 0;
    private int total = 0;
    
    //暂停选项索引
    private int pauseIndex = 0;
    //暂停图片
    private Image pauseImage = null;
    //游戏结束选项索引
    private int overIndex = 0;
    //游戏结束图片
    private Image overImage = null;
    
    /**
     * 构造方法
     * */
    public LinezCanvas(Display display)
    {
    	//获得Display对象
    	this.display = display;
    	//设屏全屏
    	this.setFullScreenMode(true);
        //得到屏幕的宽度与高度
    	scrWidth = this.getWidth();
    	scrHeight = this.getHeight();

    	//计算格子的宽度与高度
    	gridWidth = ( scrWidth - 6 ) / grid[0].length;
    	gridHeight = gridWidth;
    	
    	//设置格子与顶部、左端的距离
    	spaceTop = (scrHeight - gridHeight*9)/2 + 6;
    	spaceLeft = (scrWidth - gridWidth*9)/2;
        //获得次画笔对象
    	image = Image.createImage(scrWidth, scrHeight);
    	offG = image.getGraphics();
        //创建Random对象
    	random = new Random();
    	//游戏初始化
        gameInit();
        
    	//导入图片
        for ( int i = 0; i < colorTotal; i++ )
        {
        	try
        	{
        		img[i] = Image.createImage("/res/0"+i+".png");
        	}
        	catch(Exception e)
        	{}
        }
        
    	//创建Timer对象
    	timer = new Timer();
    	//启动时间线程
    	timer.scheduleAtFixedRate(new TimeTimerTask(),100,1000);
    }
    
    //游戏初始化
    public void gameInit()
    {
    	//游戏进行中
    	gameFlag = 0;
    	//球的颜色数
    	colorTotal = 3+(ColorLinezMIDlet.gameGrade-1)*2;
    	//球的总数
    	sphereTotal = 0;
    	//选择框初始位置
    	initRow = 4;
    	initCol = 4;
    	//分数
        score = 0;
    	//时间分钟数
    	minute = 0;
    	//秒数
    	second = 0;

    	for ( int i = 0, m = 0; i < grid.length; i++ )
    	{
    		for ( int j = 0; j < grid[i].length; j++, m++ )
    		{
    			grid[i][j] = 0;
                //新出现球的位置
    		    position[m][0] = i;
    		    position[m][1] = j;
    		}
    	}
    	//游戏开始新出现5个小球
    	createSphere(5);
    }
    
    protected void paint(Graphics g)
    {
    	//清屏
    	offG.setColor(0xFFFFFF);
    	offG.fillRect(0, 0, scrWidth, scrHeight);
    	offG.setColor(0x000000);
    	//0代表游戏进行中 1游戏暂停 2游戏结束
    	switch ( gameFlag )
    	{
    	    case 0:
                drawGrid(offG);
                break;
    	    case 1:
    	    	pauseGame(offG);
    	    	break;
    	    case 2:
    	    	gameOver(offG);
    	    	break;
    	}
   	    g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
    }
    
    /**
     * 启动线程
     * */
    public void start()
    {
    	if ( timer == null )
    	{
    	      timer = new Timer();
    	      timer.schedule(new TimeTimerTask(),100,1000);
    	}
    }

    /**
     * 停止线程
     * */
    public void stop()
    {
        if(timer != null)
        {
            timer.cancel();
            timer = null;
        }
    }
    
    /**
     * 退出游戏
     * */
    public void quitGame()
    {
    	stop();
    	for ( int i = 0; i < colorTotal; i++ )
    	{
    		img[i] = null;
    	}
    	if ( pauseImage != null )
    	{
    		pauseImage = null;
    	}
    	if ( overImage != null )
    	{
    		overImage = null;
    	}
    }
    
    protected void hideNotify()
    {
        stop();
    }
    
    protected void showNotify()
    {
    	start();
    }
    protected void keyPressed(int keyCode)
    {
    	//球在消失中立即返回
    	if ( deleting )
    	{
    		return;
    	}
        //游戏进行中,进行上下左右移动选择
    	if ( gameFlag == 0 )
    	{
    		int action = this.getGameAction(keyCode);
        	switch (action)
        	{
        	    case Canvas.UP:
                    moveUp();
        	    	break;
        	    case Canvas.DOWN:
                    moveDown();
        	    	break;
        	    case Canvas.LEFT:
                    moveLeft();
        	    	break;
        	    case Canvas.RIGHT:
                    moveRight();
        	    	break;
        	    case Canvas.FIRE:
                    commitPosition();
                    break;
                default:
                	if ( keyCode == -7 )
                	{
                		gameFlag = 1;
                	}
                    break;
        	}
    	}
    	//游戏暂停
    	else if ( gameFlag == 1 )
    	{
        	switch ( keyCode )
        	{
        	    case -1:
        	    	pauseIndex--;
        	    	if ( pauseIndex < 0 )
        	    	{
        	    		pauseIndex = 3;
        	    	}
        	    	break;
        	    case -2:
        	    	pauseIndex++;
        	    	if ( pauseIndex > 3 )
        	    	{
        	    		pauseIndex = 0;
        	    	}
        	    	break;
        	    case -5:
        	    	switch ( pauseIndex )
        	    	{
        	    	case 0:
            	    	gameFlag = 0;
            	    	start();
        	    		break;
        	    	case 1:
        	    		gameInit();
        	    		start();
        	    		break;
        	    	case 2:
        	    		quitGame();
        	    	    display.setCurrent(new MainMenuCanvas(display));
        	    	    break;
        	    	case 3:
        	    		quitGame();
        	    		ColorLinezMIDlet.quitApp();
        	    		break;
        	    	}
        	}
    	}
    	//游戏结束
    	else if ( gameFlag == 2 )
    	{
        	switch ( keyCode )
        	{
        	    case -1:
        	    	overIndex--;
        	    	if ( overIndex < 0 )
        	    	{
        	    		overIndex = 2;
        	    	}
        	    	break;
        	    case -2:
        	    	overIndex++;
        	    	if ( overIndex > 2 )
        	    	{
        	    		overIndex = 0;
        	    	}
        	    	break;
        	    case -5:
        	    	switch ( overIndex )
        	    	{
        	    	case 0:
        	    		gameInit();
        	    		start();
        	    		break;
        	    	case 1:
        	    		quitGame();
        	    	   	display.setCurrent(new MainMenuCanvas(display));
        	    	   	break;
        	    	case 2:
        	    		quitGame();
        	    		ColorLinezMIDlet.quitApp();
        	    		break;
        	    	}
        	}
    	}
    		
    	if ( ColorLinezMIDlet.isPlaySound )
    	{
    	    playSound();
    	}
    	repaint();
    }
    
    //向上移
    private void moveUp()
    {
    	if ( initRow > 0 )
    	{
    		if ( clickFlag )
    		{
    			if ( (grid[initRow-1][initCol] == 0) | ((choseRow == initRow - 1) & choseCol == initCol ) )
    			{
    				initRow -= 1;
    			}
    		}
    		else
    		{
    	        initRow -= 1;
    		}
    	}
    }
    
    //向下移
    private void moveDown()
    {
    	if ( initRow < 8 )
    	{
    		if ( clickFlag )
    		{
    			if ( (grid[initRow+1][initCol] == 0) | ((choseRow == initRow + 1) & choseCol == initCol ) )
    			{
    				initRow += 1;
    			}
    		}
    		else
    		{
    	        initRow += 1;
    		}
    	}
    }

    //向左移
    private void moveLeft()
    {
    	if ( initCol > 0 )
    	{
    		if ( clickFlag )
    		{
    			if ( grid[initRow][initCol-1] == 0 | ((choseCol == initCol - 1) & choseRow == initRow ) )
    			{
    				initCol -= 1;
    			}
    		}
    		else
    		{
    	        initCol -= 1;
    		}
    	}
    }
    
    //向右移
    private void moveRight()
    {
    	if ( initCol < 8 )
    	{
    		if ( clickFlag )
    		{
    			if ( grid[initRow][initCol+1] == 0 | ((choseCol == initCol + 1) & choseRow == initRow ))

⌨️ 快捷键说明

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