xxxxxxxxxx
257
let flute_sound;
function preload() {
soundFormats('mp3', 'ogg');
flute_sound = loadSound("audio/Melo2_flute_b.ogg");
}
let colors = ["RED", "YELLOW", "BLUE", "WHITE"];
let currentIndex = 0;
let gameState = "incredibox";
let info_logo;
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
startScreen();
//background balls
ball1 = new Ball(0, 200, 4, -2);
//new obj ball
ball2 = new Ball(600, 100, 5, -1);
}
function draw() {
//stuff can be added here
if (gameState == "startScreen") {
startScreen();
}
if (gameState == "gameScreen") {
gameScreen();
}
if (gameState == "mainScreen") {
mainScreen();
}
if (gameState == "infoScreen") {
infoScreen();
}
if (gameState == "incredibox") {
incredibox();
}
}
function mouseClicked() {
if (gameState == "gameScreen") {
if (mouseX > 20 && mouseX < 120 && mouseY > 200 && mouseY < 300) {
if (colors[currentIndex] === "RED") {
currentIndex = (currentIndex + floor(random(4))) % colors.length;
startScreen();
}
}
if (mouseX > 140 && mouseX < 240 && mouseY > 200 && mouseY < 300) {
if (colors[currentIndex] === "YELLOW") {
currentIndex = (currentIndex + floor(random(4))) % colors.length;
startScreen();
}
}
if (mouseX > 260 && mouseX < 360 && mouseY > 200 && mouseY < 300) {
if (colors[currentIndex] === "BLUE") {
currentIndex = (currentIndex + floor(random(4))) % colors.length;
startScreen();
}
}
if (mouseX > 380 && mouseX < 480 && mouseY > 200 && mouseY < 300) {
if (colors[currentIndex] === "WHITE") {
currentIndex = (currentIndex + floor(random(4))) % colors.length;
startScreen();
}
}
}
if (gameState == "startScreen") {
if (
mouseX > width / 2 &&
mouseX < width / 2 + 100 &&
mouseY > height / 1.5 &&
mouseY < height / 1.5 + 100
) {
gameState = "gameScreen";
}
}
if (gameState == "mainScreen") {
if (
mouseX > width / 2 - 120 &&
mouseX < width / 2 + 80 &&
mouseY > 400 &&
mouseY < 500
) {
gameState = "gameScreen";
}
if (
mouseX > width / 2 + 100 &&
mouseX < width / 2 + 200 &&
mouseY > 400 &&
mouseY < 500
) {
gameState = "infoScreen";
}
}
if (gameState == "infoScreen") {
if (
mouseX > 30 &&
mouseX < 130 &&
mouseY > height - 150 &&
mouseY < height - 50
) {
fill(0);
rect(0, 0, width, height);
gameState = "mainScreen";
}
}
if (gameState == "incredibox")
{
if(mouseX>30 && mouseX<130 && mouseY>(height-150) && mouseY<height-50){
flute_sound.play();
}
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function startScreen() {
background(255, 200, 30);
textSize(22);
fill(255);
text("THE ULTIMATE SPEED GAME", width / 3, height / 2);
fill(255, 200, 200);
noStroke();
square(width / 2, height / 1.5, 100);
}
function gameScreen() {
background(0);
// red box
noStroke();
fill(255, 0, 0);
rect(20, 200, 100, 100);
// yellow box
fill(255, 255, 0);
rect(140, 200, 100, 100);
// blue box
fill(0, 0, 255);
rect(260, 200, 100, 100);
//white box
fill(255);
rect(380, 200, 100, 100);
// the color text
fill(255);
textSize(24);
text(colors[currentIndex], width / 2, 150);
}
//MAIN SCREEN
let x = 300;
let y = 200;
let xspeed = 4;
let yspeed = -2;
let ball1;
let ball2;
let background_check = false;
function mainScreen() {
if (background_check == false) {
fill(0);
rect(0, 0, width, height);
background_check = true;
}
//array
let Balls = [ball1, ball2];
for (
let i = 0;
i < Balls.length;
i++ //runs through the balls array and displays each obj
) {
Balls[i].display();
Balls[i].move();
Balls[i].bounce();
}
//play Button
stroke(255);
strokeWeight(10);
rect(width / 2 - 130, 400, 200, 100, 40);
//info button
square(width / 2 + 100, 400, 100, 40);
noStroke();
fill(255);
textSize(40);
text("PLAY", width / 2 - 75, 465);
textSize(30);
text("INFO", width / 2 + 115, 460);
}
function infoScreen() {
var r = map(sin(frameCount), -1, 1, 50, 255);
var g = map(cos(frameCount / 2), -1, 1, 50, 255);
var b = map(sin(frameCount / 9), -1, 1, 50, 255);
background(r, g, b);
fill(255);
rect(width / 2 - 100, 50, 200, 100, 40);
textSize(50);
fill(r, g, b);
text("INFO", width / 2 - 60, 115);
fill(255);
square(30, height - 150, 100, 40);
fill(r, g, b);
textSize(30);
text("BACK", 37, height - 90);
}
function incredibox() {
background(0);
fill(255);
square(30, height - 150, 100, 40);
}
function readSerial(data) {
//READ FROM ARDUINO HERE
if (data != null) {
fromArduino = split(trim(data), ",");
red_button = fromArduino[0] == 1 ? true : false;
yellow_button = fromArduino[1] == 1 ? true : false;
blue_button = fromArduino[2] == 1 ? true : false;
white_button = fromArduino[3] == 1 ? true : false;
}
}