Java游戏---俄罗斯方块
生活随笔
收集整理的這篇文章主要介紹了
Java游戏---俄罗斯方块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個一個簡答的俄羅斯方塊,純干貨!!!
package cn.hncu.games;import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent;import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer;public class Tetris extends JFrame {private TetrisPanel tp;public Tetris() {// 添加菜單,這里只是意思一下,留待大家自己做更詳細的// 菜單條JMenuBar menubar = new JMenuBar();setJMenuBar(menubar);// 菜單JMenu menuGame = new JMenu("游戲");menubar.add(menuGame);// 菜單項JMenuItem mi1 = new JMenuItem("新游戲");mi1.setActionCommand("new");JMenuItem mi2 = new JMenuItem("暫停");mi2.setActionCommand("pause");JMenuItem mi3 = new JMenuItem("繼續");mi3.setActionCommand("continue");JMenuItem mi4 = new JMenuItem("退出");mi4.setActionCommand("exit");menuGame.add(mi1);menuGame.add(mi2);menuGame.add(mi3);menuGame.add(mi4);//菜單項監聽MenuListener menuListener = new MenuListener();mi1.addActionListener(menuListener);mi2.addActionListener(menuListener);mi3.addActionListener(menuListener);mi4.addActionListener(menuListener);// 版本菜單JMenu menuHelp = new JMenu("幫助");menubar.add(menuHelp);menuHelp.add("版本所有@湖南城院 QQ:666688888");setLocation(700, 200);setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(220, 275);setResizable(false);tp = new TetrisPanel();getContentPane().add(tp);// 讓整個畫布添加鍵盤監聽// tp.addKeyListener(tp.listener);//不行,畫布不方便獲得鍵盤焦點this.addKeyListener(tp.listener); // 讓框架來監聽鍵盤}public static void main(String[] args) {Tetris te = new Tetris();te.setVisible(true);}class MenuListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {if(e.getActionCommand().equalsIgnoreCase("new")){getContentPane().remove(tp);tp = new TetrisPanel();getContentPane().add(tp);getContentPane().validate();//校驗當前容器,有刷新功能 }else if(e.getActionCommand().equalsIgnoreCase("pause")){tp.getTimer().stop();}else if(e.getActionCommand().equalsIgnoreCase("continue")){tp.getTimer().restart();}else if(e.getActionCommand().equalsIgnoreCase("exit")){System.exit(0);}}} }class TetrisPanel extends JPanel {private int map[][] = new int[13][23];// map[列號][行號]。真正的方塊區是:21行*10列。邊框(2列,1行)// 方塊的形狀:// 第一維代表方塊類型(包括7種:S、Z、L、J、I、O、T)// 第二維代表旋轉次數// 第三四維代表方塊矩陣// shapes[type][turnState][i] i--> block[i/4][i%4]int shapes[][][] = new int[][][] {/** 模板 { {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,* 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0,0,0,0,0,0,0,0, 0,0,0,0,* 0,0,0,0} }*/// I (※把版本1中的橫條從第1行換到第2行){ { 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 } },// S{ { 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, 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 } },// Z 第3行: shapes[2][2][]{ { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 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= 0;private Timer timer = null;private int delay = 1000;TimerListener listener = null;public TetrisPanel() {newGame();nextBlock();listener = new TimerListener();timer = new Timer(delay, listener);timer.start();}private void newGame() {// 初始化游戲地圖for (int i = 0; i < 12; i++) {for (int j = 0; j < 21; j++) {if (i == 0 || i == 11) {// 邊框map[i][j] = 3;} else {map[i][j] = 0;}}map[i][21] = 3;}score = 0;}private void nextBlock() {type = (int) (Math.random() * 1000) % 7; // type=5;turnState = (int) (Math.random() * 1000) % 4; // turnState=3;x = 4;y = 0;if (crash(x, y, type, turnState) == 0) {timer.stop();int op = JOptionPane.showConfirmDialog(null,"Game Over!....笨蛋,敢再來一局嗎?!");if (op == JOptionPane.YES_OPTION) {newGame();} else if (op == JOptionPane.NO_OPTION) {System.exit(0);}}}private void down() {if (crash(x, y + 1, type, turnState) == 0) {// 判斷當前塊往下落一格后是否和地圖存在填充塊完全重合---注意實參:y+1add(x, y, type, turnState);// 把該塊加到地圖---形成堆積塊nextBlock();} else {y++;}repaint();}private void left() {if (x >= 0) {x -= crash(x - 1, y, type, turnState);}repaint();}private void right() {if (x < 8) {x += crash(x + 1, y, type, turnState);}repaint();}private void turn() {if (crash(x, y, type, (turnState + 1) % 4) == 1) {turnState = (turnState + 1) % 4;}repaint();}// 讓一個塊堆積,其實是把當前塊中的填充塊信息記錄到map[][]中private void add(int x, int y, int type, int turnState) {for (int a = 0; a < 4; a++) {for (int b = 0; b < 4; b++) {if (shapes[type][turnState][a * 4 + b] == 1) {map[x + b + 1][y + a] = 1;}}}tryDelLine();}// 消塊private void tryDelLine() {// 從上往下,一行行依次遍歷,如果某一行的map[i][j]值全是1,則把這一行消掉---上一行往下落for (int b = 0; b < 21; b++) {int c = 1;for (int a = 0; a < 12; a++) {c &= map[a][b];}if (c == 1) {// 全是1--下落一行score += 10;for (int d = b; d > 0; d--) {for (int e = 0; e < 11; e++) {map[e][d] = map[e][d - 1];}}// 更改游戲的難度(加快下落速度)delay /= 2;timer.setDelay(delay);}}}private int crash(int x, int y, int blockType, int turnState) {for (int a = 0; a < 4; a++) {for (int b = 0; b < 4; b++) {if ((shapes[blockType][turnState][a * 4 + b] & map[x + b + 1][y+ a]) == 1) {// 和填充塊或框架重合,都算碰撞return 0; // 碰撞了---方塊的填充塊和地圖中的填充塊完全重合}}}return 1;// 沒有碰撞}// 表現層@Overridepublic void paint(Graphics g) {super.paint(g);// 清除殘影// 畫當前塊for (int j = 0; j < 16; j++) {if (shapes[type][turnState][j] == 1) {g.setColor(Color.green);g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);}}/** for(int a=0;a<4;a++){ for(int b=0;b<4;b++){* if(shapes[type][turnState][a*4+b]==1){ g.fillRect((b+x+1)*10,* (a+y)*10, 10, 10); } } }*/// 畫地圖(整個游戲的方塊區和邊框)for (int i = 0; i < 12; i++) {for (int j = 0; j < 22; j++) {if (map[i][j] == 1) {g.setColor(Color.red);g.fillRect(i * 10, j * 10, 10, 10);// 填充g.setColor(Color.yellow);g.drawRect(i * 10, j * 10, 10, 10);// 格線} else if (map[i][j] == 3) {g.setColor(Color.red);g.drawRect(i * 10, j * 10, 10, 10);}}}// 顯示分數,同時為版面美觀,在界面上再加點東西// 畫方塊區右側部分g.setColor(Color.blue);g.setFont(new Font("aa", Font.BOLD, 18));g.drawString("score=" + score, 130, 20);g.setFont(new Font("aa", Font.PLAIN, 13));g.drawString("拒絕盜版游戲", 130, 70);g.drawString("注意自我保護", 130, 90);g.drawString("謹防受騙上當。", 125, 110);g.drawString("適度游戲益腦,", 125, 130);g.drawString("沉迷游戲傷身。", 125, 150);g.drawString("合理安排時間,", 125, 170);g.drawString("享受健康生活。", 125, 190);}class TimerListener extends KeyAdapter implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {down();}@Overridepublic void keyPressed(KeyEvent e) {// System.out.println("aaaaa");switch (e.getKeyCode()) {case KeyEvent.VK_DOWN:down();break;case KeyEvent.VK_LEFT:left();break;case KeyEvent.VK_RIGHT:right();break;case KeyEvent.VK_UP:turn();}}}public Timer getTimer() {return timer;} }總結
以上是生活随笔為你收集整理的Java游戏---俄罗斯方块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云的ECS服务器1M宽带等于多少入网
- 下一篇: java美元兑换,(Java实现) 美元