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 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();
}
}