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

📄 gamemainframe.java

📁 基本的俄罗斯游戏 基本的俄罗斯游戏 基本的俄罗斯游戏
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class GameMainFrame extends JFrame implements KeyListener{
	
	JPanel gameJPanel,clew1JPanel,clew2JPanel;//游戏区面板
	JLabel defen_JLabel,score_JLabel;//游戏记分等标签
	JLabel dengji_JLabel,level_JLabel;
	JButton[][] playBlocks;//游戏方块数组
    int[][] flagBlocks;//游戏方块标志数组
    JButton[][] nextBlocks;//游戏提示区方块
    long score;//积分
    int level,speed,totalLines,currentLines;//游戏标志
    boolean isPause,isEnd;//游戏开始与暂停标志
    int blockType,nextblockType;//方块类型标志
    Block block;//方块类型
    GameThread thread;//游戏主线程
   
   
	//主窗体构造方法
	public GameMainFrame(){
		super("稀饭的俄罗斯方块");
		this.addGameMenu();
		gameJPanel=new JPanel(new GridLayout(20,15));
		gameJPanel.setBackground(Color.DARK_GRAY);
		clew1JPanel=new JPanel(new GridLayout(10,1));
		clew2JPanel=new JPanel(new GridLayout(4,4));
		defen_JLabel=new JLabel("得分");
		score_JLabel=new JLabel();
		dengji_JLabel=new JLabel("等级");
		level_JLabel=new JLabel();

		clew1JPanel.add(clew2JPanel);
		clew1JPanel.add(defen_JLabel);
		clew1JPanel.add(score_JLabel);
		clew1JPanel.add(dengji_JLabel);
		clew1JPanel.add(level_JLabel);
		BorderLayout borderlayout=new BorderLayout();
	    this.setLayout(borderlayout);
		this.add(gameJPanel,borderlayout.CENTER);
		this.add(clew1JPanel,borderlayout.EAST);
		
		
		this.addKeyListener(this);
		this.setFocusable(true);
		
		//创建并初始化游戏区方块数组
		playBlocks=new JButton[20][15];
		for(int i=0;i<20;i++){
			for(int j=0;j<15;j++){
				playBlocks[i][j]=new JButton();
				playBlocks[i][j].setBackground(Color.lightGray);
				playBlocks[i][j].setVisible(false);
				playBlocks[i][j].setEnabled(false);
				gameJPanel.add(playBlocks[i][j]);
			}
		}
		//创建并初始化游戏区方块标志数组
		flagBlocks=new int[20][15];
		for(int i=0;i<20;i++){
			for(int j=0;j<15;j++){
				flagBlocks[i][j]=0;
			}
		}
		//创建并初始化方块提示区方块数组
		nextBlocks=new JButton[4][4];
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				nextBlocks[i][j]=new JButton();
				nextBlocks[i][j].setBackground(Color.lightGray);
				nextBlocks[i][j].setVisible(false);
				nextBlocks[i][j].setEnabled(false);
				clew2JPanel.add(nextBlocks[i][j]);
				
			}
		}
		score=0;//初始化游戏参数
		level=0;
		speed=1000;
		totalLines=0;
		currentLines=0;
		isPause=false;
		isEnd=false;

		blockType=(int)(Math.random()*1000%7);
		switch(blockType){//随即产生游戏区方块
			case 0:block=new BlockOne(this);break;
			case 1:block=new BlockTwo(this);break;
			case 2:block=new BlockThree(this);break;
			case 3:block=new BlockFour(this);break;
			case 4:block=new BlockFive(this);break;
			case 5:block=new BlockSix(this);break;
			case 6:block=new BlockSeven(this);break;
		}
		nextblockType=(int)(Math.random()*1000%7);//随即产生提示区方块
		showNextBlock(nextblockType);
		score_JLabel.setText(Long.toString(score));
		level_JLabel.setText(Integer.toString(level));
		
	    
		thread=new GameThread(this);
		try{
			thread.start();//启动游戏主线程
		}catch(IllegalThreadStateException e){}
	this.setSize(500,500);
		this.setVisible(true);
	}
	
	
	//显示下一方块
	public void showNextBlock(int type){
		for(int i=0;i<4;i++){//清楚提示区的方块
			for(int j=0;j<4;j++){
				nextBlocks[i][j].setBackground(Color.lightGray);
				nextBlocks[i][j].setVisible(false);
			}
		}
		switch(type){//根据方块类型在提示区显示方块
			case 0:
			       nextBlocks[1][0].setBackground(Color.green);
			       nextBlocks[1][0].setVisible(true);
			       nextBlocks[1][1].setBackground(Color.green);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[2][1].setBackground(Color.green);
			       nextBlocks[2][1].setVisible(true);
			       nextBlocks[2][2].setBackground(Color.green);
			       nextBlocks[2][2].setVisible(true);
			       break;
			case 1:
			       nextBlocks[1][1].setBackground(Color.blue);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.blue);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[2][0].setBackground(Color.blue);
			       nextBlocks[2][0].setVisible(true);
			       nextBlocks[2][1].setBackground(Color.blue);
			       nextBlocks[2][1].setVisible(true);
			       break;
			case 2:
			       nextBlocks[1][1].setBackground(Color.cyan);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.cyan);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[2][1].setBackground(Color.cyan);
			       nextBlocks[2][1].setVisible(true);
			       nextBlocks[2][2].setBackground(Color.cyan);
			       nextBlocks[2][2].setVisible(true);
			       break;
		    case 3:
			       nextBlocks[1][0].setBackground(Color.yellow);
			       nextBlocks[1][0].setVisible(true);
			       nextBlocks[1][1].setBackground(Color.yellow);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.yellow);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[2][2].setBackground(Color.yellow);
			       nextBlocks[2][2].setVisible(true);
			       break;
		    case 4:
			       nextBlocks[1][1].setBackground(Color.magenta);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.magenta);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[1][0].setBackground(Color.magenta);
			       nextBlocks[1][0].setVisible(true);
			       nextBlocks[2][0].setBackground(Color.magenta);
			       nextBlocks[2][0].setVisible(true);
			       break;
		    case 5:
			       nextBlocks[1][1].setBackground(Color.darkGray);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.darkGray);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[1][0].setBackground(Color.darkGray);
			       nextBlocks[1][0].setVisible(true);
			       nextBlocks[2][1].setBackground(Color.darkGray);
			       nextBlocks[2][1].setVisible(true);
			       break;
		    case 6:
			       nextBlocks[1][1].setBackground(Color.red);
			       nextBlocks[1][1].setVisible(true);
			       nextBlocks[1][2].setBackground(Color.red);
			       nextBlocks[1][2].setVisible(true);
			       nextBlocks[1][3].setBackground(Color.red);
			       nextBlocks[1][3].setVisible(true);
			       nextBlocks[1][0].setBackground(Color.red);
			       nextBlocks[1][0].setVisible(true);
			       break;
		}
	}
	
	private void addGameMenu(){
		JMenuBar jmunebar=new JMenuBar();//菜单栏
		this.setJMenuBar(jmunebar);
		
		JMenu file_JMenu=new JMenu("文件");
		JMenuItem newgame_JMenuItem=new JMenuItem("新游戏");
		JMenuItem pause_JMenuItem=new JMenuItem("暂停");
		JMenuItem continue_JMenuItem=new JMenuItem("继续");
		JMenuItem exit_JMenuItem=new JMenuItem("退出");
		file_JMenu.add(newgame_JMenuItem);
		file_JMenu.add(pause_JMenuItem);
		file_JMenu.add(continue_JMenuItem);
		file_JMenu.addSeparator();
		file_JMenu.add(exit_JMenuItem);
		jmunebar.add(file_JMenu);
		
		JMenu opinion_JMenu=new JMenu("选项");
		JMenu mode_JMenu=new JMenu("模式");
		JMenuItem singlemode_JMenuItem=new JMenuItem("单人模式");
		JMenuItem doublemode_JMenuItem=new JMenuItem("双人模式");
		mode_JMenu.add(singlemode_JMenuItem);
		mode_JMenu.add(doublemode_JMenuItem);
		opinion_JMenu.add(mode_JMenu);
		JMenuItem levelset_JMenuItem=new JMenuItem("级别设置");
		JMenuItem heriorecord_JMenuItem=new JMenuItem("英雄榜");
		opinion_JMenu.add(levelset_JMenuItem);
		opinion_JMenu.add(heriorecord_JMenuItem);
		jmunebar.add(opinion_JMenu);
		
		JMenu help_JMenu=new JMenu("帮助");
		JMenuItem help_JMenuItem=new JMenuItem("游戏帮助");
		JMenuItem aboutgame_JMenuItem =new JMenuItem("关于稀饭的俄罗斯");
		help_JMenu.add(help_JMenuItem);
		help_JMenu.addSeparator();
		help_JMenu.add(aboutgame_JMenuItem);
		jmunebar.add(help_JMenu);
		
	}
	
	public void keyPressed(KeyEvent e){
		//if(!isEnd&&!isPause){
			if(e.getKeyCode()==KeyEvent.VK_UP)block.setDirection(block.TURN);
			if(e.getKeyCode()==KeyEvent.VK_DOWN)block.setDirection(block.DOWN);
			if(e.getKeyCode()==KeyEvent.VK_LEFT)block.setDirection(block.LEFT);
			if(e.getKeyCode()==KeyEvent.VK_RIGHT)block.setDirection(block.RIGHT);
		//}
	}
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}  
	

	
public static void main(String []args){
	new GameMainFrame();
}

   
  
  
}






 

⌨️ 快捷键说明

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