⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameframe.java~1~

📁 java 版经典俄罗斯方块游戏,用jbuilder 开发的。包含所有原代码
💻 JAVA~1~
📖 第 1 页 / 共 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.*;public class GameFrame extends Frame implements ActionListener,ItemListener{				private MenuBar m_MenuBar;		private Menu m_level,m_height,m_music;		private Menu m_play,m_option,m_about;		private MenuItem mi_start,mi_hero,mi_pause,mi_close;		private MenuItem mi_setkey;		private MenuItem mi_about,mi_readme;		private CheckboxMenuItem mi_level[] = new CheckboxMenuItem[10],		                         mi_height[] = new CheckboxMenuItem[10],		                         mi_bgmusic,mi_sound;		private Dialog helpDialog; 		private Button btnY;		private GameCanvas gameCanvas;		private String s[] = {"0","1","2","3","4","5","6","7","8","9"};		GameFrame(){		super("俄罗斯方块 V1.0 孙伟");		System.out.print("游戏开始...");		btnY = new Button("确定");		gameCanvas = new GameCanvas();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_level = 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<10;i++){			mi_level[i] = new CheckboxMenuItem(s[i]);			mi_height[i] = new CheckboxMenuItem(s[i]);			m_level.add(mi_level[i]);			m_height.add(mi_height[i]);			mi_level[i].addItemListener(this);			m_height.add(mi_height[i]);			mi_height[i].addItemListener(this);					}		mi_level[0].setState(true);		mi_height[0].setState(true);		mi_bgmusic.setState(true);		mi_sound.setState(true);		m_option.add(m_level);		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.thread.stop();                              System.exit(0);	                }		 });	}	public void actionPerformed(ActionEvent e){		if(e.getSource() == mi_start){		   mi_pause.enable();		   if(!gameCanvas.thread.isAlive())			  gameCanvas.thread.start();		   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,320,230);			heroDialog.show();		        }		else if(e.getSource() == mi_close){			gameCanvas.thread.stop();			this.dispose();			System.exit(0);		 }		else if(e.getSource() == mi_setkey){			SetDialog setDialog = new SetDialog(this,"键盘设定",true);			setDialog.setBounds(200,150,300,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  作者:孙伟"));			p3.add(new Label("试用版 v1.0"));			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,250,150);			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 < 10; i++)		    if(e.getSource() == mi_level[i]){		       for(j = 0; j < 10; j++)		           mi_level[j].setState(false);		       mi_level[i].setState(true);		       gameCanvas.setSleepTime(i);	         }		 for(i = 0; i < 10; i++)		    if(e.getSource() == mi_height[i]){		       for(j = 0; j < 10; j++)		           mi_height[j].setState(false);		       mi_height[i].setState(true);		       gameCanvas.setHeight(i);	         }	}} 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 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 byte[][] getTurnBlock(){		byte[][] turnBlock = new byte[getCubeEach()][getCubeEach()];	    	boolean goBreak = 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) {                                	    				goBreak = true;	    				break;	    			}	    		if(goBreak) break;	    	}	    	int yy = y;	    	for(; y < getCubeEach(); y++) {	                         	    		for(x=0; x < getCubeEach(); x++)	    			turnBlock[y-yy][x] = turnBlock[y][x];	    	}	    	for(y = getCubeEach()-yy; y < getCubeEach(); y++) {		    		for(x=0; x < getCubeEach(); x++)	    			turnBlock[y][x] = 0;	    	}	    	return turnBlock;	}	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++)					   this.bgInformation[x][y] = this.bgInformation[x][y-1];		}	}	public int[] getDelLine(){		int[] line = new int[this.getCubeEach()];		int x, y;		for(y = this.getCurrentY(); y < this.getCurrentY() + line.length; y++) {			if(y >= this.getbgHeight()) break;			for(x = 0; x < this.getbgWidth(); x++)				if(this.bgInformation[x][y] == 0) break;			if(x == this.getbgWidth())				line[y - this.getCurrentY()] = y;			else				line[y - this.getCurrentY()] = 0;		}		return line;	}}class GameCanvas extends Canvas implements Runnable{		Thread thread;	private Block block;	private int height = 0;	private int sleep = 700;	private Color bgColor = Color.white;	private int blockScore = 0,blockLevel = 0,blockSpeed = 0;	private boolean isStart = true,isPause = true;	private Color colors[] = {Color.red,Color.orange,Color.gray,Color.green,                              Color.blue,Color.pink,Color.magenta,Color.white};                                    	public GameCanvas(){		block = new Block();		thread = new Thread(this,"gamethread");	    this.addKeyListener(new KeyAdapter() {		            public void keyPressed(KeyEvent e) {		                handleKeyEvent(e);		            }				});		}    public void setPause(boolean b){ isPause = b;}	public boolean getPause(){ return isPause; }	public Color getBGColor(){ return this.bgColor;}			public void run(){		block.setNextBlock();		block.reset();		paintNextBlock(getGraphics());		while(true){			if(!isStart)continue;		    if(!isPause)continue;		    repaint();		    try{thread.sleep(this.sleep);}catch(InterruptedException e){ return; }			if(!block.goDown()){				this.setBGInformation();				this.delLine();				block.setCurrentXY(4,0);				block.setCurrentBlock();				block.setNextBlock();				paintNextBlock(getGraphics());				repaint();				if(this.isStop()){this.paintStop(); this.isStart = false;this.popDialog();} 				continue;			}		 }    }    public void popDialog(){    	GameFrame gameFrame = new GameFrame();    	InputDialog inputDialog = new InputDialog(gameFrame,"请输入您的名字",true);    	inputDialog.setBounds(200,150,300,150);    	inputDialog.show();    }    public void setSleepTime(int i){    	switch(i){    		case 0: this.sleep = 800;break;			case 1: this.sleep = 700;break;			case 2: this.sleep = 600;break;			case 3: this.sleep = 500;break;			case 4: this.sleep = 400;break;			case 5: this.sleep = 300;break;			case 6: this.sleep = 200;break;			case 7: this.sleep = 100;break;			case 8: this.sleep = 70;break;			case 9: this.sleep = 40;break;		}    }    public void delLine(){    	int oneTime = 0;    	int[] line = block.getDelLine();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -