Game Development Reference
In-Depth Information
if
(stain.type == Stain.
TYPE_2
)
stainPixmap = Assets.
stain2
;
if
(stain.type == Stain.
TYPE_3
)
stainPixmap = Assets.
stain3
;
int
x = stain.x * 32;
int
y = stain.y * 32;
g.drawPixmap(stainPixmap, x, y);
int
len = snake.parts.size();
for
(
int
i = 1; i < len; i++) {
SnakePart part = snake.parts.get(i);
x = part.x * 32;
y = part.y * 32;
g.drawPixmap(Assets.
tail
, x, y);
}
Pixmap headPixmap =
null
;
if
if(snake.direction == Snake.
UP
)
headPixmap = Assets.
headUp
;
if
if(snake.direction == Snake.
LEFT
)
headPixmap = Assets.
headLeft
;
if
if(snake.direction == Snake.
DOWN
)
headPixmap = Assets.
headDown
;
if
if(snake.direction == Snake.
RIGHT
)
headPixmap = Assets.
headRight
;
x = head.x * 32 + 16;
y = head.y * 32 + 16;
g.drawPixmap(headPixmap, x - headPixmap.getWidth() / 2, y - headPixmap.getHeight() / 2);
}
The
drawWorld()
method draws the world, as we just discussed. It starts off by choosing the
Pixmap
to use for rendering the stain, and then it draws it and centers it horizontally at its screen
position. Next, we render all the tail parts of Mr. Nom, which is pretty simple. Finally, we choose
which
Pixmap
of the head to use, based on Mr. Nom's direction, and draw that
Pixmap
at the
position of the head in the screen coordinates. As with the other objects, we also center the
image around that position. And that's the code of the view in MVC.
private void
drawReadyUI() {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.
ready
, 47, 100);
g.drawLine(0, 416, 480, 416, Color.
BLACK
);
}
private void
drawRunningUI() {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.
buttons
, 0, 0, 64, 128, 64, 64);
g.drawLine(0, 416, 480, 416, Color.
BLACK
);
g.drawPixmap(Assets.
buttons
, 0, 416, 64, 64, 64, 64);
g.drawPixmap(Assets.
buttons
, 256, 416, 0, 64, 64, 64);
}