xxxxxxxxxx
55
/*******************
Zenek Chapman
Computer Science 30
Jan 6th 2024
this code creates a small coin scavenger hunt game. it is quite rudimentary
************************************/
let coins = []; //create array for coin objects
let ship // create variable for background imge\
var gameStart
function preload(){ //preload the image that will be used
ship = loadImage('beach pirate2.jpg')
}
function setup() {
createCanvas(ship.width*2, ship.height*2); //create canvas the size of the ship image times 2
let xpositions = [width/10, width/4, width/2, width/1.5, width/1.4]; //create arrays for the set positions of the coins
let ypositions = [height* 0.33, height*0.23, height*0.2, height*0.15, height*0.1];
for (let i = 0; i < 5; i++){ //create 5 coins and set their repective x and y values
x = xpositions[i]
y = ypositions[i]
coins[i] = new Coin(x, y, 10)
}
}
function draw() {
if (gameStart == true){image(ship, 0, 0, ship.width*2, ship.height*2); //draw the image as the backgound
for (let i = 0; i < coins.length; i++){ //display coins
coins[i].show()
}
}
if (coins.length == 0){ // congrats for when all coins are found
fill(0)
textAlign(CENTER)
textSize(width/20)
text( "Congrats!! You have found all of the coins!!", width/2, height*0.9)
}
}
function mousePressed() { //splicfe coins when they are clicked on
for (let i = coins.length - 1; i >= 0; i--) {
if (coins[i].clickedOn(mouseX, mouseY)) {
coins.splice(i, 1)
}
}
}
function keyPressed(){
gameStart = true
}