# Java, spawn algorithm that gets harder each level

I am currently in the process of making my 3rd Java game (everything I have learned is from YouTube videos, so I may be doing some things wrong) named "Blocked". But I have run into a problem: how do I make an algorithm that gets progressively harder each "level". but first let me briefly explain the game, so you are a small human and blocks are falling from the sky, and you try to avoid them, there are 5 types of blocks: Basic, fast, shop, killer, safe. So I want my game to start with very few (basic only) tiles to fall, while the game is going (infinitely) I want it to get harder but not just unlocking new blocks, but also changing the frequency the more difficult blocks spawn (up to a certain threshold of course).

P.s. pseudo code is fine by me. P.s. (2) if you need any clarifications ask me!

• Hmm, so you want to spawn varieties of blocks and increase the frequency as the level goes up right? What frequency (block spawn rate or harder block spawn chance)? What have you tried? – Greffin28 May 20 '16 at 4:29
• Can you show us the code of how you implemented the block selection for the first level? – Thomas May 20 '16 at 7:49

A first aproach could be giving a varying weight to each type of block, modifying such weight each level, decressing the probability of spawning an easy blocks and increasing the probability to spawn a harder ones.

I'm also going to suppose you have some kind of a parent Block class and different child classes for each type of block.

public Block createBlock(int level) {
double wBasic = 1.0/(1*level);
double wFast = 0.5*level;
double wShop = 0.1*level;
double wKiller = 0.05*level;
double wSafe = 1.0/(2*level);

double psum = wBasic + wFast + wShop + wKiller + wSafe;

double wBasicNorm = wBasic / psum;
double wFastNorm = wFast / psum;
double wShopNorm = wShop / psum;
double wKillerNorm = wKiller / psum;
double wSafeNorm = wSafe / psum;

double rangeBasic = wBasicNorm;
double rangeFast = rangeBasic + wFastNorm;
double rangeShop = rangeFast + wShopNorm;
double rangeKiller = rangeShop + wKillerNorm;

Random rgen = new Random();
double r = rgen.nextDouble();

if (r < rangeBasic) {
return new BasicBlock();
} else if (r < rangeFast ) {
return new FastBlock();
} else if (r < rangeShop) {
return new ShopBlock();
} else if (r < rangeKiller) {
return new KillerBlock();
} else {
return new SafeBlock();
}
}


With this we'll create a different probability for each block to spawn, and then normalize that probability giving it a value between 0 and 1 (wich is the range of the numbers returned by the random number generator).

Then we want to know the different "ranges" of probability of each block.The random number generated will fall in one of those ranges, and this is what will set the type of block created. This can be seen as placing the probabilities of each block in a line like this:

     basic    fast        shop         killer       safe
0.0|-------|---------|------------|--------------|--------|1.0
^
|
random numer


PS: This is my first answer ever, hope it helps.

• that actually really helps :D – Feconiz May 20 '16 at 16:00

Let's assume you have float gameTime;

Let's also assume you spawn in the blocks completley randomly.

And finally let's assume you have some kind of update method which gets called every frame.

With the above assumptions you could probably write something like this

float gameTime = 0; //The total game time, in seconds.
float spawnTime = 0; //A counter to keep track of the next spawn

public void update(){
gameTime += Time.deltaTime; //increment the gameTime by the game delta
spawnTime += Time.deltaTime;

//The game difficulty must have some kind of limit or it will no longer be fun
//after you go over a certain time. With this example the spawn time will vary from
//once per 1.6 seconds down to once every .16 seconds. You can obviously change the values
//to make the game harder or easier
float difficulty = 60f; // Higher is harder
float minDifficulty = 10; //Low means the game can get harder
float maxDifficulty = 100; //Higher means the game starts off easier
float spawnLimit = clamp(maxDifficulty-gameTime, minDifficulty, maxDifficulty) / difficulty;

if(spawnTime > spawnLimit){
spawnBlock();
spawnTime = 0;
}

}

public void spawnBlock(){
//...code for spawning certain blocks based on how much time has passed ...//
}

• thats the problem i dont want it to be so random, but thanks for your answer, my question was actually answered by Falkian! – Feconiz May 20 '16 at 16:00
• When I say random I mean the position and type of block spawned. This is basically Falkian's answer but configurable and more simple. – Shaun Wild May 23 '16 at 11:41