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

📄 gamekk.java

📁 一个俄罗斯方块游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:



package iiist1;

import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class  GameKK extends KeyAdapter implements KeyListener, Runnable
{
	/// define variables
	int col = 24;
	int row = 13;
	int [][] fSquare = new int [col][row];   // frist crate square
	int [][] bSquare = new int [col][row];  // second crate square
	int [][] pSquare = new int [col][row];
	int [][] rSquare = new int [col][row];   // rotation square
	int [][] sSquare = new int [3][5]; // prepare square
	int score = 0;
	int state = 0;
	int rotaCol;
	int rotaRow;
	int ssquare;
	int fsquare;



	///
	JFrame frame;
	boolean start = false;
	boolean stop = false;
	Thread timerThread;

	public GameKK()
	{
		// structure initural value;
		createFirstSquare();
       createSecondSquare();
	  

		// 
		frame = new JFrame();
		frame.getContentPane().add(new Screen());
		frame.addKeyListener(this);
		frame.setSize(640+5*2, 480+5*2+25);
		frame.setVisible(true);
		frame.repaint();

	}
	class Screen extends JComponent
	{
		public void paint(Graphics g)
		{
			Graphics2D g2d =(Graphics2D)g;
			// paint picture;
			showOther(g2d,this);
			showFirstSquare(g2d);
			showSecondSquare(g2d, this);
			showMergeSquare(this, g2d);
			showScore(g2d,this);
			showState(g2d,this);


			// finishe picture;
		}
	}
	public void run()
	{
		while (true)
		{
			try
			{
				timerThread.sleep(500-50*state);
				
			}
			catch (InterruptedException ie)
			{
				ie.printStackTrace();
			}
			if(start==false)
				return;
			//   定时运行
			if(isFall())
			{
				fallSquare();
			}
            else
			{
				mergeSquare();
				score(cancelLine());
				state();
				if(isDown())
				{
					gameOver();
				}
				else
				{
				   createFirstSquare();
				   createSecondSquare();
				}
			}

			//结束 定时运行
			frame.repaint();
		}
	}

	public void keyPressed(KeyEvent e)
	{
		switch (e.getKeyCode())
		{
		case KeyEvent.VK_ENTER:
			timerThread = new Thread(this);
		    timerThread.start();
			start=true;
			break;
		case KeyEvent.VK_ESCAPE:
			start=false;
		     System.exit(0);
			break;
		case KeyEvent.VK_RIGHT:
			if(isRight()==true)
			     rightMove();

			break;
		case KeyEvent.VK_UP:
			if(isTurn()==true)
			{
			    turn();
			}
			break;
		case KeyEvent.VK_LEFT:
			if(isLeft()==true)
			    leftMove();

			break;
		case KeyEvent.VK_DOWN:
			
			quikFall();

			break;
		case KeyEvent.VK_S:
			if(stop==true)
			{
				stop=false;
			}
			{
				stop=true;
			}

			break;
		
		}
	}
	public void showPicture(Screen s, Graphics2D g2d, String files, int x, int y)
	{
		java.io.File file  = new java.io.File(files);
		if(file.exists())
		{
			Image photo =(new ImageIcon(files)).getImage();
			g2d.drawImage(photo, x,y,s);
		}
		else
		{
			System.out.println("the picture path dose exists!!!");
		}

	}
    
	public int rand(int from, int to)
	{
		return (int)(from+(to-from+1)*Math.random());
	}

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

	public void createFirstSquare()
	{
		fsquare = ssquare;
		rotaRow=6;
		rotaCol=0;
		switch(fsquare)
		{
			case 0:
			fSquare[0][4]=1;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=0;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 1:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 2:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=0;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 3:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=0;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 4:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 5:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 6:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=1;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 7:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=1;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 8:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=0;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 9:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=0;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 10:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=0;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 11:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 12:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=1;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 13:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=0;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=0;fSquare[1][8]=0;		
			break;
		case 14:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=0;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;		
			break;
		case 15:
			fSquare[0][4]=0;fSquare[0][5]=0;fSquare[0][6]=0;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;	
			fSquare[2][4]=0;fSquare[2][5]=1;fSquare[2][6]=0;fSquare[2][7]=0;fSquare[2][8]=0;	
			
			break;
		case 16:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=0;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;
			fSquare[2][4]=0;fSquare[2][5]=0;fSquare[2][6]=0;fSquare[2][7]=1;fSquare[2][8]=0;			
			break;
		case 17:
			fSquare[0][4]=1;fSquare[0][5]=1;fSquare[0][6]=0;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=1;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;
			fSquare[2][4]=0;fSquare[2][5]=0;fSquare[2][6]=0;fSquare[2][7]=0;fSquare[2][8]=0;			
			break;
		case 18:
			fSquare[0][4]=1;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=0;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=1;fSquare[1][7]=1;fSquare[1][8]=0;
			fSquare[2][4]=0;fSquare[2][5]=0;fSquare[2][6]=0;fSquare[2][7]=0;fSquare[2][8]=0;			
			break;
		case 19:
			fSquare[0][4]=0;fSquare[0][5]=1;fSquare[0][6]=1;fSquare[0][7]=1;fSquare[0][8]=0;
			fSquare[1][4]=0;fSquare[1][5]=0;fSquare[1][6]=1;fSquare[1][7]=0;fSquare[1][8]=0;
			fSquare[2][4]=0;fSquare[2][5]=0;fSquare[2][6]=1;fSquare[2][7]=0;fSquare[2][8]=0;			
			break;		
		}
		ssquare = rand(0,7+state);
	}

	public void createSecondSquare()
	{
		switch (ssquare)
		{
		case 0:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=1;sSquare[0][0]=1;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=0;sSquare[1][3]=0;sSquare[1][0]=0;		
			break;
		case 1:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 2:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=0;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 3:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=0;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 4:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 5:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 6:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=1;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 7:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=1;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 8:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=0;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 9:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=0;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 10:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=0;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 11:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 12:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=1;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 13:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=0;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=0;sSquare[1][4]=0;		
			break;
		case 14:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=0;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;		
			break;
		case 15:
			sSquare[0][0]=0;sSquare[0][1]=0;sSquare[0][2]=0;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;	
			sSquare[2][0]=0;sSquare[2][1]=1;sSquare[2][2]=0;sSquare[2][3]=0;sSquare[2][4]=0;	
			
			break;
		case 16:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=0;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;
			sSquare[2][0]=0;sSquare[2][1]=0;sSquare[2][2]=0;sSquare[2][3]=1;sSquare[2][4]=0;			
			break;
		case 17:
			sSquare[0][0]=1;sSquare[0][1]=1;sSquare[0][2]=0;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=1;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;
			sSquare[2][0]=0;sSquare[2][1]=0;sSquare[2][2]=0;sSquare[2][3]=0;sSquare[2][4]=0;			
			break;
		case 18:
			sSquare[0][0]=1;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=0;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=1;sSquare[1][3]=1;sSquare[1][4]=0;
			sSquare[2][0]=0;sSquare[2][1]=0;sSquare[2][2]=0;sSquare[2][3]=0;sSquare[2][4]=0;			
			break;
		case 19:
			sSquare[0][0]=0;sSquare[0][1]=1;sSquare[0][2]=1;sSquare[0][3]=1;sSquare[0][4]=0;
			sSquare[1][0]=0;sSquare[1][1]=0;sSquare[1][2]=1;sSquare[1][3]=0;sSquare[1][4]=0;
			sSquare[2][0]=0;sSquare[2][1]=0;sSquare[2][2]=1;sSquare[2][0]=0;sSquare[2][4]=0;			
			break;		
		}

	}

	public void showFirstSquare(Graphics2D g2d)
	{
		for (int c=0;c<col ;c++ )
		{

⌨️ 快捷键说明

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