xxxxxxxxxx
325
/*
ARTG 2260
Professor Mark Sivak
Project 2
Tanya Kler
UFO Game:
Abduct the cows using the ufo and avoid the goats. Earn extra points by feeding the goats stars.
- move mouse to move ufo
- click mouse to turn abduction light on
- type s key to launch stars
- use left and right arrow keys to switch sky modes
*/
// Sky color variables
var blSky;
var dusk;
var sunset;
var skies;
var skyNum;
var sky;
// animal variables
var cow; //image
var goat; //image
let animals = [];
let easy = 5;
let hard = 20;
//stars
let stars = [];
let score = 0;
//button colors
var button;
var hover;
// images
var ufo;
var ufo1;
var ufo2;
// game variables
var easyMode;
var hardMode;
let stage = 1;
let timer = 60;
//preload images to initialize image variables
function preload() {
ufo1 = loadImage("ufo.png");
ufo2 = loadImage("ufolight.png");
cow = loadImage("cow.png");
goat = loadImage("goat.png");
}
//set up canvas and initialize variables
function setup() {
createCanvas(800, 800);
//sky initializations
blSky = color(84, 232, 255);
dusk = color(36, 20, 219);
sunset = color(255, 106, 0);
skies = [];
skies = [blSky, dusk, sunset];
skyNum = 0;
button = color(245, 66, 188);
hover = color(236, 207, 255);
//set initial ufo to ufo without light
ufo = ufo1;
easyMode = false;
hardMode = false;
//creates list of animals
for (let i = 0; i < 4; i++) {
if (i % 2 === 0) { // adds a cow on even indices
animals[i] = new Animal(200 * (i + 1), easy, true, i + 1);
} else { // adds a goat on odd indices
animals[i] = new Animal(200 * (i + 1), easy, false, i + 1);
}
}
}
//draw function to display game
function draw() {
imageMode(CENTER);
rectMode(CENTER);
textAlign(CENTER);
sky = skies[skyNum];
background(sky);
fill(0, 200, 0);
noStroke();
rect(400, 700, 800, 200); //grass
switch(stage){
case 1: // display easy and hard mode buttons
if (mouseX > 337 & mouseX < 463
& mouseY > 275 & mouseY < 325) { //hovering over easy button
fill(hover);
rect(400, 300, 125, 50, 20);
textSize(36);
fill(0);
text("easy", 400, 310)
fill(button);
rect(400, 375, 125, 50, 20);
fill(255);
text("hard", 400, 385);
} else if (mouseX > 337 & mouseX < 463
& mouseY > 350 & mouseY < 400) { //hovering over hard button
fill(button);
rect(400, 300, 125, 50, 20);
textSize(36);
fill(255);
text("easy", 400, 310)
fill(hover);
rect(400, 375, 125, 50, 20);
fill(0);
text("hard", 400, 385);
} else { //mouse anywhere on screen other than buttons
fill(button);
rect(400, 300, 125, 50, 20);
textSize(36);
fill(255);
text("easy", 400, 310)
fill(button);
rect(400, 375, 125, 50, 20);
fill(255);
text("hard", 400, 385);
}
break;
case 2: //start game screen
if (mouseX > 325 & mouseX < 475
& mouseY > 362 & mouseY < 438) { //if mouse hovering over start button
fill(hover);
rect(400, 400, 150, 75, 20);
textSize(44);
fill(0);
text("start", 400, 413)
} else { //mouse anywhere else on screen
fill(button);
rect(400, 400, 150, 75, 20);
textSize(44);
fill(255);
text("start", 400, 413)
}
stars = []; //reset stars to empty
break;
case 3: // playing the game
// draw and move animals
for (let i = 0; i < 4; i++) {
let curr = animals[i];
if (curr.isCow) {
curr.move();
image(cow, curr.x, curr.y, cow.width / 3, cow.height / 3);
} else {
curr.move();
image(goat, curr.x, curr.y, goat.width / 3, goat.height / 3);
}
}
//display and decrement timer
textSize(44);
fill(255);
text(timer, 400, 150);
text("score: " + score, 400, 200);
if (frameCount % 60 == 0 && timer > 0) { //60 makes it close to a minute in chrome but much slower in safari
timer --;
}
//if timer is 0 game is done
if (timer == 0) {
stage++;
text("game over", 400, 400);
timer = 60; //reset timer
}
//display moving stars and check for collisions with goats
for (let i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].display();
for (let j = 0; j < 4; j++) {
if (!animals[j].isCow & !animals[j].hidden) {
if(stars[i].x > animals[j].x - (goat.width / 6)
& stars[i].x < animals[j].x + (goat.width / 6)
& stars[i].y > animals[j].y - (goat.height / 6)
& stars[i].y < animals[j].y + (goat.height /6)) {
score += 5;
stars[i].scored(); //move star off screen when it collides with goat
}
}
}
}
//draws correct ufo at mouse position
if (ufo === ufo2) {
image(ufo, mouseX, mouseY + 51, ufo.width/ 20, ufo.height / 20);
abduct(); //if light is on then check for abductions
} else {
image(ufo, mouseX, mouseY, ufo.width/ 20, ufo.height / 20);
}
break;
case 4: //time is up and game is over
textSize(44);
fill(255);
text("score: " + score, 400, 200); //displays final score
textSize(36);
if (mouseX > 310 & mouseX < 490
& mouseY > 350 & mouseY < 400) { //if mouse is hovering over play again button
fill(hover);
rect(400, 375, 180, 50, 20);
fill(0);
text("play again", 400, 385);
} else { //if mouse is anywhere else on screen
fill(button);
rect(400, 375, 180, 50, 20);
fill(255);
text("play again", 400, 385);
}
break;
default: //empty default
}
}
// handles key inputs of left and right arrow keys to change sky color
function keyPressed() {
if (keyCode === LEFT_ARROW) {
if (skyNum === 0){
skyNum = 2;
} else {
skyNum--;
}
} else if (keyCode === RIGHT_ARROW) {
if (skyNum === 2){
skyNum = 0;
} else {
skyNum++;
}
}
}
// creates a MyStar custom PShape at mouse position when s is typed
function keyTyped() {
if (key === 's') {
let s = new MyStar(mouseX, mouseY);
stars.push(s);
}
}
//toggles ufo light and handles mouse presses
function mouseClicked() {
switch(stage) {
case 1: //choosing game mode -> easy or hard
if (mouseX > 337 & mouseX < 463
& mouseY > 275 & mouseY < 325) { //easy button is pressed
easyMode = true;
for (let i = 0; i < 4; i++) {
animals[i].updateSpeed(easy); //sets speed to easy speed for all animals
}
stage++;
} else if(mouseX > 337 & mouseX < 463
& mouseY > 350 & mouseY < 400) { //hard button is pressed
hardMode = true;
for (let i = 0; i < 4; i++) {
animals[i].updateSpeed(hard); //sets speed to hard speed for all animals
}
stage++;
}
break;
case 2:
if (mouseX > 325 & mouseX < 475
& mouseY > 362 & mouseY < 438) { //start button is pressed
stage++;
}
break;
case 3:
//toggles ufo light with clicks
if (ufo === ufo1) {
ufo = ufo2;
} else {
ufo = ufo1;
}
break;
case 4:
if (mouseX > 310 & mouseX < 490
& mouseY > 350 & mouseY < 400) { //play again button is pressed
stage = 1; //resets game to mode screen by resetting to stage 1
score = 0; //resets score
}
break;
}
}
//abduction function checks if ufo light touches an animal
function abduct(){
for (let j = 0; j < 4; j++) {
a = animals[j];
if(mouseY > a.y - ((ufo2.height / 40) + 100)){ //checks if ufo light is at right y level on screen
if (a.isCow){
if (mouseX > a.x - (cow.width / 6)
& mouseX < a.x + (cow.width / 6)){ //checks if ufo light is touching cow
score += 3;
a.abducted(); //animal abducted function moves abducted animal off screen
}
} else {
if (mouseX > a.x - (goat.width / 6)
& mouseX < a.x + (goat.width / 6)){ //checks if ufo light is touching goat
score -= 3;
a.abducted(); //animal abducted function moves abducted animal off screen
}
}
}
}
}