测试类: package zw.xierui.game.test;import javax.swing.JFrame; import zw.xierui.game.controller.Controller; import zw.xierui.game.entities.Ground; import zw.xierui.game.entities.ShapeFactory; import zw.xierui.game.view.GamePanel;/** * * @author Fish */ public class test { public static void main(String args[]){ ShapeFactory shapefactory = new ShapeFactory(); Ground ground = new Ground(); GamePanel gamepanel = new GamePanel(); Controller controller = new Controller(ground, shapefactory, gamepanel); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(gamepanel.getSize().width+10, gamepanel.getSize().height+35); frame.add(gamepanel); gamepanel.addKeyListener(controller); frame.addKeyListener(controller);
frame.setVisible(true); controller.newGame(); }}
图形工厂: package zw.xierui.game.entities;import java.util.Random; import zw.xierui.game.listener.ShapeListener;/** * * @author Fish */ public class ShapeFactory { private int shapes[][][] =new int[][][] { { { 1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0},
{1,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0}
} }; public Shape makeShape(ShapeListener listener){ System.out.println("Make a Shape"); Shape shape = new Shape(); shape.addShapeListener(listener); int type = new Random().nextInt(shapes.length); shape.setShape(shapes[type]); shape.setStataus(1); return new Shape(); }} 障碍物:package zw.xierui.game.entities;/** * * @author Fish */ public class Ground { public void recieve(Shape shape){ System.out.println("recieve a shape"); } public void drawMe(){ System.out.println("Ground's drawMe"); }} 监听接口: package zw.xierui.game.listener;import zw.xierui.game.entities.Shape;/** * * @author Fish */ public interface ShapeListener { public void shapeMoveDown(Shape shape);} 常量类:package zw.xierui.game.util;/** * * @author Fish */ public class Global { public static final int CELL_SIZE = 20;}
图形类: package zw.xierui.game.entities;import java.awt.Color; import java.awt.Graphics; import zw.xierui.game.listener.ShapeListener; import zw.xierui.game.util.Global;/** * * @author Fish */public class Shape { private ShapeListener shapelistener; private int[][] shapeBody; private int status = 0;//图形现有状态 private int top = 3; private int left = 3; public Shape() { new Thread(new ShapeDown()).start(); } public void moveLeft() { System.out.println("Shape's move left"); left--; } public void moveRight() { System.out.println("Shape's move right"); left++; } public void moveDown() { System.out.println("Shape's move down"); top++; } public void rorate() { System.out.println("Shape's rorate"); status = (status+1)%shapeBody.length; } public void drawMe(Graphics graphic) { System.out.println("shape's drawMe"); graphic.setColor(Color.ORANGE); for(int x = 0;x<4;x++) { for(int y = 0; y<4;y++) { if(getFlagByPoint(x, y)) { graphic.fill3DRect((left+x)*Global.CELL_SIZE, (top+y)*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true); } } } } private boolean getFlagByPoint(int x, int y) { return shapeBody[status][y * 4 + x] == 1; } private class ShapeDown implements Runnable { public void run() { while(true) { moveDown(); shapelistener.shapeMoveDown(Shape.this); try { Thread.sleep(1000); } catch(InterruptedException ie) { ie.printStackTrace(); } } } } public void addShapeListener(ShapeListener shapelistener) { if(shapelistener != null) { this.shapelistener = shapelistener; } } public void setShape(int shapeBody[][]){ this.shapeBody = shapeBody; } public void setStataus(int status){ this.status = status; }} 控制器:package zw.xierui.game.controller;import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import zw.xierui.game.entities.Ground; import zw.xierui.game.entities.Shape; import zw.xierui.game.entities.ShapeFactory; import zw.xierui.game.listener.ShapeListener; import zw.xierui.game.view.GamePanel;/** * * @author Fish */ public class Controller extends KeyAdapter implements ShapeListener { private Ground ground; private Shape shape; private ShapeFactory shapefactory; private GamePanel gamepanel;
@Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()){
package zw.xierui.game.test;import javax.swing.JFrame;
import zw.xierui.game.controller.Controller;
import zw.xierui.game.entities.Ground;
import zw.xierui.game.entities.ShapeFactory;
import zw.xierui.game.view.GamePanel;/**
*
* @author Fish
*/
public class test {
public static void main(String args[]){
ShapeFactory shapefactory = new ShapeFactory();
Ground ground = new Ground();
GamePanel gamepanel = new GamePanel();
Controller controller = new Controller(ground, shapefactory, gamepanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(gamepanel.getSize().width+10, gamepanel.getSize().height+35);
frame.add(gamepanel);
gamepanel.addKeyListener(controller);
frame.addKeyListener(controller);
frame.setVisible(true);
controller.newGame(); }}
package zw.xierui.game.entities;import java.util.Random;
import zw.xierui.game.listener.ShapeListener;/**
*
* @author Fish
*/
public class ShapeFactory {
private int shapes[][][] =new int[][][]
{
{
{ 1,0,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0},
{1,1,0,0,
1,0,0,0,
1,0,0,0,
0,0,0,0},
{1,1,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0},
{0,1,0,0,
0,1,0,0,
1,1,0,0,
0,0,0,0}
}
};
public Shape makeShape(ShapeListener listener){
System.out.println("Make a Shape");
Shape shape = new Shape();
shape.addShapeListener(listener);
int type = new Random().nextInt(shapes.length);
shape.setShape(shapes[type]);
shape.setStataus(1);
return new Shape();
}}
障碍物:package zw.xierui.game.entities;/**
*
* @author Fish
*/
public class Ground {
public void recieve(Shape shape){
System.out.println("recieve a shape"); }
public void drawMe(){
System.out.println("Ground's drawMe"); }}
监听接口:
package zw.xierui.game.listener;import zw.xierui.game.entities.Shape;/**
*
* @author Fish
*/
public interface ShapeListener {
public void shapeMoveDown(Shape shape);}
常量类:package zw.xierui.game.util;/**
*
* @author Fish
*/
public class Global
{
public static final int CELL_SIZE = 20;}
package zw.xierui.game.entities;import java.awt.Color;
import java.awt.Graphics;
import zw.xierui.game.listener.ShapeListener;
import zw.xierui.game.util.Global;/**
*
* @author Fish
*/public class Shape { private ShapeListener shapelistener;
private int[][] shapeBody;
private int status = 0;//图形现有状态
private int top = 3;
private int left = 3;
public Shape()
{
new Thread(new ShapeDown()).start();
} public void moveLeft()
{
System.out.println("Shape's move left");
left--;
}
public void moveRight()
{
System.out.println("Shape's move right");
left++;
}
public void moveDown()
{
System.out.println("Shape's move down");
top++;
}
public void rorate()
{
System.out.println("Shape's rorate");
status = (status+1)%shapeBody.length;
}
public void drawMe(Graphics graphic)
{
System.out.println("shape's drawMe");
graphic.setColor(Color.ORANGE);
for(int x = 0;x<4;x++)
{
for(int y = 0; y<4;y++)
{
if(getFlagByPoint(x, y))
{
graphic.fill3DRect((left+x)*Global.CELL_SIZE,
(top+y)*Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
}
}
private boolean getFlagByPoint(int x, int y)
{
return shapeBody[status][y * 4 + x] == 1;
}
private class ShapeDown implements Runnable
{
public void run()
{
while(true)
{
moveDown();
shapelistener.shapeMoveDown(Shape.this);
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
} } }
public void addShapeListener(ShapeListener shapelistener)
{
if(shapelistener != null)
{
this.shapelistener = shapelistener;
} }
public void setShape(int shapeBody[][]){
this.shapeBody = shapeBody;
}
public void setStataus(int status){
this.status = status;
}}
控制器:package zw.xierui.game.controller;import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import zw.xierui.game.entities.Ground;
import zw.xierui.game.entities.Shape;
import zw.xierui.game.entities.ShapeFactory;
import zw.xierui.game.listener.ShapeListener;
import zw.xierui.game.view.GamePanel;/**
*
* @author Fish
*/
public class Controller extends KeyAdapter implements ShapeListener {
private Ground ground;
private Shape shape;
private ShapeFactory shapefactory;
private GamePanel gamepanel;
@Override
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
System.out.println("rorate");
shape.rorate();
break;
case KeyEvent.VK_DOWN:
System.out.println("down");
shape.moveDown();
break;
case KeyEvent.VK_LEFT:
System.out.println("left");
shape.moveLeft();
break;
case KeyEvent.VK_RIGHT:
System.out.println("right");
shape.moveRight();
break;
}
gamepanel.display(ground, shape); } public void shapeMoveDown(Shape shape)
{
gamepanel.display(ground, shape); }
public void newGame(){
shape = shapefactory.makeShape(this);
}
public Controller(Ground ground,ShapeFactory shapefactory,GamePanel gamepanel){
super();
this.gamepanel = gamepanel;
this.ground = ground;
this.shapefactory = shapefactory;
}
}
游戏面板:package zw.xierui.game.view;import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import zw.xierui.game.entities.Ground;
import zw.xierui.game.entities.Shape;
import zw.xierui.game.entities.ShapeFactory;
import zw.xierui.game.listener.ShapeListener;/**
*
* @author Fish
*/
public class GamePanel extends JPanel{
private Ground ground;
private Shape shape;
private ShapeFactory shapefactory;
// private ShapeListener l;
public void display(Ground ground,Shape shape)
{
System.out.println("GamePanel's display");
this.ground = ground;
this.shape = shape;
this.repaint(); }
public GamePanel()
{
this.setSize(300, 300); } @Override
protected void paintComponent(Graphics g)
{
g.setColor(new Color(0xcfcfcf));
g.fillRect(0, 0, 300, 300);
if(shape != null&& ground != null)
{
shape.drawMe(g);
ground.drawMe();
}
}
}