【问题标题】:How to gradually increase the rate of something?如何逐步提高某事的速率?
【发布时间】:2014-01-04 18:50:10
【问题描述】:

我正在创建一个 2d 僵尸射击游戏,并且我正在尝试想一个好方法来逐渐提高僵尸的创建率。

我用下面的代码创建了一个僵尸。

    public void createZombies(){

    int direction = new Random().nextInt(4);

    if (direction == 0) {
        // spawn from top
        zombies.add(new Zombie(new Random().nextInt(1120), new Random()
                .nextInt(1)));
    }
    if (direction == 1) {
        // spawn from left
        zombies.add(new Zombie(new Random().nextInt(1), new Random()
                .nextInt(640)));
    }
    if (direction == 2) {
        // spawn from bottom
        zombies.add(new Zombie(new Random().nextInt(1120), 640));
    }
    if (direction == 3) {
        // spawn from right
        zombies.add(new Zombie(1120, new Random().nextInt(640)));
    }


}

我基本上想从我的主要方法(连续运行)中调用该方法。我想也许可以使用模块化并做类似的事情:

    int x = 1;
    if(x  % 1000 == 0){
        createZombies();
    }

    x++;

但这看起来很混乱 - 它不会改变它们的创建频率。

我只是有点难以找到一个好的方法来做到这一点 - 令人惊讶的是,我在这里也找不到任何有用的东西。 因此,如果有人能指出这样做的好主意,将不胜感激!

【问题讨论】:

  • 你的僵尸目前的汇率是多少,我不明白?
  • 您的游戏是否会记分或记录玩家玩了多长时间?

标签: java loops sprite increment


【解决方案1】:

Guava 有一个RateLimiter,它可能对您的用例有用。特别是,您可以执行以下操作:

//initially, create one zombie every 10 seconds
final RateLimiter zombieRate = RateLimiter.create(0.1);
Runnable increaseRate = new Runnable() {
    @Override public void run() {
        //increase rate by 20%
        zombieRate.setRate(zombieRate.getRate() * 1.2);
    }
};

//then increase the rate every minute
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(increaseRate, 1, 1, TimeUnit.MINUTES);

然后你的僵尸创造变成:

while (true) {
    zombieRate.acquire();
    createZombie();
}

【讨论】:

    【解决方案2】:

    您可以简单地减少每次创建僵尸之间的时间间隔:

        int x = 1;
        int tau = 1000;
    
        if(x  % tau == 0){
            createZombies();
        }
    
        x++;
        tau = tau > 0 ? --tau : 1;
    

    【讨论】:

      【解决方案3】:

      您必须根据经过的时间定义僵尸创建的“速度”。

      double velocity=0.5; //every 2ms 1 zomby
      long latestCreation = System.currentTimeMillis();
      double rest = 0;
      
      public synchronized void createZombies() {
         double number=velocity * (System.currentTimeMillis() - latestCreation) + rest;
         latestCreation = System.currentTimeMillis()
      
         int n = Math.round(number);
         rest = number - n; //n° of partial zomby
         for (int i=0; i<n; i++) createZomby();
      }
      

      从您的线程或当您更喜欢调用 createZombies() 时。

      不幸的是,您无法知道线程何时真正执行,那么您必须定义一个函数时间相关。当数字将返回一些小数时,var "rest" 是一种优化。

      【讨论】:

        【解决方案4】:

        也许你可以这样做:

               float maxZombieRate = 0.8; //for example
               float zombieRate = 0.05;
               while(zombieRate<=maxZombieRate){ //you could have a timer too
                    if(Math.random <= zombieRate){ //Math.random returns values between 0 and 1
                        createZombies(); //
                        zombieRate+=0.05; //increase in 5% the probability of run createZombies()
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-23
          • 1970-01-01
          • 1970-01-01
          • 2022-10-18
          • 1970-01-01
          • 2019-12-20
          • 1970-01-01
          相关资源
          最近更新 更多