请问,下面的程序如何实现暂停与重新开始游戏还有双缓冲技术 !
目前暂停无法实现立即暂停,而且暂停后线程无法重新开启。
请各位帮帮忙。
------解决方案--------------------
额 太多了就不想看了。
不过我自己做俄罗斯方块的时候是在线程中run方法里面的while里面加一个 if(boolean变量)(静态的) ,当false就不执行,重新开始的话把数组初始化下就好,这是我的思路哈 ,希望对你有帮助哈!
------解决方案--------------------
- Java code
import game.Game;import game.Global;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.util.Random;import cn.itcast.tetris.listener.ShapeListener;import cn.itcast.tetris.view.GamePanel;public class Shape {public Game game; public GamePanel gamePanel; public static final int ROTATE =0; public static final int LEFT =1; public static final int RIGHT =2; public static final int DOWN =3; private int[][] body; private int status; private int left; private int top; Image t ;private ShapeListener listener;public Thread MyThread = null; public void moveLeft(){left--; } public void moveRight(){left++; } public void moveDown(){top++; } public void rotate(){status = (status +1) % body.length; } public void drawMe(Graphics g){ g.setColor(Color.white);for (int x =0;x<4;x++) {for(int y =0; y<4;y++){if (getFlagByPoint(x,y)){g.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 body[status][y*4+x]==1; } public boolean isMember(int x,int y,boolean rotate){int tempStatus =status;if(rotate){tempStatus =(status +1) % body.length;}return body[tempStatus][y * 4 + x]==1; } public class ShapeDriver implements Runnable{int x =450; public void run() {while(listener.isShapeMoveDownable(Shape.this)){moveDown();listener.shapeMoveDown(Shape.this);try {if(x>200){Thread.sleep(x-=1);}elseThread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} } public Shape(){MyThread = new Thread(new ShapeDriver());MyThread.start();}public void Shapestop(){ Thread.yield(); } public void addShapeListener(ShapeListener l){if(l!=null)this.listener=l; } public void setBody(int body[][]){this.body=body; } public void setStatus(int status){this.status=status; } public int getTop(){return top; } public int getLeft(){return left; }}