xxxxxxxxxx
function setup() {
createCanvas(1200,1000);
rectMode(CENTER);
}
//note array
var n = [];
//hitbox array
var h = [];
var score = 1000;
var streak = 0;
var hit = false;
var ht = 0;
var speed = 50;
var inc = 0;
var level = "easy";
var size2 = 1000;
var long = 0;
var tot = 0;
var notes = 0;
var scr = "menu";
function draw() {
//menu screen
if (scr == "menu") {
background(255);
drawBack();
menu();
}
//game screen
if (scr == "game") {
background(255);
drawBack();
//sets longest streak
if (long < streak) {
long = streak;
}
//red background when miss
if (hit && ht < 5) {
background(255, 0, 0, 100);
ht++;
}
if (ht > 5) {
hit = false;
}
drawLines();
stroke(0);
//creates notes
if (level == "easy" || level == "medium") {
if (frameCount % speed === 0) {
n.push(new Note(floor(random(1, 5))));
}
}
if (level == "hard" || level == "expert") {
if (frameCount % speed === 0) {
//generates random notes
ran = floor(random(1, 5));
ran2 = floor(random(1, 5));
while (ran2 == ran) {
ran2 = floor(random(1, 5));
}
n.push(new Note(ran));
n.push(new Note(ran2));
}
}
//displays and moves notes
for (let i = 0; i < n.length; i++) {
n[i].display();
n[i].move();
}
//removes notes from array when off screen
for (let i = 0; i < n.length; i++) {
if (n[i].y > 1100) {
score -= 200;
streak = 0;
hit = true;
ht = 0;
n.splice(i, 1);
tot++;
}
}
//displays and fades hitboxes
for (let i = 0; i < h.length; i++) {
h[i].display();
h[i].fade();
}
//removes hitboxes from array after faded
for (let i = 0; i < h.length; i++) {
if (h[i].f < 0) {
h.splice(i, 1);
}
}
//increases speed
if (inc % 20 === 0) {
speed--;
inc++;
}
sideBar();
//gameover if negative score
if (score < 0) {
scr = "gameover";
}
}
//gameover screen
if (scr == "gameover") {
sideBar();
for (i = 20; i > 0; i--) {
fill(255 - i * 10, 10, 255 - i * 2);
textSize(120);
text("GAME OVER", 100 + i, 320 + i);
}
}
}
//draws sidebar
function sideBar() {
stroke(0);
fill(255, 0, 255, 200);
rect(1100, 500, 300, 1200, 20);
fill(255, 100);
rect(1075, 800, 200, 80, 20);
rect(1075, 900, 200, 80, 20);
noStroke();
fill(0);
textSize(45);
text("SCORE", 980, 50);
text(score, 1010, 100);
text("STREAK", 980, 200);
text(streak, 1010, 250);
text("LONGEST", 970, 350);
text("STREAK", 970, 400);
text(long, 1010, 450);
text("NOTES", 980, 550);
text("HIT", 980, 600);
var per = round((notes / tot) * 100);
if (tot === 0) {
per = "-";
}
text(per + "%", 1010, 650);
text("RETRY", 990, 820);
text("QUIT", 1020, 920);
//retry
if (mouseX > 975 && mouseX < 1175) {
if (mouseY > 760 && mouseY < 840) {
if (mouseIsPressed) {
mouseX = 2000;
score = 1000;
streak = 0;
hit = false;
ht = 0;
inc = 0;
notes = 0;
long = 0;
tot = 0;
h.splice(0, h.length);
n.splice(0, n.length);
scr = "game";
if (level == "easy" || level == "hard") {
speed = 50;
} else {
speed = 40;
}
}
}
}
//quit
if (mouseX > 975 && mouseX < 1175) {
if (mouseY > 860 && mouseY < 940) {
if (mouseIsPressed) {
mouseX = 2000;
score = 1000;
streak = 0;
hit = false;
ht = 0;
inc = 0;
notes = 0;
long = 0;
tot = 0;
h.splice(0, h.length);
n.splice(0, n.length);
scr = "menu";
if (level == "easy" || level == "hard") {
speed = 50;
} else {
speed = 40;
}
}
}
}
}
//menu screen
function menu() {
//title
for (i = 20; i > 0; i--) {
fill(255 - i * 10, 10, 255 - i * 2);
textSize(120);
text("GUITAR HERO", 100 + i, 120 + i);
}
fill(0, 255, 0);
strokeWeight(10);
rect(500, 350, 400, 100, 20);
fill(255, 255, 0);
rect(500, 500, 400, 100, 20);
fill(255, 153, 0);
rect(500, 650, 400, 100, 20);
fill(255, 0, 0);
rect(500, 800, 400, 100, 20);
noStroke();
fill(0);
textSize(70);
text("EASY", 400, 375);
text("MEDIUM", 360, 525);
text("HARD", 400, 675);
text("EXPERT", 360, 825);
//buttons
//easy
if (mouseX > 300 && mouseX < 700) {
if (mouseY > 300 && mouseY < 400) {
if (mouseIsPressed) {
scr = "game";
level = "easy";
mouseX = 2000;
}
}
}
//medium
if (mouseX > 300 && mouseX < 700) {
if (mouseY > 450 && mouseY < 550) {
if (mouseIsPressed) {
scr = "game";
level = "medium";
speed = 40;
mouseX = 2000;
}
}
}
//hard
if (mouseX > 300 && mouseX < 700) {
if (mouseY > 600 && mouseY < 700) {
if (mouseIsPressed) {
scr = "game";
level = "hard";
mouseX = 2000;
}
}
}
//expert
if (mouseX > 300 && mouseX < 700) {
if (mouseY > 750 && mouseY < 850) {
if (mouseIsPressed) {
scr = "game";
level = "expert";
speed = 40;
mouseX = 2000;
}
}
}
}
//draws background
function drawBack() {
circ(500, 500, size2);
size2 += 8;
if (size2 > 3000) {
size2 = 2000;
}
}
//more background
function circ(x, y, s) {
stroke(0);
noFill();
strokeWeight(s / 10);
ellipse(x, y, s, s);
if (s > 10) {
circ(x, y, s / 1.5);
}
}
//draws rows and note buttons
function drawLines() {
stroke(0);
strokeWeight(10);
fill(255, 200);
rect(200, 500, 150, 1200, 20);
rect(400, 500, 150, 1200, 20);
rect(600, 500, 150, 1200, 20);
rect(800, 500, 150, 1200, 20);
fill(255, 0, 0, 200);
rect(200, 850, 150, 100, 20);
fill(255, 255, 0, 200);
rect(400, 850, 150, 100, 20);
fill(0, 255, 0, 200);
rect(600, 850, 150, 100, 20);
fill(51, 153, 255);
rect(800, 850, 150, 100, 20);
fill(0);
noStroke();
text("Z", 180, 870);
text("X", 380, 870);
text("C", 580, 870);
text("V", 780, 870);
stroke(0);
}
//checks which key is pressed
function keyTyped() {
if (key === "z") {
checkDist(1);
}
if (key == "x") {
checkDist(2);
}
if (key == "c") {
checkDist(3);
}
if (key == "v") {
checkDist(4);
}
}
//note object
class Note {
constructor(n) {
switch (n) {
case 1:
this.x = 200;
break;
case 2:
this.x = 400;
break;
case 3:
this.x = 600;
break;
case 4:
this.x = 800;
break;
}
this.y = -200;
this.n = n;
}
display() {
switch (this.n) {
case 1:
fill(255, 0, 0);
break;
case 2:
fill(255, 255, 0);
break;
case 3:
fill(0, 255, 0);
break;
case 4:
fill(51, 153, 255);
break;
}
rect(this.x, this.y, 150, 100, 20);
}
move() {
this.y += 7;
}
}
//hitbox object
class Hit {
constructor(n, y, c) {
switch (n) {
case 1:
this.x = 200;
break;
case 2:
this.x = 400;
break;
case 3:
this.x = 600;
break;
case 4:
this.x = 800;
break;
}
this, (y = y);
this.c = c;
this.f = 255;
this.w = 150;
this.h = 100;
}
display() {
switch (this.c) {
case "e":
stroke(102, 255, 255, this.f);
break;
case "g":
stroke(102, 255, 153, this.f);
break;
case "m":
stroke(255, 255, 102, this.f);
break;
case "b":
stroke(204, 0, 0, this.f);
break;
}
noFill();
strokeWeight(12);
rect(this.x, 850, this.w, this.h, 20);
}
fade() {
this.f -= 10;
this.w += 0.5;
this.h += 0.5;
}
}
//checks how close each note is to the hitbox
function checkDist(num) {
for (let i = 0; i < n.length; i++) {
if (n[i].n == num) {
//excellent
if (abs(n[i].y - 850) < 10) {
h.push(new Hit(num, n[i].y, "e"));
score += 300;
streak++;
inc++;
notes++;
n.splice(i, 1);
tot++;
break;
}
//good
else if (abs(n[i].y - 850) < 30) {
h.push(new Hit(num, n[i].y, "g"));
score += 150;
streak++;
inc++;
notes++;
n.splice(i, 1);
tot++;
break;
}
//medium
else if (abs(n[i].y - 850) < 40) {
h.push(new Hit(num, n[i].y, "m"));
score += 100;
inc++;
streak++;
notes++;
n.splice(i, 1);
tot++;
break;
}
//bad
else if (abs(n[i].y - 850) < 50) {
h.push(new Hit(num, n[i].y, "b"));
score += 50;
streak++;
inc++;
notes++;
n.splice(i, 1);
tot++;
break;
}
//miss
else {
if (scr != "gameover") {
score -= 100;
}
streak = 0;
hit = true;
ht = 0;
}
}
}
stroke(0);
}