Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2015-02-20T22:34:47+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) { | |
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; | |
| − | self.waitTimer = 0; |
| − | self.wait = function(f) { |
| − | if(self.speed < 1) self.speed = 1; |
| − | if(self.speed > 100) self.speed = 100; |
| − | this.waitTimer += 1000-(self.speed-1)*10; |
| − | setTimeout(f,this.waitTimer); |
| − | }; |
| − | |
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.wait(function(){ |
| − | self.Feld.drawField(self.x,self.y); |
self.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.wait(function(){ |
self.draw(); | |
| − | }); |
} | |
self.nimm = function() { | |
if(self.Feld.map[self.x][self.y] > 0) { | |
self.Feld.map[self.x][self.y]--; | |
self.inMouth++; | |
| − | self.wait(function(){ |
self.draw(); | |
| − | }); |
}else alert("Kein Korn hier"); | |
} | |
self.gib = function() { | |
if(self.inMouth > 0){ | |
self.Feld.map[self.x][self.y]++; | |
self.inMouth--; | |
| − | self.wait(function(){ |
self.draw(); | |
| − | }); |
}else alert("Kein Korn im Mund"); | |
} | |
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; | |
} | |
self.kornDa = function() { | |
return self.Feld.map[self.x][self.y] > 0; | |
} | |
self.maulLeer = function() { | |
return self.inMouth == 0; | |
} | |
self.draw = function(){ | |
| − | self.Feld.drawField(self.x,self.y); |
| − | self.Feld.canvas.drawImage(self.x*GridSize,self.y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/hamster"+self.look+".png"); |
| + | self.Feld.drawHamsterFeld(self.x,self.y,self.look); |
} | |
return this; | |
} | |
function Spielbrett(c) { | |
var self = this; | |
self.sizeX = 8; | |
self.sizeY = 8; | |
self.map= [[]]; | |
self.canvas = c; | |
self.initMap = function(){ | |
canvas.clear(); | |
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; | |
} | |
} | |
self.drawMap(); | |
} | |
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); } | |
} | |
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); } | |
} | |
self.drawMap = function(){ | |
for(var y=0; y < self.sizeY; y++) | |
for(var x=0; x < self.sizeX; x++) | |
self.drawField(x,y); | |
} | |
| + | 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"); |
| + | }); |
| + | } |
| + | |
self.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(self.map[x][y] == -1) { |
| − | canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/brick_wall.png"); |
| − | } |
| − | if(self.map[x][y] > 0) { |
| − | var image = self.map[x][y]; |
| − | if(image > 9) image = 9; |
| − | canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/grain"+image+".png"); |
| − | } |
| + | 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> |