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

📄 game.java

📁 俄罗斯方块-JAVA源代码 的逐步实现
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;

public class Game extends Applet implements Runnable{

	private int xpos=100,ypos=0;
	private SquareShape currShape;
	private Image buffer;//画布对象
    private Graphics bg;
    private Thread th;
    private JButton btnStart=new JButton("开始");
    private boolean filled[][]=new boolean[10][20];
    private boolean next=false;
    
    public Game()
    {
    	th=new Thread(this);
    	
    	for(int i=0;i<10;i++)
    	{
    		for(int j=0;j<20;j++)
    		{
    			filled[i][j]=false;
    		}
    	}
    }
	public void init() {
		
		this.setLayout(null);
		
		buffer=this.createImage(200,400);//创建画布大小	
		bg=buffer.getGraphics();
		bg.fillRect(0,0,200,400);
		
		currShape=new Ding();
		currShape.setPlace(xpos,ypos);

		btnStart.setBounds(220,20,60,20);
		btnStart.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				th.start();
				
				requestFocus(true);//让applet得到焦点
		    }
		}
		);
		this.add(btnStart);
		
		this.addKeyListener(new GameKeyLister());
	}

	public void update(Graphics g)//实现不闪烁
	{
		g.clipRect(0,0,200,400);
		paint(g);
	}
	
	public void paint(Graphics g) {//图画由两部分组成,一部分为背景,另一部分为图形
	   
	   btnStart.repaint();

	   g.drawImage(buffer,0,0,null);//背景
	   currShape.drawShape(g);//图形
	}
	public void run()
	{
		while(true)
		{
			if(next)
			{
				currShape=new Ding();
				xpos=100;
				ypos=0;
				next=false;
			}
			else if(currShape.getBottom()==400)
			{
				setFill();

				currShape=new Ding();
				xpos=100;
				ypos=0;
			}
			else
			{
				ypos+=20;
				currShape.setPlace(xpos,ypos);
				if(isFilled())
				{
					ypos-=20;
					currShape.setPlace(xpos,ypos);
					
					setFill();
					currShape=new Ding();
					xpos=100;
					ypos=0;
				}
			}
		
			repaint();
			try{
				
				Thread.sleep(500);				
			}
			catch(InterruptedException e)
			{	}
		}
		
	}
	private void setFill()
	{
		int first[]=currShape.getFirstPlace();
		int second[]=currShape.getSecondPlace();
		int third[]=currShape.getThirdPlace();
		int fourth[]=currShape.getFourthPlace();
		filled[first[0]][first[1]]=true;
		filled[second[0]][second[1]]=true;
		filled[third[0]][third[1]]=true;
		filled[fourth[0]][fourth[1]]=true;
		
		currShape.drawShape(bg);
	}
	
	private boolean isFilled()
	{
		int first[]=currShape.getFirstPlace();
		int second[]=currShape.getSecondPlace();
		int third[]=currShape.getThirdPlace();
		int fourth[]=currShape.getFourthPlace();	
		if(filled[first[0]][first[1]] || filled[second[0]][second[1]] || filled[third[0]][third[1]] || filled[fourth[0]][fourth[1]])
		{
			return true;
		}
		else
		{
			return false;
		}
	}	
	class GameKeyLister extends KeyAdapter
	{
		public void keyReleased(KeyEvent e)
		{
			int keyCode=e.getKeyCode();
			if(keyCode==KeyEvent.VK_UP)
			{
				currShape.rotate();
				currShape.setPlace(xpos,ypos);
				
				while(currShape.getLeft()<0)
				{
					xpos+=20;
					currShape.setPlace(xpos,ypos);
				}
				while(currShape.getRight()>200)
				{
					xpos-=20;
					currShape.setPlace(xpos,ypos);					
				}
				while(currShape.getBottom()>400)
				{
					ypos-=20;
					currShape.setPlace(xpos,ypos);										
				}
				if(isFilled())
				{
					currShape.contraRotate();
					currShape.setPlace(xpos,ypos);
				}
				repaint();
			}
			else if(keyCode==KeyEvent.VK_RIGHT && currShape.getRight()<200)
			{
				xpos+=20;
				currShape.setPlace(xpos,ypos);
				
				if(isFilled())
				{
					xpos-=20;
					currShape.setPlace(xpos,ypos);
				}
				
				repaint();
			}
			else if(keyCode==KeyEvent.VK_LEFT && currShape.getLeft()>0)
			{
				xpos-=20;
				currShape.setPlace(xpos,ypos);
				
				if(isFilled())
				{
					xpos+=20;
					currShape.setPlace(xpos,ypos);
				}
				
				repaint();
				
			}
			else if(keyCode==KeyEvent.VK_DOWN && currShape.getBottom()<400)
			{
				ypos+=20;
				currShape.setPlace(xpos,ypos);
				
				if(isFilled())
				{
					ypos-=20;
					currShape.setPlace(xpos,ypos);
					
					setFill();
					next=true;										
				}
				else if(currShape.getBottom()==400)
				{
					setFill();
					next=true;
				}
				
				repaint();
				
			}
		}
	}
}

⌨️ 快捷键说明

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