Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2010-10-12T07:16:24+00:00 → 2010-11-23T18:37:46+00:00
<language>Java</language> | |
Auf dieser Seite finden Sie einige Java Bibliotheksfunktionen. Sie können diese in Ihre Wikiseiten einbinden (siehe [[Code referenzieren]]). | |
== Java Hamster == | |
Beispiel für graphische Ausgabe. Einbinden mit <nowiki>[[JavaLib|Hamster]]</nowiki>. | |
<code id="Hamster"> | |
int GridSize = 50; | |
public Object Hamster(Object Spielfeld) { | |
public int x; | |
public int y; | |
public int look = 0; // 0 up, 1 right, 2 down, 3 left | |
int speed = 50; | |
Object Feld= Spielfeld; | |
int inMouth = 0; | |
void wait() { | |
if(speed < 1) speed = 1; | |
if(speed > 100) speed = 100; | |
Thread.sleep(1000-(speed-1)*10); | |
} | |
void vor() { | |
wait(); | |
Feld.drawField(x,y); | |
if(look == 0) | |
if(y > 0) y--; else y = Feld.sizeY-1; | |
if(look == 1) | |
if(x < Feld.sizeX-1) x++; else x = 0; | |
if(look == 2) | |
if(y < Feld.sizeY-1) y++; else y = 0; | |
if(look == 3) | |
if(x > 0) x--; else x = Feld.sizeX-1; | |
draw(); | |
} | |
void linksUm() { | |
wait(); | |
if(look == 0) look = 3; else | |
if(look == 1) look = 0; else | |
if(look == 2) look = 1; else | |
if(look == 3) look = 2; | |
draw(); | |
} | |
void nimm() { | |
wait(); | |
if(Feld.map[x][y] > 0) { | |
Feld.map[x][y]--; | |
inMouth++; | |
draw(); | |
}else alert("Kein Korn hier"); | |
} | |
void gib() { | |
wait(); | |
if(inMouth > 0){ | |
Feld.map[x][y]++; | |
inMouth--; | |
draw(); | |
}else alert("Kein Korn im Mund"); | |
} | |
boolean vornFrei() { | |
if(look == 0) | |
if(y > 0) return Feld.map[x][y-1] != -1; | |
if(look == 1) | |
if(x < Feld.sizeX-1) return Feld.map[x+1][y] != -1; | |
if(look == 2) | |
if(y < Feld.sizeY-1) return Feld.map[x][y+1] != -1; | |
if(look == 3) | |
if(x > 0) return Feld.map[x-1][y] != -1; | |
return false; | |
} | |
boolean kornDa() { | |
return Feld.map[x][y] > 0; | |
} | |
boolean maulLeer() { | |
return inMouth == 0; | |
} | |
void draw(){ | |
Feld.drawField(x,y); | |
Feld.canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/hamster"+look+".png"); | |
} | |
return this; | |
} | |
| − | public Object Spielbrett(Canvas c) { |
| − | public int sizeX = 8; |
| − | public int sizeY = 8; |
| − | public int[][] map= new int[sizeX][sizeY]; |
| − | public Canvas canvas = c; |
| + | public Object Spielbrett(c) { |
| + | this.sizeX = 8; |
| + | this.sizeY = 8; |
| + | this.map= new int[sizeX][sizeY]; |
| + | this.canvas = c; |
void initMap(){ | |
canvas.clear(); | |
for(int y=0; y < sizeY; y++) | |
for(int x=0; x < sizeX; x++) | |
if(x==0 ||y == 0||x == sizeX-1|| y == sizeY-1) map[x][y] = -1; | |
drawMap(); | |
} | |
void placeWall(int x, int y){ | |
if(x>=0 &&y >= 0&&x < sizeX && y < sizeY) { map[x][y] = -1; drawField(x,y); } | |
} | |
void clearWall(int x, int y){ | |
if(x>=0 &&y >= 0&&x < sizeX && y < sizeY) { map[x][y] = 0; drawField(x,y); } | |
} | |
void drawMap(){ | |
for(int y=0; y < sizeY; y++) | |
for(int x=0; x < sizeX; x++) | |
drawField(x,y); | |
} | |
void drawField(int x, int 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(map[x][y] == -1) { | |
canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/brick_wall.png"); | |
} | |
if(map[x][y] > 0) { | |
int image = map[x][y]; | |
if(image > 9) image = 9; | |
canvas.drawImage(x*GridSize,y*GridSize,GridSize,GridSize,"http://michael-hielscher.de/hamster/grain"+image+".png"); | |
} | |
} | |
void putGrain(int x, int y,int count) { | |
map[x][y] = count; | |
drawField(x,y); | |
} | |
return this; | |
} | |
</code> |