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

📄 game.java

📁 此游戏仅是用来使Java初学者掌握Java基础语法和面向对象的编程思想。
💻 JAVA
字号:

/**
 *声明:1、本程序是作为金鹰教育网学习Java基本语法的例子。可以自由使用,但必须保留此声明!
 *      2、具体的介绍、说明文档,请见金鹰教育网:www.eagledu.org
 *	    3、有问题的,请到金鹰教育网:www.eagledu.org中的Java标准版本课程中提出,我们会及时给予答复!
 *      4、有其它想法的,也可直接交流:eagleduorg@126.com
 */


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Game extends Applet implements Runnable{

	private int xpos=100,ypos=0;//当前活动图形的相对坐标
	private BaseShape currShape;//当前活动的图形
	
	private Image buffer;//双缓存内存图
    private Graphics bg;
    
    private Thread th;
    private Button btnStart=new Button("开始");
    
    private Pane cell[][]=new Pane[20][10];
    private boolean next=false;//是否开始下一个图形
    
    
    public Game()
    {
    	th=new Thread(this);
    }

	public void init() {
		
		buffer=this.createImage(200,400);
		bg=buffer.getGraphics();
				
		currShape=new Ding();
		currShape.setShape(xpos,ypos);

		bg.setColor(Color.BLACK);
		bg.fillRect(0,0,200,400);
		currShape.paint(bg);

		this.addKeyListener(new GameKeyLister());
		
		this.setLayout(null);
		btnStart.setBounds(220,20,60,20);
		btnStart.addActionListener(new StartActionListener());
		this.add(btnStart);
	}
	
	public void update(Graphics g)
	{
		g.clipRect(0,0,200,400);
		
		bg.setColor(Color.BLACK);
		bg.fillRect(0,0,200,400);
		
	    for(int rows=0;rows<20;rows++)
	    {
	   		for(int cols=0;cols<10;cols++)
	   		{
	   			if(cell[rows][cols]!=null)
	   			{
	   				cell[rows][cols].paint(bg);
	   		    }
	   		}
	    }
	    
	    currShape.paint(bg);
		
		paint(g);
	}
	
	public void paint(Graphics g) {

	   g.drawImage(buffer,0,0,this);
	}
	
	//将图形保存到单元格之中
	private void setFill()
	{
		for(int i=0;i<4;i++)
		{
			int index[]=currShape.getPaneSuffix(i);
			cell[index[0]][index[1]]=currShape.getPane(i);
		}
   }
    
	//判断图形间是否碰撞
	private boolean isFilled()
	{
		int first[]=currShape.getPaneSuffix(0);
		int second[]=currShape.getPaneSuffix(1);
		int third[]=currShape.getPaneSuffix(2);
		int fourth[]=currShape.getPaneSuffix(3);
		
		if(cell[first[0]][first[1]]!=null || cell[second[0]][second[1]]!=null || cell[third[0]][third[1]]!=null || cell[fourth[0]][fourth[1]]!=null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}	
	
	
	//子线程的代码,每隔0.5移,下移一格
	public void run()
	{
		while(true)
		{
			if(next)
			{
				currShape=new Ding();
				xpos=100;
				ypos=0;
				currShape.setShape(xpos,ypos);
				next=false;
			}
			else if(currShape.getBottom()==400)
			{
				setFill();

				currShape=new Ding();
				xpos=100;
				ypos=0;
				currShape.setShape(xpos,ypos);
			}
			else
			{
				ypos+=20;
				currShape.setShape(xpos,ypos);
				if(isFilled())
				{
					ypos-=20;
					currShape.setShape(xpos,ypos);
					
					setFill();
					currShape=new Ding();
					xpos=100;
					ypos=0;
					currShape.setShape(xpos,ypos);
				}
			}

		
			repaint();
			try{
				
				Thread.sleep(500);				
			}
			catch(InterruptedException e)
			{	}
		}
		
	}
		
	//内部类,处理按钮单击事件
	class StartActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			th.start();
			
			requestFocus(true);//让applet得到焦点
	    }		
	}
	
	//内部类,以实现键盘的事件处理
	class GameKeyLister extends KeyAdapter
	{
		public void keyReleased(KeyEvent e)
		{
			int keyCode=e.getKeyCode();
			
			if(keyCode==KeyEvent.VK_UP)
			{
				int oldLoc[]=currShape.getShapeLocation();
				
				currShape.rotate();
				currShape.setShape(xpos,ypos);
				
				if(isFilled())
				{
					currShape.contraRotate();
					currShape.setShape(oldLoc[0],oldLoc[1]);
				}
				while(currShape.getLeft()<0)
				{
					xpos+=20;
					currShape.setShape(xpos,ypos);
					
					if(isFilled())
					{
						currShape.contraRotate();
						currShape.setShape(oldLoc[0],oldLoc[1]);
						break;
					}
					
				}
				while(currShape.getRight()>200)
				{
					xpos-=20;
					currShape.setShape(xpos,ypos);
					
					if(isFilled())
					{
						currShape.contraRotate();
						currShape.setShape(oldLoc[0],oldLoc[1]);
						break;
					}
										
				}
				while(currShape.getBottom()>400)
				{
					ypos-=20;
					currShape.setShape(xpos,ypos);	
					
					if(isFilled())
					{
						currShape.contraRotate();
						currShape.setShape(oldLoc[0],oldLoc[1]);
						break;
					}
														
				}
				
				repaint();
			}
			else if(keyCode==KeyEvent.VK_RIGHT && currShape.getRight()<200)
			{
				xpos+=20;
				currShape.setShape(xpos,ypos);
				
				if(isFilled())
				{
					xpos-=20;
					currShape.setShape(xpos,ypos);
				}
				
				repaint();
			}
			else if(keyCode==KeyEvent.VK_LEFT && currShape.getLeft()>0)
			{
				xpos-=20;
				currShape.setShape(xpos,ypos);
				
				if(isFilled())
				{
					xpos+=20;
					currShape.setShape(xpos,ypos);
				}
				
				repaint();
			}
			else if(keyCode==KeyEvent.VK_DOWN && currShape.getBottom()<400)
			{
				ypos+=20;
				currShape.setShape(xpos,ypos);
				
				if(isFilled())
				{
					ypos-=20;
					currShape.setShape(xpos,ypos);
					
					setFill();
					next=true;										
				}
				else if(currShape.getBottom()==400)
				{
					setFill();
					next=true;
				}
				
				repaint();
			}
		}
	}	
}

⌨️ 快捷键说明

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