引言:

俄罗斯方块的代码实现很简单,很有意思!

思路:

1、创建主窗口,加载菜单及游戏面板。

2、在游戏面板中初始化各种参数,并建立各种功能组件。

3、利用paint()函数开始画方块。

4、游戏结束,收尾,准备下一局。

本游戏用的是JDK1.8,编码UTF-8;

共有5个类,Gobang.java是游戏入口类。GameFrame.java是主窗口类。GamePanel.java是游戏面板类。GameLogic.java是游戏逻辑类。先一口气把所有的代码贴上来再说。

1、Tetris.java 游戏入口类

package com.game.tetris;/*** 功能:俄罗斯方块<br>* 作者:我是小木鱼(Lag)<br>*/
public class Tetris
{public static void main(String[] args){new GameFrame();}}

2、GameFrame.java 主窗口类。

package com.game.tetris;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;/*** 功能:游戏窗口<br>* 作者:我是小木鱼(Lag)<br>*/
public class GameFrame extends JFrame implements ActionListener
{private static final long serialVersionUID = 1L;/** 游戏面板 */private GamePanel gamePanel;/*** 功能:构造函数<br>*/public GameFrame(){try{//菜单JMenuBar jmb_tetris = new JMenuBar();JMenu jm_game = new JMenu("游戏");JMenuItem jmi_game_new = jm_game.add("新游戏");jmi_game_new.addActionListener(this);jmi_game_new.setActionCommand("new");JMenuItem jmi_game_pause = jm_game.add("暂停");jmi_game_pause.addActionListener(this);jmi_game_pause.setActionCommand("pause");JMenuItem jmi_game_continue = jm_game.add("继续");jmi_game_continue.addActionListener(this);jmi_game_continue.setActionCommand("continue");jm_game.addSeparator();JMenuItem jmi_game_exit = jm_game.add("退出");jmi_game_exit.addActionListener(this);jmi_game_exit.setActionCommand("exit");jmb_tetris.add(jm_game);JMenu jm_help = new JMenu("帮助");JMenuItem jmi_help_about = jm_help.add("关于");jmi_help_about.addActionListener(this);jmi_help_about.setActionCommand("about");jmb_tetris.add(jm_help);this.setJMenuBar(jmb_tetris);//面板this.gamePanel = new GamePanel();this.add(this.gamePanel);//显示this.setTitle("俄罗斯方块");this.setSize(400,500);this.setResizable(false);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}catch(Exception e){JOptionPane.showMessageDialog(this,"程序出现异常错误,即将退出!\r\n\r\n"+e.toString(),"提示",JOptionPane.ERROR_MESSAGE);System.exit(0);}}/*** 功能:事件监听<br>*/@Overridepublic void actionPerformed(ActionEvent e){String command = e.getActionCommand();if("new".equals(command)){this.gamePanel.newGame();}else if("pause".equals(command)){this.gamePanel.pauseGame();}else if("continue".equals(command)){this.gamePanel.continueGame();}else if("exit".equals(command)){System.exit(0);}else if("about".equals(command)){JOptionPane.showMessageDialog(this,"左右键移动,向上键旋转!\r\n\r\n我是小木鱼(Lag)\r\n\r\n","提示",JOptionPane.INFORMATION_MESSAGE);}}}

3、GamePanel.java 游戏面板类。

package com.game.tetris;import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;/*** 功能:游戏面板<br>* 作者:我是小木鱼(Lag)<br>*/
public class GamePanel extends JPanel implements KeyListener,ActionListener
{private static final long serialVersionUID = 1L;/** 游戏逻辑 */private GameLogic gameLogic;/** 游戏地图(共21行12列,该地图包含墙和固定死的方块信息) */private byte[][] map;/** 是否接收键盘信息(true-接收,false-不接收) */private boolean isAcceptKey = true;/** 定时器 */private Timer timer;/*** 功能:构造函数<br>*/public GamePanel(){//与主窗口大小保持一致(去掉菜单高度)this.setSize(400,440);//获得焦点(没焦点就不能截获键盘监听)this.setFocusable(true);//键盘监听this.addKeyListener(this);//游戏逻辑this.gameLogic = new GameLogic();//初始化游戏this.initGame();//定时器timer = new Timer(500,this);timer.start();}/*** 功能:初始化游戏<br>*/private void initGame(){//初始化游戏逻辑this.gameLogic.init();//得到地图this.map = this.gameLogic.getMap();//重画地图this.repaint();}/*** 功能:绘图<br>* 备注:给我地图剩下的你就不用管了。<br>*/@Overridepublic void paint(Graphics g){int blockSize = 20;        //默认方块大小int row = 0;int column = 0;//调用父类,让其做一些事前的工作,如刷新屏幕等super.paint(g);//画大地图中已经固定的方块和围墙//==================地图==================//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, -1},   //{-1, 0, 0, 7, 7, 0, 0, 1, 1, 1, 1, -1},   //{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1}    //==================结束==================row = this.map.length;column = this.map[0].length;for(int i=0;i<row;i++){for(int j=0;j<column;j++){if(this.map[i][j] == -1) //墙{g.setColor(Color.GRAY);g.fillRect(j * blockSize,i * blockSize,blockSize,blockSize);g.setColor(Color.WHITE);g.drawRect(j * blockSize,i * blockSize,blockSize,blockSize);}else if(this.map[i][j] > 0)  //方块{switch(this.map[i][j]){case 1:g.setColor(Color.RED);break;case 2:g.setColor(Color.ORANGE);break;case 3:g.setColor(Color.YELLOW);break;case 4:g.setColor(Color.GREEN);break;case 5:g.setColor(Color.BLUE);break;case 6:g.setColor(Color.PINK);break;case 7:g.setColor(Color.MAGENTA);break;}g.fillRect(j * blockSize,i * blockSize,blockSize,blockSize);g.setColor(Color.WHITE);g.drawRect(j * blockSize,i * blockSize,blockSize,blockSize);}}}//画当前方块byte[][] curShape = this.gameLogic.getShape();row = curShape.length;column = curShape[0].length;for(int i=0;i<row;i++){for(int j=0;j<column;j++){if(curShape[i][j] > 0) //方块{switch(curShape[i][j]){case 1:g.setColor(Color.RED);break;case 2:g.setColor(Color.ORANGE);break;case 3:g.setColor(Color.YELLOW);break;case 4:g.setColor(Color.GREEN);break;case 5:g.setColor(Color.BLUE);break;case 6:g.setColor(Color.PINK);break;case 7:g.setColor(Color.MAGENTA);break;}g.fillRect((j + this.gameLogic.getShapeColumn()) * blockSize,(i + this.gameLogic.getShapeRow()) * blockSize,blockSize,blockSize);g.setColor(Color.WHITE);g.drawRect((j + this.gameLogic.getShapeColumn()) * blockSize,(i + this.gameLogic.getShapeRow()) * blockSize,blockSize,blockSize);}}}//画记分牌g.setColor(Color.BLACK);g.setFont(new Font("宋体",Font.PLAIN,12));g.drawString("当前分数: " + this.gameLogic.getScore(),this.map[0].length * blockSize + 20,30);//画下一个方块g.drawString("下一个方块是:",this.map.length * blockSize + 20,60);byte[][] nextShape = this.gameLogic.getNextShape();row = nextShape.length;column = nextShape[0].length;for(int i=0;i<row;i++){for(int j=0;j<column;j++){if(nextShape[i][j] > 0){switch(nextShape[i][j]){case 1:g.setColor(Color.RED);break;case 2:g.setColor(Color.ORANGE);break;case 3:g.setColor(Color.YELLOW);break;case 4:g.setColor(Color.GREEN);break;case 5:g.setColor(Color.BLUE);break;case 6:g.setColor(Color.PINK);break;case 7:g.setColor(Color.MAGENTA);break;}g.fillRect(this.map[0].length * blockSize + 36 + j * blockSize,100 + i * blockSize,blockSize,blockSize);g.setColor(Color.WHITE);g.drawRect(this.map[0].length * blockSize + 36 + j * blockSize,100 + i * blockSize,blockSize,blockSize);}}}}/*** 功能:方块移动<br>*/private void move(int keyCode){if(!this.isAcceptKey){return;}//开始移动if(!this.gameLogic.move(keyCode)){return;}//得到移动后的新地图this.map = this.gameLogic.getMap();//重绘界面this.repaint();//判断是否Gave Overif(this.gameLogic.gameOver()){//屏蔽键盘信息this.isAcceptKey = false;return;}}/*** 功能:开始新游戏<br>*/public void newGame(){this.isAcceptKey = true;this.initGame();timer.start();}/*** 功能:暂停游戏<br>*/public void pauseGame(){this.isAcceptKey = false;timer.stop();}/*** 功能:继续游戏<br>*/public void continueGame(){this.isAcceptKey = true;timer.start();}/*** 功能:键盘监听<br>*/@Overridepublic void keyPressed(KeyEvent e){int keyCode = e.getKeyCode();if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT){this.move(keyCode);}}@Overridepublic void keyTyped(KeyEvent e){}@Overridepublic void keyReleased(KeyEvent e){}/*** 功能:事件监听<br>*/@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource() == this.timer)     //定时器{this.move(KeyEvent.VK_DOWN);}}}

4、GameLogic.java 游戏逻辑类。

package com.game.tetris;import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;/*** 功能:游戏逻辑<br>* 作者:我是小木鱼(Lag)<br>*/
public class GameLogic
{/** 大地图(共21行12列,该地图包含墙和后加入固定死的方块信息) */private byte[][] map;/** 方块类型(共7种) */private int blockType = 0;/** 方块状态(共4种) */private int turnState = 0;/** 当前方块图形(1-长条形方块,2-Z形方块,3-倒Z字形方块,4-J形方块,5-L字形,6-T字形方块,7-田字形方块) */private byte[][] shape;/** 当前方块图形(4×4矩阵)左上角所在行 */private int shapeRow;/** 当前方块图形(4×4矩阵)左上角所在列 */private int shapeColumn;/** 下一个方块类型 */private int nextBlockType = -1;/** 下一个方块状态 */private int nextTurnState = -1;/** 下一个方块图形 */private byte[][] nextShape;/** 得分 */private int score = 0;/*** 功能:构造函数<br>*/public GameLogic(){}/*** 功能:初始化<br>*/public void init(){//得到大地图this.map = GameMap.getMap();//随机生成新方块this.newBlock();}/*** 功能:随机生成新方块<br>*/private void newBlock(){//如果当前没有下一方块(即游戏刚开局,还未生成当前方块和下一方块)if(this.nextBlockType == -1 && this.nextTurnState == -1){this.blockType = (int)(Math.random() * 7);      //随机生成7种形状中的一种this.turnState = (int)(Math.random() * 4);       //随机生成4种旋转中的一种this.nextBlockType = (int)(Math.random() * 7);this.nextTurnState = (int)(Math.random() * 4);}else{this.blockType = this.nextBlockType;this.turnState = this.nextTurnState;this.nextBlockType = (int)(Math.random() * 7);this.nextTurnState = (int)(Math.random() * 4);}//获得当前方块,并设置初始位置(方块(4×4矩阵)最左上角方格初始行列位置即最顶端水平居中)this.shape = GameMap.getShape(this.blockType,this.turnState);this.shapeRow = 0;this.shapeColumn = 5;//获得下一个方块this.nextShape = GameMap.getShape(this.nextBlockType,this.nextTurnState);}/*** 功能:将方块信息添加到地图数组信息中<br>* 备注:方块信息是已经固定死了的<br>*/private void add(){//添加当前方块位置状态int rowCount = this.shape.length;int columnCount = this.shape[0].length;for(int i=this.shapeRow;i<this.shapeRow+rowCount;i++){for(int j=this.shapeColumn;j<this.shapeColumn+columnCount;j++){if(this.shape[i - this.shapeRow][j - this.shapeColumn] > 0){this.map[i][j] = this.shape[i - this.shapeRow][j - this.shapeColumn];}}}}/*** 功能:销毁满行的部分<br>* 原理:一行一行对地图信息进行扫描,如果一行的每个元素值均大于0,说明满行,销毁该行,该行上面的方块依次下落一行。<br>*/private void deleteLine(){int count = 0;int mapRowCount = this.map.length;int mapColumnCount = this.map[0].length;for(int i=0;i<mapRowCount - 1;i++)     //20行{for(int j=1;j<mapColumnCount - 1;j++)   //10列{if(this.map[i][j] > 0){count++;if(count == (mapColumnCount - 2))   //满行了,可以销毁该行{for(int m=i;m>0;m--)            //从当前行往回计算{for(int n=1;n<mapColumnCount - 1;n++){this.map[m][n] = this.map[m-1][n];}}this.score += 10;}}}count = 0;   //开始下一行计数}}/*** 功能:判断当前方块的位置是否合法<br>* 参数:_row -> 图形左上角所在行 <br>* 参数:_column -> 图形左上角所在列 <br>* 参数:_shape -> 移动变化的图形 <br>* 返回:0-不合法(可能与围墙或其他固定的方块位置重叠),1-合法<br>*/private int blow(int _row,int _column,byte[][] _shape){int rowCount = _shape.length;int columnCount = _shape[0].length;//方块所在的行列与墙或其他方块的行列都大于0或-1for(int i=0;i<rowCount;i++){for(int j=0;j<columnCount;j++){if(_shape[i][j] > 0)  //对该图形进行循环,找出大于零的数,即是方块{//判断墙if(this.map[_row + i][_column + j] == -1){return 0;}//判断固定方块if(this.map[_row + i][_column + j] > 0){return 0;}}}}return 1;}/*** 功能:方块移动<br>* 返回:true -> 可以移动旋转,需要刷新地图,false ->不可以,也不需要刷新地图<br>*/public boolean move(int keyCode){if(keyCode == KeyEvent.VK_UP)           //向上旋转{//得到逆时针要变的旋转变形int tempTurnState = (this.turnState + 1) % this.shape.length;byte[][] turnShape = GameMap.getShape(this.blockType,tempTurnState);if(blow(this.shapeRow,this.shapeColumn,turnShape) == 1){this.shape = turnShape;this.turnState = tempTurnState;}else{return false;}}else if(keyCode == KeyEvent.VK_DOWN)      //向下移动{if(blow(this.shapeRow + 1,this.shapeColumn,this.shape) == 1){this.shapeRow++;}else{this.add();this.deleteLine();this.newBlock();}}else if(keyCode == KeyEvent.VK_LEFT)        //向左移动{if(blow(this.shapeRow,this.shapeColumn - 1,this.shape) == 1)   //可以向左移{this.shapeColumn--;}else{return false;}}else if(keyCode == KeyEvent.VK_RIGHT)     //向右移动{if(blow(this.shapeRow,this.shapeColumn + 1,this.shape) == 1)  //可以向右移{this.shapeColumn++;}else{return false;}}return true;}/*** 功能:判断游戏是否结束<br>* 参数:true -> 已结束<br>* 参数:false -> 未结束<br>*/public boolean gameOver(){if(blow(this.shapeRow,this.shapeColumn,this.shape) == 0) //如果碰到围墙或固定方块,游戏结束。{this.score = 0;JOptionPane.showMessageDialog(null,"大虾,请重新来过吧!","提示",JOptionPane.ERROR_MESSAGE);return true;}else{return false;}}/*** 功能:返回大地图(共21行12列,该地图包含墙和固定死的方块信息)<br>*/public byte[][] getMap(){return map;}/*** 功能:返回当前方块图形<br>*/public byte[][] getShape(){return shape;}/*** 功能:返回当前方块图形(4×4矩阵)左上角所在行<br>*/public int getShapeRow(){return shapeRow;}/*** 功能:返回当前方块图形(4×4矩阵)左上角所在列<br>*/public int getShapeColumn() {return shapeColumn;}/*** 功能:返回下一个随机方块<br>*/public byte[][] getNextShape(){return nextShape;}/*** 功能:返回分数 <br>*/public int getScore() {return score;}}

5、GameMap.java 游戏地图类。

package com.game.tetris;/*** 功能:游戏地图<br>* 作者:我是小木鱼(Lag)<br>*/
public class GameMap
{/** 游戏大地图(存储方块[1~7]和围墙[-1]及空块[0]的信息,共21行12列252个小方格) */private static byte[][] map = {{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1}  };/** 方块图形(存储七种形状及其四种旋转变形<逆时针旋转90度>,每种旋转变形用包含4×4的数组表示具体图形<盯住大于零的数字猛看,就能看到图形了>) */private static byte shape[][][][] = new byte[][][][]{//长条形方块{//旋转变形一{{0, 0, 0, 0},{1, 1, 1, 1},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形二{{0, 1, 0, 0},{0, 1, 0, 0},{0, 1, 0, 0},{0, 1, 0, 0}},//旋转变形三{{0, 0, 0, 0},{1, 1, 1, 1},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{0, 1, 0, 0},{0, 1, 0, 0},{0, 1, 0, 0},{0, 1, 0, 0}}},//Z形方块{//旋转变形一{{2, 2, 0, 0},{0, 2, 2, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形二{{0, 2, 0, 0},{2, 2, 0, 0},{2, 0, 0, 0},{0, 0, 0, 0}},//旋转变形三{{2, 2, 0, 0},{0, 2, 2, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{0, 2, 0, 0},{2, 2, 0, 0},{2, 0, 0, 0},{0, 0, 0, 0}}},//倒Z字形方块{//旋转变形一{{0, 3, 3, 0},{3, 3, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形二{{3, 0, 0, 0},{3, 3, 0, 0},{0, 3, 0, 0},{0, 0, 0, 0}},//旋转变形三{{0, 3, 3, 0},{3, 3, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{3, 0, 0, 0},{3, 3, 0, 0},{0, 3, 0, 0},{0, 0, 0, 0}}},//J形方块{//旋转变形一{{0, 4, 0, 0},{0, 4, 0, 0},{4, 4, 0, 0},{0, 0, 0, 0}},//旋转变形二{{4, 4, 4, 0},{0, 0, 4, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形三{{4, 4, 0, 0},{4, 0, 0, 0},{4, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{4, 0, 0, 0},{4, 4, 4, 0},{0, 0, 0, 0},{0, 0, 0, 0}}},//L字形{//旋转变形一{{5, 0, 0, 0},{5, 0, 0, 0},{5, 5, 0, 0},{0, 0, 0, 0}},//旋转变形二{{0, 0, 5, 0},{5, 5, 5, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形三{{5, 5, 0, 0},{0, 5, 0, 0},{0, 5, 0, 0},{0, 0, 0, 0}},//旋转变形四{{5, 5, 5, 0},{5, 0, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}}},//T字形方块{//旋转变形一{{0, 6, 0, 0},{6, 6, 6, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形二{{0, 6, 0, 0},{6, 6, 0, 0},{0, 6, 0, 0},{0, 0, 0, 0}},//旋转变形三{{6, 6, 6, 0},{0, 6, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{6, 0, 0, 0},{6, 6, 0, 0},{6, 0, 0, 0},{0, 0, 0, 0}}},//田字形方块{//旋转变形一{{7, 7, 0, 0},{7, 7, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形二{{7, 7, 0, 0},{7, 7, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形三{{7, 7, 0, 0},{7, 7, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},//旋转变形四{{7, 7, 0, 0},{7, 7, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}}}};/*** 功能:返回大地图<br>* 备注:由于原始地图数据不允许修改,因此只返回复制后的地图赝品<br>*/public static byte[][] getMap(){//由于数组是个对象,而原始地图是不允许被修改的,所以不能直接赋值(引用地址),得复制一个新的地图让游戏随便修改。int row = map.length;int column = map[0].length;byte[][] copyMap = new byte[row][column];for(int i=0;i<row;i++){for(int j=0;j<column;j++){copyMap[i][j] = map[i][j];}}return copyMap;}/*** 功能:返回方块图形<br>* 参数:int _blockType -> 方块类型<br>* 参数:int _turnState -> 旋转状态<br>* 备注:由于原始方块图形数据不允许修改,因此只返回复制后的方块图形赝品<br>*/public static byte[][] getShape(int _blockType,int _turnState){byte[][] temp = shape[_blockType][_turnState];//开始复制int row = temp.length;int column = temp[0].length;byte[][] shapeMap = new byte[row][column];for(int i=0;i<row;i++){for(int j=0;j<column;j++){shapeMap[i][j] = temp[i][j];}}return shapeMap;}}

详解:

待续...

运行:

做一个DOS批处理文件,tetris.bat,内容如下:

@echo off
start javaw com.game.tetris.Tetris

下载:

        百度网盘链接:https://pan.baidu.com/s/1g1h9Zn1KmIoMWCMGLV1nCA 提取码:h5m2

感言:

Java游戏开发 —— 俄罗斯方块相关推荐

  1. Java游戏开发——俄罗斯方块

    游戏介绍 俄罗斯方块是一款风靡全球的电视游戏机和掌上游戏机的游戏,它曾经造成的轰动与造成的经济价值可以说是游戏史上的一件大事.这款游戏最初是由苏联的游戏制作人Alex Pajitnov制作的,它看似简 ...

  2. 【11款最全最新】Java游戏开发项目合集_Java项目实战_Java练手项目

    黄金矿工.俄罗斯方块.飞机大战.超级玛丽.坦克大战.飞翔的小鸟.扫雷.王者荣耀.推箱子.贪吃蛇.大鱼吃小鱼这些经典小游戏相信很多人都玩过.那大家有没有想过亲自制作出这些小游戏呢? 下面就给大家分享这1 ...

  3. 【11款最全最新】Java游戏开发项目合集(上)_Java项目实战_Java练手项目

    黄金矿工.俄罗斯方块.飞机大战.超级玛丽.坦克大战.飞翔的小鸟等等经典小游戏相信很多人都玩过.大家有没有想过亲自制作出这些小游戏呢? 今天给大家分享11款经典小游戏的Java开发教程,快来戳下方视频学 ...

  4. java游戏开发(java游戏开发教程)

    Java游戏开发绘图器是什么呢? 众所周知,Java GUI以paint进行绘图,以repaint进行图像刷新,而完成repaint及paint这一连贯过程中所用到绘图组件,我将其称为绘图器.就我个人 ...

  5. 【源码+图片素材+详细教程】Java游戏开发_Java开发经典游戏飞翔的小鸟_飞扬的小鸟_Java游戏项目Flappy Bird像素鸟游戏_Java课程设计项目

    课程目标: 1.通过本课程的学习巩固Java的相关基础知识,例如循环判断,数组和集合的使用,对象的继承,接口的实现,窗口的创建,事件监听,图形绘制. 2.完成小鸟的移动,管道自动生成.碰撞死亡,计分系 ...

  6. java游戏开发杂谈 - 实现游戏主菜单

    经常玩游戏的同学,大家都知道,游戏都会有个主菜单,里面有多个菜单选项:开始游戏.游戏设置.关于游戏.退出游戏等等,这个菜单是怎么实现的呢. 有一定桌面软件开发基础的同学可能会想到,用JButton组件 ...

  7. java游戏开发杂谈 - 游戏编程浅析

    每个游戏,你所看到的它的一切,都是计算机画出来的! 地图是画出来,人物是画出来的,树木建筑是画出来的,菜单按钮是画出来的,滚动的文字.闪烁的图标.云雾烟火,都是画出来的. 游戏编程,所要做的,就是控制 ...

  8. java游戏开发入门(十) -粒子特效

    java游戏开发入门十 - 粒子特效 java游戏开发入门十 - 粒子特效 前言 编码 创建一个粒子发射器,并将粒子发射器添加到实体对象 效果图 完整代码 完整项目 java游戏开发入门十 - 粒子特 ...

  9. Java游戏开发组件LGame简易测试版发布(版本号 0 1 5)

    LGame-Simple-0.1.5组件下载地址:http://code.google.com/p/loon-simple/downloads/list 2009-09-13 更新内容: Java游戏 ...

  10. 三七互娱 Java游戏开发工程师 面试(两轮技术+HR面)

    (2020春招补招,已拿到offer) 抱着投着试试看的心态去的 自己还是不太了解游戏开发 框架啊这些都没怎么问 比较重视基础知识,面的也基本都是基础知识 所以面完才发现自己太基础的知识反而一问三不知 ...

最新文章

  1. spring入门详细教程(五)
  2. 在 ubuntu 20.04 LTS 上安装 ROS2 执行 rosdep update 命令时出现的问题的解决办法
  3. js ajax 浏览器兼容,JS跨浏览器兼容,一点点总结
  4. 浅谈数据结构-平衡二叉树
  5. 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider...
  6. python新手入门总结_初学python的操作难点总结(新手必看篇)
  7. 跳转到企业缓存之前要考虑的事项
  8. 【luogu P5022 旅行】 题解
  9. 容大打印机ip修改工具_M1芯片版Mac无法连接打印机怎么办?
  10. 推销计算机英语作文,如何自我推销英语作文
  11. Python 列表 append()函数使用详解
  12. iOS开发字符串倒序,倒序单词字母,不倒序单词位置
  13. python3爬虫爬取网页图片简单示例
  14. 计算机电子表格计算怎么做,excel表格如何做总计|excel表格总计教程
  15. linux编辑vim指令,Linux系统文本编辑器vim指令大全
  16. 幸福总会来的,对吗?
  17. Geany下载与安装
  18. HttpClient的使用教程
  19. 软件测试常用的linux命令
  20. Java导入Excel数据

热门文章

  1. redis设计与实现 二
  2. 常用的laplace变换公式表
  3. 软件工程期末复习汇总
  4. 网络工程制图论文计算机,工程制图论文
  5. 星益80个在线小游戏HTML网页源码
  6. C++基于MFC编程——课程管理系统
  7. 三种class反编译工具jad、frontEndPlus、jd-gui 的使用方法
  8. termux 安装python3教程_termux怎么安装python
  9. GeForce Experience显示帧数
  10. OpenCV快速入门六:图解Numpy