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

📄 linezcanvas.java

📁 用J2ME写的一种五子连线的手机游戏。ColorLinez是一款由玩家通过功能键移动各色小球
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    			{
    				initCol += 1;
    			}
    		}
    		else
    		{
    	        initCol += 1;
    		}
    	}
    }
    
    //确定移动球
    private void commitPosition()
    {
    	//选择要移动的球
    	if ( grid[initRow][initCol] > 0 )
    	{
    		//球是否已经被选择
    		if ( !clickFlag )
    		{
    			//格子的位置
    		    choseRow = initRow;
    		    choseCol = initCol;
    		    //球的位置
    		    chSphere = grid[initRow][initCol];
    		    clickFlag = true;
    		}
    		else
    		{
    			choseRow = -1;
    			choseCol = -1;
    			clickFlag = false;
    		}
    	}
    	//选择要移动到的位置
    	else
    	{
    		if ( clickFlag )
    		{
    		    //球的下标
    		    grid[initRow][initCol] = chSphere;
    		    grid[choseRow][choseCol] = 0;
    		    for ( int i = 0; i < position.length; i++ )
    		    {
    		    	if ( position[i][0] == initRow && position[i][1] == initCol )
    		    	{
    		    		position[i][0] = choseRow;
    		    		position[i][1] = choseCol;
    		    		break;
    		    	}
    		    }
    		    clickFlag = false;
    		    //删除球
    		    deleteSphere();
    		    //产生新球
    		    createSphere(3);
    		}
    	}
    }
    
    
    private void pauseGame(Graphics g)
    {
    	stop();
    	try
    	{
    		if ( pauseImage == null )
    		{
    			pauseImage = Image.createImage("/res/pausegame.png");
    		}
    	}
    	catch(Exception e)
    	{}
        g.drawImage(pauseImage, 0, 0, Graphics.TOP | Graphics.LEFT);
    	g.drawRect(46, 72 + pauseIndex * 25, 80, 20);
    }
    
    private void gameOver(Graphics g)
    {
    	stop();
    	try
    	{
    		if ( overImage == null )
    		{
    			overImage = Image.createImage("/res/gameover.png");
    		}
    	}
    	catch(Exception e)
    	{
    		System.out.println(e);
    	}
	    g.drawImage(overImage, 0, 0, Graphics.TOP | Graphics.LEFT);
        HighScoreRecordStore.saveHighScore(((minute>9)?"":"0") + minute + ":" + ((second>9)?"":"0") + second, score);
        g.drawRect(46, 75 + overIndex * 25, 80, 20);
    }
    
    private void drawGrid(Graphics g)
    {
    	//显示分数
  	    displayScore(g);
  	    //显示时间
  	    displayTime(g);

  	    count = 0;
  	    //画格子和球
    	for ( int i = 0; i < grid.length; i++ )
    	{
    		for ( int j = 0; j < grid[i].length; j++ )
    		{
    			//格子的坐标
    			gridX = j*gridWidth+spaceLeft;
    			gridY = i*gridHeight+spaceTop;

    			g.drawRect(gridX, gridY, gridWidth, gridHeight);

                //画已经选择框
                if ( j == choseCol && i == choseRow && clickFlag )
                {
                	g.setColor(0xFF0000);
                	g.drawRect(gridX + 2, gridY + 2, gridWidth-4, gridHeight - 4);
                	g.setColor(0x000000);
                }
    			
    			//画选择框
                if ( j == initCol && i == initRow )
                {
                	g.setColor(0x0055bb);
                	g.drawRect(gridX + 1, gridY + 1, gridWidth-2, gridHeight - 2);
                	g.setColor(0x000000);
                }
                //画球
                if ( grid[i][j] > 0 )
                {
        		    g.drawImage(img[grid[i][j] - 1], gridX + (gridWidth - 8) /2 , gridY + (gridHeight - 8) /2, Graphics.TOP | Graphics.LEFT);
        		    count++;
                }
    		}
    	}
    	if ( !deleting )
    	{
    	    total = count;
    	}
    	g.drawString("球数:" + String.valueOf(total), spaceLeft + 62, spaceTop - 15,Graphics.TOP | Graphics.LEFT);
    }

    /**
     * 显示分数
     * */
    private void displayScore(Graphics g)
    {
    	g.drawString("分数:" + score, spaceLeft, spaceTop - 15, Graphics.TOP | Graphics.LEFT);
    }
    
    /**
     * 显示时间
     * */
    private void displayTime(Graphics g)
    {
    	g.drawString("时间:" + ((minute>9)?"":"0") + minute + ":" + ((second>9)?"":"0") + second, scrWidth - spaceLeft - 53, spaceTop - 15, Graphics.TOP | Graphics.LEFT);
    }
    
    /**
     * 产生新球
     * */
    private void createSphere(int mach)
    {
    	if ( deleting )
    	{
    		return;
    	}

    	//产生新球
        for ( int i = 0; i < mach; i++ )
        {
        	if ( sphereTotal > 80 )
        	{
        		gameFlag = 2;
        	    break;
        	}
        	if ( sphereTotal < 81 )
        	{
            	if ( (81 - sphereTotal)/2 > 0 )
            	{
            		newPos = random.nextInt()%((81 - sphereTotal)/2) + (81 - sphereTotal)/2+1;
            	}
            	else
            	{
            	    newPos = random.nextInt()%((81 - sphereTotal)/2+1) + (81 - sphereTotal)/2;
            	    if ( newPos == 0 )
            	    {
            	    	newPos = 1;
            	    }
            	}
        	}
        	posRow = 80-sphereTotal;
           	grid[position[newPos-1][0]][position[newPos-1][1]] = random.nextInt()%ColorLinezMIDlet.gameGrade + ColorLinezMIDlet.gameGrade;
           	position[newPos-1][0] = position[posRow][0];
           	position[newPos-1][1] = position[posRow][1];
           	position[posRow][0] = 0;
           	position[posRow][1] = 0;
           	sphereTotal++;
        	if ( sphereTotal > 80 )
        	{
        		gameFlag = 2;
        	    break;
        	}
        }
    }
    
    /**
     * 删除球
     * */
    public void deleteSphere()
    {

    	for ( int i = 0; i < grid.length; i++ )
    	{
	    	for ( int n = 0; n < 6; n++ )
	    	{
	    		same[n] = 1;
	    		rows[n] = -1;
	    		cols[n] = -1;
	    	    isUper[n] = false;
	    	}
	    	
            for ( int j = 0; j < grid[i].length; j ++ )
            {
                //横行上出现大于或等于5个颜色相同并且连续的小球
    			if ( (grid[i][j] > 0) && (j < 8) )
    			{
    				if ( (grid[i][j] == grid[i][j+1]) & !isUper[0] )
    				{
    					same[0]++;
    					rows[0] = i;
    					cols[0] = j+1;
    				}
    				else if ( same[0] < delNum )
    				{
    					same[0] = 1;
    				}
    				else if ( same[0] > delNum - 1 )
    				{
    					isUper[0] = true;
    				}
    			}
    		    //竖列出现大于或等于5个颜色相同并且连续的小球
    			if ( (grid[j][i] > 0) && (j < 8) )
    			{
        			if ( (grid[j][i] == grid[j+1][i]) & !isUper[1] )
        			{
        				same[1]++;
        				rows[1] = j+1;
        				cols[1] = i;
        			}
        			else if ( same[1] < delNum )
        			{
        				same[1] = 1;
        			}
    				else if ( same[1] > delNum - 1 )
    				{
    					isUper[1] = true;
    				}
        		}
            }
			
			for ( int j = 0, n = i; j < i; j++, n-- )
    		{
    			//左斜1
    			if ( grid[n][j] > 0 & (grid[n][j] == grid[n-1][j+1]) & !isUper[2] )
    			{
    				same[2]++;
    				cols[2] = j + 1;
    				rows[2] = n - 1;
    			}
    			else if ( same[2] < delNum )
    			{
    				same[2] = 1;
    			}
				else if ( same[2] > delNum - 1 )
				{
					isUper[2] = true;
				}
    			
    			//左斜2
    			if ( (grid[8-j][8-n] > 0) & (grid[8-j][8-n] == grid[8-j-1][8-n+1]) & !isUper[3] )
    			{
    				same[3]++;
    				cols[3] = 8 - n + 1;
    				rows[3] = 8 - j - 1;
    			}
    			else if ( same[3] < delNum )
    			{
    				same[3] = 1;
    			}
				else if ( same[3] > delNum - 1 )
				{
					isUper[3] = true;
				}
    			
    			//右斜1
    			if ( (grid[j][8-n] > 0) & (grid[j][8-n] == grid[j+1][8-n+1]) & !isUper[4] )
    			{
    				same[4]++;
    				cols[4] = 8 - n + 1;
    				rows[4] = j + 1;
    			}
    			else if ( same[4] < delNum )
    			{
    				same[4] = 1;
    			}
				else if ( same[4] > delNum - 1 )
				{
					isUper[4] = true;
				}
    			
    			//右斜2
    			if ( (grid[8-n][j] > 0) & (grid[8-n][j] == grid[8-n+1][j+1]) & !isUper[5] )
    			{
    				same[5]++;
    				cols[5] = j + 1;
    				rows[5] = 8 - n + 1;
    			}
    			else if ( same[5] < delNum )
    			{
    				same[5] = 1;
    			}
				else if ( same[5] > delNum - 1 )
				{
					isUper[5] = true;
				}
    		}
            
            for ( int n = 0; n < 6; n++ )
            {
                //启动球消失的线程
        		if ( same[n] > (delNum - 1) && !deleting )
        		{
        			DeleteTimerTask delTask = new DeleteTimerTask( rows[n], cols[n], same[n], delDir[n]);
        			timer.schedule( delTask, 100, 500);
        			deleting = true;
        			score += same[n];
        			break;
        		}
            }
    	}
    }
    
    /**
     * 播放声音
     */
    public void playSound()
    {
        try
        {
            Manager.playTone(80, 200, 60);
        }catch(Exception e){}
    }
    
    public class TimeTimerTask extends TimerTask
    {
    	public void run()
    	{
   		    second++;
   		    //如果秒数大于59分钟数加1
   		    if( second > 59 )
   		    {
    			minute++;
    			second = 0;
    		}
    		displayTime(offG);
    		//重新画时间
    		repaint(scrWidth - spaceLeft - 53, spaceTop - 15, 66, 12);
    		System.gc();
    	}
    }
    
    public class DeleteTimerTask extends TimerTask
    {
    	private int delCol = 0;
    	private int delRow = 0;
    	private int delTotal = 0;
    	private int oper = 0;
    	private int color = 0;
    	
    	private int tempCol = 0;
    	private int tempRow = 0;
    	private int tempCount = 0;
    	private int tempColor = 0;

    	public DeleteTimerTask( int row, int col, int total, int oper)
    	{
    		this.delCol = col;
    		this.delRow = row;
    		this.delTotal = total;
    		this.oper = oper;
    		this.color = grid[row][col];
    	}
    	
    	public void run()
    	{
    		tempCol = delCol;
    		tempRow = delRow;
    		tempCount = delTotal;
    		tempColor = color;

    		if ( grid[delRow][delCol] > 0 )
    		{
    			tempColor = 0;
    		}

			while ( tempCount > 0 )
			{
				grid[tempRow][tempCol] = tempColor;
                
				if ( disCount == 0 )
				{
					posRow = 80 - sphereTotal + 1;
					position[posRow][0] = tempRow;
					position[posRow][1] = tempCol;
					sphereTotal--;
				}
				
				switch ( oper )
				{
	    		//横
				case 0: tempCol--; break;
				//竖
				case 1: tempRow--; break;
	    		//左斜
				case 2: 
	    		    tempCol--;
	    		    tempRow++;
	    		    break;
	    		//右斜
				case 3:
					tempCol--;
	    		    tempRow--;
				}
				tempCount--;
			}
            repaint();
            if ( disCount++ > 3 )
            {
            	disCount = 0;
            	deleting = false;
            	createSphere(3);
            	repaint();
            	this.cancel();
            }
    	}
    }
}

⌨️ 快捷键说明

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