Versionsvergleich

Aus ProgrammingWiki

Vergleich: 2015-02-20T22:14:49+00:00 → 2015-02-20T23:15:11+00:00

<language>Javascript</language>
Auf dieser Seite finden Sie einige JavaScript Bibliotheksfunktionen. Sie können diese in Ihre Wikiseiten einbinden (siehe [[Code referenzieren]]). 
== JavaScript Hamster ==
Beispiel für graphische Ausgabe. Einbinden mit <nowiki>[[JavaScriptLib|Hamster]]</nowiki>.
<code id="Hamster">
var GridSize = 50;
function Hamster(Spielfeld) {
−
   this.x = 0;
−
   this.y = 0;
−
   this.look = 0; // 0 up, 1 right, 2 down, 3 left
−
   this.speed = 50;
−
   this.Feld = Spielfeld;
−
   this.inMouth = 0;
+
   var self = this;
+
   self.x = 0;
+
   self.y = 0;
+
   self.look = 0; // 0 up, 1 right, 2 down, 3 left
+
   self.speed = 50;
+
   self.Feld = Spielfeld;
+
   self.inMouth = 0;
−
   this.wait = function() {
−
     if(this.speed < 1) this.speed = 1;
−
     if(this.speed > 100) this.speed = 100;
−
     //Thread.sleep(1000-(this.speed-1)*10);
+
   self.vor = function() {
+
     self.Feld.drawField(self.x,self.y);
+
     if(self.look == 0)
+
      if(self.y > 0) self.y--; else self.y = self.Feld.sizeY-1;
+
     if(self.look == 1)
+
      if(self.x < self.Feld.sizeX-1) self.x++; else self.x = 0;
+
     if(self.look == 2)
+
      if(self.y < self.Feld.sizeY-1) self.y++; else self.y = 0;
+
     if(self.look == 3)
+
      if(self.x > 0) self.x--; else self.x = self.Feld.sizeX-1;
+
     self.draw();
   };
−
   this.vor = function() {
−
     this.wait();
−
     this.Feld.drawField(this.x,this.y);
−
     if(this.look == 0)
−
      if(this.y > 0) this.y--; else this.y = this.Feld.sizeY-1;
−
     if(this.look == 1)
−
      if(this.x < this.Feld.sizeX-1) this.x++; else this.x = 0;
−
     if(this.look == 2)
−
      if(this.y < this.Feld.sizeY-1) this.y++; else this.y = 0;
−
     if(this.look == 3)
−
      if(this.x > 0) this.x--; else this.x = this.Feld.sizeX-1;
−
     this.draw();
−
   };
−
−
   this.linksUm = function() {
−
     this.wait();
−
     if(this.look == 0) this.look = 3; else
−
     if(this.look == 1) this.look = 0; else
−
     if(this.look == 2) this.look = 1; else
−
     if(this.look == 3) this.look = 2; 
−
     this.draw();
+
   self.linksUm = function() {
+
     if(self.look == 0) self.look = 3; else
+
     if(self.look == 1) self.look = 0; else
+
     if(self.look == 2) self.look = 1; else
+
     if(self.look == 3) self.look = 2; 
+
     self.draw();
   }
−
   this.nimm = function() {
−
     this.wait();
−
     if(this.Feld.map[this.x][this.y] > 0) {
−
      this.Feld.map[this.x][this.y]--;
−
      this.inMouth++;
−
      this.draw();
+
   self.nimm = function() {
+
     if(self.Feld.map[self.x][self.y] > 0) {
+
      self.Feld.map[self.x][self.y]--;
+
      self.inMouth++;
+
      self.draw();
     }else alert("Kein Korn hier");
   }
−
   this.gib = function() {
−
     this.wait();
−
     if(this.inMouth > 0){
−
      this.Feld.map[this.x][this.y]++;
−
      this.inMouth--;
−
      this.draw();
+
   self.gib = function() {
+
     if(self.inMouth > 0){
+
      self.Feld.map[self.x][self.y]++;
+
      self.inMouth--;
+
      self.draw();
     }else alert("Kein Korn im Mund");
   }
−
   this.vornFrei = function() {
−
     if(this.look == 0)
−
      if(this.y > 0) return this.Feld.map[this.x][this.y-1] != -1;
−
     if(this.look == 1)
−
      if(this.x < this.Feld.sizeX-1) return this.Feld.map[this.x+1][this.y] != -1;
−
     if(this.look == 2)
−
      if(this.y < this.Feld.sizeY-1) return this.Feld.map[this.x][this.y+1] != -1;
−
     if(this.look == 3)
−
      if(this.x > 0) return this.Feld.map[this.x-1][this.y] != -1;
+
   self.vornFrei = function() {
+
     if(self.look == 0)
+
      if(self.y > 0) return self.Feld.map[self.x][self.y-1] != -1;
+
     if(self.look == 1)
+
      if(self.x < self.Feld.sizeX-1) return self.Feld.map[self.x+1][self.y] != -1;
+
     if(self.look == 2)
+
      if(self.y < self.Feld.sizeY-1) return self.Feld.map[self.x][self.y+1] != -1;
+
     if(self.look == 3)
+
      if(self.x > 0) return self.Feld.map[self.x-1][self.y] != -1;
     return false;
   }
−
   this.kornDa = function() {
−
     return this.Feld.map[this.x][this.y] > 0;
+
   self.kornDa = function() {
+
     return self.Feld.map[self.x][self.y] > 0;
   }
−
   this.maulLeer = function() {
−
     return this.inMouth == 0;
+
   self.maulLeer = function() {
+
     return self.inMouth == 0;
   }
−
   this.draw = function(){
−
     this.Feld.drawField(this.x,this.y);
−
     this.Feld.canvas.drawImage(this.x*GridSize,this.y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/hamster"+this.look+".png");
+
   self.draw = function(){
+
     self.Feld.drawHamsterFeld(self.x,self.y,self.look);
   }   
   return this;
}
function Spielbrett(c) {
−
   this.sizeX = 8;
−
   this.sizeY = 8;
−
   this.map= [[]];
−
   this.canvas = c;
−
   this.initMap = function(){
+
   var self = this;
+
   self.sizeX = 8;
+
   self.sizeY = 8;
+
   self.map= [[]];
+
   self.canvas = c;
+
   self.initMap = function(){
     canvas.clear();
−
     this.map = [];
−
     for(var x=0; x < this.sizeX; x++){
−
      this.map.push([]);
−
      for(var y=0; y < this.sizeY; y++){ 
−
       this.map[x].push(0);
−
       if(x==0 ||y == 0||x == sizeX-1|| y == sizeY-1) this.map[x][y] = -1;
+
     self.map = [];
+
     for(var x=0; x < self.sizeX; x++){
+
      self.map.push([]);
+
      for(var y=0; y < self.sizeY; y++){ 
+
       self.map[x].push(0);
+
       if(x==0 ||y == 0||x == self.sizeX-1|| y == self.sizeY-1) self.map[x][y] = -1;
      }
     } 
−
     this.drawMap();
+
     self.drawMap();
   }
−
   this.placeWall = function(x,y){
−
       if(x>=0 && y >= 0 && x < this.sizeX && y < this.sizeY) { this.map[x][y] = -1; this.drawField(x,y); }
+
   self.placeWall = function(x,y){
+
       if(x>=0 && y >= 0 && x < self.sizeX && y < self.sizeY) { self.map[x][y] = -1; self.drawField(x,y); }
   }
−
   this.clearWall = function(x,y){
−
       if(x>=0 && y >= 0 && x < this.sizeX && y < this.sizeY) { this.map[x][y] = 0; this.drawField(x,y); }
+
   self.clearWall = function(x,y){
+
       if(x>=0 && y >= 0 && x < self.sizeX && y < self.sizeY) { self.map[x][y] = 0; self.drawField(x,y); }
  
   }
−
   this.drawMap = function(){
−
     for(var y=0; y < this.sizeY; y++) 
−
      for(var x=0; x < this.sizeX; x++) 
−
       this.drawField(x,y);
+
   self.drawMap = function(){
+
     for(var y=0; y < self.sizeY; y++) 
+
      for(var x=0; x < self.sizeX; x++) 
+
       self.drawField(x,y);
   }
−
   this.drawField = function(x,y) {
−
     canvas.setColor(255,255,255);
−
     canvas.fillRect(x*GridSize,y*GridSize,GridSize,GridSize);
−
     canvas.setColor(0,0,0);
−
     canvas.rect(x*GridSize,y*GridSize,GridSize-1,GridSize-1);
−
     if(this.map[x][y] == -1) {
−
       canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/brick_wall.png");
−
     }
−
     if(this.map[x][y] > 0) {
−
       var image = this.map[x][y];  
−
       if(image > 9) image = 9;
−
       canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/grain"+image+".png");
−
     }
+
   self.drawHamsterFeld = function(x,y,l) {
+
     var n = self.map[x][y];
+
     self.asyncDrawStack.push(function(){
+
      canvas.setRGBColor(255,255,255);
+
      canvas.fillRect(x*GridSize,y*GridSize,GridSize,GridSize);
+
      canvas.setRGBColor(0,0,0);
+
      canvas.rect(x*GridSize,y*GridSize,GridSize-1,GridSize-1);
+
      if(n == -1) {
+
        canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/brick_wall.png");
+
      }
+
      if(n > 0) {
+
       if(n > 9) n = 9;
+
       canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/grain"+n+".png");
+
      }
+
      canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/hamster"+l+".png");
+
     });
   }
−
   this.putGrain = function(x,y,count) {
−
     this.map[x][y] = count;
−
     this.drawField(x,y);
+
+
   self.drawField = function(x,y) {
+
     var n = self.map[x][y];
+
     self.asyncDrawStack.push(function(){
+
      canvas.setRGBColor(255,255,255);
+
      canvas.fillRect(x*GridSize,y*GridSize,GridSize,GridSize);
+
      canvas.setRGBColor(0,0,0);
+
      canvas.rect(x*GridSize,y*GridSize,GridSize-1,GridSize-1);
+
      if(n == -1) {
+
        canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/brick_wall.png");
+
      }
+
      if(n > 0) {
+
       if(n > 9) n = 9;
+
       canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/grain"+n+".png");
+
      }
+
     });
   }
+
   self.putGrain = function(x,y,count) {
+
     self.map[x][y] = count;
+
     self.drawField(x,y);
+
   }
+
   self.asyncDrawStack = [];
+
+
   setInterval(function(){
+
    if(self.asyncDrawStack.length > 0){
+
      var f = self.asyncDrawStack.shift();
+
      f();  
+
    }
+
   },100);
   return this;
}
</code>
<run id="54e7b178d1bbe"></run>
+
<canvas h="500"></canvas>