Hi, I was wondering if someone could point me in the right direction.
I’m making an interactive snake game and I’d like to have a ‘click to start’ screen at the beginning. This is my sketch.js:
var screen = 0;
var x;
var y;
var speed = 1;
let bg;
let img;
let img2;
let img3;
let snake;
let rez = 40;
let food;
let w;
let h;
function preload() {
img = loadImage('spaceinvaders.png');
img2= loadImage('cdfile.png');
img3 = loadImage('10.jpg');
}
function setup() {
bg = loadImage('chrome1.png');
createCanvas(800, 800);
w = floor(width / rez);
h = floor(height / rez);
frameRate(7);
snake = new Snake();
foodLocation();
createButton("START");
createButton("RESET");
}
function foodLocation() {
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
snake.setDir(-1, 0);
} else if (keyCode === RIGHT_ARROW) {
snake.setDir(1, 0);
} else if (keyCode === DOWN_ARROW) {
snake.setDir(0, 1);
} else if (keyCode === UP_ARROW) {
snake.setDir(0, -1);
} else if (key == ' ') {
snake.grow();
}
}
function draw() {
scale(rez);
background(bg);
//background(0,255,255);
if (snake.eat(food)) {
foodLocation();
}
snake.update();
snake.show();
if (snake.endGame()) {
print("END GAME");
background(0);
noLoop();
}
noStroke();
fill(255, 0, 0);
image(img2, food.x, food.y, 1, 1);
}
The createbuttons don’t have functions yet so please ignore. I know this code might be a mess, I’m on a tight deadline.
I found this bit of code below which seemed to the job on a simple sketch, however doesn’t work on mine - I’m wondering if two if statements can run together?
if (screen == 0) {
background(96, 157, 255)
fill(255)
textAlign(CENTER);
text('WELCOME TO MY BUTTON GAME', width / 2, height / 2)
text('click to start', width / 2, height / 2 + 20);
} else if (screen == 1) {
x = random(0, width)
y = random(0, height)
frameRate(speed);
background(220);
I understand vaguely that it doesn’t work because the code has preloaded functions and the game is set to run in draw from the start.
Any advice in the right direction greatly appreciated !!