您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用java實現一個俄羅斯方塊游戲,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
鍵盤操作:
左鍵:左移; 右鍵:右移;
上鍵:變換造型 下鍵:加速下掉(沒毛病吧,沒有繼續整)
任意一行的方塊滿格,這一行就消除,消除一行方塊得10分,目前小主我還沒有設置關卡,各位喜歡的寶寶們可以自己設置關卡哦;
那么那些方塊的造型到底從哪里來的呢,那就是我們自己設計的,常見的幾種造型就是:I型,T型,L型,田字格型等等吧,自己個加唄!
那么到底咋整的咧?其實啊就是一個4*4的數組,當然了你開心設計n*n也可以,你牛皮你說了算!
那么下面舉了一個例子,用來告訴你們為啥你們看見的造型可以變換的原因就是這樣提前設計好,0為空,1為填充格,這樣你就可以在你的游戲里面凹造型了!
算了:直接放圖先看代碼運行結果吧:
喜歡嗎?喜歡就直接做吧,可能代碼寫的不夠好,請各位大神多多包涵,我回頭也會多總結,會不斷更新代碼的;
GamePanel類:游戲界面類,整個方塊掉落和顯示,游戲的邏輯斯洛都在這個類里面實現;
package tetris; 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 java.util.Random; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel implements KeyListener{ private int mapRow = 21; private int mapCol = 12; private int mapGame[][] = new int[mapRow][mapCol];//開辟一個二維數組空間,用來存放我們的地圖信息 private Timer timer; private int score = 0;//記錄成績 Random random = new Random(); private int curShapeType = -1; private int curShapeState = -1;//設置當前的形狀類型和當前的形狀狀態 private int nextShapeType = -1; private int nextShapeState = -1;//設置下一次出現的方塊組的類型和狀態 private int posx = 0; private int posy = 0; private final int shapes[][][] = new int[][][]{ //T字形按逆時針的順序存儲 { {0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}, {0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}, {1,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0}, {0,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0} }, //I字形按逆時針的順序存儲 { {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形按逆時針的順序存儲 { {0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}, {1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}, {0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}, {1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0} }, //Z形按逆時針的順序存儲 { {1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0}, {0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0}, {1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0}, {0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0} }, //J字形按逆時針的順序存儲 { {0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0}, {1,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0}, {1,1,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0}, {1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0} }, //L字形按逆時針的順序存儲 { {1,0,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0}, {0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}, {1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0}, {1,1,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0} }, //田字形按逆時針的順序存儲 { {1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}, {1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}, {1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}, {1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0} } }; private int rowRect = 4; private int colRect = 4;//這里我們把存儲的圖像看成是一個4*4的二維數組,雖然在上面我們采用一維數組來存儲,但實際還是要看成二維數組來實現 private int RectWidth = 10; public GamePanel()//構造函數----創建好地圖 { CreateRect(); initMap();//初始化這個地圖 SetWall();//設置墻 // CreateRect(); timer = new Timer(500,new TimerListener()); timer.start(); } class TimerListener implements ActionListener{ public void actionPerformed(ActionEvent e) { MoveDown(); } } public void SetWall()//第0列和第11列都是墻,第20行也是墻 { for(int i = 0; i < mapRow; i++)//先畫列 { mapGame[i][0] = 2; mapGame[i][11] = 2; } for(int j = 1; j < mapCol-1; j++)//畫最后一行 { mapGame[20][j] = 2; } } public void initMap()//初始化這個地圖,墻的ID是2,空格的ID是0,方塊的ID是1 { for(int i = 0; i < mapRow; i++) { for(int j = 0; j < mapCol; j++) { mapGame[i][j] = 0; } } } public void CreateRect()//創建方塊---如果當前的方塊類型和狀態都存在就設置下一次的,如果不存在就設置當前的并且設置下一次的狀態和類型 { if(curShapeType == -1 && curShapeState == -1)//當前的方塊狀態都為1,表示游戲才開始 { curShapeType = random.nextInt(shapes.length); curShapeState = random.nextInt(shapes[0].length); } else { curShapeType = nextShapeType; curShapeState = nextShapeState; } nextShapeType = random.nextInt(shapes.length); nextShapeState = random.nextInt(shapes[0].length); posx = 0; posy = 1;//墻的左上角創建方塊 if(GameOver(posx,posy,curShapeType,curShapeState)) { JOptionPane.showConfirmDialog(null, "游戲結束!", "提示", JOptionPane.OK_OPTION); System.exit(0); } } public boolean GameOver(int x, int y, int ShapeType, int ShapeState)//判斷游戲是否結束 { if(IsOrNoMove(x,y,ShapeType,ShapeState)) { return false; } return true; } public boolean IsOrNoMove(int x, int y, int ShapeType, int ShapeState)//判斷當前的這個圖形是否可以移動,這里重點強調x,y的坐標是指4*4的二維數組(描述圖形的那個數組)的左上角目標 { for(int i = 0; i < rowRect ; i++) { for(int j = 0; j < colRect; j++) { if(shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 1 || shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 2) { return false; } } } return true; } public void Turn()//旋轉 { int temp = curShapeState; curShapeState = (curShapeState+1) % shapes[0].length; if(IsOrNoMove(posx,posy,curShapeType,curShapeState)) { } else { curShapeState = temp; } repaint(); } public void MoveDown()//向下移動 { if(IsOrNoMove(posx+1,posy,curShapeType,curShapeState)) { posx++; } else { AddToMap();//將此行固定在地圖中 CheckLine(); CreateRect();//重新創建一個新的方塊 } repaint(); } public void MoveLeft()//向左移動 { if(IsOrNoMove(posx,posy-1,curShapeType,curShapeState)) { posy--; } repaint(); } public void MoveRight()//向右移動 { if(IsOrNoMove(posx,posy+1,curShapeType,curShapeState)) { posy++; } repaint(); } public void AddToMap()//固定掉下來的這一圖像到地圖中 { for(int i = 0; i < rowRect; i++) { for(int j = 0; j < colRect; j++) { if(shapes[curShapeType][curShapeState][i*colRect+j] == 1) { mapGame[posx+i][posy+j] = shapes[curShapeType][curShapeState][i*colRect+j]; } } } } public void CheckLine()//檢查一下這些行中是否有滿行的 { int count = 0; for(int i = mapRow-2; i >= 0; i--) { count = 0; for(int j = 1; j < mapCol-1; j++) { if(mapGame[i][j] == 1) { count++; } else break; } if(count >= mapCol-2) { for(int k = i; k > 0; k--) { for(int p = 1; p < mapCol-1; p++) { mapGame[k][p] = mapGame[k-1][p]; } } score += 10; i++; } } } public void paint(Graphics g)//重新繪制窗口 { super.paint(g); for(int i = 0; i < rowRect; i++)//繪制正在下落的方塊 { for(int j = 0; j < colRect; j++) { if(shapes[curShapeType][curShapeState][i*colRect+j] == 1) { g.fillRect((posy+j+1)*RectWidth, (posx+i+1)*RectWidth, RectWidth, RectWidth); } } } for(int i = 0; i < mapRow; i++)//繪制地圖上面已經固定好的方塊信息 { for(int j = 0; j < mapCol; j++) { if(mapGame[i][j] == 2)//畫墻 { g.drawRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth); } if(mapGame[i][j] == 1)//畫小方格 { g.fillRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth); } } } g.drawString("score = "+ score, 225, 15); g.drawString("下一個方塊:", 225, 50); for(int i = 0; i < rowRect; i++) { for(int j = 0; j < colRect; j++) { if(shapes[nextShapeType][nextShapeState][i*colRect+j] == 1) { g.fillRect(225+(j*RectWidth), 100+(i*RectWidth), RectWidth, RectWidth); } } } } public void NewGame()//游戲重新開始 { score = 0; initMap(); SetWall(); CreateRect(); repaint(); } public void StopGame()//游戲暫停 { timer.stop(); } public void ContinueGame() { timer.start(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_UP://上----旋轉 Turn(); break; case KeyEvent.VK_DOWN://下----向下移動 MoveDown(); break; case KeyEvent.VK_LEFT://左----向左移動 MoveLeft(); break; case KeyEvent.VK_RIGHT://右----向右移動 MoveRight(); break; } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }
GameFrame類:整個游戲的進入口,好吧,說白了就是有main()函數的類,這個類里面實現游戲界面的一些設計,你可以理解為一個小小小小的UI;
package tetris; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; public class GameFrame extends JFrame implements ActionListener{ private int widthFrame = 500; private int heightFrame = 600; private JMenu menuone = new JMenu("游戲");//創建一個菜單 private JMenuItem newGame = menuone.add("重新開始");//創建一個內置菜單選項 private JMenuItem exitGame = menuone.add("游戲退出"); private JMenuItem stopGame = menuone.add("游戲暫停"); private JMenuItem goOnGame = menuone.add("游戲繼續"); private JMenu menutwo = new JMenu("幫助");//創建第二個菜單 private JMenuItem aboutGame = menutwo.add("關于游戲"); GamePanel gamepanel = new GamePanel(); public GameFrame()//構造函數 { addKeyListener(gamepanel); newGame.addActionListener(this); exitGame.addActionListener(this); stopGame.addActionListener(this); goOnGame.addActionListener(this); aboutGame.addActionListener(this); this.add(gamepanel); JMenuBar menu = new JMenuBar(); menu.add(menuone); menu.add(menutwo); this.setJMenuBar(menu); this.setTitle("俄羅斯方塊"); this.setBounds(50, 10, widthFrame, heightFrame); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource() == newGame)//游戲重新開始 { gamepanel.NewGame(); } if(e.getSource() == exitGame)//游戲退出 { System.exit(0); } if(e.getSource() == stopGame)//游戲暫停 { gamepanel.StopGame(); } if(e.getSource() == goOnGame)//游戲繼續 { gamepanel.ContinueGame(); } if(e.getSource() == aboutGame)//關于游戲信息 { JOptionPane.showMessageDialog(null, "左右鍵移動,向上建旋轉", "提示", JOptionPane.OK_OPTION); } } public static void main(String[] args) { new GameFrame(); } }
Java中的集合主要分為四類:1、List列表:有序的,可重復的;2、Queue隊列:有序,可重復的;3、Set集合:不可重復;4、Map映射:無序,鍵唯一,值不唯一。
上述內容就是使用java實現一個俄羅斯方塊游戲,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。