java编写俄罗斯方块源代码一

上传人:汽*** 文档编号:478078678 上传时间:2023-01-06 格式:DOCX 页数:14 大小:21.81KB
返回 下载 相关 举报
java编写俄罗斯方块源代码一_第1页
第1页 / 共14页
java编写俄罗斯方块源代码一_第2页
第2页 / 共14页
java编写俄罗斯方块源代码一_第3页
第3页 / 共14页
java编写俄罗斯方块源代码一_第4页
第4页 / 共14页
java编写俄罗斯方块源代码一_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《java编写俄罗斯方块源代码一》由会员分享,可在线阅读,更多相关《java编写俄罗斯方块源代码一(14页珍藏版)》请在金锄头文库上搜索。

1、import javax.swing.*;import javax.swing.border.Border;import javax.swing.border.EtchedBorder;import java.awt.*;import java.awt.event.*;/* 控制面板类, 继承自 JPanel.* 上边安放预显窗口、等级、得分、控制按钮* 主要用来控制游戏进程。* /class ControlPanel extends JPanel private JTextFieldtfLevel = new JTextField( + ErsblocksGame.DEFAULT_LEVEL

2、), tfScore = new JTextField(0);private JButtonbtPlay = new JButton(Play),btPause = new JButton(Pause),btStop = new JButton(Stop),btTurnLevelUp = new JButton(Turn hard),btTurnLevelDown = new JButton(Turn easy);private JPanel plTip = new JPanel(new BorderLayout();private TipPanel plTipblock = new TipP

3、anel();private JPanel plInfo = new JPanel(new GridLayout(4, 1);private JPanel plButton = new JPanel(new GridLayout(5, 1);private Timer timer;private ErsBlocksGame game;private Border border = new EtchedBorder(EtchedBorder.RAISED, Color.white, new Color(148, 145, 140);/* 控制面板类的构造函数* param game ErsBlo

4、cksGame, ErsBoxesGame 类的一个实例弓 I 用* 方便直接控制ErsBoxesGam联的行为。* /setLayout(new GridLayout(3, 1, 0, 4);this.game = game;plTip.add(new JLabel(Next block), BorderLayout.NORTH);plTip.add(plTipblock);plTip.setBorder(border);plInfo.add(new JLabel(Level);plInfo.add(tfLevel);plInfo.add(new JLabel(Score);plInfo.a

5、dd(tfScore);plInfo.setBorder(border);tfLevel.setEditable(false);tfScore.setEditable(false);plButton.add(btPlay);plButton.add(btPause);plButton.add(btStop);plButton.add(btTurnLevelUp);plButton.add(btTurnLevelDown);plButton.setBorder(border);add(plTip);add(plInfo);add(plButton);addKeyListener(new Cont

6、rolKeyListener();btPlay.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) game.playGame(););btPause.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) if (btPause.getText().equals(new String(Pause) game.pauseGame(); else game.resumeGam

7、e(););btStop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) game.stopGame(););btTurnLevelUp.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) try int level = Integer.parseInt(tfLevel.getText();if (level 1)tfLevel.setText( + (level

8、- 1); catch (NumberFormatException e) requestFocus(););addComponentListener(new ComponentAdapter() public void componentResized(ComponentEvent ce) plTipblock.fanning(); );timer = new Timer(500, new ActionListener() public void actionPerformed(ActionEvent ae) tfScore.setText( + game.getScore();int sc

9、oreForLevelUpdate =game.getScoreForLevelUpdate();if (scoreForLevelUpdate = ErsBlocksGame.PER_LEVEL_SCORE& scoreForLevelUpdate 0)game.levelUpdate(););timer.start();/* 设置预显窗口的样式,* param style int, 对应 Ersblock 类的 STYLES的 28 个值* / public void setTipStyle(int style) plTipblock.setStyle(style);/* 取得用户设置的游

10、戏等级。* return int, 难度等级 ,1 -ErsBlocksGame.MAX_LEVEL */ public int getLevel() int level = 0;try level = Integer.parseInt(tfLevel.getText(); catch (NumberFormatException e) return level;/* 让用户修改游戏难度等级。* param level 修改后的游戏难度等级*/ public void setLevel(int level) if (level 0 & level 11) tfLevel.setText( +

11、level); /* 设置 开始 按钮的状态。* /public void setPlayButtonEnable(boolean enable) btPlay.setEnabled(enable);public void setPauseButtonLabel(boolean pause) btPause.setText(pause ? Pause : Continue);/* 重置控制面板* /public void reset() tfScore.setText(0);plTipblock.setStyle(0);/* 重新计算 TipPanel 里的 boxes 里的小框的大小*/ p

12、ublic void fanning() plTipblock.fanning();/* 预显窗口的实现细节类*/ private class TipPanel extends JPanel private Color backColor = Color.darkGray, frontColor =Color.lightGray;private ErsBox boxes = new ErsBoxErsBlock.BOXES_ROWSErsBlock.BOXES_COLS;private int style, boxWidth, boxHeight;private boolean isTiled

13、 = false;/* 预显窗口类构造函数*/public TipPanel() for (int i = 0; i boxes.length; i+) for (int j = 0; j boxesi.length; j+) boxesij = new ErsBox(false);/* 预显窗口类构造函数* param backColor Color, 窗口的背景色* param frontColor Color, 窗口的前景色* / public TipPanel(Color backColor, Color frontColor) this();this.backColor = back

14、Color;this.frontColor = frontColor;/* 设置预显窗口的方块样式* param style int, 对应 Ersblock 类的 STYLES的 28 个值*/ public void setStyle(int style) this.style = style;repaint();/* 覆盖JComponent类的函数,画组件。* param g 图形设备环境*/ public void paintComponent(Graphics g) super.paintComponent(g);if (!isTiled) fanning();int key = 0x8

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号