xxxxxxxxxx
334
/*
Mood | Score | Color
Happy | 10 | yellow
Calm | 9 | green
Loving | 8 | pink
Excited | 8 | orange
Condifent | 7 | blue
Bored | 3 | gray
Angry | 2 | red
Sad | 1 | black
*/
// Global variables and parameters
let sourceText;
let words = [];
let wordsToDisplay = [];
let scrollingText;
let startIndex = 0;
let video;
let playerSpeed = 7;
let player;
let collected = 0;
let score = 0;
let rConc = 1;
let gConc = 1;
let bConc = 1;
let startBtn;
let restartBtn;
let closeInstructions;
let helpBtn;
let gameState = 'start';
let hand;
let music;
let gameStartTime;
let gameTime = 30;
function preload() {
hand = loadImage('hand.png');
sourceText = loadStrings("words.txt");
speedrush = loadFont("speedrush.otf");
music = loadSound('music.mp3');
catchSound = loadSound('catch.wav');
}
function setup() {
createCanvas(800, 600);
textFont(speedrush);
video = createCapture(VIDEO);
video.size(60, 45);
player = new Player();
music.setVolume(0.5);
music.loop();
}
// initializing the collector hand class
class Player{
constructor() {
this.width = 100;
this.height = 40;
this.x = width / 2;
this.y = height - 40;
this.color = color(0, 255, 0);
}
}
// initalizing a class for displaying and moving each word
class Word {
constructor() {
this.x = random(15, width-30);
this.y = 0;
this.speed = random(2, 3);
this.text = randomWord();
}
move() {
this.y += this.speed;
}
display() {
text(this.text, this.x, this.y);
}
}
// choosing a random word from the words file
function randomWord() {
return random(sourceText);
}
// a function to check if the hand captures a word
function checkCollision(obj1, obj2) {
let d = dist(obj1.x + obj1.width / 2, obj1.y + obj1.height / 2, obj2.x, obj2.y);
if (d < obj1.width / 2 && obj2.y + textSize() > obj1.y) {
return true;
}
}
function videoDisplay(){
let charIndex = startIndex;
let w = width / video.width;
let h = height / video.height;
for (let j = 0; j < video.height; j++) {
for (let i = 0; i < video.width; i++) {
const pixelIndex = (i + j * video.width) * 4;
const r = video.pixels[pixelIndex + 0];
const g = video.pixels[pixelIndex + 1];
const b = video.pixels[pixelIndex + 2];
const avg = (r + g + b) / 3;
noStroke();
fill(r*rConc, g*gConc, b*bConc);
textSize(15);
textAlign(CENTER, CENTER);
text(scrollingText.charAt(charIndex % scrollingText.length), i * w, j * h);
charIndex++;
}
}
startIndex++;
}
function init()
{
textFont("unset");
// Display the player
image(hand, player.x, player.y, player.width, player.height);
// Handle player movement
if (keyIsDown(LEFT_ARROW) && player.x > 0) {
player.x -= playerSpeed;
}
if (keyIsDown(RIGHT_ARROW) && player.x < width - player.width) {
player.x += playerSpeed;
}
fill(255);
// Spawn new words at regular intervals
if (frameCount % 30 === 0) {
let word = new Word();
words.push(word);
}
// Move and display words
for (let i = words.length - 1; i >= 0; i--) {
words[i].move();
words[i].display();
// Check for collision with the player
if(checkCollision(player, words[i])){
let targetWord = words[i];
words.splice(i, 1);
collected++;
catchSound.play();
console.log(targetWord.text);
let idx = sourceText.indexOf(targetWord.text);
console.log(idx);
if(idx < 10){
rConc = 1;
gConc = 1;
bConc = 0.1;
score += 10;
}else if(idx >=10 && idx < 20){
rConc = 0.5;
gConc = 1;
bConc = 0.5;
score += 9;
}
else if(idx >=20 && idx < 30){
rConc = 0.7;
gConc = 0.5;
bConc = 0.5;
score += 8;
}
else if(idx >=30 && idx < 40){
rConc = 1;
gConc = 0.5;
bConc = 0.1;
score += 8;
}
else if(idx >=40 && idx < 50){
rConc = 0.5;
gConc = 0.5;
bConc = 2;
score += 7;
}
else if(idx >=50 && idx < 60){
rConc = 0.7;
gConc = 0.7;
bConc = 0.7;
score += 3;
}
else if(idx >=60 && idx < 70){
rConc = 1;
gConc = 0.3;
bConc = 0.3;
score += 2;
}
else if(idx >=70 && idx < 80){
rConc = 0.25;
gConc = 0.25;
bConc = 0.25;
score += 1;
}
wordsToDisplay.push(targetWord.text);
}
}
// displaying score and timer
textFont(speedrush);
text(`Collected: ${collected}`, width - 60, 20);
text(`Score: ${score}`, 40 , 20);
textSize(30);
text(`${gameTime - secondsInGame()} S`, width/2, 20);
if(gameTime - secondsInGame() == 0){
gameState = 'end';
}
}
// function to calculate seconds since game started
function secondsInGame() {
return floor(int(millis() - gameStartTime) / 1000);
}
// function to display help screen
function displayInstructions() {
fill('#FFDAA6');
stroke(50);
strokeWeight(9);
rect(70, 70, width-140, height-140, 20);
noStroke();
fill(0);
textSize(20);
textAlign(CENTER);
text("Welcome to the ChromaMood Game!", width / 2, height / 4);
text("Collect positive mood words to score more points.", width / 2, height / 3);
text("Use the arrow keys to move your hand.", width / 2, height / 2);
if(!closeInstructions){
closeInstructions = createButton('Close');
closeInstructions.position(width/2-35, height/2+90);
closeInstructions.class('custom-button');
}
closeInstructions.mousePressed(function() {
closeInstructions.remove();
closeInstructions = null;
gameState = 'start';
});
}
// function to display start screen
function gameStart(){
textAlign(CENTER);
textSize(55);
fill(255);
text("ChromaMood", width/2, height/3);
textSize(14);
text("Embrace positivity and elevate your score", width/2, height/3+30);
image(hand, width/2-105, height/2-50, 200, 100);
if(!startBtn){
startBtn = createButton('Start');
startBtn.position(width/2-35, height/2+90);
startBtn.class('custom-button');
}
if(!helpBtn){
helpBtn = createButton('Help');
helpBtn.position(width/2-35, height/2+150);
helpBtn.class('custom-button');
}
startBtn.mousePressed(function() {
startBtn.remove();
helpBtn.remove();
gameState = 'game';
let now = millis(); // time since sketch started
gameStartTime = now;
});
helpBtn.mousePressed(function(){
helpBtn.remove();
helpBtn = null;
gameState = 'instructions';
})
}
// function to display end screen
function gameEnd(){
fill(255);
text(`You collected ${collected} words`, width / 2, height/2-50);
textSize(20);
text(`Your score is: ${score}`, width / 2, height/2);
if(!restartBtn){
restartBtn = createButton('Restart');
restartBtn.position(width/2-40, height/2+40);
restartBtn.class('custom-button');
}
restartBtn.mousePressed(function() {
// resetting game parameters
rConc = 1;
gConc = 1;
bConc = 1;
wordsToDisplay = [];
words = [];
score = 0;
collected = 0;
restartBtn.remove();
restartBtn = null;
gameState = 'game';
let now = millis(); // time since sketch started
gameStartTime = now;
});
}
function draw() {
scrollingText = wordsToDisplay.join(' ');
video.loadPixels();
background(0);
frameRate(20);
if (gameState == 'instructions') {
displayInstructions();
}
if(gameState == 'start'){
gameStart();
}
if(gameState == 'game'){
music.setVolume(0.15);
videoDisplay();
init();
}
if(gameState == 'end'){
gameEnd();
}
}