ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:18.69KB ,
资源ID:6732455      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6732455.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java编写俄罗斯方块三.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java编写俄罗斯方块三.docx

1、java编写俄罗斯方块三/* * File: ErsBlocksGame.java * User: Administrator * Date: Jan 15, 2003 * Describe: 俄罗斯方块的 Java 实现 */import javax.swing.*;import java.awt.*;import java.awt.event.*;/* * 游戏主类,继承自JFrame类,负责游戏的全局控制。 * 内含 * 1, 一个GameCanvas画布类的实例引用, * 2, 一个保存当前活动块(ErsBlock)实例的引用, * 3, 一个保存当前控制面板(ControlPanel

2、)实例的引用; */public class ErsBlocksGame extends JFrame /* * 每填满一行计多少分 */ public final static int PER_LINE_SCORE = 100; /* * 积多少分以后能升级 */ public final static int PER_LEVEL_SCORE = PER_LINE_SCORE * 20; /* * 最大级数是10级 */ public final static int MAX_LEVEL = 10; /* * 默认级数是5 */ public final static int DEFAULT

3、_LEVEL = 5; private GameCanvas canvas; private ErsBlock block; private boolean playing = false; private ControlPanel ctrlPanel; private JMenuBar bar = new JMenuBar(); private JMenu mGame = new JMenu(Game), mControl = new JMenu(Control), mWindowStyle = new JMenu(WindowStyle), mInfo = new JMenu(Inform

4、ation); private JMenuItem miNewGame = new JMenuItem(New Game), miSetBlockColor = new JMenuItem(Set Block Color .), miSetBackColor = new JMenuItem(Set Background Color .), miTurnHarder = new JMenuItem(Turn up the level), miTurnEasier = new JMenuItem(Turn down the level), miExit = new JMenuItem(Eixt),

5、 miPlay = new JMenuItem(Play), miPause = new JMenuItem(Pause), miResume = new JMenuItem(Resume), miStop = new JMenuItem(Stop), miAuthor = new JMenuItem(Author : apple), miSourceInfo = new JMenuItem(You can get the source code / document by email); private JCheckBoxMenuItem miAsWindows = new JCheckBo

6、xMenuItem(Windows), miAsMotif = new JCheckBoxMenuItem(Motif), miAsMetal = new JCheckBoxMenuItem(Metal, true); /* * 主游戏类的构造函数 * param title String,窗口标题 */ public ErsBlocksGame(String title) super(title); setSize(315, 392); Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); setLocation(s

7、crSize.width - getSize().width) / 2, (scrSize.height - getSize().height) / 2); createMenu(); Container container = getContentPane(); container.setLayout(new BorderLayout(6, 0); canvas = new GameCanvas(20, 12); ctrlPanel = new ControlPanel(this); container.add(canvas, BorderLayout.CENTER); container.

8、add(ctrlPanel, BorderLayout.EAST); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent we) stopGame(); System.exit(0); ); addComponentListener(new ComponentAdapter() public void componentResized(ComponentEvent ce) canvas.fanning(); ); show(); canvas.fanning(); /* * 让游戏“复位” */

9、 public void reset() ctrlPanel.reset(); canvas.reset(); /* * 判断游戏是否还在进行 * return boolean, true-还在运行,false-已经停止 */ public boolean isPlaying() return playing; /* * 得到当前活动的块 * return ErsBlock, 当前活动块的引用 */ public ErsBlock getCurBlock() return block; /* * 得到当前画布 * return GameCanvas, 当前画布的引用 */ public Gam

10、eCanvas getCanvas() return canvas; /* * 开始游戏 */ public void playGame() play(); ctrlPanel.setPlayButtonEnable(false); miPlay.setEnabled(false); ctrlPanel.requestFocus(); /* * 游戏暂停 */ public void pauseGame() if (block != null) block.pauseMove(); ctrlPanel.setPauseButtonLabel(false); miPause.setEnabled

11、(false); miResume.setEnabled(true); /* * 让暂停中的游戏继续 */ public void resumeGame() if (block != null) block.resumeMove(); ctrlPanel.setPauseButtonLabel(true); miPause.setEnabled(true); miResume.setEnabled(false); ctrlPanel.requestFocus(); /* * 用户停止游戏 */ public void stopGame() playing = false; if (block

12、!= null) block.stopMove(); miPlay.setEnabled(true); miPause.setEnabled(true); miResume.setEnabled(false); ctrlPanel.setPlayButtonEnable(true); ctrlPanel.setPauseButtonLabel(true); /* * 得到当前游戏者设置的游戏难度 * return int, 游戏难度1MAX_LEVEL */ public int getLevel() return ctrlPanel.getLevel(); /* * 让用户设置游戏难度 *

13、param level int, 游戏难度1MAX_LEVEL */ public void setLevel(int level) if (level 0) ctrlPanel.setLevel(level); /* * 得到游戏积分 * return int, 积分。 */ public int getScore() if (canvas != null) return canvas.getScore(); return 0; /* * 得到自上次升级以来的游戏积分,升级以后,此积分清零 * return int, 积分。 */ public int getScoreForLevelUpd

14、ate() if (canvas != null) return canvas.getScoreForLevelUpdate(); return 0; /* * 当分数累计到一定的数量时,升一次级 * return boolean, ture-update successufl, false-update fail */ public boolean levelUpdate() int curLevel = getLevel(); if (curLevel MAX_LEVEL) setLevel(curLevel + 1); canvas.resetScoreForLevelUpdate();

15、 return true; return false; /* * 游戏开始 */ private void play() reset(); playing = true; Thread thread = new Thread(new Game(); thread.start(); /* * 报告游戏结束了 */ private void reportGameOver() JOptionPane.showMessageDialog(this, Game Over!); /* * 建立并设置窗口菜单 */ private void createMenu() bar.add(mGame); bar.

16、add(mControl); bar.add(mWindowStyle); bar.add(mInfo); mGame.add(miNewGame); mGame.addSeparator(); mGame.add(miSetBlockColor); mGame.add(miSetBackColor); mGame.addSeparator(); mGame.add(miTurnHarder); mGame.add(miTurnEasier); mGame.addSeparator(); mGame.add(miExit); mControl.add(miPlay); mControl.add

17、(miPause); mControl.add(miResume); mControl.add(miStop); mWindowStyle.add(miAsWindows); mWindowStyle.add(miAsMotif); mWindowStyle.add(miAsMetal); mInfo.add(miAuthor); mInfo.add(miSourceInfo); setJMenuBar(bar); miPause.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK); miResum

18、e.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); miNewGame.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) stopGame(); reset(); setLevel(DEFAULT_LEVEL); ); miSetBlockColor.addActionListener(new ActionListener() public void actionPerformed(ActionEvent

19、ae) Color newFrontColor = JColorChooser.showDialog(ErsBlocksGame.this, Set color for block, canvas.getBlockColor(); if (newFrontColor != null) canvas.setBlockColor(newFrontColor); ); miSetBackColor.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) Color newBackColor

20、= JColorChooser.showDialog(ErsBlocksGame.this, Set color for block, canvas.getBackgroundColor(); if (newBackColor != null) canvas.setBackgroundColor(newBackColor); ); miTurnHarder.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) int curLevel = getLevel(); if (curLev

21、el 1) setLevel(curLevel - 1); ); miExit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) System.exit(0); ); miPlay.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) playGame(); ); miPause.addActionListener(new ActionListener() public

22、 void actionPerformed(ActionEvent ae) pauseGame(); ); miResume.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) resumeGame(); ); miStop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) stopGame(); ); miAsWindows.addActionListener(ne

23、w ActionListener() public void actionPerformed(ActionEvent ae) String plaf = com.sun.java.swing.plaf.windows.WindowsLookAndFeel; setWindowStyle(plaf); canvas.fanning(); ctrlPanel.fanning(); miAsWindows.setState(true); miAsMetal.setState(false); miAsMotif.setState(false); ); miAsMotif.addActionListen

24、er(new ActionListener() public void actionPerformed(ActionEvent ae) String plaf = com.sun.java.swing.plaf.motif.MotifLookAndFeel; setWindowStyle(plaf); canvas.fanning(); ctrlPanel.fanning(); miAsWindows.setState(false); miAsMetal.setState(false); miAsMotif.setState(true); ); miAsMetal.addActionListe

25、ner(new ActionListener() public void actionPerformed(ActionEvent ae) String plaf = javax.swing.plaf.metal.MetalLookAndFeel; setWindowStyle(plaf); canvas.fanning(); ctrlPanel.fanning(); miAsWindows.setState(false); miAsMetal.setState(true); miAsMotif.setState(false); ); /* * 根据字串设置窗口外观 * param plaf S

26、tring, 窗口外观的描述 */ private void setWindowStyle(String plaf) try UIManager.setLookAndFeel(plaf); SwingUtilities.updateComponentTreeUI(this); catch (Exception e) /* * 一轮游戏过程,实现了Runnable接口 * 一轮游戏是一个大循环,在这个循环中,每隔100毫秒, * 检查游戏中的当前块是否已经到底了,如果没有, * 就继续等待。如果到底了,就看有没有全填满的行, * 如果有就删除它,并为游戏者加分,同时随机产生一个 * 新的当前块,

27、让它自动下落。 * 当新产生一个块时,先检查画布最顶上的一行是否已经 * 被占了,如果是,可以判断Game Over了。 */ private class Game implements Runnable public void run() int col = (int) (Math.random() * (canvas.getCols() - 3), style = ErsBlock.STYLES(int) (Math.random() * 7)(int) (Math.random() * 4); while (playing) if (block != null) /第一次循环时,block为空 if (block.isAlive() try Thread.currentThread().sleep(100); catch (InterruptedException ie) ie.printStackTrace(); continue; checkFullLine(); /检查是否有全填满的行 if (isGameOver() /检查游戏是否应该结束了

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1