xxxxxxxxxx
142
let stringInput, button, distractingWords, word, subChosenWord;
let theWords = []; //empty array
// choose the amount of words you'd like on the canvas
let howManyWords = 100;
let sceneNums = 0;
function setup() {
createCanvas(400, 400);
brain = loadImage('brain.png');
//creates text field
stringInput = createInput();
stringInput.position(width / 3.5, 460);
//creates button
button = createButton("Start Game");
button.position(stringInput.x + stringInput.width, 460);
//puts input text in "word" and initiates "storeInput"
if (button.mousePressed(storeInput)) {
sceneNums = 1;
}
for (let i = 0; i < howManyWords; i++) {
sceneNum = 1;
theWords[i] = new DrawDistractingWords(random(width), random(height));
theWords[i].body(word);
}
subChosenWord = new ChosenWord();
}
function storeInput() {
// "word" below will keep the input submitted after hitting "Start Game"
word = stringInput.value();
console.log(word);
}
function draw() {
background(255);
subChosenWord.body(word);
subChosenWord.move();
for (let i = 0; i < howManyWords; i++) {
//the words is an array
theWords[i].body(word);
theWords[i].move();
image(brain, width / 2 - 50, 25, 100, 75);
}
}
class DrawDistractingWords {
constructor(x, y) { // a special method that creates the car object
this.x = x;
this.y = y;
}
body(word1) {
push();
fill(random(255), random(255), random(255));
//where did this i come from?
// translate(random(width + i), random(height + i));
//translate(this.x, this.y);
//rotate(random(2 * PI));
// rotate(random(2 * PI));
text(word1, this.x, this.y);
pop();
}
// when you're moving your word around the canvas, these other words will move at the same time in random directions to confuse the user
move() {
if (keyIsDown(40) || keyIsDown(38) || keyIsDown(37) || keyIsDown(39)) {
this.y = this.y + random(-3, 3);
this.x = this.x + random(-4, 4);
}
}
}
class ChosenWord {
constructor() {
this.x = width / 2;
this.y = height - 50;
this.w = 30;
this.h = 30;
this.c = color(0, 255, 0);
// this.word = word;
}
//this function takes in a parameter which is the word
body(word) {
fill(random(255), random(255), random(255));
text(word, this.x, this.y, this.w, this.h);
}
move() {
//up arrow
if (keyIsDown(38)) {
this.y -= 3;
}
//down arrow
if (keyIsDown(40)) {
this.y += 3;
}
//right arrow
if (keyIsDown(39)) {
this.x += 3;
}
//left arrow
if (keyIsDown(37)) {
this.x -= 3;
}
}
hitsTheBrain() {
if (this.y < 25 && this.y < 100 && this.x > 150 && this.x < 250) {
sceneNum++;
this.y = height - 50;
}
}
}
/* amount of lives, or times to remember the word, bottom left canvas
function lives() {
for (let i = 0; i < remembrance; i++) {
ellipse(i * 20, height - 12, 20);
}
}
*/