资源描述:一款我们经常玩的猜手势小游戏。你可以和电脑进行比赛,当你出石头、剪刀、布时,电脑方会即时随机出一个手势,然后跟你出的进行比对出胜负。每一局比赛结果有:胜方、败方、平局。赶快去试一下吧,还是挺有乐趣的!
使用方法
let uScore = 0;
let cS = 0;
const rock = document.querySelector(".rock");
const scissors = document.querySelector(".scissors");
const paper = document.querySelector(".paper");
const userScore = document.querySelector(".user-score");
const computerScore = document.querySelector(".computer-score");
let statusLeft = document.querySelector(".user-status");
let statusRight = document.querySelector(".computer-status");
let result = document.querySelector(".result-para")
function win(userChoice, computerChoice) {
uScore ++;
statusLeft.innerHTML = convertToWord(userChoice);
statusRight.innerHTML = convertToWord(computerChoice);
userScore.innerHTML = uScore;
result.innerHTML = "你赢了!";
}
function loose(userChoice, computerChoice) {
cS++;
statusLeft.innerHTML = convertToWord(computerChoice);
statusRight.innerHTML = convertToWord(userChoice);
computerScore.innerHTML = cS;
result.innerHTML = "你输了 :-(";
}
function draw(userChoice, computerChoice) {
result.innerHTML = "wow! 平局了~ ";
}
function getComputerChoice() {
const choices = ['r','s','p'];
const choicesRandom = Math.floor(Math.random() * 3);
return choices[choicesRandom];
}
function game(userChoice) {
const computerChoice = getComputerChoice();
switch(userChoice + computerChoice) {
case 'rs':
case 'sp':
case 'pr':
win(userChoice, computerChoice);
break;
case 'ps':
case 'sr':
case 'rp':
loose(userChoice, computerChoice);
break;
case 'pp':
case 'ss':
case 'rr':
draw(userChoice, computerChoice);
}
}
function main() {
rock.addEventListener("click", function(){
game("r");
})
scissors.addEventListener("click", function(){
game("s");
})
paper.addEventListener("click", function(){
game("p");
})
}
main();
function convertToWord(letter) {
if(letter === "r") return "Rock";
if(letter === "p") return "Paper";
return "Scissors";
}
站长提示:
1. 苦力吧素材官方QQ群:
950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励
2K币。
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服