为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

俄罗斯方块源代码

2017-10-06 1页 doc 28KB 21阅读

用户头像

is_036899

暂无简介

举报
俄罗斯方块源代码俄罗斯方块源代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ggg extends JFrame { public ggg() { TetrisPanel a = new TetrisPanel(); addKeyListener(a.listener); add(a); JMenuBar menu = new JMenuBar(); JMenu game = new JMenu("ÓÎÏ?"...
俄罗斯方块源代码
俄罗斯方块源代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ggg extends JFrame { public ggg() { TetrisPanel a = new TetrisPanel(); addKeyListener(a.listener); add(a); JMenuBar menu = new JMenuBar(); JMenu game = new JMenu("ÓÎÏ?"); game.add("ÐÂÓÎÏ?"); game.add("ÔÝÍ?"); game.add("?ÌÐø"); game.add("ÍË?ö"); JMenu help = new JMenu("?ïÖú"); help.add("?ØÓÚ"); menu.add(game); menu.add(help); this.setJMenuBar(menu); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(220, 275); setTitle("TetrisÄÚ?â?æ"); setResizable(false); } public static void main(String[] args) { new ggg().setVisible(true); } } // ????Ò??ö?íÂÞË????éÀà class TetrisPanel extends JPanel { public TimerListener listener = new TimerListener(); // blockType ?ú?í???éÀàÐÍ // turnState?ú?í???é×?Ì? private int blockType; private int score = 0; private int turnState; private int x; private int y; int flag = 0; // ??ÒåÒÑ?,?ÅϵÄ???éx=0-11,y=0-21; int[][] map = new int[13][23]; // ???éµÄÐÎ×? µÚÒ?×é?ú?í???éÀàÐÍÓÐ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 }, { 0, 0, 0, 0, 1,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, { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 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 }, { 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 TetrisPanel() { nextBlock(); newGame(); new Timer(1000, listener).start(); } // Éú?ÉÐÂ???éµÄ???? private void nextBlock() { blockType = (int) (Math.random() * 1000) % 7; turnState = (int) (Math.random() * 1000) % 4; x = 4; y = 0; if (crash(x, y, blockType, turnState) == 0) { JOptionPane.showMessageDialog(null, "GAME OVER"); newGame(); } } // ?õÊ???µØÍ? private void newGame() { score = 0; for (int i = 0; i < 12; i++) { for (int j = 0; j < 22; j++) { map[i][j] = 0; map[11][j] = map[0][j] = 3; } map[i][21] = 3; } } // ÐýתµÄ???? private void turn() { turnState = (crash(x, y, blockType, (turnState + 1) % 4) + turnState) % 4; repaint(); } // ×óÒƵÄ???? private void left() { x -= crash(x - 1, y, blockType, turnState); repaint(); } // ÓÒÒƵÄ???? private void right() { x += crash(x + 1, y, blockType, turnState); repaint(); } // ÏÂÂäµÄ???? private void down() { y += crash(x, y + 1, blockType, turnState); if (crash(x, y + 1, blockType, turnState) == 0) { add(x, y, blockType, turnState); nextBlock(); } repaint(); } // ÊÇ?ñÅö×?µÄ???? 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; } // ??ÊÔÏûÐеÄ???? private void tryDelLine() { for (int b = 0; b < 21; b++) { int c = 1; for (int a = 0; a < 12; a++) { c &= map[a][b]; } if (c == 1) { score += 10; for (int d = b; d > 0; d--) { for (int e = 0; e < 11; e++) { map[e][d] = map[e][d - 1]; } } } } } // ?ѵ?Ç?Ìí?Ómap private void add(int x, int y, int blockType, int turnState) { for (int a = 0; a < 4; a++) { for (int b = 0; b < 4; b++) { map[x + b + 1][y + a] |= shapes[blockType][turnState][a * 4 + b]; } } tryDelLine(); } // ?,???éµÄµÄ???? public void paintComponent(Graphics g) { super.paintComponent(g); // ?,µ?Ç????é for (int j = 0; j < 16; j++) { if (shapes[blockType][turnState][j] == 1) { g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10); } } // ?,ÒÑ?,?Ì??µÄ???é for (int j = 0; j < 22; j++) { for (int i = 0; i < 12; i++) { if (map[i][j] == 1) { g.fillRect(i * 10, j * 10, 10, 10); } else if (map[i][j] == 3) { 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("ÏíÊÜ???µÉú?î??", 125, 190); } // ??Ê?Æ??àÌýºÍ?üÅÌ?àÌý class TimerListener extends KeyAdapter implements ActionListener { public void actionPerformed(ActionEvent e) { down(); } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_DOWN: down(); break; case KeyEvent.VK_UP: turn(); break; case KeyEvent.VK_RIGHT: right(); break; case KeyEvent.VK_LEFT: left(); } } } }
/
本文档为【俄罗斯方块源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索