package russion;
import java.applet.*;
import java.awt.*;
/**
* <p>Title: RussionCube</p>
* <p>Description: TheClassicGame</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Cqu</p>
* @author Ghostliang
* @version 1.0
*/
public class RussionCube extends Applet implements Runnable
{
private Interfaces interfaces;
private Thread controlThread = null;
private BorderLayout thisLayout = new BorderLayout();
private PaintPanel paintPanel;
private Cube currentCube,nextCube;
private GameInterface game;
private InfoInterface info;
private Thread autoDownThread = null;
//Construct the applet
public RussionCube()
{
}
//Initialize the applet
public void init()
{
int level = (getParameter("level") == null)?1:Integer.parseInt(getParameter("level"));
int rows = (getParameter("rows") == null)?21:Integer.parseInt(getParameter("rows"));
int cols = (getParameter("cols") == null)?11:Integer.parseInt(getParameter("cols"));
int size = (getParameter("size") == null)?15:Integer.parseInt(getParameter("size"));
interfaces = new Interfaces(20,20,rows,cols,size,Color.white,Color.white,Color.black,Cube.colors,level);
paintPanel = new PaintPanel(interfaces);
try {
currentCube = interfaces.getCurrentCube();
nextCube = interfaces.getNextCube();
game = interfaces.getGameInterface();
info = interfaces.getInfoInterface();
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
resize(interfaces.getWidth(),interfaces.getHeight());
}
public void start()
{
if(autoDownThread == null)
{
autoDownThread = new Thread(this);
autoDownThread.start();
}
}
private void jbInit() throws Exception {
this.setBackground(Color.white);
this.setLayout(thisLayout);
this.add(paintPanel,BorderLayout.CENTER);
}
public void run()
{
int sleepTime;
while(true)
//always run start!
{
//try start
try
{
//if is alive?
while(interfaces.getGameInterface().isAlived())
{
//if is run?
while(interfaces.getGameInterface().isRunning())
{
sleepTime = 1000;
for(int i = 1;i < info.getLevel();i++)
sleepTime = (int)(sleepTime * 0.8);
Thread.sleep(sleepTime);
//what to do --- down?
if(game.canGoDown(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.goDown();
game.setCurrentCube(currentCube);
}
}
//what to do --- down end?
//what to do --- can't down?
else
{
game.fireCurrentCube();
info.update(game.clearRows());
currentCube = nextCube;
//if is alive?
if(game.initCurrentCube(currentCube))
{
game.setCurrentCube(currentCube);
nextCube = new Cube(0,0,game.getBlockSize(),(int)(Math.random()*7),Cube.colors);
info.setNextCube(nextCube);
}
//if is alive end
//is not alive
else
{
game.setAlive(false);
game.setRun(false);
for(int row = game.getTotalRow() - 1;row >= 0;row--)
for(int col = game.getTotalCol() - 1;col >= 0;col--)
if(game.getBlock(row,col).isFilled())
{
game.getBlock(row,col).setColor(Color.lightGray);
paintPanel.repaint();
Thread.sleep(20);
}
}
//if not alive end
}
paintPanel.repaint();
}
//if is run end?
}
//if is alive end?
}
//try end
catch(InterruptedException ie){}
}
//always run end!
}
public boolean keyDown(Event e,int key)
{
switch(key)
{
case Event.UP:
if(game.canTranslate(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.translate();
game.setCurrentCube(currentCube);
}
}
paintPanel.repaint();
break;
case Event.LEFT:
if(game.canGoLeft(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.goLeft();
game.setCurrentCube(currentCube);
}
}
paintPanel.repaint();
break;
case Event.RIGHT:
if(game.canGoRight(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.goRight();
game.setCurrentCube(currentCube);
}
}
paintPanel.repaint();
break;
case Event.DOWN:
if(game.canGoDown(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.goDown();
game.setCurrentCube(currentCube);
}
}
//what to do --- down end?
//what to do --- can't down?
else
{
game.fireCurrentCube();
info.update(game.clearRows());
currentCube = nextCube;
//if is alive?
if(game.initCurrentCube(currentCube))
{
game.setCurrentCube(currentCube);
nextCube = new Cube(0,0,game.getBlockSize(),(int)(Math.random()*7),Cube.colors);
info.setNextCube(nextCube);
}
//if is alive end
//is not alive
else
{
game.setAlive(false);
for(int row = game.getTotalRow() - 1;row >= 0;row--)
for(int col = game.getTotalCol() - 1;col >= 0;col--)
if(game.getBlock(row,col).isFilled())
{
game.getBlock(row,col).setColor(Color.lightGray);
try {
Thread.sleep(20);
}
catch (InterruptedException ex) {
}
paintPanel.repaint();
}
}
//if not alive end
}
paintPanel.repaint();
break;
case 32:
while(game.canGoDown(currentCube))
{
if(game.isAlived() && game.isRunning())
{
game.clearCurrentCube();
currentCube.goDown();
game.setCurrentCube(currentCube);
}
}
game.fireCurrentCube();
info.update(game.clearRows());
currentCube = nextCube;
//if is alive?
if(game.initCurrentCube(currentCube))
{
game.setCurrentCube(currentCube);
nextCube = new Cube(0,0,game.getBlockSize(),(int)(Math.random()*7),Cube.colors);
info.setNextCube(nextCube);
}
//if is alive end
//is not alive
else
{
game.setAlive(false);
for(int row = game.getTotalRow() - 1;row >= 0;row--)
for(int col = game.getTotalCol() - 1;col >= 0;col--)
if(game.getBlock(row,col).isFilled())
{
game.getBlock(row,col).setColor(Color.lightGray);
try {
Thread.sleep(20);
}
catch (InterruptedException ex) {
}
paintPanel.repain
- 1
- 2
前往页