Jeu de dé 2
by Simon Hi
HTML
<!-- similar to https://dice-game-100.netlify.app/ -->
<button>Nouveau</button>
<label>Joueur 1 <output>0</output></label>
<label>En cours <output>0</output></label>
<img alt="Dé" src='data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"></svg>'>
<button>Lancer</button>
<button>Garder</button>
<label>Joueur 2 <output>0</output></label>
<label>En cours <output>0</output></label>
CSS
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@200&display=swap');
body {
box-sizing: border-box;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 8rem 1fr 1fr;
align-items: center;
justify-content: center;
background: #eee;
min-width: 42rem;
margin: 0;
padding: 1rem;
border: 0.5rem solid #a4733f;
font-family: Montserrat, sans-serif;
font-weight: 200;
min-width: 100vw;
min-height: 100vh;
}
body>* {
box-sizing: border-box;
margin: 0 auto;
width: 13rem;
position: relative;
text-transform: uppercase;
}
label {
text-align: center;
}
output {
display: block;
color: red;
}
label img {
width: 1rem;
height: 1rem;
}
body>img {
grid-column: 2;
grid-row: 2;
width: 8rem;
height: 8rem;
border-radius: 1rem;
background: #fff;
}
button {
border: 0;
background: transparent;
font-size: 1.125rem;
font-family: Montserrat, sans-serif;
font-weight: 200;
transition: font-size 0.5s;
min-height: 2.25rem;
}
button:not([disabled]):focus,
button:not([disabled]):hover {
font-size: 1.5rem;
outline: 0;
}
button:nth-of-type(1) {
grid-row: 1;
grid-column: 2;
}
button:nth-of-type(2) {
grid-row: 3;
grid-column: 2;
}
button:nth-of-type(3) {
grid-row: 4;
grid-column: 2;
}
button::before {
padding-right: 0.25em;
color: red;
display: inline-block;
width: 1em;
height: 1.125em;
line-height: 1em;
vertical-align: middle;
}
button:nth-of-type(1)::before {
content: url('data:image/svg+xml;utf8,<svg stroke="red" stroke-width="12" fill="none" viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg"><path d="M32 64H96M64 32V96"/><circle cx="64" cy="64" r="58"/></svg>');
}
button:nth-of-type(2)::before {
content: url('data:image/svg+xml;utf8,<svg stroke="red" stroke-width="12" fill="none" viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg"><path d="M104 32A52 52 0 1 0 104 96"/><path fill="red" d="M80 39L106 11L113...
JavaScript
(function() {
let players = [{}, {}],
trait, winner, winningScore = 100;
function setDice(n) {
let img = document.querySelector('body>img');
let s = 'data:image/svg+xml;utf8,<svg';
if (n) s += ' fill="red" width="128" height="128" viewBox="0 0 128 128"';
s += ' xmlns="http://www.w3.org/2000/svg">';
if (n) {
if (n & 1) s += '<circle cx="64" cy="64" r="8"/>';
if (n > 1) s += '<circle cx="32" cy="32" r="8"/><circle cx="96" cy="96" r="8"/>';
if (n > 3) s += '<circle cx="32" cy="96" r="8"/><circle cx="96" cy="32" r="8"/>';
if (n == 6) s += '<circle cx="32" cy="64" r="8"/><circle cx="96" cy="64" r="8"/>';
}
s += '</svg>';
img.src = s;
s = "Dé";
if (n) s += " " + n;
img.alt = s;
}
function changeTrait() {
trait = trait ? 0 : 1;
}
function draw() {
let J1T = document.querySelector('label:nth-of-type(1)');
let J1C = document.querySelector('label:nth-of-type(2)');
let J2T = document.querySelector('label:nth-of-type(3)');
let J2C = document.querySelector('label:nth-of-type(4)');
let B2 = document.querySelector('button:nth-of-type(2)');
let B3 = document.querySelector('button:nth-of-type(3)');
let dTrait = 'data:image/svg+xml;utf8,<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg"><circle fill="red" cx="64" cy="64" r="32"/></svg>';
let dWin = 'data:image/svg+xml;utf8,<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg"><circle fill="green" cx="64" cy="64" r="32"/></svg>';
J1T.querySelector('output').innerHTML = players[0].total;
J1C.querySelector('output').innerHTML = players[0].current;
J2T.querySelector('output').innerHTML = players[1].total;
J2C.querySelector('output').innerHTML = players[1].current;
if (winner < 0) {
J1T.classList.remove('win');
J2T.classList.remove('win');
if (trait) {
J1T.innerHTML = 'Joueur 1 ' + J1T.querySelector('output').outerHTML;
J2T.innerHTML = 'Joueur...