xxxxxxxxxx
602
// stage 1
let grapes = []; // grape array
let d;
let score = 0;
let grapeImg = [];
let myFont;
// stage 2
let roleImg;
let potImg;
let a = 90; //initial x for grape
let b = 90; //initial y for grape
let wall = []; // maze wall array
let virus = [];
// stage 3
let jam1;
let jamLives = 5
let jammy;
let stuffsNum = 7
let stuffs = []; // empty array
let stuffsImg = [];
let scenenum = 0;
function preload() {
myFont = loadFont('GochiHand-Regular.ttf');
grapeImg = loadImage('Game Assets/xs-grape.png'); //stage 1
roleImg = loadImage('Game Assets/m-grape.png'); //stage 2
potImg = loadImage('Game Assets/pot.png');
for (let i = 0; i < 5; i++) {
virus = loadImage('Game Assets/virus.png');
}
jammy = loadImage('Game Assets/jam.png'); //stage 3
fridge = loadImage('Game Assets/fridge.png');
for (let i = 0; i < 7; i++) {
stuffsImg[i] = loadImage('stuffs/stuff' + i + '.png');
}
}
function setup() {
createCanvas(600, 600);
// STAGE 1:
let i = 0
while (i < 25) { //there will be 25 grapes
let grape = new Grapes();
let validPos = true;
for (let j = 0; j < i; j++) {
d = dist(grape.x, grape.y, grapes[j].x, grapes[j].y);
if (d < 50) { // avoid overlapping grapes
validPos = false;
}
// avoid grapes to go over the canvas
if (grape.x < 50 || grape.x > (width - 50) ||
grape.y < 50 || grape.y > (height - 50)) {
validPos = false;
}
// avoid the grapes to be covered by the scoreboard
if (grape.x < 200 && grape.y < 60) {
validPos = false;
}
}
if (validPos) {
grapes[i] = grape
i = i + 1;
}
}
// STAGE 2:
for (let i = 0; i < 14; i++) { //build walls
wall[i] = new Wall(); //Connects the wall array to the Wall class
role = new Role();
for (let i = 0; i < 5; i++) {
virus[i] = new Virus();
}
}
// STAGE 3:
for (let i = 0; i < stuffsNum; i++) {
stuffs[i] = new Stuff(random(width - 50), random(height), color(250));
}
jam1 = new Jam();
}
function draw() {
switch(scenenum){
case 0:
background(198, 198, 236);
intro();
if (keyCode === 32) {
scenenum = 1;
}
break;
case 1:
background(204, 204, 255);
record();
for (let i = 0; i < grapes.length; i++) {
d = dist(mouseX, mouseY, grapes[i].x, grapes[i].y);
if (mouseIsPressed) { // click to pick grapes
if (d < 15) {
grapes.splice(i, 1);
score = score + 1
// console.log(d);
}
}
if (grapes[i] != null) {
grapes[i].show();
}
if (score >= 3) { //pop message
noStroke();
fill(255, 204, 0);
rect(100, 275, 400, 65, 10);
push();
textSize(20);
textFont(myFont);
fill(64,0,128);
text('Yay! You have collected enough grapes!', 120, 300);
text(' Press Space Key to proceed.', 120, 330);
pop();
scenenum = 2;
}
}
break;
case 2:
background(255);
move(); //move the grape
boundary();
infect();
guide();
if(a>480 && b>480){
console.log('cooking!');
scenenum = 3;
}
break;
case 3:
background(198, 198, 236);
image(fridge, 500, 100, 200, 200);
currentJamLives();
placement();
guide();
for (let i = 0; i < stuffsNum; i++) {
stuffs[i].body(i);
stuffs[i].move();
stuffs[i].checkCollision();
}
jam1.body();
if (keyIsDown(38)) {
jam1.y--;
}
if (keyIsDown(40)) {
jam1.y++;
}
if (keyIsDown(39)) {
jam1.x++;
}
if (keyIsDown(37)) {
jam1.x--;
}
break;
}
}
// FUNCTION-STAGE 1: notes & record(score board)
function intro(){
fill(102, 102, 255);
textFont(myFont);
textSize(48);
text('🍇JAMMY🍇', 220,220);
push();
fill(255);
textSize(22);
textFont(myFont);
text("This is a grape jam-making game. In this game,", 100, 280);
text("you will first collect enough grapes,", 150, 320);
text("then you will prepare the grapes before cooking the jam,", 40,360);
text("and finally you need to put the jam into the fridge.", 80,400);
pop();
fill(255,180);
textSize(20);
text('Press Space key to proceed', 190,450);
textSize(80);
text(' 🍇 🍇', 185,255);
}
function record(){
// score board box
push();
noStroke();
fill(102, 153, 153);
rect(0, 0, width, 60);
pop();
// scoreboard count
push();
textSize(20);
fill(255, 204, 0);
text('Grape number: ' + score, 430, 35);
pop();
push();
textSize(16);
fill(0);
text('Click on the grapes to collect them.', 20, 25);
text('You need to collect at least 10 grapes.', 20, 45);
pop();
}
// FUNCTIONS-STAGE 2: move & guide & walls & virus & destination
function move() { // moving the grape/role
imageMode(CENTER);
image(roleImg, a, b, 50, 50);
if (keyIsPressed) {
if (keyCode == 39) {
a = a + 3;
}
if (keyCode == 37) {
a = a - 3;
}
if (keyCode == 40) {
b = b + 3;
}
if (keyCode == 38) {
b = b - 3;
}
}
}
function guide(){
fill(255);
textSize(16);
text('Press the arrow keys to move the grape ⬆️ ⬇️ ➡️ ⬅️ ', 10, 45);
text('The grape will be sent back to the origin if it hits any wall or run into the virus.', 10,25);
textSize(20);
text('Destination ♨️', 370, 580);
imageMode(CORNER);
image(potImg, 483,540,55,55);
}
function boundary(){ // maze walls
wall[0].x = 120
wall[0].y = 60
wall[0].w = 60
wall[0].h = 180
wall[0].body();
wall[0].checkCollision();
wall[1].x = 120
wall[1].y = 300
wall[1].w = 60
wall[1].h = 180
wall[1].body();
wall[1].checkCollision();
wall[2].x = 180
wall[2].y = 360
wall[2].w = 60
wall[2].h = 60
wall[2].body();
wall[2].checkCollision();
wall[3].x = 240
wall[3].y = 120
wall[3].w = 60
wall[3].h = 120
wall[3].body();
wall[3].checkCollision();
wall[4].x = 240
wall[4].y = 480
wall[4].w = 180
wall[4].h = 60
wall[4].body();
wall[4].checkCollision();
wall[5].x = 300
wall[5].y = 120
wall[5].w = 60
wall[5].h = 60
wall[5].body();
wall[5].checkCollision();
wall[6].x = 300
wall[6].y = 300
wall[6].w = 120
wall[6].h = 60
wall[6].body();
wall[6].checkCollision();
wall[7].x = 300
wall[7].y = 420
wall[7].w = 60
wall[7].h = 60
wall[7].body();
wall[7].checkCollision();
wall[8].x = 420
wall[8].y = 60
wall[8].w = 60
wall[8].h = 120
wall[8].body();
wall[8].checkCollision();
wall[9].x = 420
wall[9].y = 240
wall[9].w = 60
wall[9].h = 180
wall[9].body();
wall[8].checkCollision();
wall[10].x = 0
wall[10].y = 0
wall[10].w = width
wall[10].h = 60
wall[10].body();
wall[10].checkCollision();
wall[11].x = 0
wall[11].y = 540
wall[11].w = 480
wall[11].h = 60
wall[11].body();
wall[10].checkCollision();
wall[12].x = 0
wall[12].y = 60
wall[12].w = 60
wall[12].h = 480
wall[12].body();
wall[12].checkCollision();
wall[13].x = 540
wall[13].y = 60
wall[13].w = 60
wall[13].h = 540
wall[13].body();
wall[13].checkCollision();
}
function infect(){ // virus
virus[0].x = 90
virus[0].y = 510
virus[0].body();
virus[0].checkCollision();
virus[1].x = 210
virus[1].y = 330
virus[1].body();
virus[1].checkCollision();
virus[2].x = 390
virus[2].y = 270
virus[2].body();
virus[2].checkCollision();
virus[3].x = 390
virus[3].y = 450
virus[3].body();
virus[3].checkCollision();
virus[4].x = 510
virus[4].y = 90
virus[4].body();
virus[4].checkCollision();
}
//FUNCTION-STAGE 3: JamLives
function currentJamLives() {
for (let i = 0; i < jamLives; i++) {
// ellipse(i*25, height-20, 20);
image(jammy, i * 45, height - 30, 60, 60)
}
}
function placement(){
push();
noFill();
strokeWeight(5);
stroke(255,0,0);
rect(450,10,50,60,10);
pop();
}
function keyPressed() {
if (keyCode === 32) {
scenenum++;
}
}
//CLASSES-STAGE 1: Grapes
class Grapes {
constructor() {
this.x = random(width);
this.y = random(height);
// this.w = 20;
}
show() {
// ellipse(this.x, this.y, this.w);
imageMode(CENTER);
image(grapeImg, this.x, this.y);
}
}
//CLASSES-STAGE 2: Role(grape) & Virus & Wall
class Role{
constructor(a,b){
this.x = a;
this.y = b;
this.w = 60;
this.h = 60;
}
body(){
imageMode(CENTER);
image(grapeImg, a, b, 50, 50);
}
}
class Virus{
constructor(c, d){
this.x = c;
this.y = d;
this.w = 50;
this.h = 50;
}
body(){
imageMode(CENTER);
image(virus,this.x,this.y);
}
//the grape will go back to the origin is it hit virus
checkCollision(){
if (a-25<this.x+this.w && a+25>this.x &&
b-25<this.y+this.h && b+25>this.y){
a=90;
b=90;
console.log('infected')
}
}
}
class Wall {
constructor(x, y) {
this.x = x;
this.y = y;
this.w = 0;
this.h = 0;
}
body() {
rectMode(CORNER);
fill(0, 51, 0);
noStroke();
rect(this.x, this.y, this.w, this.h);
}
// this is how the wall detects when the grape hit them
// the grape is in the CenterMode, the wall is in CornerMode
// the grape size is 50x50, so half is 25
// left side of grape hits the wall: a-25 <this.x+this.w
// right side of grape hits the wall: a+25 >this.x
// top side of grape hits the wall: b-30 <this.y+this.h
// bottom side of grape hits the wall: b+25 >this.y
// when the grape hits the wall, it goes back to origin
checkCollision(){
if (a-25<this.x+this.w && a+25>this.x &&
b-25<this.y+this.h && b+25>this.y){
a=90;
b=90;
console.log('hit wall')
}
}
}
//CLASSES-STAGE 3: Jam & Stuff
class Jam {
constructor() {
this.x = 220;
this.y = height - 80;
this.w = 30;
this.h = 30;
// this.c = color(0, 255, 0);
}
body() {
// fill(this.c);
// ellipse(this.x, this.y, this.w, this.h);
imageMode(CENTER);
image(jammy, this.x, this.y, 70, 70)
}
}
class Stuff {
constructor(x, y, c) {
this.x = x;
this.y = y;
this.w = 40;
this.h = 40;
this.c = c;
}
body(index) { // place the stuffs
// fill(this.c)
// rect(this.x, this.y, this.w, this.h);
imageMode(CORNER);
for (let i = 0; i < 6; i++) {
image(stuffsImg[index], this.x - 5, this.y - 5, 60, 60);
}
}
move() {
this.y++;
if (this.y > height) {
this.y = 0;
if (this.x > 450 && this.x < 495 && this.y > 10 && this.y < 70) {
push();
noStroke();
fill(102, 153, 153);
rect(200, 265, 200, 50, 10);
pop();
// final winning message
push();
textSize(20);
noStroke();
fill(255, 204, 0);
text('YOU SUCCEED ! ', 220, 300);
pop();
}
}
}
checkCollision() {
if (jam1.x + jam1.w / 2 > this.x &&
jam1.x < this.x + this.w &&
jam1.y + jam1.h / 2 > this.y &&
jam1.y < this.y + this.h
) {
// console.log('bumped');
jam1.y = height - 50;
jam1.x = width - 5;
jamLives--;
}
if (jam1.x > 450 && jam1.x < 495 && jam1.y > 10 && jam1.y < 70) {
push();
noStroke();
fill(102, 153, 153);
rect(180, 265, 240, 50, 10);
pop();
// final message
push();
textSize(30);
noStroke();
fill(255, 204, 0);
text('YOU SUCCEED ! ', 205, 300);
pop();
}
if (jamLives == 0) {
push();
noStroke();
fill(102, 153, 153);
rect(210, 265, 200, 50, 10);
pop();
push();
textSize(30);
fill(255, 204, 0);
text('Game Over !', 240, 300);
pop();
console.log('You wastes all of your jams.');
}
}
}