Done with the rendering never toucnhing it again.
This commit is contained in:
@@ -10,21 +10,35 @@ import tester.Tester;
|
|||||||
|
|
||||||
class ImgUtil {
|
class ImgUtil {
|
||||||
static Color color(int n) {
|
static Color color(int n) {
|
||||||
int hash = Integer.hashCode(n);
|
Double hh = Math.log(n) / Math.log(2);
|
||||||
|
int hash = Integer.hashCode(hh.intValue() << 5);
|
||||||
int r, g, b;
|
int r, g, b;
|
||||||
|
|
||||||
// Shift the hash so the least significant byte is the part you want,
|
// Shift the hash so the least significant byte is the part you want,
|
||||||
// and mask to isolate that byte.
|
// and mask to isolate that byte. Also bias towards yellow a bit (heh).
|
||||||
r = (hash >> 16) & 0xff;
|
r = ((hash >> 16) & 0xff) | 0xa0; // Boost red by setting the high bit.
|
||||||
g = (hash >> 8) & 0xff;
|
g = ((hash >> 8) & 0xff) | 0x90; // Boost green by setting the high bit.
|
||||||
b = (hash) & 0xff;
|
b = (hash & 0xff) & 0x7f; // Reduce blue by clearing the high bit.
|
||||||
|
|
||||||
return new Color(r, g, b);
|
return new Color(r, g + 20, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a tile image for a number.
|
||||||
static WorldImage mktile(int n) {
|
static WorldImage mktile(int n) {
|
||||||
|
return new OverlayImage(
|
||||||
|
new TextImage(
|
||||||
|
String.valueOf(n), Game.SCALE / 2, FontStyle.BOLD, Color.BLACK
|
||||||
|
),
|
||||||
|
new RectangleImage(
|
||||||
|
Game.SCALE, Game.SCALE, OutlineMode.SOLID, ImgUtil.color(n)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a fee space image.
|
||||||
|
static WorldImage mkfree() {
|
||||||
return new RectangleImage(
|
return new RectangleImage(
|
||||||
Game.SCALE, Game.SCALE, OutlineMode.SOLID, ImgUtil.color(n)
|
Game.SCALE, Game.SCALE, OutlineMode.OUTLINE, Color.GRAY
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,9 +86,7 @@ class Game extends World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw current game state.
|
// Draw current game state.
|
||||||
void draw() {
|
void draw() { this.scene = this.grid.render(); }
|
||||||
this.scene.placeImageXY(this.grid.render(), this.w / 2, this.h / 2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The grid in which the game is played.
|
// The grid in which the game is played.
|
||||||
@@ -125,10 +137,20 @@ class Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render the grid.
|
// Render the grid.
|
||||||
WorldImage render() {
|
WorldScene render() {
|
||||||
WorldImage bg = ImgUtil.mktile(this.buf.get(0));
|
WorldScene bg =
|
||||||
for (int i = 0; i < this.h) {
|
new WorldScene(Game.SCALE * this.w, Game.SCALE * this.h);
|
||||||
for (int j = 0; j < this.w) {}
|
for (int i = 0; i < this.h; i++) {
|
||||||
|
for (int j = 0; j < this.w; j++) {
|
||||||
|
int n = this.get(i, j);
|
||||||
|
int coordx = i * Game.SCALE + Game.SCALE / 2;
|
||||||
|
int coordy = j * Game.SCALE + Game.SCALE / 2;
|
||||||
|
if (n == 0) {
|
||||||
|
bg.placeImageXY(ImgUtil.mkfree(), coordx, coordy);
|
||||||
|
} else {
|
||||||
|
bg.placeImageXY(ImgUtil.mktile(n), coordx, coordy);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bg;
|
return bg;
|
||||||
}
|
}
|
||||||
@@ -203,6 +225,23 @@ class Examples {
|
|||||||
|
|
||||||
void testGame(Tester t) {
|
void testGame(Tester t) {
|
||||||
game = new Game();
|
game = new Game();
|
||||||
|
game.grid.set(0, 0, 2);
|
||||||
|
game.grid.set(0, 1, 4);
|
||||||
|
// Random free space.
|
||||||
|
game.grid.set(0, 3, 8);
|
||||||
|
game.grid.set(1, 0, 16);
|
||||||
|
game.grid.set(1, 1, 32);
|
||||||
|
game.grid.set(1, 2, 64);
|
||||||
|
game.grid.set(1, 3, 128);
|
||||||
|
game.grid.set(2, 0, 256);
|
||||||
|
game.grid.set(2, 1, 512);
|
||||||
|
game.grid.set(2, 2, 1024);
|
||||||
|
game.grid.set(2, 3, 2048);
|
||||||
|
game.grid.set(3, 0, 4096);
|
||||||
|
// Random free space.
|
||||||
|
game.grid.set(3, 2, 8192);
|
||||||
|
game.grid.set(3, 3, 16384);
|
||||||
|
|
||||||
game.launchGame();
|
game.launchGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user