本篇文章為大家展示了使用java實(shí)現(xiàn)一個(gè)俄羅斯方塊游戲,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)主要從事做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)白銀區(qū),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):13518219792
鍵盤(pán)操作:
左鍵:左移; 右鍵:右移;
上鍵:變換造型 下鍵:加速下掉(沒(méi)毛病吧,沒(méi)有繼續(xù)整)
任意一行的方塊滿(mǎn)格,這一行就消除,消除一行方塊得10分,目前小主我還沒(méi)有設(shè)置關(guān)卡,各位喜歡的寶寶們可以自己設(shè)置關(guān)卡哦;
那么那些方塊的造型到底從哪里來(lái)的呢,那就是我們自己設(shè)計(jì)的,常見(jiàn)的幾種造型就是:I型,T型,L型,田字格型等等吧,自己個(gè)加唄!
那么到底咋整的咧?其實(shí)啊就是一個(gè)4*4的數(shù)組,當(dāng)然了你開(kāi)心設(shè)計(jì)n*n也可以,你牛皮你說(shuō)了算!
那么下面舉了一個(gè)例子,用來(lái)告訴你們?yōu)樯赌銈兛匆?jiàn)的造型可以變換的原因就是這樣提前設(shè)計(jì)好,0為空,1為填充格,這樣你就可以在你的游戲里面凹造型了!
算了:直接放圖先看代碼運(yùn)行結(jié)果吧:
喜歡嗎?喜歡就直接做吧,可能代碼寫(xiě)的不夠好,請(qǐng)各位大神多多包涵,我回頭也會(huì)多總結(jié),會(huì)不斷更新代碼的;
GamePanel類(lèi):游戲界面類(lèi),整個(gè)方塊掉落和顯示,游戲的邏輯斯洛都在這個(gè)類(lèi)里面實(shí)現(xiàn);
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];//開(kāi)辟一個(gè)二維數(shù)組空間,用來(lái)存放我們的地圖信息 private Timer timer; private int score = 0;//記錄成績(jī) Random random = new Random(); private int curShapeType = -1; private int curShapeState = -1;//設(shè)置當(dāng)前的形狀類(lèi)型和當(dāng)前的形狀狀態(tài) private int nextShapeType = -1; private int nextShapeState = -1;//設(shè)置下一次出現(xiàn)的方塊組的類(lèi)型和狀態(tài) private int posx = 0; private int posy = 0; private final int shapes[][][] = new int[][][]{ //T字形按逆時(shí)針的順序存儲(chǔ) { {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字形按逆時(shí)針的順序存儲(chǔ) { {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形按逆時(shí)針的順序存儲(chǔ) { {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形按逆時(shí)針的順序存儲(chǔ) { {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字形按逆時(shí)針的順序存儲(chǔ) { {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字形按逆時(shí)針的順序存儲(chǔ) { {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} }, //田字形按逆時(shí)針的順序存儲(chǔ) { {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;//這里我們把存儲(chǔ)的圖像看成是一個(gè)4*4的二維數(shù)組,雖然在上面我們采用一維數(shù)組來(lái)存儲(chǔ),但實(shí)際還是要看成二維數(shù)組來(lái)實(shí)現(xiàn) private int RectWidth = 10; public GamePanel()//構(gòu)造函數(shù)----創(chuàng)建好地圖 { CreateRect(); initMap();//初始化這個(gè)地圖 SetWall();//設(shè)置墻 // 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++)//先畫(huà)列 { mapGame[i][0] = 2; mapGame[i][11] = 2; } for(int j = 1; j < mapCol-1; j++)//畫(huà)最后一行 { mapGame[20][j] = 2; } } public void initMap()//初始化這個(gè)地圖,墻的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()//創(chuàng)建方塊---如果當(dāng)前的方塊類(lèi)型和狀態(tài)都存在就設(shè)置下一次的,如果不存在就設(shè)置當(dāng)前的并且設(shè)置下一次的狀態(tài)和類(lèi)型 { if(curShapeType == -1 && curShapeState == -1)//當(dāng)前的方塊狀態(tài)都為1,表示游戲才開(kāi)始 { 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;//墻的左上角創(chuàng)建方塊 if(GameOver(posx,posy,curShapeType,curShapeState)) { JOptionPane.showConfirmDialog(null, "游戲結(jié)束!", "提示", JOptionPane.OK_OPTION); System.exit(0); } } public boolean GameOver(int x, int y, int ShapeType, int ShapeState)//判斷游戲是否結(jié)束 { if(IsOrNoMove(x,y,ShapeType,ShapeState)) { return false; } return true; } public boolean IsOrNoMove(int x, int y, int ShapeType, int ShapeState)//判斷當(dāng)前的這個(gè)圖形是否可以移動(dòng),這里重點(diǎn)強(qiáng)調(diào)x,y的坐標(biāo)是指4*4的二維數(shù)組(描述圖形的那個(gè)數(shù)組)的左上角目標(biāo) { 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()//旋轉(zhuǎn) { int temp = curShapeState; curShapeState = (curShapeState+1) % shapes[0].length; if(IsOrNoMove(posx,posy,curShapeType,curShapeState)) { } else { curShapeState = temp; } repaint(); } public void MoveDown()//向下移動(dòng) { if(IsOrNoMove(posx+1,posy,curShapeType,curShapeState)) { posx++; } else { AddToMap();//將此行固定在地圖中 CheckLine(); CreateRect();//重新創(chuàng)建一個(gè)新的方塊 } repaint(); } public void MoveLeft()//向左移動(dòng) { if(IsOrNoMove(posx,posy-1,curShapeType,curShapeState)) { posy--; } repaint(); } public void MoveRight()//向右移動(dòng) { if(IsOrNoMove(posx,posy+1,curShapeType,curShapeState)) { posy++; } repaint(); } public void AddToMap()//固定掉下來(lái)的這一圖像到地圖中 { 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()//檢查一下這些行中是否有滿(mǎn)行的 { 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++)//繪制地圖上面已經(jīng)固定好的方塊信息 { for(int j = 0; j < mapCol; j++) { if(mapGame[i][j] == 2)//畫(huà)墻 { g.drawRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth); } if(mapGame[i][j] == 1)//畫(huà)小方格 { g.fillRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth); } } } g.drawString("score = "+ score, 225, 15); g.drawString("下一個(gè)方塊:", 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()//游戲重新開(kāi)始 { 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://上----旋轉(zhuǎn) Turn(); break; case KeyEvent.VK_DOWN://下----向下移動(dòng) MoveDown(); break; case KeyEvent.VK_LEFT://左----向左移動(dòng) MoveLeft(); break; case KeyEvent.VK_RIGHT://右----向右移動(dòng) MoveRight(); break; } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }
GameFrame類(lèi):整個(gè)游戲的進(jìn)入口,好吧,說(shuō)白了就是有main()函數(shù)的類(lèi),這個(gè)類(lèi)里面實(shí)現(xiàn)游戲界面的一些設(shè)計(jì),你可以理解為一個(gè)小小小小的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("游戲");//創(chuàng)建一個(gè)菜單 private JMenuItem newGame = menuone.add("重新開(kāi)始");//創(chuàng)建一個(gè)內(nèi)置菜單選項(xiàng) private JMenuItem exitGame = menuone.add("游戲退出"); private JMenuItem stopGame = menuone.add("游戲暫停"); private JMenuItem goOnGame = menuone.add("游戲繼續(xù)"); private JMenu menutwo = new JMenu("幫助");//創(chuàng)建第二個(gè)菜單 private JMenuItem aboutGame = menutwo.add("關(guān)于游戲"); GamePanel gamepanel = new GamePanel(); public GameFrame()//構(gòu)造函數(shù) { 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)//游戲重新開(kāi)始 { gamepanel.NewGame(); } if(e.getSource() == exitGame)//游戲退出 { System.exit(0); } if(e.getSource() == stopGame)//游戲暫停 { gamepanel.StopGame(); } if(e.getSource() == goOnGame)//游戲繼續(xù) { gamepanel.ContinueGame(); } if(e.getSource() == aboutGame)//關(guān)于游戲信息 { JOptionPane.showMessageDialog(null, "左右鍵移動(dòng),向上建旋轉(zhuǎn)", "提示", JOptionPane.OK_OPTION); } } public static void main(String[] args) { new GameFrame(); } }
Java中的集合主要分為四類(lèi):1、List列表:有序的,可重復(fù)的;2、Queue隊(duì)列:有序,可重復(fù)的;3、Set集合:不可重復(fù);4、Map映射:無(wú)序,鍵唯一,值不唯一。
上述內(nèi)容就是使用java實(shí)現(xiàn)一個(gè)俄羅斯方塊游戲,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。