移动一个球一个接一个 [英] move a ball one after another

查看:174
本文介绍了移动一个球一个接一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写游戏豆芽机(高尔顿盒)的code,其作用机制,一个球落地,直到得到它的插槽(随机)中的一个移动随机,然后又球停在做同样的事情,等等,问题是,所有的球落在同一时间,这件事情我不想,我想每个球时,当前完成其travel..can任何人可以帮助我倒下!谢谢..

这里是code:

 进口java.awt中的*。
java.awt.event中导入*。
进口的java.util.ArrayList;
进口java.util.Collection中;
进口了java.util.Random;
进口java.util.Scanner中;
进口java.lang.Iterable;进口的javax.swing *。
公共类BallPanel继承JPanel {INT X = 1,Y = 1;
收集和LT;球>球;
公共静态无效的主要(字串[] args){
      //创建列表
    SwingUtilities.invokeLater(Runnable的新(){
        公共无效的run(){
    JFrame的帧=新的JFrame(豆游戏);
    frame.add(新BallPanel());
    frame.setSize(300,500);
    frame.setLocationRelativeTo(NULL);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(真);
        }
    });
}公共BallPanel(){    的JPanel面板=新JPanel();
    球=新的ArrayList<>();
    的for(int i = 1; I< = 4;我++){
        ball.add(新球(X,Y));
        X ++;
        ÿ++;
    }
     定时器定时器=新定时器(400,新的ActionListener()
    {
        公共无效的actionPerformed(ActionEvent的五){
            对于(球B:球){
                  b.move();重画();
            }        }
    });
      timer.start();
}保护无效paintComponent(图形G){
    super.paintComponent方法(G);
    INT RADUIS = 10;
    INT I = 0;
    INT宽度=(的getWidth()/ 8);
    INT空间=(的getWidth() - 8 * 30)/ 2;
    g.setColor(Color.BLACK);
    对于(I = 0; I&下; = 8;我++){
        g.drawLine(空格+(30 * i)中,的getHeight() - 5 *(RADUIS),空格+(30 * i)中,的getHeight());
        如果(i = 0&安培;!&安培;!I = 8)
            g.fillOval(空格+(30 *ⅰ)-5,的getHeight() - 5 *(RADUIS)-5,10,10);
        如果(我== 0){
            g.drawLine(空间的getHeight() - 5 *(RADUIS),空间+((8)* 15) - (RADUIS / 2)-15,的getHeight() - 15(5 * RADUIS) - (30 *(8 -2)) - RADUIS / 2);
            g.drawLine(空格+((8)* 15) - (RADUIS / 2)-15,的getHeight() - 15(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2,空间+(( 8)* 15) - (RADUIS / 2)-15,的getHeight()/ 2-8 * 8); //垂直线        }
        如果(我== 8){
            g.drawLine(空格+(30 * I)的getHeight() - 5 *(RADUIS),空间+((8)* 15)+(RADUIS / 2)+ 15,的getHeight() - 15(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2);
            g.drawLine(空格+((8)* 15)+(RADUIS / 2)+ 15,的getHeight() - 15-(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2,空格+(( 8)* 15)+(RADUIS / 2)+ 15,的getHeight()/ 2-8 * 8); //垂直线        }    }
    INT O = 1;
    对于(i = 1; I< = 8-2;我++){
        INT K = 2;
        对于(INT J =; J< = 8-2; J ++){
            g.fillOval(空格+(第(i + k)的* 15)-raduis / 2,的getHeight() - (5 * RADUIS) - (30 * O)-raduis / 2,RADUIS,RADUIS);
            K = K + 2;
         }Ø++;
    }
    对于(球B:球){
         b.draw(G);
    }
}
公共类球{
    随机兰特=新的随机();
    私人诠释N;
    私人INT RADUIS = 10;
    私人INT moveRaduis = 12;
    私人INT L = 0;
    私人INT R = 0;
    私人INT D = 0;
    INT X,Y;    公共球(INT X,int y)对{
        this.x = X;
        this.y = Y;
    }    公共无效抽奖(图形G){
        g.setColor(Color.RED);
        g.fillOval(的getWidth()/ 2 -raduis / 2 + R + L,0 + D moveRaduis,moveRaduis);    }
    公共无效移动(){
        n = 1的+ rand.nextInt(100);
        如果(N< = 51){
            D + = 15;            如果(D< =(的getHeight() - 15(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2))
                L = 0;
            否则如果(d取代;的getHeight() - 15-(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2及和D&下;的getHeight() - 5 *(RADUIS))
            {
                D + = 15;
                L- = 15;            }
            否则,如果(D> =的getHeight() - 5 *(RADUIS))
            {
                如果(D == 31 * 15)D- = 15;
                D = D组;            }        }
        否则,如果(N> = 51安培;&安培; N< = 100){
            D + = 15;
            如果(D&下; =的getHeight() - 15-(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2)
                R = 0;
            否则如果(d取代;的getHeight() - 15-(5 * RADUIS) - (30 *(8-2)) - RADUIS / 2及和D&下;的getHeight() - 5 *(RADUIS))
                {
                D + = 15;
                R + = 15;
            }
            否则,如果(D> =的getHeight() - 5 *(RADUIS)){
                如果(D == 31 * 15)D- = 15;
                D = D组;            }        }    }
}
}


解决方案

所以,你的运动code全押在每次迭代,你需要做的是有一个活动的球,比如什么样的球,..

 私人球activeBall;定时器定时器=新定时器(400,新的ActionListener(){
    公共无效的actionPerformed(ActionEvent的五){
        如果(activeBall =空&放大器;!&放大器; activeBall.hasCompleted()){
            activeBall = NULL;
        }
        如果(activeBall == NULL&放大器;&放大器; ball.size()大于0){
            activeBall = ball.remove(0);
        }
        如果(activeBall!= NULL){
            activeBall.move();
            重绘();
        }
    }
});

您将需要某种检查添加判断球已经完成了它的运行,虽然

I am writing a code of game bean machine(Galton box), its mechanism that one ball fall and move randomly until it get to one of the slots (randomly) and then another ball comes doing the same thing and so on, the problem is that all balls fall at the same time which something I do not want, I want each ball to fall when the current one finishes its travel..can anyone helps me ! Thanks..

here is the code:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Scanner;
import java.lang.Iterable;

import javax.swing.*;


public class BallPanel extends JPanel  {

int x=1, y=1;
Collection <Ball> ball;
public static void main(String [] args){
      // create the list
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
    JFrame frame= new JFrame("Bean game");
    frame.add(new BallPanel());
    frame.setSize(300,500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
        }
    });    
}

public BallPanel() {

    JPanel panel = new JPanel();
    ball=new ArrayList<>(); 
    for(int i=1;i<=4;i++){
        ball.add(new Ball(x,y));
        x++;
        y++;
    }
     Timer timer = new Timer(400, new ActionListener()
    { 
        public void actionPerformed(ActionEvent e) {
            for (Ball b: ball) {
                  b.move();repaint();
            }

        }
    });
      timer.start();
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int raduis=10;
    int  i=0;
    int width=(getWidth()/8);
    int space=(getWidth()-8*30)/2;
    g.setColor(Color.BLACK);
    for(i=0;i<=8;i++){
        g.drawLine(space+(30*i),getHeight()-5*(raduis) ,space+(30*i) ,getHeight() );
        if(i!=0 && i!=8)
            g.fillOval(space+(30*i)-5, getHeight()-5*(raduis)-5, 10, 10);
        if(i==0){
            g.drawLine(space,getHeight()-5*(raduis) , space+((8)*15)-(raduis/2)-15 , getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2);
            g.drawLine(space+((8)*15)-(raduis/2)-15 , getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2 , space+((8)*15)-(raduis/2)-15 , getHeight()/2-8*8);//vertical line

        }
        if(i==8){
            g.drawLine(space+(30*i), getHeight()-5*(raduis), space+((8)*15)+(raduis/2)+15, getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2);
            g.drawLine(space+((8)*15)+(raduis/2)+15 , getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2 , space+((8)*15)+(raduis/2)+15 , getHeight()/2-8*8);//vertical line

        }

    }


    int o=1;
    for(i=1;i<=8-2;i++){
        int k=2;
        for(int j=i;j<=8-2;j++){
            g.fillOval(space+((i+k)*15)-raduis/2, getHeight()-(5*raduis)-(30*o)-raduis/2, raduis, raduis); 
            k=k+2;   
         }o++;
    }
    for (Ball b : ball) {
         b.draw(g);
    }
}
public class Ball {
    Random rand=new Random();
    private int n;
    private int raduis = 10;
    private int moveRaduis=12;
    private int L = 0;
    private int R = 0;
    private int D = 0;
    int x, y; 

    public Ball(int x, int y){
        this.x=x;
        this.y=y;
    }  

    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(getWidth()/2 -raduis/2  + R + L, 0 + D , moveRaduis, moveRaduis);

    }
    public void move() {
        n=1+rand.nextInt(100);
        if(n<=51){
            D+=15;

            if(D<=(getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2))
                L=0;
            else if(D>getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2 && D<getHeight()-5*(raduis))
            {
                D+=15;
                L-=15;

            }
            else if(D>=getHeight()-5*(raduis))
            {
                if(D==31*15)D-=15;
                D=D;

            }

        }
        else if (n>=51 && n<=100){
            D+=15;
            if(D<=getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2)
                R=0;
            else if(D>getHeight()-15-(5*raduis)-(30*(8-2))-raduis/2 && D<getHeight()-5*(raduis))
                {
                D+=15;
                R+=15;
            }
            else if(D>=getHeight()-5*(raduis)){
                if(D==31*15)D-=15;
                D=D;

            }

        }

    }
}
}

解决方案

So, your movement code moves all the balls on each iteration, what you need to do is have an "active" ball, for example...

private Ball activeBall;

Timer timer = new Timer(400, new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
        if (activeBall != null && activeBall.hasCompleted()) {
            activeBall = null;
        }
        if (activeBall == null && ball.size() > 0) {
            activeBall = ball.remove(0);
        }
        if (activeBall != null) {
            activeBall.move();
            repaint();
        }
    }
});

You'll need to add in some kind of check to determine the ball has completed it's run though

这篇关于移动一个球一个接一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆