Need help coding a game


#1

Hi, I am trying to code a game and I need my character to move with my arrow keys. I put the code in but it doesnt seem to be working. I’m grateful for any help

this is what I have so far:(the character is the ellipse)

var player;
var playerX = 40;
var playerY = 40;
var screen = “menu”;
function setup () {
createCanvas(600, 450);
}

function menu() {
if (keyIsDown(73)) { //press i
screen = “instructions”;
}
if (keyIsDown(80)) { //press p
screen = “playScreen”
}

// DRAW MENU
background(65,105,225);
textSize(45);
textFont(“Syncopate”);
text(“Spaceship Run 3000”, 75, 70);
textSize(25);
text(“Press i for the instructions screen”, 100, 140);
text(“Press p to go to play game”, 130, 200);
}

function instructions() {
if (keyIsDown(27)) { //press escape
screen = “menu”;
}
//INSTRUCTION SCREEN
background(0, 0, 200);
textSize(70);
textFont(“Syncopate”);
text(“Instructions”, 100, 100);
textSize(25);
text(“Use left and right arrow keys to move side to side.”, 20, 160);
text(“The longer you last, the higher your score will be!”, 20, 220);
text(“Press escape to go back”, 150, 280);
}

function playScreen() {
if(keyIsDown(77)) { //press m
screen = “menu”;
}
if(keyIsDown(32)) { //press space
screen = “game”
}
background(0, 0, 200);
textFont(“Syncopate”);
text(“Press m for menu”, 120, 30);
var score = 0;
text("Score: " + score, 10, 30);
var playerHealth = 100;
text("Player Health: " + playerHealth, 340, 30);
line(0, 38, 600, 38);

keyPressed = function () {
if (keyIsDown === 38) { // Pressing up
playerY = playerY + 3;
} else if (keyIsDown === 40) { // down
playerY = playerY + 3;
} else if (keyIsDown === 37) { // left
playerX = playerX - 3;
} else if (keyIsDown === 39) { // right
playerX = playerX + 3;
}
}

ellipse(playerX, playerY, 100, 100);
}

function draw() {
if (screen === “menu”) {
menu();
} else if (screen === “instructions”) {
instructions();
} else if (screen === “playScreen”) {
playScreen();
}
}


#2

Is there a certain error or issue you’re seeing? The more specific you can be, the easier and faster it will be to help you out :slight_smile: