




已阅读5页,还剩46页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import javax.swing.*;import javax.swing.Timer;import java.awt.*;import java.awt.event.*;import java.util.*;/*Title: 俄罗斯方块*Description: 俄罗斯方块*Copyright: 俄罗斯方块*Company:俄罗斯方块* author Zhu Yong* version 1.0*/public class SquaresGamepublic static void main(String args) new SquaresFrame().play();class SquaresFrame extends JFrame public final static int WIDTH = 500; public final static int HEIGHT = 600; /* 游戏区*/private BoxPanel boxPanel;/* 暂停标记*/private boolean isPause = true;/* 默认构造函数*/public SquaresFrame()/* 记分面板*/InfoPanel infoPanel = new InfoPanel();boxPanel = new BoxPanel(infoPanel);JMenuBar bar = new JMenuBar();JMenu menu = new JMenu(菜单);JMenuItem begin = new JMenuItem(新游戏);final JMenuItem pause = new JMenuItem(暂停);JMenuItem stop = new JMenuItem(结束);setJMenuBar(bar);bar.add(menu);menu.add(begin);menu.add(pause);menu.add(stop);stop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)System.exit(0););pause.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)if ( isPause)boxPanel.supend(false);isPause = ! isPause;pause.setText(恢复);else boxPanel.supend(true);isPause = ! isPause;pause.setText(暂停););begin.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)boxPanel.newGame(););boxPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.cre ateEtchedBorder(),Box);infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.cr eateEtchedBorder(), Info);add(boxPanel,BorderLayout.CENTER);add(infoPanel,BorderLayout.EAST);setTitle(Squares Game);setSize(WIDTH,HEIGHT);/不可改变框架大小setResizable(false);/定位显示到屏幕中间setLocation(int)Toolkit.getDefaultToolkit().getScreenSize().getWidt h()-WIDTH)/2,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-HEIGHT) /2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);public void play() boxPanel.newGame();/* author Zhu Yong* 游戏区面板*/class BoxPanel extends JPanel /* 定义行*/public final static int ROWS = 20;/* 定义列*/public final static int COLS = 10;public final static Color DISPLAY = new Color(128,128,255); /* 没有方块时显示的颜色*/public final static Color HIDE = new Color(238,238,238); /* 记分面板上显示的下一个方块的颜色*/public final static Color NEXTDISPLAY = Color.ORANGE; /* 是否显示网络*/public final static boolean PAINTGRID = false; /* 用4个按钮表示一个方块*/private JButton squares = new JButtonROWSCOLS; /* 定义对前位置是否有方块.用颜色区别.*/private int stateTable = new intROWSCOLS; private InfoPanel scorePanel;private ABox currentBox;private ABox nextBox;/*各方块颜色数组.其中0号颜色为无方块时颜色.共7种方块 .最后一个消行时颜色 */private static Color boxColor= new Colornew Color(238,238,238),new Color(128,128,255),new Color(189,73,23),new Color(154,105,22),new Color(101,124,52),new Color(49,100,128),new Color(84,57,119),new Color(111,54,102),new Color(126,50,57),Color.RED;/* 定时器,负责方块自动下移*/private Timer timer;/* Level 用定时器的延时控制*/private int level;/* 初时化时的延时*/private final static int INITDELAY = 940; /* 每一级别延时减少的步进值*/private final static int INC = 90;public BoxPanel(InfoPanel panel)setLayout(new GridLayout(ROWS,COLS,1,1);/可能获取焦点,才能接受键盘事件setFocusable(true);/setCursor(new Cursor(1);/* 初时化,生成JButton,设置颜色.JButton数组按照先第一行再第二行的顺序添加.下标为行列和xy正好相反.*/for(int i=0;iROWS;i+)for(int j=0;jCOLS;j+) JButton square= new JButton();square.setEnabled(false);square.setBorderPainted(PAINTGRID);squaresij = square;stateTableij = 0;add(square);scorePanel = panel;timer = new Timer(INITDELAY,new autoDownHandler();/注册键盘事件addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) int key = e.getKeyCode();/ System.out.println(bt.getX()+:+bt.getY(); if (key = KeyEvent.VK_DOWN) dropdown(); else if (key = KeyEvent.VK_UP) rotate(); else if (key = KeyEvent.VK_RIGHT) right(); else if (key = KeyEvent.VK_LEFT) left(););/* 重新所有方块. 即重新改变所有JButton背景色*/privatevoidshowBox()for(int i=0;iROWS;i+)for(int j=0;jCOLS;j+)squaresij.setBackground(boxColorstateTableij);/* 只重新显示当前方块*/private void showCurrentBox()point location = currentBox.getLocation(); for(int i=0;i=0;i-)for(int j=0;j0) scorePanel.winScore(clearLines);upgrade();/* 消行,重置属性表.下移* param line*/public void removeLine(int line) timer.stop();for(int j=0;j=0;i-)for(int j=0;jCOLS;j+)if (i!=0)stateTableij = stateTablei-1j; elsestateTableij = 0;showBox();timer.start();/* 检查把方块移动到(x,y)时是否合法 .* param x* param y* return*/public boolean checkAction(int x,int y) /先移除当前方块,再判断.当前方块和下一位置的方块有共用的位置.removeCurrentBox();boolean ret = true;point list = currentBox.getShape().getList();int xx = 0;int yy = 0;for (int i=0;i+xx+:+yy);if ( xx=COLS | xx=ROWS | yy+xx+:+yy); if (stateTableyyxx!=0) ret = false; break;addCurrentBox();return ret;/* 左移*/public void left()if(checkAction(currentBox.getHead().getX()-1,currentBox.getHead().getY() removeCurrentBox();showCurrentBox();currentBox.setHead(currentBox.getHead().getX()-1,currentBox.getHead().getY();addCurrentBox();/showBox();showCurrentBox();/* 右移*/public void right()if(checkAction(currentBox.getHead().getX()+1,currentBox.getHead().getY() removeCurrentBox();showCurrentBox();currentBox.setHead(currentBox.getHead().getX()+1,currentBox.getHead().getY();addCurrentBox();/showBox();showCurrentBox();/* 下移* return*/public boolean down()int choose=0;if(checkAction(currentBox.getHead().getX(),currentBox.getHead().getY()+1) removeCurrentBox();showCurrentBox();currentBox.setHead(currentBox.getHead().getX(),currentBox.getHead().getY()+1);addCurrentBox();showCurrentBox();/showBox();returntrue;else clearBox();currentBox = nextBox.setDefault();nextBox = BoxFactory.getBox().setDefault(); scorePanel.setNext(nextBox); /showBox();/检查是否游戏结束if (checkGameOver()supend(false);choose = JOptionPane.showConfirmDialog(this, 游戏结束n你的积分为+scorePanel.getScore()+n要开始新游戏吗?,GameOver,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);if (choose = JOptionPane.YES_OPTION) newGame();addCurrentBox();showCurrentBox();/showBox();return false;/* 丢下.*/public void dropdown() while (down() ;/* 判断是否结束* return*/private boolean checkGameOver()point location = currentBox.getLocation(); for(int i=0;i4;i+)int row = locationi.getY();int col = locationi.getX();if (stateTablerowcol!=0) return true;return false;/* 变形*/public void rotate()if (checkRotate()removeCurrentBox();showCurrentBox();currentBox.rotate();addCurrentBox();showCurrentBox();/showBox();/* 检查变形是否合法* return*/private boolean checkRotate()removeCurrentBox();int oldheadx = currentBox.getHead().getX(); int oldheady = currentBox.getHead().getY(); /System.out.println(oldhead:+oldheadx+,+oldheady); boolean ret = true;point shape = currentBox.getNextShape().getList(); int x=0,y=0,newheadx = 0,newheady=0; for(int i=0;i+x+:+y);if ( x=COLS | x=ROWS | y+x+:+y); if (stateTableyx!=0) ret = false; break;addCurrentBox();return ret;/* 移除当前方块.设置颜色为无色.主要用于移动,变形时.清除当前位置方块.显示下一位置方块.*/private void removeCurrentBox()/System.out.println(removeCurrentBox:+Thread.currentThread().getNa me();for(int i=0;i4;i+)int x=currentBox.getLocation()i.getX(); int y=currentBox.getLocation()i.getY(); stateTableyx = 0;/* 添加当前方块到属性表.*/private void addCurrentBox()for(int i=0;i 3000 & level 6000 & level 10000 & level 15000 & level 21000 & level 28000 & level 35000 & level 43000 & level 51000 & level 10) setLevel(10);/* 重置,开始新游戏.*/public void newGame()for(int i=0;iROWS;i+)for(int j=0;jCOLS;j+)stateTableij = 0;showBox();setLevel(1);scorePanel.clean();currentBox = BoxFactory.getBox();nextBox = BoxFactory.getBox();scorePanel.setNext(nextBox);setFocusable(true);requestFocus();addCurrentBox();showBox();timer.start();/* 记分面板* author Administrator*/class InfoPanel extends JPanelprivate JLabel scoreLabel;private JLabel lineLabel;private JLabel levelLabel;private JPanel nextPanel;/* 下一个方块*/private JButton nextBox;public InfoPanel()setLayout(new BorderLayout();Box vbox = Box.createVerticalBox(); add(vbox,BorderLayout.CENTER);vbox.add(Box.createRigidArea(new Dimension(12,50);Box vbox1 = Box.createVerticalBox();vbox.add(vbox1);scoreLabel = new JLabel(0);lineLabel = new JLabel(0);levelLabel = new JLabel(0);nextPanel = new JPanel();nextBox = new JButton44;nextPanel.setLayout(new GridLayout(4,4,1,1); for(int i=0;i4;i+)for(int j=0;j4;j+)JButton bt = new JButton();bt.setEnabled(false);bt.setBorderPainted(BoxPanel.PAINTGRID);nextPanel.add(bt);nextBoxji = bt;vbox1.add(new JLabel(Next :);vbox1.add(nextPanel);vbox1.add(Box.createVerticalStrut(90);vbox1.add(new JLabel(得分 :);vbox1.add(Box.createVerticalStrut(10);vbox1.add(scoreLabel);vbox1.add(Box.createVerticalStrut(10);vbox1.add(new JLabel(行数 :);vbox1.add(Box.createVerticalStrut(10);vbox1.add(lineLabel);vbox1.add(Box.createVerticalStrut(10);vbox1.add(new JLabel(Level :);vbox1.add(Box.createVerticalStrut(10);vbox1.add(levelLabel);vbox1.add(Box.createVerticalStrut(80);/* 记分.显示在面板上* param lines*/public void winScore(int lines)int score = 0;lineLabel.setText(+(Integer.parseInt(lineLabel.getText()+lines); if (lines=1) score = 100;else if (lines =2) score = 300;else if (lines =3) score = 500;else if (lines =4) score = 1000;scoreLabel.setText(+(Integer.parseInt(scoreLabel.getText()+score);/* 设置下一个方块.* param next*/public void setNext(ABox next) for(int i=0;i4;i+)for(int j=0;j4;j+)nextBoxij.setBackground(BoxPanel.HIDE); int headx = 2; int heady = 0;/设置方块位置if (next.getId() = 1) headx = 2;heady=0;else if (next.getId() = 2) headx = 1;heady = 1; else if (next.getId() = 3) headx = 2;heady = 1; else if (next.getId() = 4) headx = 1;heady = 1; else if (next.getId() = 5) headx = 2;heady = 1; else if (next.getId() = 6) headx = 1;heady = 1; else if (next.getId() = 7) headx = 1;heady = 1;point shape = next.getShape(0).getList();nextBoxheadxheady.setBackground(BoxPanel.NEXTDISPLAY);nextBoxheadx+shape1.getX()heady+shape1.getY().setBackground(B oxPanel.NEXTDISPLAY);nextBoxheadx+shape2.getX()heady+shape2.getY().setBackground(B oxPanel.NEXTDISPLAY);nextBoxheadx+shape3.getX()heady+shape3.getY().setBackground(B oxPanel.NEXTDISPLAY);/* 设置显示级别* param level*/public void setLevel(int level) levelLabel.setText(+level);/* 获取当前积分* return*/public int getScore()return Integer.parseInt(scoreLabel.getText();/* 重置*/public void clean()scoreLabel.setText(+0);setLevel(1);for(int i=0;i4;i+)for(int j=0;j4;j+)nextBoxij.setBackground(BoxPanel.HIDE);/* 方块类* author Administrator*/class ABox public final static int ROWS = 20;public final static int COLS = 10;privateint id;/* 用于存放方块所有样子坐标.*/private ArrayList shapeTable; private String name;private int currentShapeId;/* 当前方块位置.注意这里是坐标(x,y)和squares,stateTable数组的行列顺序正好相反.*/private point location;/* 方块起始位置.*/private point head = new point(0,COLS/2); public ABox(String name,int id) = name;this.id = id;shapeTable = new ArrayList(); location = new point4;/* 方块变形,选取下一个坐标集.从小到大顺序选取.变形后重设方块位置(头结点) */public void rotate()currentShapeId=(currentShapeId+1)=shapeTable.size()?0:(currentShapeI d+1);point shapehead = shapeTable.get(currentShapeId).getList()0;setHead(getHead().getX()+shapehead.getX(),getHead().getY()+shapehead. getY();/* 添加方块坐标集* param shape*/public void addShape(BoxShape shape) shapeTable.add(shape);/* 重设方块位置.定义新的头结点.根据坐标集计算下其它子方块位置.* param x 新位置头结点坐标x* param y*/public void setHead(int x,int y) head.setxy(x, y);point shapePoint = getShape().getList(); location0 = head;location1 = head.add(shapePoint1); location2 = head.add(shapePoint2); location3 = head.add(shapePoint3);/* 获取位置数组* return*/public point getLocation()return location;/* 获取头结点* return*/public point getHead()return head;/* 获取当前坐标集* return*/public BoxShape getShape()/ get current shapereturn shapeTable.get(currentShapeId);/* 获取下一个坐标集*/public BoxShape getNextShape() /int index = currentShapeId+1;/if (index=shapeTable.size() index = 0; returnshapeTable.get(currentShapeId+1=shapeTable.size()?0:currentShapeId+1);/* 重置当前方块 .因为游戏中一直在重用方块对象.* return*/public ABox setDefault()setHead(4,0);currentShapeId = 0;return this;/* 获取对应坐标集* param index* return*/public BoxShape getShape(int index) return shapeTable.get(index);/* 用于调试*/public String toString()return +name+ +getHead();public int getId()return id;/* 产生方块* author Administrator*/class BoxFactory /* ArrayLIst中存放每个方块的一个对象.随机获取.*/private static ArrayList aboxes = new ArrayList(); /* 产生方块对象.*/static /*其中包含4个点.都是相对坐标.第一个坐标是相对变形前根结点坐标的变化量,其它3坐标是相对当前根结点的变化量.对任一方块,可让其处一个3*3或4*4的方阵中,变化时不应脱离此方块,然后以此来计算根结点相对变化值.*/ABox abox = new ABox(changtiao,1); abox.addShape(new BoxShape(2,-2,0,1,0,2,0,3); abox.addShape(new BoxShape(-2,2,1,0,2,0,3,0); aboxes.add(abox);/* * */abox = new ABox(fangkuai,2); abox.addShape(new BoxShape(0,0,1,0,0,1,1,1); aboxes.add(abox);/* */abox = new ABox(3A,3);abox.addShape(new BoxShape(1,-1,-1,1,0,1,-1,2); abox.addShape(new BoxShape(-1,1,1,0,1,1,2,1); aboxes.add(abox);/* */abox = new ABox(3B,4);abox.addShape(new BoxShape(0,0,0,1,1,1,1,2); abox.addShape(new Box
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 青优网数学试卷
- 宁波小学4年级数学试卷
- 全国高考近五年数学试卷
- 祁阳市七下数学试卷
- 钦州市钦北区数学试卷
- 七年级班级总结数学试卷
- 清政府考试数学试卷
- 衢州成人高考数学试卷
- 七下智慧学堂数学试卷
- 清朝高考数学试卷
- 算力中心计算任务优化方案
- 劳务派遣工作知识培训课件
- 无人机反制设备原理课件
- 北京市2025年普通高中学业水平等级性考试政治试题(解析版)
- 2025年道路运输两类人员安全员考核分享题库及答案
- 中国肺血栓栓塞症诊治、预防和管理指南(2025版)
- 2024年全国工会财务知识大赛备赛试题库500(含答案)
- 工资现金发放证明书
- (高清正版)JJF(浙)1160-2019圆柱螺纹指示量规校准规范
- 光电及光化学转化原理与应用电化学课件:光催化与光催化体系
- 建筑与相关专业配合拍图主要注意点汇总
评论
0/150
提交评论