GameScreen
public void render () {
spriteBatch.begin();
// draw(Art.bg, -xLevel * 160, -yLevel * 120);
draw(Art.bg, 0, 0);
spriteBatch.end();
level.render(this, camera);
spriteBatch.begin();
if (mayRespawn) {
String msg = "PRESS X TO TRY AGAIN";
drawString(msg, 160 - msg.length() * 3, 120 - 3);
}
if (Gdx.app.getType() == ApplicationType.Android) {
draw(Art.buttons[0][0], 0, 240 - 32);
draw(Art.buttons[1][0], 32, 240 - 32);
draw(Art.buttons[4][0], 160 - 32, 240 - 32);
draw(Art.buttons[5][0], 160, 240 - 32);
draw(Art.buttons[2][0], 320 - 64, 240 - 32);
draw(Art.buttons[3][0], 320 - 32, 240 - 32);
}
spriteBatch.end();
}
//绘制纹理,完全可以认为绘制一个图片,默认情况
public void draw (TextureRegion region, int x, int y) {
int width = region.getRegionWidth();
if (width < 0) width = -width;
spriteBatch.draw(region, x, y, width, -region.getRegionHeight());
//高度是负数
//final float fx2 = x + width;
//final float fy2 = y + height;
}
//绘制一个字符串
public void drawString (String string, int x, int y) {
string = string.toUpperCase();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
for (int ys = 0; ys < chars.length; ys++) {
int xs = chars[ys].indexOf(ch);
if (xs >= 0) {
draw(Art.guys[xs][ys + 9], x + i * 6, y);
}
}
}
}
//没什么奇怪的,就是用图片来代替文字
public static void load () {
bg = load("res/background.png", 320, 240);
level = new Pixmap(Gdx.files.internal("res/levels.png"));
titleScreen = load("res/titlescreen.png", 320, 740);
guys = split("res/guys.png", 6, 6);
player1 = split("res/player.png", 16, 32);
player2 = split("res/player.png", 16, 32, true);
walls = split("res/walls.png", 10, 10);
gremlins = split("res/gremlins.png", 30, 30);
buttons = split("res/buttons.png", 32, 32);
shot = new TextureRegion(guys[0][0].getTexture(), 3, 27, 2, 2);
winScreen1 = load("res/winscreen1.png", 320, 240);
winScreen2 = load("res/winscreen2.png", 320, 240);
}
guys = split("res/guys.png", 6, 6);
private static TextureRegion[][] split (String name, int width, int height, boolean flipX) {
Texture texture = new Texture(Gdx.files.internal(name));
int xSlices = texture.getWidth() / width;
int ySlices = texture.getHeight() / height;
TextureRegion[][] res = new TextureRegion[xSlices][ySlices];
for (int x = 0; x < xSlices; x++) {
for (int y = 0; y < ySlices; y++) {
res[x][y] = new TextureRegion(texture, x * width, y * height, width, height);
res[x][y].flip(flipX, true);
}
}
return res;
}
//每6个像素一切,分成若干块, 注意这里再次强调TexttureRegion是通过像素来切分,和opengl的u,v分法不同,而且绘制也是按像素