Skip to content

Commit

Permalink
RockPaperScissors (#2689)
Browse files Browse the repository at this point in the history
* Add files via upload

* Delete [BEAT ME!] Rock .js

* Add files via upload

* Delete games/[BEAT ME!] Rock .js

* RockPaperScissors

* Fix `addedOn`

* Fix tags

---------

Co-authored-by: Gus Ruben <95830851+gusruben@users.noreply.github.com>
  • Loading branch information
AndreaIvanov and gusruben authored Dec 17, 2024
1 parent 59c656c commit 331ab43
Showing 1 changed file with 196 additions and 0 deletions.
196 changes: 196 additions & 0 deletions games/RockPaperScissors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
@title: [BEAT ME!] Rock & Paper & Scissors
@author: Andrea Ivanov
@tags: []
@addedOn: 2024-12-06
*/

const player = 'p';
const rock = 'r';
const paper = 'a';
const scissors = 's';
const door = 'd';


setLegend(
[player, bitmap`
................
................
.....00000......
....0.....0.....
....0.0.0.0.....
....0.....0.....
....0.000.0.....
....0.....0.....
.....00000......
.......0........
.....00000......
.......0........
.......0........
......0.0.......
.....0...0......
................`],
[door, bitmap`
................
......8888......
.....888888.....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
....88888888....
................`]
);

setMap(map`
.p............d`);


let gameRunning = true;
let playerChoice = null;
let botChoice = null;
let choices = ['r', 'a', 's'];
let score = 0;
let streak = 0;
let level = 1;


onInput('w', () => makeChoice('r'));
onInput('s', () => makeChoice('a'));
onInput('d', () => makeChoice('s'));


function makeChoice(choice) {
if (!gameRunning) return;
gameRunning = false;
playerChoice = choice;
botChoice = choices[Math.floor(Math.random() * 3)];
resolveGame();
}


function resolveGame() {
clearText();
clearText();
if (!playerChoice || !botChoice) return;

let result = "";
let playerSprite = getFirst(player);

if (playerChoice === botChoice) {
result = ("Tie! -_-");
updateProvocativeText('tie');

resultColor = color`6`;
} else if (
(playerChoice === 'r' && botChoice === 's') ||
(playerChoice === 'a' && botChoice === 'r') ||
(playerChoice === 's' && botChoice === 'a')
) {
result = "You won >:( !";
updateProvocativeText('win');
resultColor = color`7`;
score++;
streak++;
playerSprite.x += 1;
if (playerSprite.x === 14) {
playerSprite.remove();
addSprite(1, 0, player);
}
} else {
result = "You lost AHAHA!";
updateProvocativeText('lose');
resultColor = color`3`;
playerSprite.x = 1;
streak = 0;

}


if (playerSprite.x === 14) {
level++;
playerSprite.x = 1;
addText("Wait? you are creazy!", { x: 0, y: 6, color: color`6` });
score = 0;
streak = 0;
updateText(`Level ${level}!`, color`6`);
setTimeout(() => {
displayInstructions();
gameRunning = true;
}, 2500);
return;
}

updateText(`You chose ${getChoiceName(playerChoice)}. Bot chose ${getChoiceName(botChoice)}.`, resultColor);
addText(result, { x: 5, y: 6, color: resultColor });
setTimeout(() => {
clearText();
displayInstructions();
gameRunning = true;
}, 2500);
resetGame();
}


function getChoiceName(choice) {
if (choice === 'r') return 'Rock';
if (choice === 'a') return 'Paper';
if (choice === 's') return 'Scissors';
}

function resetGame() {

playerChoice = null;
botChoice = null;
setTimeout(() => {
displayInstructions();
}, 2500);
}

function updateProvocativeText(outcome) {
const texts = {
win: ["Not bad...!", "Luck!", "Impossible!"],
lose: ["Pathetic!", "You call that skill?", "Try harder!"],
tie: ["That's all?", "A tie? Seriously?", "NO SKILL"]
};

const randomText = texts[outcome][Math.floor(Math.random() * texts[outcome].length)];
addText(randomText, { x: 0, y: 14, color: color`1` });
}


function updateText(message, colorCode) {

const lines = message.split('. ');
lines.forEach((line, index) => {
addText(line, { x: 0, y: index, color: colorCode });
});

addText(`Score: ${score}`, { x: 0, y: 10, color: color`2` });
addText(`Streak: ${streak}`, { x: 0, y: 11, color: color`2` });
addText(`Level: ${level}`, { x: 0, y: 12, color: color`2` });
}

function displayInstructions(resetColor = false) {
addText('Press W for Rock', { x: 0, y: 0, color: color`3` });
addText('Press S for Paper', { x: 0, y: 1, color: color`4` });
addText('Press D for Scissors', { x: 0, y: 2, color: color`5` });
addText("Credits : A.Ivanov", { x: 1, y: 15, color: color`L` });
addText("Ready to lose?", { x: 0, y: 6, color: color`3` });
addText("!", { x: 19, y: 6, color: color`6` });
const scoreColor = color`2`;
addText(`Score: ${score}`, { x: 0, y: 10, color: scoreColor });
addText(`Streak: ${streak}`, { x: 0, y: 11, color: scoreColor });
addText(`Level: ${level}`, { x: 0, y: 12, color: scoreColor });
}

clearText();
displayInstructions();

0 comments on commit 331ab43

Please sign in to comment.