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

📄 game.java

📁 用java实现的俄罗斯方块游戏
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
//Download by http://www.codefans.net
import java.io.*;

public class Game extends Applet {

	private int xpos=100,ypos=0;
	private SquareShape currShape;
	private Image buffer;//画布对象
    private Graphics bg;
	public void init() {
		
		buffer=this.createImage(200,400);//创建画布大小	
		bg=buffer.getGraphics();
		bg.fillRect(0,0,200,400);
		
		currShape=new Ding();
		currShape.setPlace(xpos,ypos);

		this.addKeyListener(new GameKeyLister());
	}

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

	   g.drawImage(buffer,0,0,null);//背景

	   currShape.drawShape(g);//图形
	}
	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);										
				}
				repaint();
			}
			else if(keyCode==KeyEvent.VK_RIGHT)
			{
				xpos+=20;
				currShape.setPlace(xpos,ypos);
				
				if(currShape.getRight()>200)
				{
					xpos-=20;
					currShape.setPlace(xpos,ypos);
				}
				
				repaint();
			}
			else if(keyCode==KeyEvent.VK_LEFT)
			{
				xpos-=20;
				currShape.setPlace(xpos,ypos);
				
				if(currShape.getLeft()<0)
				{
					xpos+=20;
					currShape.setPlace(xpos,ypos);
				}
				
				repaint();
				
			}
			else if(keyCode==KeyEvent.VK_DOWN)
			{
				ypos+=20;
				currShape.setPlace(xpos,ypos);
				
				if(currShape.getBottom()>400)
				{
					ypos-=20;
					currShape.setPlace(xpos,ypos);										
				}
				
				repaint();
				
			}
		}
	}
}

⌨️ 快捷键说明

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