xxxxxxxxxx
// --------------------------------------------
// Define Game Variables
// --------------------------------------------
let currentChapter = 0; // 0: Wordle, 1: Snake, 2: Brick Breaker
// Wordle Variables
let wordleWords = ["belief", "faith", "choice"];
let guessedLetters = [
["", "", "", "", "", ""], // for 'belief' (6 letters)
["", "", "", "", ""], // for 'faith' (5 letters)
["", "", "", "", "", ""] // for 'choice' (6 letters)
];
let currentWordIndex = 0; // which row/word is currently being guessed
let wordleInputActive = true;
let wordColors = ["white", "white", "white"]; // Word colors for feedback (white, green, red)
// Snake Game Variables
let snake = [];
let snakeDir;
let food;
let gridSize = 20;
let snakeGameActive = false;
let applesEaten = 0; // Track apples eaten
let lastAppleImg; // Custom image for the last apple
// Images
let startImage; // Start screen image
let endImage; // End screen image
let showStartImage = true; // Whether to show the start image
// Brick Breaker Variables
let paddle;
let ball;
let bricks = [];
let rows = 5;
let cols = 7;
let brickBreakerActive = false;
let lives = 3; // Player starts with 3 lives
// --------------------------------------------
// Preload Images
// --------------------------------------------
function preload() {
lastAppleImg = loadImage("Raen1.png"); // Replace with your PNG file path
startImage = loadImage("ground.png"); // Replace with your PNG file path
endImage = loadImage("fire.png"); // Replace with your PNG file path
}
// --------------------------------------------
// Setup Function
// --------------------------------------------
function setup() {
createCanvas(600, 600);
// Snake setup
resetSnake();
// Brick Breaker setup
paddle = new Paddle();
ball = new Ball();
setupBricks();
}
// --------------------------------------------
// Draw Function
// --------------------------------------------
function draw() {
background(50);
if (currentChapter === 0 && showStartImage) {
image(startImage, 0, 0, width, height);
} else if (currentChapter === 0) {
drawWordle();
} else if (currentChapter === 1) {
drawSnake();
} else if (currentChapter === 2) {
drawBrickBreaker();
} else if (currentChapter === 3) {
image(endImage, 0, 0, width, height); // Display end image full screen
}
}
// --------------------------------------------
// Chapter 1: Wordle
// --------------------------------------------
function drawWordle() {
fill(255);
textSize(16);
textAlign(CENTER);
text("This six-letter word represents confidence in the truth", width / 2, 200);
text("or existence of something, often without proof. What is it?", width / 2, 220);
fill(255);
textSize(24);
text("Guess the word", width / 2, 100);
// Draw the rows of input boxes for each word
drawWordleRow(guessedLetters[0], 150, wordColors[0]); // First row
drawWordleRow(guessedLetters[1], 250, wordColors[1]); // Second row
drawWordleRow(guessedLetters[2], 350, wordColors[2]); // Third row
// Add prompt text for each word
fill(255);
textSize(16);
text(
"A five-letter word that means complete trust or confidence in someone",
width / 2,
300
);
text(
"or something, often associated with religion or spirituality. What is it?",
width / 2,
320
);
text(
"A six-letter word that represents the act of selecting between options",
width / 2,
400
);
text("or alternatives. What is it?", width / 2, 420);
}
function drawWordleRow(letters, y, color) {
for (let i = 0; i < letters.length; i++) {
let x = width / 2 - (letters.length * 30) / 2 + i * 30;
fill(color === "white" ? 255 : color === "green" ? "green" : "red");
rect(x, y, 25, 25);
fill(0);
text(letters[i].toUpperCase(), x + 12.5, y + 20);
}
}
function keyPressed() {
if (showStartImage && keyCode === ENTER) {
showStartImage = false; // Start Chapter 1
return;
}
// Wordle Input Handling
if (wordleInputActive && currentChapter === 0) {
let activeRow = guessedLetters[currentWordIndex];
let activeWord = wordleWords[currentWordIndex];
if (keyCode === BACKSPACE) {
for (let i = activeRow.length - 1; i >= 0; i--) {
if (activeRow[i] !== "") {
activeRow[i] = "";
break;
}
}
return;
}
if (keyCode === ENTER) {
let guessedWord = activeRow.join("").toLowerCase();
if (guessedWord.length === activeWord.length) {
if (guessedWord === activeWord) {
wordColors[currentWordIndex] = "green"; // Turn green if correct
currentWordIndex++;
if (currentWordIndex >= wordleWords.length) {
wordleInputActive = false;
currentChapter = 1; // Move to Snake game
snakeGameActive = true;
}
} else {
wordColors[currentWordIndex] = "red"; // Turn red if incorrect
}
}
return;
}
if (key.length === 1 && /[a-zA-Z]/.test(key)) {
for (let i = 0; i < activeRow.length; i++) {
if (activeRow[i] === "") {
activeRow[i] = key.toUpperCase();
break;
}
}
}
}
// Snake Controls
if (snakeGameActive && currentChapter === 1) {
if (keyCode === UP_ARROW && snakeDir !== 'DOWN') snakeDir = 'UP';
else if (keyCode === DOWN_ARROW && snakeDir !== 'UP') snakeDir = 'DOWN';
else if (keyCode === LEFT_ARROW && snakeDir !== 'RIGHT') snakeDir = 'LEFT';
else if (keyCode === RIGHT_ARROW && snakeDir !== 'LEFT') snakeDir = 'RIGHT';
}
}
// --------------------------------------------
// Chapter 2: Snake Game
// --------------------------------------------
function drawSnake() {
updateSnake();
checkSnakeCollision();
// Draw food
if (applesEaten === 4) {
image(lastAppleImg, food.x * gridSize, food.y * gridSize, gridSize * 10, gridSize * 10); // 10x size
} else {
fill(255, 0, 0);
rect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}
// Draw snake
fill(0, 255, 0);
for (let segment of snake) {
rect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
}
}
function resetSnake() {
snake = [{ x: 10, y: 10 }]; // Initial snake position
snakeDir = 'RIGHT'; // Default direction
food = createFood(); // Generate initial food
applesEaten = 0; // Reset apples eaten count
}
function updateSnake() {
if (frameCount % 10 === 0) {
let head = { snake[0] };
if (snakeDir === 'UP') head.y--;
else if (snakeDir === 'DOWN') head.y++;
else if (snakeDir === 'LEFT') head.x--;
else if (snakeDir === 'RIGHT') head.x++;
// Wrap around screen
head.x = (head.x + width / gridSize) % (width / gridSize);
head.y = (head.y + height / gridSize) % (height / gridSize);
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
applesEaten++;
if (applesEaten >= 5) {
snakeGameActive = false;
currentChapter = 2; // Move to Brick Breaker
brickBreakerActive = true;
} else {
food = createFood();
}
} else {
snake.pop();
}
}
}
function checkSnakeCollision() {
let head = snake[0];
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === head.x && snake[i].y === head.y) {
resetSnake();
}
}
}
function createFood() {
let newFood;
while (true) {
newFood = {
x: floor(random(width / gridSize)),
y: floor(random(height / gridSize)),
};
let isOnSnake = snake.some(segment => segment.x === newFood.x && segment.y === newFood.y);
if (!isOnSnake) break;
}
return newFood;
}
// --------------------------------------------
// Chapter 3: Brick Breaker
// --------------------------------------------
function drawBrickBreaker() {
paddle.show();
ball.show();
ball.update();
paddle.update();
for (let brick of bricks) {
brick.show();
if (ball.hits(brick)) {
brick.destroyed = true;
ball.reverseY();
}
}
bricks = bricks.filter(brick => !brick.destroyed);
fill(255);
textSize(16);
textAlign(LEFT);
text(`Lives: ${lives}`, 10, 20); // Display lives
if (bricks.length === 0) {
currentChapter = 3; // Show end image
}
if (ball.outOfBounds()) {
lives--; // Lose a life if ball goes out of bounds
if (lives > 0) {
ball.reset();
} else {
fill(255);
textSize(32);
textAlign(CENTER);
text("Game Over", width / 2, height / 2);
noLoop();
}
}
}
function setupBricks() {
let brickWidth = width / cols;
let brickHeight = 20;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
bricks.push(new Brick(c * brickWidth, r * brickHeight, brickWidth, brickHeight));
}
}
}
class Paddle {
constructor() {
this.w = 100;
this.h = 20;
this.x = width / 2 - this.w / 2;
this.y = height - this.h - 10;
this.speed = 8; // Faster paddle
}
show() {
fill(255);
rect(this.x, this.y, this.w, this.h);
}
update() {
if (keyIsDown(LEFT_ARROW) && this.x > 0) this.x -= this.speed;
if (keyIsDown(RIGHT_ARROW) && this.x < width - this.w) this.x += this.speed;
}
}
class Ball {
constructor() {
this.r = 8; // Smaller ball
this.x = width / 2;
this.y = height / 2;
this.xSpeed = 3;
this.ySpeed = 3;
}
show() {
fill(255);
ellipse(this.x, this.y, this.r * 2);
}
update() {
this.x += this.xSpeed;
this.y += this.ySpeed;
if (this.x < 0 || this.x > width) this.xSpeed *= -1;
if (this.y < 0) this.ySpeed *= -1;
if (
this.y + this.r > paddle.y &&
this.x > paddle.x &&
this.x < paddle.x + paddle.w
) {
this.reverseY();
}
}
reverseY() {
this.ySpeed *= -1;
}
reset() {
this.x = width / 2;
this.y = height / 2;
this.xSpeed = 3;
this.ySpeed = 3;
}
outOfBounds() {
return this.y - this.r > height;
}
hits(brick) {
if (
this.x + this.r >= brick.x &&
this.x - this.r <= brick.x + brick.w &&
this.y + this.r >= brick.y &&
this.y - this.r <= brick.y + brick.h &&
!brick.destroyed
) {
return true;
}
return false;
}
}
class Brick {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.destroyed = false;
}
show() {
if (!this.destroyed) {
fill(255, 150, 0);
rect(this.x, this.y, this.w, this.h);
}
}
}