




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package els;import java.awt.*;/包含用于创建用户界面和绘制图形图像的所有类。import javax.swing.*;/设置布局public class Myels static JFrame els ; static JMenuItem mi4;static JMenuItem mi5;static JMenuItem mi6;private GameCanvas canvas;/设置界面public void ginterface()els = new JFrame(简单的俄罗斯方块游戏 by 金殿昊);els.setBounds(280,30,743,680);els.setLayout(new BorderLayout();Container c = els.getContentPane();/获得所有面板 canvas = GameCanvas.getCanvasInstance();c.add(new tx(),BorderLayout.WEST);c.add(canvas,BorderLayout.CENTER);c.add(new ControlPanel(),BorderLayout.EAST);ObtainComponent(c.getComponents();addmyJMenu();els.dispose();els.setVisible(true);private void ObtainComponent(Component obj)/遍历所有组件并设置字体 for(int i = 0;i obj.length;i+) if(obji instanceof JPanel) JPanel panel = (JPanel)obji; ObtainComponent(panel.getComponents(); else if(obji instanceof Component) obji.setFont(new Font(Monospaced,Font.BOLD,24); obji.setForeground(Color.blue);/设置字体颜色 obji.setBackground(Color.lightGray); private void addmyJMenu() JMenuBar mb = new JMenuBar(); els.setJMenuBar(mb); JMenu m1 = new JMenu( 编辑(E)); JMenu m2 = new JMenu( 查看(V) ); JMenu m3 = new JMenu( 帮助(H) ); JMenuItem mi1 = new JMenuItem( 复制(C)); JMenuItem mi2 = new JMenuItem( 粘贴(P) ); JMenuItem mi3 = new JMenuItem( 标准型(T) ); mi4= new JMenuItem( 复杂型(C)); mi5= new JMenuItem( 帮助主题(H) ); mi6= new JMenuItem( 关于此游戏(A) ); m1.add(mi1); m1.add(mi2); m2.add(mi3); m2.add(mi4); m3.add(mi5); m3.add(mi6); mb.add(m1); mb.add(m2); mb.add(m3);package els;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;class ControlPanel extends JPanel implements ActionListener/控制按钮和两边的布局 /* * */ private static final long serialVersionUID = 1L; private JPanel InfoPanel,buttonPanel; private static JTextField levelField,scoreField,zscoreField,yxh; private JButton exitButton; private static JButton turnEasilyButton,turnHarderButton; private EtchedBorder border=new EtchedBorder(EtchedBorder.RAISED,Color.red,Color.red);/设置边框 public static JButton playButton,restartButton; public static int fulnums = 0; private GameCanvas canvas; public ControlPanel() this.setFocusable(true); canvas = GameCanvas.getCanvasInstance(); this.setLayout(new GridLayout(2,1,0,0); InfoPanel = new JPanel(new GridLayout(8,1,0,5); levelField = new JTextField(1); levelField.setEditable(false); scoreField = new JTextField(10); scoreField.setEditable(false); yxh = new JTextField(0); yxh.setEditable(false); zscoreField = new JTextField(0); zscoreField.setEditable(false); InfoPanel.add(new JLabel( 当前级别: ); InfoPanel.add(levelField); InfoPanel.add(new JLabel( 单行分数: ); InfoPanel.add(scoreField); InfoPanel.add(new JLabel( 已经消行: ); InfoPanel.add(yxh); InfoPanel.add(new JLabel( 你的总分数: ); InfoPanel.add(zscoreField); buttonPanel = new JPanel(new GridLayout(5,1,0,0);/行,列,列间距,行间距 playButton = new JButton (开始/暂停 ); playButton.addActionListener(canvas); restartButton = new JButton ( 重新游戏 ); restartButton.setEnabled(false); restartButton.addActionListener(canvas); turnHarderButton = new JButton( 提高难度 ); turnHarderButton.addActionListener(this); turnEasilyButton = new JButton( 降低难度 ); turnEasilyButton.setEnabled(false); turnEasilyButton.addActionListener(this); exitButton = new JButton ( 退出游戏 ); exitButton.addActionListener(this); zscoreField.setBorder(border); buttonPanel.add(playButton); buttonPanel.add(restartButton); buttonPanel.add(turnHarderButton); buttonPanel.add(turnEasilyButton); buttonPanel.add(exitButton); buttonPanel.setFont(new Font(Monospaced,Font.BOLD,20); this.add(InfoPanel); this.add(buttonPanel); public static void restart() levelField.setText(0); turnEasilyButton.setEnabled(false); turnHarderButton.setEnabled(true); public static void ful() yxh.setText(String.valueOf(+fulnums); zscoreField.setText(String.valueOf(fulnums*10); public static int speed()int sp = Integer.parseInt(levelField.getText()*(-60)+660;return sp; public void actionPerformed(ActionEvent e) if(e.getSource()= exitButton)System.exit(0);if(e.getSource()= turnHarderButton)if(Integer.parseInt(levelField.getText()1)levelField.setText(String.valueOf(Integer.parseInt(levelField.getText()-1);if(Integer.parseInt(levelField.getText()=1)turnEasilyButton.setEnabled(false);canvas.requestFocus(); class tx extends JPanel /* * */private static final long serialVersionUID = 1L;private static TipBlockPanel tipBlockPanel,tipBlockPanel1,tipBlockPanel2; private JPanel tipPanel,tipPanel1,tipPanel2; JLabel le1 = new JLabel ( 下面第一个方块:); JLabel le2 = new JLabel ( 下面第二个方块:); JLabel le3 = new JLabel ( 下面第三个方块:); public static int style; public tx()this.setLayout(new GridLayout(3,1,0,0); tipBlockPanel = new TipBlockPanel(); tipPanel = new JPanel( new BorderLayout(); /创建预显方块 tipPanel.add(le1, BorderLayout.NORTH ); tipPanel.add( tipBlockPanel , BorderLayout.CENTER ); this.add(tipPanel); tipBlockPanel1 = new TipBlockPanel(); tipPanel1 = new JPanel( new BorderLayout(); tipPanel1.add(le2, BorderLayout.NORTH ); tipPanel1.add( tipBlockPanel1 , BorderLayout.CENTER ); this.add(tipPanel1); tipBlockPanel2 = new TipBlockPanel(); tipPanel2 = new JPanel( new BorderLayout(); tipPanel2.add( le3 , BorderLayout.NORTH ); tipPanel2.add( tipBlockPanel2 , BorderLayout.CENTER ); this.add(tipPanel2); public static void reset() tipBlockPanel.style = RussiaBlock.get(); tipBlockPanel.repaint(); tipBlockPanel1.style = RussiaBlock.get(); tipBlockPanel1.repaint(); tipBlockPanel2.style = RussiaBlock.get(); tipBlockPanel2.repaint(); public static void set() style = tipBlockPanel.style; tipBlockPanel.style = tipBlockPanel1.style; tipBlockPanel.repaint(); tipBlockPanel1.style = tipBlockPanel2.style; tipBlockPanel1.repaint(); tipBlockPanel2.style = RussiaBlock.get(); tipBlockPanel2.repaint(); package els;import javax.swing.JOptionPane;public class RussiaBlock /方块 public static int ROWS = 4; public static int COLS = 4; public static int style ,x,y; public int box= new int44; private GameCanvas canvas; public int row,col = 3;/当前方块的坐标 public final static int styles = / 共28种状态 0x0f00, 0x4444, 0x0f00, 0x4444, 0x04e0, 0x0464, 0x00e4, 0x04c4, 0x4620, 0x6c00, 0x4620, 0x6c00, 0x2640, 0xc600, 0x2640, 0xc600, 0x6220, 0x1700, 0x2230, 0x0740, 0x6440, 0x0e20, 0x44c0, 0x8e00, 0x0660, 0x0660, 0x0660, 0x0660, ; public RussiaBlock(GameCanvas canvas) this.canvas = canvas;x = (int)(Math.random()*6+1); y = (int)(Math.random()*3+1); style = stylesxy; public void moveleft() if(judge(row,col-1) col -; canvas.repaint(); public void moveright() if(judge(row,col+1) col +; canvas.repaint(); public void movedown() if(down() row +; canvas.repaint(); public boolean down() if(judge(row+1,col)return true; return false; public void moveup() if(judge(row-1,col) row -; canvas.repaint(); public void turnNext() style = stylesx(+y)%4; if(judge(row,col) canvas.repaint(); else style = stylesx(-y)%4; canvas.repaint(); public void reset()style = tx.style;for(int i = 0;i 7;i +)for(int j = 0;j 4;j +)if(style = stylesij)x = i;y = j; row = 0; col = 3; if(!judge(row,col) JOptionPane.showMessageDialog(null, GAME OVER,双击Enter继续游戏); canvas.play = 3; public static int get()x = (int)(Math.random()*6+1); y = (int)(Math.random()*3+1); return style = stylesxy;public boolean setcolor(int x ,int y)int key = 0x8000; if(canvas.play = 1) for(int i = 0; i RussiaBlock.ROWS;i+) for(int j = 0; j= 1; tryif(boxx-rowy-col= 1) return true; catch(Exception e) return false; public boolean judge(int x,int y)/是否正确int key = 0x8000;for(int i = 0; i RussiaBlock.ROWS;i+) for(int j = 0; j= 1;return true; public void delline() for(int i = 0; i 20;i+) for(int j = 0; j 10;j+) try if(boxi - rowj - col = 1) canvas.boxesij = 1; catch(Exception e) int c = 0; for (int b = 0; b 20; b+) for (int a = 0; a 0; b-) for (a = 0; a 10; a+) canvas.boxesba = canvas.boxesb-1a; c = 0; /类package els;import java.awt.*;import javax.swing.*;/显示下一个方块class TipBlockPanel extends JPanel /* * */private static final long serialVersionUID = 1L;private Color bgColor = Color.lightGray, blockColor = Color.RED; private int box ; private int boxWidth, boxHeight; public int style; public TipBlockPanel() box = new intRussiaBlock.ROWSRussiaBlock.COLS; public void paintComponent(Graphics g) boxWidth = getSize().width / RussiaBlock.COLS; boxHeight = getSize().height /RussiaBlock.ROWS; int key = 0x8000; for(int i = 0; i box.length; i +) for(int j = 0; j= 1; package els;import java.awt.*;import java.awt.event.*;import javax.swing.*;/游戏画布类 public class GameCanvas extends JPanel implements KeyListener,ActionListener /* * */private static final long serialVersionUID = 1L;private static GameCanvas canvas=null; public int boxes; private int rows = 20 , cols = 10, boxWidth, boxHeight; private Color blockColor = Color.RED, bgColor = new Color(50,100,50), fbColor = new Color(200,50,100); private MyTimer timer; public int play = 0; RussiaBlock block; int style; GameCanvas() block = new RussiaBlock(this); boxes = new introwscols; this.setFocusable(true);/JFrame要有焦点 切记切记 addKeyListener(this); timer = new MyTimer(); timer.setDaemon(true); public static GameCanvas getCanvasInstance()/获取此类的实例太有用了 if(canvas = null) canvas = new GameCanvas(); return canvas; public void paintComponent(Graphics g) boxWidth = getSize().width / cols; boxHeight = getSize().height /rows; for(int i = 0; i boxes.length; i+) for(int j = 0; jboxesi.length ;j+) Color color = block.setcolor(i,j)?blockColor :bgColor; g.setColor(color); g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true); for(int i = 0; i boxes.length; i+) for(int j = 0; jboxesi.length ;j+) if(boxesij = 1) g.setColor(fbColor); g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true); public int getXY(int row,int col)if (row = rows | col = cols) return(-1); else return(boxesrowcol); public void play() if(play = 0) tx.reset(); timer.start(); repaint(); play = 1; else if(play =1) play = 2; else if(play = 2) play = 1; else if(play = 3) restart(); requestFocus(); public void restart() for(int i = 0; i boxes.length; i+) for(int j = 0; jboxesi.length ;j+) boxesij = 0; block.row = 0; block.col = 3; tx.reset(); play = 1; repaint(); requestFocus(); p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年北京林业大学雄安校区规划建设指挥部招聘劳动合同制人员8名考试参考题库及答案解析
- 研判整改专项活动方案
- 美容店策划活动方案
- 正规二手房买卖合同范本及注意事项
- 2025年中国盐酸环丙沙星水溶性粉数据监测报告
- 2025年中国豪华型双层带钩浴巾架数据监测报告
- 合同履约保证与服务承诺规范
- 劳动合同管理规范及纠纷处理案例
- 可持续水资源循环利用20000立方米中水回用工程可行性研究报告
- 可持续绿色20平方公里生态城规划可行性研究报告
- 格拉斯哥昏迷评分法详解(GCS)
- GB/T 45403-2025数字化供应链成熟度模型
- 盒饭采购合同协议
- QGDW11337-2023输变电工程工程量清单计价规范
- 小学昆虫知识科普单选题100道及答案
- 防性侵教师培训
- 纸箱厂应急救援预案演练方案
- 人工智能通识基础 课件 第1章 人工智能概述
- 《建设法规》教案+第1次课+法律体系
- 患者的卧位课件
- 中药香囊与车载香氛结合企业制定与实施新质生产力战略研究报告
评论
0/150
提交评论