Distance between randomly spawned objects

I’m a beginner who just started out learning js and p5. I started working on making my own game from scratch, it’s actually a replica of the chrome dino game, I managed to do everything I wanted to and I’m almost finished, but… its sort of unplayable, some times. The enemies that come from the right are spawned randomly, and sometimes they spawn kinda close, making it very difficult jumping over them without losing. And I don’t know how to make them spawn at a certain distance between them.
Please let me know if I need to provide something else, i’m a newbie :slight_smile: my code from the main js file is below:

let d;
let bg;
let imgDino;
let imgEnemy;
let enemies = [];
let song;
let hitSound;
let s = 0;
let jSound;
let scoreSound;
let myint;
let gameover = false;
function preload() {
	bg = loadImage("resources2/backgrnd.jpg");
	imgDino = loadImage("resources2/dino.png");
	imgEnemy = loadImage("resources2/snakecrop.png");
	hitSound = loadSound("resources2/hit.wav");
	jSound = loadSound("resources2/jump.wav");
	scoreSound = loadSound("resources2/sound.mp3");

}
function setup() {
    createCanvas(1400, 400);
    d = new Dino();
    jSound.setVolume(0.5);
    myint = setInterval(score, 100);
}

function keyPressed() {
	if(key == ' ') {
		d.jump();
	}
	if(keyCode == DOWN_ARROW) {
		d.duck();
	}
	if(keyCode == 82) {
		location.reload();
	}
}


function draw() {
	background(bg);
		d.show();
	    d.move();
	    for(let e of enemies) {
		if(d.hits(e)) {
        e.endgame();
	}
		e.move();
		e.show();
	}
	//spawning an enemy
	if(random(1) < 0.005) {
		enemies.push(new Enemy());
	  }

        if(gameover == true) {
	    hitSound.play();
	    noLoop();
	    clearInterval(myint);
        loadGameOverInterface();

        }
        scoreDisplay();
}


function score() {
	s += 1;
	if(s == 200) {
		scoreSound.play();
	}
		if(s == 400) {
		scoreSound.play();
	}
		if(s == 500) {
		scoreSound.play();
	}
		if(s == 600) {
		scoreSound.play();
	}
		if(s == 700) {
		scoreSound.play();
	}
		if(s == 800) {
		scoreSound.play();
	}
		if(s == 1000) {
		scoreSound.play();
	}
}
2 Likes

Can you describe the rule that you want?

Is it like:

If there is no enemy closer than x pixels from the edge of the screen, check to sometimes spawn a new enemy

Or

If it has been at least x frames / seconds since the last enemy was spawned, check to sometimes spawn a new enemy

Or…?

Something like that. An enemy spawns at the beginning of the game and the rest need to be spawned randomly, but the first one must not be closer than x pixels from the right edge of the canvas.
I actually did something like this, but it was like: an enemy comes, i jump, another one comes, i jump, it wasn’t very challenging, although it was playable.
The second option sounds good too, for ex: don’t spawn another enemy if the previous one hasn’t been on the screen for 1 second

1 Like

Does that approach resolve your issue? Are you able to to add this kind of logic around here?

if(random(1) < 0.005) {

I don’t really know how… That if statement in my code spawns them randomly, sometimes I’m lucky and they spawn at good distance between each other, but sometimes, since it’s completely random, they spawn even 3 or 4 near each other.

Exactly. So add an extra if clause that checks the enemy location.

I forgot to respond :))
I fixed it, thanks for the help. I managed to do something else, generating a minimum and maximum gap between the spawned enemy.

1 Like