Shape public class Shape private Shape Listener listener
编写各个类主体框架-Shape类 public class Shape { //private Shape. Listener listener; public void move. Left(){ System. out. println("shape's move. Left"); } public void move. Right(){ System. out. println("shape's moveright"); } public void move. Down(){ System. out. println("shape's move. Down"); } public void rotate(){ System. out. println("shape's rotate"); } public void draw. Me(){ System. out. println("shape's drawme"); } private class Shape. Driver implements Runnable{ public void run() { // TODO Auto-generated method stub while(true){ move. Down(); //listener. shape. Move. Down(Shape. this); try { Thread. sleep(1000); } catch (Interrupted. Exception e) { // TODO Auto-generated catch block e. print. Stack. Trace();
编写各个类主体框架-Shape. Factory类 public class Shape. Factory { public Shape get. Shape(Shape. Listener listener){ System. out. println("Shape. Factory's get. Shape"); Shape shape=new Shape(); return shape; } }
编写各个类主体框架-Ground类 package cn. tetris. entities; public class Ground { public void accept(){ System. out. println("Ground's accept"); } public void draw. Me(){ System. out. println("Ground's draw. Me"); } }
编写各个类主体框架-Game. Panel类 public class Game. Panel extends JPanel { private Ground ground; private Shape shape; public void display(Ground ground, Shape shape){ System. out. println("Game. Panel's display"); this. ground=ground; this. shape=shape; this. repaint(); } @Override protected void paint. Component(Graphics arg 0) { // TODO Auto-generated method stub //重新显示 if (ground!=null && shape!=null){ ground. draw. Me(); shape. draw. Me(); } }
编写各个类主体框架-Controller类 public class Controller extends Key. Adapter { private Ground ground; private Shape shape; private Shape. Factory shape. Factory; private Game. Panel game. Panel; public void key. Pressed(Key. Event e) { switch(e. get. Key. Code()){ case Key. Event. VK_UP: shape. rotate(); break; case Key. Event. VK_LEFT: shape. move. Left(); break; case Key. Event. VK_RIGHT: shape. move. Right(); break; case Key. Event. VK_DOWN: shape. move. Down(); break; } game. Panel. display(ground, shape); }
编写各个类主体框架-Shape. Listener接口 public interface Shape. Listener { void shape. Move. Down(Shape shape); }
Shape类增加监听器对象及下落后调用 public class Shape { private Shape. Listener listener; public void move. Left(){ System. out. println("shape's move. Left"); } public void move. Right(){ System. out. println("shape's moveright"); } public void move. Down(){ System. out. println("shape's move. Down"); } public void rotate(){ System. out. println("shape's rotate"); } public void draw. Me(){ System. out. println("shape's drawme"); } private class Shape. Driver implements Runnable{ public void run() { // TODO Auto-generated method stub while(true){ move. Down(); listener. shape. Move. Down(Shape. this); try { Thread. sleep(1000); } catch (Interrupted. Exception e) { // TODO Auto-generated catch block e. print. Stack. Trace();
Shape类中增加注册监听器的方法 public void add. Shape. Listener(Shape. Listener l){ if(l!=null){ this. listener =l; } }
Shape构造方法中启动下落线程 public Shape(){ new Thread(new Shape. Driver()). start(); }
Controller类实现Shape. Listener接口 public class Controller extends Key. Adapter implements Shape. Listener { public void shape. Move. Down(Shape shape) { // TODO Auto-generated method stub game. Panel. display(ground, shape); }
生产图形时同时注册监听器 public class Shape. Factory { public Shape get. Shape(Shape. Listener listener){ System. out. println("Shape. Factory's get. Shape"); Shape shape=new Shape(); shape. add. Shape. Listener(listener); return shape; } }
Game. Panel类设置大小 public Game. Panel(){ this. set. Size(300, 300); }
Controller类中增加开始新游戏方法 public void new. Game(){ shape=shape. Factory. get. Shape(this); }
Controller类中如何接收外部控制的对象 public Controller(Shape. Factory shape. Factory, Ground ground, Game. Panel game. Panel){ this. shape. Factory=shape. Factory; this. ground=ground; this. game. Panel=game. Panel; }
测试类Game public class Game { public static void main(String[] args) { // TODO Auto-generated method stub Shape. Factory shape. Factory=new Shape. Factory(); Ground ground=new Ground(); Game. Panel game. Panel=new Game. Panel(); Controller controller=new Controller(shape. Factory, ground, game. Panel); JFrame frame=new JFrame(); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Size(game. Panel. get. Size(). width+10, game. Panel. get. Size(). height+35); frame. add(game. Panel); game. Panel. add. Key. Listener(controller); frame. set. Visible(true); controller. new. Game(); } }
Shape类,增加图形的描述 //二维变量用于保存图形的所有状态 private int[][] body; //用于保存图形当前的状态 private int status; //设置状态的方法 public void set. Body(int body[][]){ this. body=body; } //设置当前是第几种状态 public void set. Status(int status){ this. status=status; }
Shape. Factory类增加生产各种图形 //三维数组用于表示一种图形 的多种形状 private int shapes[][][]=new int[][][]{ {{1, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {1, 1, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 0} }}; public Shape //生产一个随机数,用于表示图形的 状态 int type=new Random(). next. Int(shapes. length); //设置图形有几种状态 shape. set. Body(shapes[type]); //设置默认状态 shape. set. Status(0); return shape; } }
Shape类中增加图形的位置信息 //表示图形距离左侧的距离 private int left; //表示图形距离上边界的距离 private int top; public void move. Left(){ left--; } public void move. Right(){ left++; } public void move. Down(){ top++; } public void rotate(){ //显示下一个状态,但得保证状态值不超过4,所以需处理 status=(status+1)%body. length; }
Shape类中增加图形的绘制方法 public void draw. Me(Graphics g){ System. out. println("shape's drawme"); g. set. Color(Color. RED); //循环访问代表方正的数组 for(int x=0; x<4; x++){ for(int y=0; y<4; y++){ if(get. Flag. By. Point(x, y)){ g. fill 3 DRect((left+x)*Global. CELL_SIZE, (top+y)*Global. CELL_SIZE, true); } } //获取方正中标志是 1还是 0,1表示要绘图 0表示不绘图 private boolean get. Flag. By. Point(int x, int y){ return body[status][y*4+x]==1; }
游戏常量的存放——Glaobal public class Global { //表示每个方格的像素值 public static final int CELL_SIZE=20; //表示图形面板有多少个格子宽和高 public static final int WIDTH=15; public static final int HEIGHT=15; }
图形如何避免移出边界 public boolean is. Moveable(Shape shape, int action){ //得到图像当前的位置信息 int left=shape. get. Left(); int top=shape. get. Top(); //根据图形的动作,得出图形最新 的位置信息 switch(action){ case Shape. LEFT: left--; break; case Shape. RIGHT: left++; break; case Shape. DOWN: top++; break; } //依次取出图形中的每一个点,判断 是否超出显示区域 for(int x=0; x<4; x++){ for(int y=0; y<4; y++){ //判断xy点是否是图形的成员 if(shape. is. Member(x, y, action==Shape. ROTATE)){ if(top+y>=Global. HEIGHT|| left+x<0|| left+x>=Global. WIDTH) return false; } } } return true; }
- Slides: 25