My take on this game [RPS in JS]

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

Hi, everyone! I just finished my take on this game. Below you can find the html code and the JavaScript code for the game. I would love if someone had a read through and found something that was badly written or redundant.

Thanks!
<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

``` HTML Game

Javascript named it script.js

var canvas = document.getElementById(ā€œcanvasā€);
var context = canvas.getContext(ā€œ2dā€);
var fps = 30;
var winText = " ";
var computerChoice = " ā€œ;
var userChoice =ā€ ";
var computerText;
var playerText;
var rectRock = {
x: 20,
y: 10,
width: 60,
heigth: 20
};
var rectPaper = {
x: 20,
y: 50,
width: 60,
heigth: 20
};
var rectScissors = {
x: 20,
y: 90,
width: 60,
heigth: 20
};

canvas.onselectstart = function () { return false; }

function capitalizeFirstLetter(string) {
ā€œuse strictā€;
return string.charAt(0).toUpperCase() + string.slice(1);
}

function rockBoxText() {
ā€œuse strictā€;
context.fillStyle = ā€œblueā€;
context.font = ā€œ30px Garamondā€;
context.fillText(ā€œRockā€, 100, 30);
context.fillRect(20, 10, 60, 20);
}

function paperBoxText() {
ā€œuse strictā€;
context.fillStyle = ā€œredā€;
context.font = ā€œ30px Garamondā€;
context.fillText(ā€œPaperā€, 100, 70);
context.fillRect(20, 50, 60, 20);
}

function scissorsBoxText() {
ā€œuse strictā€;
context.fillStyle = ā€œgreenā€;
context.font = ā€œ30px Garamondā€;
context.fillText(ā€œScissorsā€, 100, 110);
context.fillRect(20, 90, 60, 20);
}

//Function to check whether a point is inside a rectangle
function isInside(pos, rect) {
ā€œuse strictā€;
return pos.x > rect.x && pos.x < rect.x + rect.width && pos.y < rect.y + rect.heigth && pos.y > rect.y;
}
//Function to get the mouse position
function getMousePos(canvas, event) {
ā€œuse strictā€;
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}

//Function that checks if the player won or the computer.
function compare (choice1, choice2) {
ā€œuse strictā€;
if (choice1 === choice2) {
winText =ā€œIt’s a tie!ā€;

} else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        winText = "You win!"

    } else {
        winText = "You lose!";
    }
} else if (choice1 === "paper") {
    if (choice2 === "rock") {
        winText = "You win!";
    } else {
        winText = "You lose!";
    }
} else if (choice1 === "scissors") {
    if (choice2 === "rock") {
        winText = "You lose!";
    } else {
        winText = "You win!";
    }
}

};

function generateComChoice () {
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = ā€œrockā€;
} else if(computerChoice <= 0.67) {
computerChoice = ā€œpaperā€;
} else {
computerChoice = ā€œscissorsā€;
}
};

function comChoice(){

if (canvas.addEventListener) {
	canvas.addEventListener ("click", function (evt) {
     var mousePos = getMousePos(canvas, evt);
if (isInside(mousePos, rectRock)) {
    generateComChoice();
} else if (isInside(mousePos, rectPaper)) {
    generateComChoice();
} else if (isInside(mousePos, rectScissors)) {
    generateComChoice();
} else {
}
    }, false);
}

};

function playerChoice (){
ā€œuse strictā€;
if (canvas.addEventListener) {
canvas.addEventListener(ā€˜click’, function (evt) {
var mousePos = getMousePos(canvas, evt);
if (isInside(mousePos, rectRock)) {
userChoice = ā€œrockā€;
} else if (isInside(mousePos, rectPaper)) {
userChoice = ā€œpaperā€;
} else if (isInside(mousePos, rectScissors)) {
userChoice = ā€œscissorsā€;
} else {
}

}, false);

}

};

function checkIfWin (){
if (userChoice === ā€œrockā€ || userChoice === ā€œpaperā€ || userChoice === ā€œscissorsā€) {
compare(userChoice, computerChoice);
} else {}
};

function update (){
comChoice();
playerChoice();
checkIfWin();

};

function render (){
context.clearRect(0, 0, canvas.width, canvas.height);
rockBoxText();
paperBoxText();
scissorsBoxText();
context.font = ā€œ15px Georgiaā€;
if (computerChoice === ā€œrockā€) context.fillStyle = ā€œblueā€;
else if (computerChoice === ā€œpaperā€) context.fillStyle = ā€œredā€;
else if (computerChoice === ā€œscissorsā€) context.fillStyle = ā€œgreenā€;
else context.fillStyle =ā€œwhiteā€;
computerText = context.fillText("Computer Choice: " + capitalizeFirstLetter(computerChoice.toString()), 10, 195);
if (userChoice === ā€œrockā€) context.fillStyle = ā€œblueā€;
else if (userChoice === ā€œpaperā€) context.fillStyle = ā€œredā€;
else if (userChoice === ā€œscissorsā€) context.fillStyle = ā€œgreenā€;
else context.fillStyle =ā€œwhiteā€;
playerText = context.fillText("Your choice: " + capitalizeFirstLetter(userChoice.toString()), 10, 175);
context.fillStyle = ā€œblackā€;
context.fillText(winText, 10, 150);
};

function game (){
update();
render();
};

setInterval (function() {
game();
}, 1000/fps);

<do not remove the three backticks above>
2 Likes

This is really cool.

I think this is the first time I’ve seen someone take one of the games/exercises from here and put it into an html doc.

You have a few unnecessary semicolons, but other than that I think it looks good.

Good job! I’m sure you’ll go far if you keep up the ambition and skills.

Here’s an idea, though. Instead of colored boxes, make it a picture or whatever of the item.

super cool :slight_smile:

i put the code in a bin: http://jsbin.com/yajixetanu/edit?html,js,output

this way, anyway can super simply run the code and see and modify it if they have suggestions :slight_smile: I will look over it later, why did you use canvas? Just curious

Thanks for the response and the nice words!

I’m going to polish it soon and remove what’s unnecessary, thanks!

That’s a great idea, I think I will try that next now :slight_smile:

Please post the finished one.

If you were so inclined, you could use github pages to host your game and/or code.

Thanks for the response!

Thanks for putting it in a bin!

Canvas was the first thing that I came across when searching the web, what’s the better alternative?

My last critique would be that it only runs the script when the box is hit.

You should make it to where it runs even if the text were hit, for simplicity and more flexibility.

I will! I’ve created a webpage where I’m going to have all my projects posted and this one you can find on www.gamesquestionmark.com. under Project I.

1 Like

I can implement that as well, thanks for the input!

1 Like

i would personally simply use ` to let the user choice and textContent to insert the result into paragraph elements.

I’ll have to look up textContent. I’m unsure if your text was supposed to show only the apostrophe. Thanks a lot!

Script now runs when text is hit: http://jsbin.com/zimusulefa/edit?html,js,output

Your input helps a lot!

1 Like

I have now added sprites to the game instead of boxes. Thanks again for the input!
http://gamesquestionmark.com/ProjectI/ProjectI.html

var img = new Image();
img.src = "sprites/size2.png";
var img2 = new Image();
img2.src = "sprites/paper2.png";
var img3 = new Image();
img3.src = "sprites/scissors2.png";

var rockSprite = {
    spriteWidth: 200,
    spriteHeight: 200,
    pixelsLeft: 0,
    pixelsTop: 0,


    canvasPosX: 35,
    canvasPosY: 5
};

var paperSprite = {
    spriteWidth:  200,
    spriteHeight: 200,
    pixelsLeft: 0,
    pixelsTop: 0,

    canvasPosX: 35,
    canvasPosY: 45
};

var scissorsSprite = {
    spriteWidth:  200,
    spriteHeight: 200,
    pixelsLeft: 0,
    pixelsTop: 0,

    canvasPosX: 35,
    canvasPosY: 90
};


function addSprite (type, source) {
context.drawImage(source,
    type.pixelsLeft,
    type.pixelsTop,
    type.spriteWidth,
    type.spriteHeight,
    type.canvasPosX,
    type.canvasPosY,
    type.spriteWidth,
    type.spriteHeight);
};
function render (){
    addSprite(rockSprite, img);
    addSprite(paperSprite, img2);
    addSprite(scissorsSprite, img3);
};

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.