我有这个骰子滚动脚本,我似乎无法让脚本显示玩家骰子的正确值,银行骰子似乎显示正确的值,这个是必不可少的,因为要使游戏工作需要比较玩家骰子和银行骰子的输出,我在if / else函数中放置了window.alert来查看speleruitkomst是否有效。
VAR LIST:
speleruitkomst = players dice
bankuitkomst = banks dice
Rekening = how much the player has in his bank
Inzet = how much a user bets on the game.
游戏名为高/低
的index.html
<!DOCTYPE html >
<html>
<head>
<title>Hoger Lager</title>
<script type="text/javascript" src="js/dobbelsteen.js"></script>
<script type="text/javascript" src="js/canvas.js"></script>
<link rel="stylesheet" type="text/css" href="css/canvas.css">
</head>
<body>
<canvas id='bank' width="600" height="500"></canvas>
<canvas id='speler' width="600" height="500"></canvas>
<body onload="beginSpel()">
<div id="wrapper">
<article>
<button id="Hoger" onClick="Hoger()">Hoger</button>
<button id="Lager" onClick="Lager()">Lager</button>
<button id="verhoogInzet" onClick="verhoogInzet()">+</button>
<button id="verlaagInzet" onClick="verlaagInzet()">-</button>
<button id="reset" onClick="resetSpel()">Reset</button>
</article>
<br></div>
</body>
</html>
dobbelsteen.js
/*function tekensteen(x)
{
steennummer = x;
beeldsteenaf();
}*/
function tekenbank()
{
canvas = document.getElementById("bank");
pen = canvas.getContext('2d');
pen.clearRect(0,0, canvas.width, canvas.height);
nummer=1;
gooiDobbelsteen();
tekenDobbelsteen(nummer, ogen);
bankuitkomst = ogen;
nummer=2;
gooiDobbelsteen();
bankuitkomst=bankuitkomst+ogen;
tekenDobbelsteen(nummer, ogen);
}
function tekenspeler()
{
canvas = document.getElementById("speler");
pen = canvas.getContext('2d');
pen.clearRect(0,0, canvas.width, canvas.height);
nummer=3;
gooiDobbelsteen();
tekenDobbelsteen(nummer, ogen);
var speleruitkomst = ogen;
nummer=4;
gooiDobbelsteen();
speleruitkomst = bankuitkomst+ogen;
tekenDobbelsteen(nummer, ogen);
}
function tekenVierkant()
{
pen.beginPath();
pen.lineWidth=8;
pen.rect(xoff + 0, yoff + 0,240,240);
pen.strokeStyle="Khaki";
pen.stroke();
}
function tekenOog1()
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 60,yoff + 60,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog2()
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 60,yoff + 120,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog3(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 60,yoff + 180,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog4(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 120,yoff + 60,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog5(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 120,yoff + 120,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog6(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 120,yoff + 180,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog7(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 180,yoff + 60,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog8(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 180,yoff + 120,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
function tekenOog9(pos)
{
pen.beginPath();
pen.strokeStyle="Maroon";
pen.arc(xoff + 180,yoff + 180,20,0,2*Math.PI);
pen.stroke();
pen.fillStyle="Maroon";
pen.fill();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function wisTekening()
{
var canvas = document.getElementById('figuur1');
var pen = canvas.getContext('2d');
pen.clearRect(0,0, canvas.width, canvas.height);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function waarde1()
{
tekenVierkant();
tekenOog5();
}
function waarde2()
{
tekenVierkant();
tekenOog1();
tekenOog9();
}
function waarde3()
{
tekenVierkant();
tekenOog1();
tekenOog5();
tekenOog9();
}
function waarde4()
{
tekenVierkant();
tekenOog1();
tekenOog3();
tekenOog7();
tekenOog9();
}
function waarde5()
{
tekenVierkant();
tekenOog1();
tekenOog3();
tekenOog5();
tekenOog7();
tekenOog9();
}
function waarde6()
{
tekenVierkant();
tekenOog1();
tekenOog2();
tekenOog3();
tekenOog7();
tekenOog8();
tekenOog9();
}
function gooiDobbelsteen()
{
ogen = Math.floor((Math.random() * 6) + 1);
return ogen;
}
function tekenDobbelsteen(nummer, ogen)
{
switch (nummer){
case 1:
xoff = 10;
yoff = 60;
break;
case 2:
xoff = 260;
yoff = 60;
break;
case 3:
xoff = 10;
yoff = 60;
break;
case 4:
xoff = 260;
yoff = 60;
break;
}
switch (ogen){
case 1:
waarde1();
break;
case 2:
waarde2();
break;
case 3:
waarde3();
break;
case 4:
waarde4();
break;
case 5: waarde5();
break;
case 6: waarde6();
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
canvas.js
function beginSpel()
{
saldo();
tekenbank();
laadcanvasbank();
laadcanvasspeler();
disableBtn(false);
}
function saldo()
{
speleruitkomst=0;
Rekening=500;
}
function disableBtn(x) {
document.getElementById("Hoger").disabled = x;
document.getElementById("Lager").disabled = x;
document.getElementById("verhoogInzet").disabled = x;
document.getElementById("verlaagInzet").disabled = x;
}
function speler()
{
bah();
}
function laadcanvasbank()
{
canvas = document.getElementById("bank");
pen = canvas.getContext("2d");
showFillText();
}
function showFillText()
{
Inzet = 10;
pen.fillStyle = '#black';
pen.font = 'italic bold 30px sans-serif';
pen.textBaseline = 'bottom';
pen.fillText('Bank:', 10, 40);
}
function laadcanvasspeler()
{
canvas1 = document.getElementById("speler");
pen2 = canvas1.getContext("2d");
Inzetten();
}
function showFillText2()
{
pen2.fillStyle = '#black';
pen2.font = 'italic bold 30px sans-serif';
pen2.textBaseline = 'bottom';
pen2.fillText('Gebruiker:', 10, 40);
}
function Inzetten()
{
showFillText2();
pen2.fillStyle = '#black';
pen2.font = 'italic bold 30px sans-serif';
pen2.textBaseline = 'bottom';
pen2.fillText('Saldo: '+ Rekening, 20, 80);
pen2.fillText('Hoeveel wilt u inzetten: '+ Inzet+ ' punten', 20, 120);
}
function verhoogInzet()
{
Inzet = Inzet+10;
if (Inzet > Rekening)
{
Inzet=Rekening;
}
pen2.clearRect(0, 0, canvas.width, canvas.height);
Inzetten();
}
function verlaagInzet()
{
Inzet = Inzet-10;
if (Inzet < 0)
{
Inzet=0;
}
pen2.clearRect(0, 0, canvas.width, canvas.height);
Inzetten();
}
function Hoger()
{
disableBtn(true);
if (bankuitkomst < speleruitkomst)
{
window.alert("speleruitkomst: " + speleruitkomst + " Bankuitkomst: " + bankuitkomst);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenspeler();
pen2.fillStyle = '#black';
pen2.fillText('U heeft ' + Inzet +' punten gewonnen!', 20, 340);
Rekening=Rekening+Inzet;
pen2.fillText('Uw nieuwe Saldo: '+ Rekening + ' punten', 20, 380);
}
else
{
window.alert("speleruitkomst: " + speleruitkomst + " Bankuitkomst: " + bankuitkomst);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenspeler();
pen2.fillStyle = '#black';
pen2.fillText('U heeft ' + Inzet +' punten verloren!', 20, 340);
Rekening=Rekening-Inzet;
pen2.fillText('Uw nieuwe Saldo: '+ Rekening + ' punten', 20, 380);
}
}
function Lager()
{
disableBtn(true);
if (bankuitkomst > speleruitkomst)
{
window.alert("speleruitkomst: " + speleruitkomst + " Bankuitkomst: " + bankuitkomst);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenspeler();
pen2.fillStyle = '#black';
pen2.fillText('U heeft ' + Inzet +' punten gewonnen!', 20, 340);
Rekening=Rekening+Inzet;
pen2.fillText('Uw nieuwe Saldo: '+ Rekening + ' punten', 20, 380);
}
else
{
window.alert("speleruitkomst: " + speleruitkomst + " Bankuitkomst: " + bankuitkomst);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenspeler();
pen2.fillStyle = '#black';
pen2.fillText('U heeft ' + Inzet +' punten verloren!', 20, 340);
Rekening=Rekening-Inzet;
pen2.fillText('Uw nieuwe Saldo: '+ Rekening + ' punten', 20, 380);
}
}
function resetSpel()
{
if (Rekening === 0)
{
Rekening=500;
disableBtn(false);
pen.clearRect(0, 0, canvas.width, canvas.height);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenbank();
laadcanvasbank();
laadcanvasspeler();
}
else
{
disableBtn(false);
pen.clearRect(0, 0, canvas.width, canvas.height);
pen2.clearRect(0, 0, canvas.width, canvas.height);
tekenbank();
laadcanvasbank();
laadcanvasspeler();
}
}
提前感谢