俄羅斯方塊源碼_第1頁
俄羅斯方塊源碼_第2頁
俄羅斯方塊源碼_第3頁
俄羅斯方塊源碼_第4頁
俄羅斯方塊源碼_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java俄羅斯方塊源碼import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.*;import javax.swing.Timer;public class Tetris extends JFrame public Tetris() Tetrisblok a = new Tetrisblok(); addKe

2、yListener(a); add(a); public static void main(String args) Tetris frame = new Tetris(); JMenuBar menu = new JMenuBar(); frame.setJMenuBar(menu); JMenu game = new JMenu("游戲"); JMenuItem newgame = game.add("新游戲"); JMenuItem pause = game.add("暫停"); JMenuItem goon = game.ad

3、d("繼續(xù)"); JMenuItem exit = game.add("退出"); JMenu help = new JMenu("幫助"); JMenuItem about = help.add("關于"); menu.add(game); menu.add(help); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(220, 275); frame.se

4、tTitle("Tetris內測版"); / frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false); / 創(chuàng)建一個俄羅斯方塊類class Tetrisblok extends JPanel implements KeyListener / blockType 代表方塊類型 / turnState代表方塊狀態(tài) private int blockType; private int score = 0; private int turnState; private int x;

5、private int y; private int i = 0; int j = 0; int flag = 0; / 定義已經(jīng)放下的方塊x=0-11,y=0-21; int map = new int1323; / 方塊的形狀 第一組代表方塊類型有S、Z、L、J、I、O、T 7種 第二組 代表旋轉幾次 第三四組為 方塊矩陣 private final int shapes = new int / 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

6、, 0, 0, 0, 0, 1, 1, 1, 1, 0, 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0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 , / 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

10、, 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 ; / 生成新方塊的方法 public void newblock() blockType = (int) (Math.random() * 1000) % 7; turnState = (int) (Math.random() * 1000) % 4; x = 4; y = 0; if (gameover(x, y) = 1) newmap(); drawwall(); score = 0; JOp

11、tionPane.showMessageDialog(null, "GAME OVER"); / 畫圍墻 public void drawwall() for (i = 0; i < 12; i+) mapi21 = 2; for (j = 0; j < 22; j+) map11j = 2; map0j = 2; / 初始化地圖 public void newmap() for (i = 0; i < 12; i+) for (j = 0; j < 22; j+) mapij = 0; / 初始化構造方法 Tetrisblok() newblock

12、(); newmap(); drawwall(); Timer timer = new Timer(1000, new TimerListener(); timer.start(); / 旋轉的方法 public void turn() int tempturnState = turnState; turnState = (turnState + 1) % 4; if (blow(x, y, blockType, turnState) = 1) if (blow(x, y, blockType, turnState) = 0) turnState = tempturnState; repain

13、t(); / 左移的方法 public void left() if (blow(x - 1, y, blockType, turnState) = 1) x = x - 1; ; repaint(); / 右移的方法 public void right() if (blow(x + 1, y, blockType, turnState) = 1) x = x + 1; ; repaint(); / 下落的方法 public void down() if (blow(x, y + 1, blockType, turnState) = 1) y = y + 1; delline(); ; if

14、(blow(x, y + 1, blockType, turnState) = 0) add(x, y, blockType, turnState); newblock(); delline(); ; repaint(); / 是否合法的方法 public int blow(int x, int y, int blockType, int turnState) for (int a = 0; a < 4; a+) for (int b = 0; b < 4; b+) if (shapesblockTypeturnStatea * 4 + b = 1) && (map

15、x + b + 1y + a = 1) | (shapesblockTypeturnStatea * 4 + b = 1) && (mapx + b + 1y + a = 2) return 0; return 1; / 消行的方法 public void delline() int c = 0; for (int b = 0; b < 22; b+) for (int a = 0; a < 12; a+) if (mapab = 1) c = c + 1; if (c = 10) score += 10; for (int d = b; d > 0; d-)

16、 for (int e = 0; e < 11; e+) maped = maped - 1; c = 0; / 判斷你掛的方法 public int gameover(int x, int y) if (blow(x, y, blockType, turnState) = 0) return 1; return 0; / 把當前添加map public void add(int x, int y, int blockType, int turnState) int j = 0; for (int a = 0; a < 4; a+) for (int b = 0; b < 4

17、; b+) if (mapx + b + 1y + a = 0) mapx + b + 1y + a = shapesblockTypeturnStatej; ; j+; / 畫方塊的的方法 public void paintComponent(Graphics g) super.paintComponent(g); / 畫當前方塊 for (j = 0; j < 16; j+) if (shapesblockTypeturnStatej = 1) g.fillRect(j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10); / 畫已經(jīng)固定的方塊

18、for (j = 0; j < 22; j+) for (i = 0; i < 12; i+) if (mapij = 1) g.fillRect(i * 10, j * 10, 10, 10); if (mapij = 2) g.drawRect(i * 10, j * 10, 10, 10); g.drawString("score=" + score, 125, 10); g.drawString("抵制不良游戲,", 125, 50); g.drawString("拒絕盜版游戲。", 125, 70); g.drawString("注意自我保護,", 125, 90); g.drawString("謹防受騙上當。", 125, 110); g.drawString("適度游戲益腦,", 125, 130); g.drawString("沉迷游戲傷身。", 125, 150); g.drawString("合理安排時間,", 125, 170); g.drawString("享受健康生活。&quo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論