這篇文章主要講解了“怎么用Java實(shí)現(xiàn)俄羅斯方塊小游戲”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Java實(shí)現(xiàn)俄羅斯方塊小游戲”吧!
成都創(chuàng)新互聯(lián)公司是一家以成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、營銷推廣、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都iso認(rèn)證等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
一、心得
二、游戲?qū)嵗?/p>
三、代碼
點(diǎn)擊獲取更多素材游戲源碼?。?/p>
百度盤鏈接
鏈接:http://pan.baidu.com/s/1mhQ9SYc 密碼:9ujo
目錄結(jié)構(gòu)
package com.hsj.tetris; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * 俄羅斯方塊游戲面板 * */ public class Tetris extends JPanel { /** 正在下落方塊 */ private Tetromino tetromino; /** 下一個下落方塊 */ private Tetromino nextOne; /** 行數(shù) */ public static final int ROWS = 20; /** 列數(shù) */ public static final int COLS = 10; /** 墻 */ private Cell[][] wall = new Cell[ROWS][COLS]; /** 消掉的行數(shù) */ private int lines; /** 分?jǐn)?shù) */ private int score; public static final int CELL_SIZE = 26; private static Image background;//背景圖片 public static Image I; public static Image J; public static Image L; public static Image S; public static Image Z; public static Image O; public static Image T; static{//加載靜態(tài)資源的,加載圖片 //建議將圖片放到 Tetris.java 同包中! //從包中加載圖片對象,使用Swing API實(shí)現(xiàn) // Toolkit toolkit = Toolkit.getDefaultToolkit(); // background = toolkit.getImage( // Tetris.class.getResource("tetris.png")); // T = toolkit.getImage(Tetris.class.getResource("T.png")); // S = toolkit.getImage(Tetris.class.getResource("S.png")); // Z = toolkit.getImage(Tetris.class.getResource("Z.png")); // L = toolkit.getImage(Tetris.class.getResource("L.png")); // J = toolkit.getImage(Tetris.class.getResource("J.png")); // I = toolkit.getImage(Tetris.class.getResource("I.png")); // O = toolkit.getImage(Tetris.class.getResource("O.png")); //import javax.imageio.ImageIO; try{ background = ImageIO.read( Tetris.class.getResource("tetris.png")); T=ImageIO.read(Tetris.class.getResource("T.png")); I=ImageIO.read(Tetris.class.getResource("I.png")); S=ImageIO.read(Tetris.class.getResource("S.png")); Z=ImageIO.read(Tetris.class.getResource("Z.png")); L=ImageIO.read(Tetris.class.getResource("L.png")); J=ImageIO.read(Tetris.class.getResource("J.png")); O=ImageIO.read(Tetris.class.getResource("O.png")); }catch(Exception e){ e.printStackTrace(); } } public void action(){ //tetromino = Tetromino.randomTetromino(); //nextOne = Tetromino.randomTetromino(); //wall[19][2] = new Cell(19,2,Tetris.T); startAction(); repaint(); KeyAdapter l = new KeyAdapter() { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_Q){ System.exit(0);//退出當(dāng)前的Java進(jìn)程 } if(gameOver){ if(key==KeyEvent.VK_S){ startAction(); } return; } //如果暫停并且按鍵是[C]就繼續(xù)動作 if(pause){//pause = false if(key==KeyEvent.VK_C){ continueAction(); } return; } //否則處理其它按鍵 switch(key){ case KeyEvent.VK_RIGHT: moveRightAction(); break; case KeyEvent.VK_LEFT: moveLeftAction(); break; case KeyEvent.VK_DOWN: softDropAction() ; break; case KeyEvent.VK_UP: rotateRightAction() ; break; case KeyEvent.VK_Z: rotateLeftAction() ; break; case KeyEvent.VK_SPACE: hardDropAction() ; break; case KeyEvent.VK_P: pauseAction() ; break; } repaint(); } }; this.requestFocus(); this.addKeyListener(l); } public void paint(Graphics g){ g.drawImage(background, 0, 0, null);//使用this 作為觀察者 g.translate(15, 15);//平移繪圖坐標(biāo)系 paintTetromino(g);//繪制正在下落的方塊 paintWall(g);//畫墻 paintNextOne(g); paintScore(g); } public static final int FONT_COLOR = 0x667799; public static final int FONT_SIZE = 0x20; private void paintScore(Graphics g) { Font f = getFont();//獲取當(dāng)前的 面板默認(rèn)字體 Font font = new Font( f.getName(), Font.BOLD, FONT_SIZE); int x = 290; int y = 162; g.setColor(new Color(FONT_COLOR)); g.setFont(font); String str = "SCORE:"+this.score; g.drawString(str, x, y); y+=56; str = "LINES:"+this.lines; g.drawString(str, x, y); y+=56; str = "[P]Pause"; if(pause){str = "[C]Continue";} if(gameOver){ str = "[S]Start!";} g.drawString(str, x, y); } private void paintNextOne(Graphics g) { Cell[] cells = nextOne.getCells(); for(int i=0; i=1; i--){ //復(fù)制 [i-1] -> [i] System.arraycopy(wall[i-1], 0, wall[i], 0, COLS); } Arrays.fill(wall[0], null); } /** 檢查當(dāng)前的4格方塊能否繼續(xù)下落 */ public boolean tetrominoCanDrop(){ Cell[] cells = tetromino.getCells(); for(int i = 0; i =COLS){ return true;//出界了 } } return false; } private boolean coincide(){ Cell[] cells = tetromino.getCells(); //for each 循環(huán)、迭代,簡化了"數(shù)組迭代書寫" for(Cell cell: cells){//Java 5 以后提供增強(qiáng)版for循環(huán) int row = cell.getRow(); int col = cell.getCol(); if(row<0 || row>=ROWS || col<0 || col>=COLS || wall[row][col]!=null){ return true; //墻上有格子對象,發(fā)生重合 } } return false; } /** 向右旋轉(zhuǎn)動作 */ public void rotateRightAction(){ //旋轉(zhuǎn)之前 //System.out.println(tetromino); tetromino.rotateRight(); //System.out.println(tetromino); //旋轉(zhuǎn)之后 if(outOfBound() || coincide()){ tetromino.rotateLeft(); } } /** Tetris 類中添加的方法 */ public void rotateLeftAction(){ tetromino.rotateLeft(); if(outOfBound() || coincide()){ tetromino.rotateRight(); } } public void hardDropAction(){ while(tetrominoCanDrop()){ tetromino.softDrop(); } tetrominoLandToWall(); destroyLines(); checkGameOver(); tetromino = nextOne; nextOne = Tetromino.randomTetromino(); } private boolean pause; private boolean gameOver; private Timer timer; /** Tetris 類中添加的方法, 用于啟動游戲 */ public void startAction(){ clearWall(); tetromino = Tetromino.randomTetromino(); nextOne = Tetromino.randomTetromino(); lines = 0; score = 0; pause=false; gameOver=false; timer = new Timer(); timer.schedule(new TimerTask(){ public void run() { softDropAction(); repaint(); } }, 700, 700); } private void clearWall(){ //將墻的每一行的每個格子清理為null for(int row=0; row 二、Cell.java
package com.fry.tetris; import java.awt.Image; /** * 格子 * 每一個小格子,就有所在的行 列 和圖片 */ public class Cell { private int row; private int col; //private int color; private Image image;//格子的貼圖 public Cell() { } public Cell(int row, int col, Image image) { super(); this.row = row; this.col = col; this.image = image; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getCol() { return col; } public void setCol(int col) { this.col = col; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } public void moveRight(){ col++; //System.out.println("Cell moveRight()" + col); } public void moveLeft(){ col--; } public void moveDown(){ row++; } @Override public String toString() { return "["+row+","+col+"]"; } }三、功能實(shí)現(xiàn) Tetromino.java
package com.fry.tetris; import java.util.Arrays; import java.util.Random; /** * 4格方塊 */ public class Tetromino { protected Cell[] cells = new Cell[4]; /** 保存旋轉(zhuǎn)的相對于軸位置狀態(tài) */ protected State[] states; /** 隨機(jī)生成 4格方塊, 使用簡單工廠方法模式! * randomTetromino 隨機(jī)生成一個四格方塊 * 這個方面的返回值是多態(tài)的! * */ public static Tetromino randomTetromino(){ Random r = new Random(); int type = r.nextInt(7); switch(type){ case 0: return new T(); case 1: return new I(); case 2: return new J(); case 3: return new L(); case 4: return new O(); case 5: return new S(); case 6: return new Z(); } return null; } public Cell[] getCells() { return cells; } /** 下落 */ public void softDrop(){ for(int i=0; i感謝各位的閱讀,以上就是“怎么用Java實(shí)現(xiàn)俄羅斯方塊小游戲”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用Java實(shí)現(xiàn)俄羅斯方塊小游戲這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站標(biāo)題:怎么用Java實(shí)現(xiàn)俄羅斯方塊小游戲
鏈接URL:http://weahome.cn/article/psjhgj.html