使用计时器的随机落物

时间:2015-01-01 03:22:12

标签: java random timer

我正在尝试建立一个游戏(板块马戏团),我需要创建随机形状下降,玩家应该抓住它们以增加他们的分数。基本上,我没有随机创建对象的问题,我只需要它们就可以了。

我使用对象池设计模式创建一个包含已创建对象的池,使用方法borrowShape()从池中获取一个对象。

在下面的代码中,我尝试使用time.schedule依次制作顺序落下的对象。问题是我无法让它们随机掉落。我想也许问题在于计时器,因为它们没有完全掉落 - 它们停在框架上的任何一点,然后其他物体开始下降,它们也不会完全掉落。

 public class view extends JFrame {
    private static final long serialVersionUID = 1L;
    public  int x1 ;
    public  int y1 ;
    public  int x2 ;
    public  int y2 ;
    public  int Xframe ;
    public  int Yframe ;
//  Keyboard c ;
    private Image bfimage ;
    private Graphics bfg ;
    Image clown1 ;
    Image clown2 ;
    private Queue<Shapes> queue_1 = new LinkedList<Shapes>();
    private Queue<Shapes> queue_2 =  new LinkedList<Shapes>();
    private Queue<Shapes> queue_3 = new LinkedList<Shapes>();
    private Queue<Shapes> queue_4 = new LinkedList<Shapes>();
    private LinkedList<Shapes> onScreen = new LinkedList<Shapes>();
    Shapes shape_1 = new BlueMushroom();
    Shapes shape_2 = new RedMushroom();
    Shapes shape_3 = new GreenMushroom();
    Shapes shape_4 = new RedMushroom();

    ShapesPool pool;
    FirstState frstate;
    public view () throws InterruptedException{
        ImageIcon i1 = new ImageIcon("clown111.gif");
        clown1= i1.getImage() ;
        ImageIcon i2 = new ImageIcon("clown22.gif");
        clown2= i2.getImage() ;
        // create pool
        onScreen.add(new BlueMushroom());



        Xframe= 1370 ;
        Yframe = 700 ;
        setTitle("game");
        setSize(Xframe , Yframe);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
        x1 = 0 ;
        y1 = Yframe - 240 ;
        x2 = 650 ;
        y2 = Yframe - 240 ;
        controller k = new controller(this , x1 , y1 , x2 , y2 , Xframe , Yframe) ;
        this.addKeyListener(k);
        Timer time = new Timer();
         pool = ShapesPool.getInstance();
         frstate = new FirstState();
        Shapes shape = new BlueMushroom();
        for(int i = 0 ; i < 5 ; i++){
            shape = pool.borrowShape();
            shape.setCurrentX(randomX());
            shape.setCurrentY(10);
            shape.setState(frstate);
            queue_1.add(shape);
        }
        for(int i = 0 ; i < 7 ; i++){
            shape = pool.borrowShape();
            shape.setCurrentX(randomX());
            shape.setCurrentY(10);
            shape.setState(frstate);
            queue_2.add(shape);
        }
        for(int i = 0 ; i < 9 ; i++){
            shape = pool.borrowShape();
            shape.setCurrentX(randomX());
            shape.setCurrentY(10);
            shape.setState(frstate);
            queue_3.add(shape);
        }
        for(int i = 0 ; i < 11 ; i++){
            shape = pool.borrowShape();
            shape.setCurrentX(randomX());
            shape.setCurrentY(10);
            shape.setState(frstate);
            queue_4.add(shape);
        }

        time.schedule(new RunShape(),2*1000, 2*1000);
        time.wait();

    }

int ypos1=0;
int ypos2=0;
int ypos3=0;
int ypos4=0;

    Random rand = new Random();
    public int randomX(){
        return rand.nextInt(1700);
    }

    class RunShape extends TimerTask{
        @Override
        public void run(){
            System.out.println("run");
            Shapes shape = new BlueMushroom();
            if(!queue_1.isEmpty()){
                System.out.println("awl saaf ");

                shape_1 = queue_1.poll();
                addObject(shape_1);
                shape = pool.borrowShape();
                shape.setCurrentX(randomX());
                shape.setCurrentY(10);
                shape.setState(frstate);
                queue_1.add(shape);
            }
            if(!queue_2.isEmpty()){

                shape_2 = queue_2.poll();
                addObject(shape_2);
                shape = pool.borrowShape();
                shape.setCurrentX(randomX());
                shape.setCurrentY(10);
                shape.setState(frstate);
                queue_2.add(shape);
            }
            if(!queue_3.isEmpty()){

                shape_3 = queue_3.poll();
                addObject(shape_3);
                shape = pool.borrowShape();
                shape.setCurrentX(randomX());
                shape.setCurrentY(10);
                shape.setState(frstate);
                queue_3.add(shape);
            }
            if(!queue_4.isEmpty()){

                shape_4 = queue_4.poll();
                addObject(shape_4);
                shape = pool.borrowShape();
                shape.setCurrentX(randomX());
                shape.setCurrentY(10);
                shape.setState(frstate);
                queue_4.add(shape);
            }
            Timer time = new Timer();
            time.schedule(new TimerTask(){
                public void run(){
                    while(ypos1 <= 1600){
                        ypos1++;
                        shape_1.setCurrentY(ypos1);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        ypos2++;
                        shape_2.setCurrentY(ypos2);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        ypos3++;
                        shape_3.setCurrentY(ypos3);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        ypos4++;
                        shape_4.setCurrentY(ypos4);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        repaint();
                    }
                    if(ypos1 == 1600 ){ ypos1 = 0;
                    shape_1.setCurrentY(ypos1);
                    }
                    if(ypos2 == 1600 ){ ypos2 = 0;
                    shape_2.setCurrentY(ypos2);
                    }
                    if(ypos3 == 1600 ){ ypos3 = 0;
                    shape_3.setCurrentY(ypos3);
                    }
                    if(ypos4 == 1600 ){ ypos4 = 0;
                    shape_4.setCurrentY(ypos4);
                    }

            }
            },1*1000 ,1*1000);
            try {
                time.wait(1*1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
    }


    public void addObject(Shapes shape){
        onScreen.add(shape);
        repaint();
    }
    public void paint(Graphics g) {
        bfimage = createImage(getWidth(), getHeight()) ;
        bfg = bfimage.getGraphics() ;
        paintcomponent(bfg) ;
        g.drawImage(bfimage, 0, 0, this) ;

    }

    public void paintcomponent(Graphics g){ 
        g.drawImage(clown1, x1, y1, this) ;
        g.drawImage(clown2, x2, y2, this) ;
        for(Shapes shape : onScreen){
                g.drawImage(shape.getMushroom(), shape.getCurrentX(), shape.getCurrentY(), this);   
        }

        repaint() ;
    }   

}

0 个答案:

没有答案