📄 gameframe.java~2~
字号:
package RussiaBlock;/** * <p>Title: RussionBlock</p> * <p>Description: TheSmallGame</p> * <p>Copyright: Copyright (c) 2002</p> * @author sunwei * @version 1.0 */import java.awt.*;import java.awt.event.*;import java.applet.*;class GameFrame extends Frame implements ActionListener,ItemListener{ //游戏框架 MenuBar m_MenuBar; Menu m_speed,m_height,m_music; Menu m_play,m_option,m_about; MenuItem mi_start,mi_hero,mi_pause,mi_close; MenuItem mi_setkey; MenuItem mi_about,mi_readme; CheckboxMenuItem mi_speed[] = new CheckboxMenuItem[10], mi_height[] = new CheckboxMenuItem[10], mi_bgmusic,mi_sound; Dialog helpDialog; Button btnY; GameCanvas gameCanvas; String s[] = {"0","1","2","3","4","5","6","7","8","9"}; public GameFrame(){ super("俄罗斯方块 V1.0 sunwei"); btnY = new Button("确定"); gameCanvas = new GameCanvas(this);gameCanvas.setSize(450,400); add(gameCanvas); m_MenuBar = new MenuBar(); m_play = new Menu("游戏"); mi_start = new MenuItem("新游戏"); mi_pause = new MenuItem("暂停游戏"); mi_hero = new MenuItem("英雄榜"); mi_close = new MenuItem("退出"); mi_pause.disable(); m_play.add(mi_start); m_play.add(mi_pause); m_play.addSeparator(); m_play.add(mi_hero); m_play.addSeparator(); m_play.add(mi_close); m_MenuBar.add(m_play); mi_start.addActionListener(this); mi_hero.addActionListener(this); mi_pause.addActionListener(this); mi_close.addActionListener(this); m_option = new Menu("选项"); m_speed = new Menu("速度设置"); m_height = new Menu("高度设置"); mi_setkey = new MenuItem("键盘设置"); m_music = new Menu("音效设置"); mi_bgmusic = new CheckboxMenuItem("声音"); mi_sound = new CheckboxMenuItem("音乐"); for(int i = 0; i<mi_speed.length;i++){ mi_speed[i] = new CheckboxMenuItem(s[i]); mi_height[i] = new CheckboxMenuItem(s[i]); m_speed.add(mi_speed[i]); m_height.add(mi_height[i]); mi_speed[i].addItemListener(this); m_height.add(mi_height[i]); mi_height[i].addItemListener(this); } mi_speed[0].setState(true); mi_height[0].setState(true); mi_bgmusic.setState(true); mi_sound.setState(true); m_option.add(m_speed); m_option.add(m_height); m_option.addSeparator(); m_option.add(m_music); m_option.addSeparator(); m_option.add(mi_setkey); m_music.add(mi_bgmusic); m_music.add(mi_sound); m_MenuBar.add(m_option); mi_bgmusic.addActionListener(this); mi_sound.addActionListener(this); mi_setkey.addActionListener(this); m_about=new Menu("帮助"); mi_readme = new MenuItem("帮助"); mi_about=new MenuItem("关于..."); m_about.add(mi_readme); m_about.addSeparator(); m_about.add(mi_about); m_MenuBar.add(m_about); mi_about.addActionListener(this); mi_readme.addActionListener(this); this.setMenuBar(m_MenuBar); btnY.addActionListener(this); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ ((Frame)e.getWindow()).dispose(); gameCanvas.getThread().stop(); System.exit(0); } }); } public void actionPerformed(ActionEvent e){ //加入菜单的动作 if(e.getSource() == mi_start){ mi_pause.enable(); if(!gameCanvas.getThread().isAlive()){ gameCanvas.getThread().start(); gameCanvas.reset(); } else gameCanvas.reset(); }else if(e.getSource() == mi_pause){ if(gameCanvas.getPause()==true){ gameCanvas.setPause(false); mi_pause.setLabel("继续游戏"); }else{ gameCanvas.setPause(true); mi_pause.setLabel("暂停游戏"); } }else if(e.getSource() == mi_hero){ BlockDialog heroDialog = new BlockDialog(this,"英雄榜",false); heroDialog.setBounds(200,150,270,260); heroDialog.show(); }else if(e.getSource() == mi_close){ gameCanvas.getThread().stop(); this.dispose(); System.exit(0); }else if(e.getSource() == mi_setkey){ SetDialog setDialog = new SetDialog(this,"键盘设定",true); setDialog.setBounds(200,150,330,220); setDialog.show(); }else if(e.getSource() == mi_about){ helpDialog=new Dialog(this,"关于",false); Panel p1=new Panel(); Panel p3=new Panel(); p1.add(new Label("俄罗斯方块 v1.0 作者:sunwei")); p3.add(new Label("谢谢使用!")); helpDialog.add("North",p1); helpDialog.add("Center",p3); Panel p2 = new Panel(); p2.add(btnY); helpDialog.add("South",p2); helpDialog.setSize(300,200); helpDialog.setBounds(200,150,200,130); helpDialog.show(); helpDialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { helpDialog.dispose(); } }); }else if(e.getSource() == btnY){ helpDialog.dispose(); } } public void itemStateChanged(ItemEvent e){ int i,j; for(i = 0; i < mi_speed.length; i++){ if(e.getSource() == mi_speed[i]){ //设置速度 for(j = 0; j < mi_speed.length; j++) mi_speed[j].setState(false); mi_speed[i].setState(true); gameCanvas.setSpeed(i); break; } } for(i = 0; i < mi_height.length; i++){ if(e.getSource() == mi_height[i]){ //设置高度 for(j = 0; j < mi_height.length; j++) mi_height[j].setState(false); mi_height[i].setState(true); gameCanvas.setHeight(i); break; } } }} class Block{ //方块类 private int cubeEach = 4; //小方块的个数 private int cubeKind = 7; //大方块的种类 private int cubeWidth = 20; //小方块的宽 private int cubeHeight = 20; //小方块的高 private int bgX = 200,bgY = 50; //背景起始位置 private int currentX = 4,currentY = 0; //大方块起始位置 private int bgWidth = 11,bgHeight = 19; //背景宽和高 public byte[][] bgInformation = new byte[this.getbgWidth()][this.getbgHeight()]; private byte[][] //保存背景信息的数组 line = new byte[][]{ {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, square = new byte[][]{ {1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, l = new byte[][]{ {1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, turnL = new byte[][]{ {1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, s = new byte[][]{ {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, turnS = new byte[][]{ {0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, t = new byte[][]{ {0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, currentBlock = new byte[][]{ {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, nextBlock = new byte[][]{ {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }; private Color currentBlockColor,nextBlockColor; //保存颜色变量 private byte[][][] cube = new byte [][][]{ line,square,l,turnL,s,turnS,t }; private Color colors[] = { Color.red,Color.orange,Color.gray,Color.green, Color.blue,Color.pink,Color.magenta,Color.white }; /* 以下是方块的方法 */ public int getBGX() { return this.bgX; } public int getBGY() { return this.bgY; } public int getbgWidth() { return this.bgWidth; } public int getbgHeight() { return this.bgHeight; } public int getCubeKind() { return this.cubeKind; } public int getCubeEach() { return this.cubeEach; } public int getCubeWidth() { return this.cubeWidth; } public int getCubeHeight(){ return this.cubeHeight;} public int getCurrentX() { return this.currentX; } public int getCurrentY() { return this.currentY; } public void setCurrentXY(int x,int y){ this.currentX = x; this.currentY = y;} public Color getCurrentBlockColor(){ return this.currentBlockColor; } public Color getNextBlockColor(){ return this.nextBlockColor; } public byte[][] getCurrentBlock(){ return this.currentBlock; } public byte[][] getNextBlock(){ return this.nextBlock; } /* 以下是方块的动作 */ public void reset(){ int random = (int)(Math.random()*7); this.currentBlock = this.cube[random]; this.currentBlockColor = this.colors[random]; } public void setNextBlock(){ int random = (int)(Math.random()*7); this.nextBlock = this.cube[random]; this.nextBlockColor = this.colors[random]; } public void setCurrentBlock(){ this.currentBlock = this.nextBlock; this.currentBlockColor = this.nextBlockColor; } public boolean goLeft(){ if(this.isBound(this.currentX - 1, this.currentY, this.getCurrentBlock())){ this.currentX--;return true; } else return false; } public boolean goRight(){ if(this.isBound(this.currentX + 1, this.currentY, this.getCurrentBlock())){ this.currentX++;return true; } else return false; } public boolean goDown(){ if(this.isBound(this.currentX, this.currentY + 1, this.getCurrentBlock())){ this.currentY++;return true; } else return false; } public boolean goTurn(){ if(this.isBound(this.currentX,this.currentY,this.getTurnBlock())){ currentBlock = this.getTurnBlock(); return true; } else for(int i = 1; i<4; i++) if(this.isBound(this.currentX - i,this.currentY,this.getTurnBlock())){ this.currentX = this.currentX - i; currentBlock = this.getTurnBlock(); return true; } return false; } /* 获得旋转的方块 */ public byte[][] getTurnBlock(){ byte[][] turnBlock = new byte[getCubeEach()][getCubeEach()]; boolean goReturn = false; int x, y; for(y = 0; y < getCubeEach(); y++){ for(x=0; x < getCubeEach(); x++) turnBlock[getCubeEach()-1-x][y] = this.currentBlock[y][x]; } for(y = 0; y < getCubeEach(); y++) { for(x=0; x < getCubeEach(); x++) if(turnBlock[y][x] == 1) { goReturn = true; break; } if(goReturn) break; } int temp = y; for(; y < getCubeEach(); y++) { for(x=0; x < getCubeEach(); x++) turnBlock[y-temp][x] = turnBlock[y][x]; } for(y = getCubeEach()-temp; y < getCubeEach(); y++) { for(x=0; x < getCubeEach(); x++) turnBlock[y][x] = 0; } return turnBlock; } /* 判断边界 */ public boolean isBound(int locateX, int locateY, byte[][] block){ int tempx, tempy; if((locateX >= 0)&&(locateX <= this.getbgWidth())&&(locateY >=0)&&(locateY < this.getbgHeight())) { for(int x = 0; x < this.getCubeEach(); x++) for(int y = 0; y < this.getCubeEach(); y++) { tempx = locateX + x; tempy = locateY + y; if(block[y][x] == 1) { if((tempx >= this.getbgWidth()) || (tempy >= this.getbgHeight())) return false; else if(block[y][x] == this.bgInformation[tempx][tempy]) return false; } } return true; } else return false; } /* 删行 */ public void delLine(int position){ int x,y; if(position == 0){ for(x = 0; x < this.getbgWidth(); x++) this.bgInformation[x][0] = 0; }else { for(y = position; y > 0; y--) for(x = 0; x < this.getbgWidth(); x++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -