Augenzahlen würfeln

Erläuterungen

Lernziele

Programmablauf

Übung

Änderung von Farbe und Größe der Punkte

Quelltext

<!DOCTYPE html><html><body onload="Wuerfeln()">
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas><input type="button" onclick="Wuerfeln()" value="W&uuml;rfeln"><script>var canvas = document.getElementById("myCanvas");var ctx = canvas.getContext("2d");
class Punkt{  constructor(Radius){  this.Radius=Radius;  }    zeichnen(x,y){    ctx.beginPath();    ctx.arc(x, y, this.Radius, 0, 2 * Math.PI);    ctx.fillStyle="black";    ctx.fill();    ctx.stroke();  }  Farbe(){    ctx.fillStyle="black";  }}class Zeile{  constructor(Anzahl){    this.Punkt=[];    this.Anzahl=Anzahl;  }}
class Augenzahl{  constructor(){  this.Zeile=[];  }  zeichnen(){        var Zahl=Math.floor(Math.random()*6)+1;    for (var j=1;j<=3;j=j+1){      this.Zeile[j]=new Zeile();      for (var i=1;i<=3;i=i+1){                    this.Zeile[j].Punkt[i]=new Punkt(20);       if (Zahl==6){          if (j==1 || j==3) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }        if (Zahl==5){          if ((j==1 && i==1) || (j==1 && i==3) || (j==3 && i==1) || (j==3 && i==3) || (j==2 && i==2)) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }        if (Zahl==4){          if ((j==1 && i==1) || (j==1 && i==3) || (j==3 && i==1) || (j==3 && i==3)) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }        if (Zahl==3){          if ((j==1 && i==1) || (j==2 && i==2) || (j==3 && i==3)) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }        if (Zahl==2){          if ((j==1 && i==1) || (j==3 && i==3)) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }        if (Zahl==1){          if (j==2 && i==2) {               this.Zeile[j].Punkt[i].zeichnen(100*i,100*j);          }         }      }       }  }}for (var n=1;n<=3;n=n+1){  Zeile[n]=new Zeile(3);}function Wuerfeln(){       var Augenzahl1=new Augenzahl();  ctx.clearRect(0,0,canvas.width,canvas.height);     Augenzahl1.zeichnen();  }




</script> 
</body></html>

Programm