📄 game.java
字号:
import java.awt.event.*;
import javax.swing.JButton;
import java.awt.*;
import java.applet.*;
import java.util.Random;
public class Game extends Applet implements Runnable,ActionListener{
private Graphics bg,fbg;
private boolean filled[][]=new boolean[10][20];
private JButton bnStart=new JButton("开始");
private Image buffer,foreBuffer;
private SquareShape currShape;
private int xpos=100,ypos=0;
private int forexpos=50,foreypos=100;
private JButton bnPause=new JButton("暂停");
private Thread th;
private Ding ding;
private Square square;
private RightL rightl;
private Long long1;
private LeftL leftl;
private Turnz turnz;
private ObsverZ obsverz;
private Random r=new Random();
private int fore,back;
private boolean next=false;
private boolean going=false,pause=false;
public Game() {
for(int i=0;i<10;i++)
for(int j=0;j<20;j++)
filled[i][j]=false;
// TODO Auto-generated constructor stub
}
public void init()
{
buffer=this.createImage(200,400);
foreBuffer=this.createImage(120,300);
fbg=foreBuffer.getGraphics();
bg=buffer.getGraphics();
bg.fillRect(0,0,200,400);
fbg.fillRect(0,0,120,300);
back=r.nextInt(7);
currShape=this.getShape(back);
currShape.setPlace(xpos,ypos);
fore=r.nextInt(7);
foreSet(fore);
bnStart.setBounds(220,320,60,20);
bnStart.addActionListener(this);
this.add(bnPause);
bnPause.setBounds(220,360,60,20);
bnPause.addActionListener(this);
this.add(bnStart);
this.addKeyListener(new GameKeyListener());
this.setLayout(null);
this.setSize(400,420);
this.setLocation(200,100);
this.setVisible(true);
}
public static void main(String args[])
{
new Game();
}
public SquareShape getShape(int i)
{
switch(i)
{
case 0:
currShape=new Ding();
break;
case 1:
currShape=new Long();
break;
case 2:
currShape=new RightL();
break;
case 3:
currShape=new LeftL();
break;
case 4:
currShape=new Square();
break;
case 5:
currShape=new Turnz();
break;
case 6:
currShape=new ObsverZ();
break;
}
return currShape;
}
public void paint(Graphics g)
{
bnStart.repaint();
bnPause.repaint();
g.drawImage(buffer,0,0,null);
g.drawImage(foreBuffer,220,0,null);
currShape.drawShape(g);
}
public void update(Graphics g)
{
g.clipRect(0,0,340,420);
paint(g);
}
public void run()
{
while(going)
{
if(next)
{
currShape=this.getShape(fore);
fore=r.nextInt(7);
foreSet(fore);
xpos=100;
ypos=0;
next=false;
}
else if(currShape.getBottom()==400)
{
currShape.setPlace(xpos,ypos);
setFill();
currShape=this.getShape(fore);
fore=r.nextInt(7);
foreSet(fore);
xpos=100;
ypos=0;
}
else
{
ypos+=20;
currShape.setPlace(xpos,ypos);
if(isFilled())
{
ypos-=20;
currShape.setPlace(xpos,ypos);
setFill();
currShape=this.getShape(fore);
fore=r.nextInt(7);
foreSet(fore);
xpos=100;
ypos=0;
}
}
repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException e)
{ }
try{
if(pause)
{
synchronized(this)
{
wait();
}
}
}catch(InterruptedException e)
{
}
}
}
public 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;
}
public 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();
for(int i=19;i>=topRow;i--)
{
int j;
for(j=0;j<10;j++)
{
if(!filled[j][i])
break;
}
if(j==10)
{
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++;
}
}
}
public 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 GameKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==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(e.getKeyCode()==KeyEvent.VK_LEFT&&currShape.getLeft()>0)
{
xpos-=20;
currShape.setPlace(xpos,ypos);
if(isFilled())
{
xpos+=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT&&currShape.getRight()<200)
{
xpos+=20;
currShape.setPlace(xpos,ypos);
if(isFilled())
{
xpos-=20;
currShape.setPlace(xpos,ypos);
}
repaint();
}
else if(e.getKeyCode()==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)
{
currShape.setPlace(xpos,ypos);
setFill();
next=true;
}
repaint();
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bnStart)
{
if(th==null)
{
th=new Thread(this);
th.start();
going=true;
this.requestFocus(true);
}
else
{
pause=false;
this.requestFocus(true);
synchronized(this)
{
notify();
}
}
}
else
{
pause=true;
}
}
public void foreSet(int fore)
{
switch(fore)
{
case 0:
fbg.copyArea(0,20,120,80,0,80);
ding=new Ding();
ding.setPlace(forexpos,foreypos);
ding.drawShape(fbg);
break;
case 1:
fbg.copyArea(0,20,120,80,0,80);
long1=new Long();
long1.setPlace(forexpos,foreypos);
long1.drawShape(fbg);
break;
case 2:
fbg.copyArea(0,20,120,80,0,80);
rightl=new RightL();
rightl.setPlace(forexpos,foreypos);
rightl.drawShape(fbg);
break;
case 3:
fbg.copyArea(0,20,120,80,0,80);
leftl=new LeftL();
leftl.setPlace(forexpos,foreypos);
leftl.drawShape(fbg);
break;
case 4:
fbg.copyArea(0,20,120,80,0,80);
square=new Square();
square.setPlace(forexpos,foreypos);
square.drawShape(fbg);
break;
case 5:
fbg.copyArea(0,20,120,80,0,80);
turnz=new Turnz();
turnz.setPlace(forexpos,foreypos);
turnz.drawShape(fbg);
break;
case 6:
fbg.copyArea(0,20,120,80,0,80);
obsverz=new ObsverZ();
obsverz.setPlace(forexpos,foreypos);
obsverz.drawShape(fbg);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -