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

📄 gamefrm.java

📁 俄罗斯方块源代码
💻 JAVA
字号:
import java.util.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/*
 * Created on 2004-10-21
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GameFrm extends Canvas
{
    private int		iState; 
    private byte	cell[][] = null;
    private int		iRows=18, iCols=10;
    private Shape	curShape = null;
    public int		curRow, curCol;
    public MoveThread	run = null;
    /* (non-Javadoc)
     * @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
     */
    protected GameFrm()
    {
        super();
        init();
        iState=0;
    }
    protected void paint(Graphics gs)
    {
        // TODO Auto-generated method stub
        //清屏
        gs.setColor(255, 255, 255);
        gs.fillRect(0, 0, getWidth(), getHeight());
        
        int iCellLen = 8;
        int iFenceLen = 2;
        int x0=(this.getWidth() - iFenceLen*2 - (iCols-2)*iCellLen) / 2;
        int y0=(this.getHeight() - iFenceLen*2 - (iRows-2)*iCellLen) / 2;
        int x=x0, y=y0;
        //栅栏和背景
        gs.setColor(140, 178, 100);
        gs.fillRect(x, y, iFenceLen*2 + (iCols-2)*iCellLen, (iFenceLen*2) + (iRows-2)*iCellLen);
        
        //显示已堆积的
        if (cell != null)
        {
            x += iFenceLen;
            y += iFenceLen;
            for(int i=1; i<iRows-1; i++)
            {
                for (int j=1; j<iCols-1; j++)
                {
                    if (0 == cell[i][j]) //空
                    {
                        gs.setColor(239, 243, 255);
                    }
                    else
                    {
                        gs.setColor(0, 0, 0);
                    }
                    gs.fillRect(x + (j-1)*iCellLen + 1, y + (i-1)*iCellLen + 1, iCellLen - 2, iCellLen - 2);
                }
            }
        }
        //显示在移动的形状
        if (curShape != null)
        {
            x = x0 + iFenceLen + (curCol - 1) * iCellLen;
            y = y0 + iFenceLen + (curRow - 1) * iCellLen;
            gs.setColor(0, 0, 0);
            for (int i=0; i<curShape.iSideLength; i++)
            {
                for (int j=0; j<curShape.iSideLength; j++)
                {
                    if (0 != curShape.cell[i][j])
                    {
                        gs.fillRect(x + j*iCellLen + 1, y + i*iCellLen + 1, iCellLen - 2, iCellLen - 2);
                    }
                }
            }
        }
        
        //显示提示
        gs.setColor(128, 0, 0);
        if (0 == iState)
        {
            gs.drawString("请开始新游戏", x0, y0-15, Graphics.TOP | Graphics.LEFT);
        }
        else if (1 == iState)
        {
            gs.drawString("正在玩游戏呢", x0, y0-15, Graphics.TOP | Graphics.LEFT);
        }
        else if (2 == iState)
        {
            gs.drawString("游戏已经结束", x0, y0-15, Graphics.TOP | Graphics.LEFT);
        }
    }
    
    protected void keyPressed(int keyCode)
    {
        if (curShape == null)
            return ;
        
        switch (this.getGameAction(keyCode))
        {
        case Canvas.UP:
            if (CanRotate())
            {
                curShape.Rotate();
                this.repaint();
            }
            break;
        case Canvas.LEFT:
        	if (!IsConflict(curRow, curCol - 1))
        	{
        	    curCol -= 1;
        	    this.repaint();
        	}
            break;
        case Canvas.RIGHT:
        	if (!IsConflict(curRow, curCol + 1))
        	{
        	    curCol += 1;
        	    this.repaint();
        	}
            break;
        case Canvas.DOWN:
            MoveDown();
        	this.repaint();
            break;    
        }
        
    }
    
    protected void MoveDown()
    {
        if (!IsConflict(curRow + 1, curCol))
    	{
    	    curRow += 1;
    	}
    	else
    	{
    	    next();
    	}
    }
    
    protected boolean CanRotate()
    {
        if (curRow + curShape.iSideLength > iRows - 1)
            return false;
        if (curCol <= 0 || curCol + curShape.iSideLength > iCols - 1)
            return false;
        for (int i = 0; i < curShape.iSideLength; i++)
    	{
            for (int j = 0; j < curShape.iSideLength; j++)
            {
                if (1 == cell[curRow + i][curCol + j] || 9 == cell[curRow + i][curCol + j])
                {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    protected boolean IsConflict(int r, int c)
    {
        for (int i = 0; i < curShape.iSideLength; i++)
    	{
            for (int j = 0; j < curShape.iSideLength; j++)
            {
                if (1 == curShape.cell[i][j] && 
                        (1 == cell[r + i][c + j] || 9 == cell[r + i][c + j])) //冲突
                {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    //初始化
    protected void init()
    {
        cell = null;
        cell = new byte[iRows][iCols];
        
        //初值
        for (int i=1; i<iRows-1; i++)
            for (int j=1; j<iCols-1; j++)
                cell[i][j] = 0;
        
        //栅栏
        for (int j=0; j<iCols; j++)
        {
            cell[0][j] = 9;
            cell[iRows - 1][j] = 9;
        }
        for (int i=1; i<iRows-1; i++)
        {
            cell[i][0] = 9;
            cell[i][iCols - 1] = 9;
        }
    }
    
    //产生形状
    protected void make()
    {
        Random rd = new Random();
        int	iRet = Math.abs(rd.nextInt()) % 7;
        curShape = null;
        curShape = ShapeFactory.GetShape(iRet);
        curRow = 1;
        curCol = iCols / 2 - curShape.iSideLength / 2;
        if (IsConflict(curRow, curCol))
        {
            stop();
        }
    }
    
    protected void next()
    {
        SaveCurShape();
        ClearLine();
	    this.make(); //新形状
    }
    
    protected void SaveCurShape()
    {
        for (int i=0; i<curShape.iSideLength; i++)
        {
            for (int j=0; j<curShape.iSideLength; j++)
            {
                if (0 != curShape.cell[i][j])
                {
                    cell[curRow + i][curCol + j] = curShape.cell[i][j];
                }
            }
        }
    }
    
    protected void ClearLine()
    {
        boolean bAll;
        for (int i=curRow; i<curRow+curShape.iSideLength; i++)
        {
            if (i > iRows - 1 - 1) //超过底行
                return ;
            
            bAll = true;
            for (int j=1; j<iCols-1; j++)
            {
                if (0 == cell[i][j])
                {
                    bAll = false;
                    break;
                }
            }
            if (bAll) //满一行, 将该行上面的行依次下移
            {
               for (int r=i-1; r>0; r--)
               {
                   for (int j=1; j<iCols-1; j++)
                   {
                       cell[r+1][j] = cell[r][j]; 
                   }
               }
               for (int j=1; j<iCols-1; j++)
                   cell[1][j] = 0;
            }
        }
    }
    
    protected void start()
    {
        this.init();
        this.make();
        run = null;
        run = new MoveThread(this);
        run.start();
        iState=1;
    }

    protected void stop()
    {
        if (run != null)
        {
            run.bStop = true;
            run = null;
        }
        curShape = null;
        iState=2;
    }
}

⌨️ 快捷键说明

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