xxxxxxxxxx
425
// This is a game titled 'Run, Doctor!'
// Made by Apurv Rayate
// Code 1 Faculty: Xin Xin
let protag;
let per = [];
let per2 = [],
cirper = [];
let per3 = [],
cirper2 = [],
ranper = [];
let perno;
let perno2, cirno;
let perno3, cirno2, ranno;
let sceneNum = 1;
let bg1, bg2;
let intro1, winner, loser;
let proimg;
let obst1, obst2, obst3, obst4, covid;
let obst;
function preload() {
proimg = loadImage('assets/protag.png');
obst1 = loadImage('assets/obst1.png');
obst2 = loadImage('assets/obst2.png');
obst3 = loadImage('assets/obst3.png');
obst4 = loadImage('assets/obst4.png');
covid = loadImage('assets/covid.png');
bg1 = loadImage('assets/bg1.png');
bg2 = loadImage('assets/bg2.png');
intro1 = loadImage('assets/intro1.png');
winner = loadImage('assets/win.png');
loser = loadImage('assets/lose.png');
}
function setup() {
createCanvas(600, 600);
angleMode(DEGREES);
imageMode(CENTER);
textAlign(CENTER, CENTER);
reset();
}
function reset() {
obst = [obst1, obst2, obst3, obst4, covid];
protag = new Protag();
perno = 5;
for (let i = 0; i < perno; i++) {
per[i] = new LinObst();
}
perno2 = 3;
cirno = 3;
for (let i = 0; i < perno2; i++) {
per2[i] = new LinObst();
}
for (let i = 0; i < cirno; i++) {
cirper[i] = new CirObst();
}
perno3 = 2;
cirno2 = 2;
ranno = 3;
for (let i = 0; i < perno3; i++) {
per3[i] = new LinObst();
}
for (let i = 0; i < cirno2; i++) {
cirper2[i] = new CirObst();
}
for (let i = 0; i < ranno; i++) {
ranper[i] = new RanObst();
}
}
function draw() {
background(220);
//console.log(protag.x, protag.y);
switch (sceneNum) {
case 1:
intro();
break;
case 2:
lev1();
break;
case 3:
lev2();
break;
case 4:
lev3();
break;
case 5:
lose();
break;
case 6:
win();
break;
}
function intro() {
image(intro1, width/2, height/2, 600, 600);
if (keyIsDown(13)) {
sceneNum = 2;
}
}
function lev1() {
image(bg1, width/2, height/2, 600, 600);
protag.show();
fill(180);
for (let i = 0; i < perno; i++) {
per[i].show();
per[i].move();
per[i].collide();
}
if (protag.x > 575 && protag.y > 230 && protag.y < 370) {
sceneNum = 3;
protag.x = 20;
protag.y = height / 2;
}
noStroke();
fill(0, 255, 0);
rect(595, 250, 5, 100);
}
function lev2() {
push();
blendMode(MULTIPLY);
fill(120, 100, 200);
rect(0, 0, 600, 600);
image(bg2, width/2, height/2, 600, 600);
pop();
//protag.reset();
protag.show();
fill(180);
for (let i = 0; i < perno2; i++) {
per2[i].show();
per2[i].move();
per2[i].collide();
}
for (let i = 0; i < cirno; i++) {
cirper[i].show();
cirper[i].move();
cirper[i].collide();
}
if (protag.x > 575 && protag.y > 230 && protag.y < 370) {
sceneNum = 4;
protag.x = 20;
protag.y = height / 2;
}
noStroke();
fill(0, 255, 0);
rect(595, 250, 5, 100);
}
function lev3() {
push();
blendMode(MULTIPLY);
fill(150, 150, 150);
rect(0, 0, 600, 600);
image(bg1, width/2, height/2, 600, 600);
pop();
protag.show();
fill(50);
rect(595, 250, 5, 100);
fill(180);
for (let i = 0; i < perno3; i++) {
per3[i].show();
per3[i].move();
per3[i].collide();
}
for (let i = 0; i < cirno2; i++) {
cirper2[i].show();
cirper2[i].move();
cirper2[i].collide();
}
for (let i = 0; i < ranno; i++) {
ranper[i].show();
ranper[i].move();
ranper[i].collide();
}
if (protag.x > 575 && protag.y > 230 && protag.y < 370) {
sceneNum = 6;
protag.x = 20;
protag.y = height / 2;
}
noStroke();
fill(0, 255, 0);
rect(595, 250, 5, 100);
}
function lose() {
image(loser, width/2, height/2, 600, 600);
if (keyIsDown(32)) {
sceneNum = 1;
}
}
function win() {
image(winner, width/2, height/2, 600, 600);
if (keyIsDown(32)) {
sceneNum = 1;
}
}
if (keyIsDown(UP_ARROW)) {
protag.up();
}
if (keyIsDown(DOWN_ARROW)) {
protag.down();
}
if (keyIsDown(LEFT_ARROW)) {
protag.lside();
}
if (keyIsDown(RIGHT_ARROW)) {
protag.rside();
}
}
class Protag {
constructor() {
this.x = 20;
this.y = height / 2;
this.step = 5;
}
show() {
push();
translate(this.x, this.y);
image(proimg, 0, 0, 40, 50);
pop();
this.x = constrain(this.x, 20, 580);
this.y = constrain(this.y, 20, 580);
}
up() {
this.y -= this.step;
}
down() {
this.y += this.step;
}
lside() {
this.x -= this.step;
}
rside() {
this.x += this.step;
}
}
class LinObst {
constructor() {
this.x = random(70, 550);
this.y = 20;
this.step = random(1, 8);
this.d = 0;
this.img = random(obst);
}
show() {
push();
translate(this.x, this.y);
if (this.img == covid) {
image(this.img, 0, 0, 50, 50);
} else {
image(this.img, 0, 0, 38, 50);
}
pop();
this.x = constrain(this.x, 70, 580);
this.y = constrain(this.y, 20, 580);
}
move() {
if (this.y == 20 || this.y > 579) {
this.step = -this.step;
}
this.y += this.step;
}
collide() {
this.d = dist(this.x, this.y, protag.x, protag.y);
if (this.d < 40) {
sceneNum = 5;
reset();
}
}
}
class CirObst {
constructor() {
this.cenx = random(260, 600);
this.ceny = random(600);
this.rad = random(150, 300);
this.ang = random(360);
this.x = 0;
this.y = 0;
this.step = random(-4, 4);
this.d = 0;
this.img = random(obst);
}
show() {
this.x = this.cenx + this.rad * sin(this.ang);
this.y = this.ceny + this.rad * cos(this.ang);
push();
translate(this.x, this.y);
if (this.img == covid) {
image(this.img, 0, 0, 50, 50);
} else {
image(this.img, 0, 0, 38, 50);
}
pop();
}
move() {
this.ang += this.step;
}
collide() {
this.d = dist(this.x, this.y, protag.x, protag.y);
if (this.d < 40) {
sceneNum = 5;
reset();
}
}
}
class RanObst {
constructor() {
this.x = random(60, 550);
this.y = random(600);
this.step = random(20, 30);
this.r = 0;
this.d = 0;
this.img = random(obst);
}
show() {
push();
translate(this.x, this.y);
if (this.img == covid) {
image(this.img, 0, 0, 50, 50);
} else {
image(this.img, 0, 0, 38, 50);
}
pop();
this.x = constrain(this.x, 90, 580);
this.y = constrain(this.y, 90, 580);
}
move() {
this.r = int(random(4));
if (frameCount % 10 == 0) {
if (this.r == 0) {
this.x += this.step;
} else if (this.r == 1) {
this.x -= this.step;
} else if (this.r == 2) {
this.y += this.step;
} else {
this.y -= this.step;
}
}
}
collide() {
this.d = dist(this.x, this.y, protag.x, protag.y);
if (this.d < 40) {
sceneNum = 5;
reset();
}
}
}