📄 game.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 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;//是否开始下一个图形
private boolean going=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)
{
going=true;
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(going)
{
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 int getTopRow()
{
int topRow=19;
int i,j;
for(i=19;i>=0;i--)
{
for(j=0;j<10;j++)
{
if(filled[j][i])
break;
}
if(j==10)
{
topRow=i+1;
break;
}
}
return topRow;
}
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);//
//判断是否结束
boolean isOver=false;
for(int i=0;i<10;i++)
{
if(filled[i][0])
isOver=true;
}
if(isOver)
{
going=false;
}
//判断是否有满行
int topRow=getTopRow();
System.out.println(topRow);
for(int i=19;i>=topRow;i--)
{
int j;
for(j=0;j<10;j++)
{
if(!filled[j][i])
break;
}
if(j==10)//i行是满的
{
System.out.println(i);
for(int ii=i-1;ii>=topRow-1;ii--)
{
bg.copyArea(0,(ii)*20,200,20,0,20);
}
for(int ii=i-1;ii>=topRow-1;ii--)
{
for(int jj=0;jj<10;jj++)
{
filled[jj][ii+1]=filled[jj][ii];
}
}
i++;
topRow++;
}
}
}
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 + -