GameScreen类的主要工作是负责显示几种游戏不同状态下的界面
这个分屏分别是:准备游戏,游戏中,游戏暂停,游戏结束
package com.badlogic.androidgames.mrnom;
import java.util.List;
import android.graphics.Color;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Screen;
//枚举游戏的四种状态,准备游戏,游戏中,游戏暂停,游戏结束
public class GameScreen extends Screen {
enum GameState {
Ready,
Running,
Paused,
GameOver
}
GameState state = GameState.Ready;
World world;
int oldScore = 0;
String score = "0";
public GameScreen(Game game) {
super(game);
world = new World();
}
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
if(state == GameState.Ready)
updateReady(touchEvents);
if(state == GameState.Running)
updateRunning(touchEvents, deltaTime);
if(state == GameState.Paused)
updatePaused(touchEvents);
if(state == GameState.GameOver)
updateGameOver(touchEvents);
}
//在准备游戏状态,触碰屏幕就进入游戏
private void updateReady(List<TouchEvent> touchEvents) {
if(touchEvents.size() > 0)
state = GameState.Running;
}
//游戏中状态
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
//如果碰到暂停按钮
if(event.x < 64 && event.y < 64) {
if(Settings.soundEnabled)
Assets.click.play(1);
state = GameState.Paused;
return;
}
}
if(event.type == TouchEvent.TOUCH_DOWN) {
//如果碰到逆时钟转向按钮
if(event.x < 64 && event.y > 416) {
world.snake.turnLeft();
}
//如果碰到顺时钟转向按钮
if(event.x > 256 && event.y > 416) {
world.snake.turnRight();
}
}
}
world.update(deltaTime);
//游戏结束
if(world.gameOver) {
if(Settings.soundEnabled)
Assets.bitten.play(1);
state = GameState.GameOver;
}
//累加游戏得分,并播放吃中目标的音效
if(oldScore != world.score) {
oldScore = world.score;
score = "" + oldScore;
if(Settings.soundEnabled)
Assets.eat.play(1);
}
}
private void updatePaused(List<TouchEvent> touchEvents) {
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
if(event.x > 80 && event.x <= 240) {
//如果选择恢复游戏
if(event.y > 100 && event.y <= 148) {
if(Settings.soundEnabled)
Assets.click.play(1);
state = GameState.Running;
return;
}
//如果选择退出
if(event.y > 148 && event.y < 196) {
if(Settings.soundEnabled)
Assets.click.play(1);
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
}
}
private void updateGameOver(List<TouchEvent> touchEvents) {
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
//如果选择退出
if(event.x >= 128 && event.x <= 192 &&
event.y >= 200 && event.y <= 264) {
if(Settings.soundEnabled)
Assets.click.play(1);
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
}
//初始化游戏背景等
@Override
public void present(float deltaTime) {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.background, 0, 0);
drawWorld(world);
if(state == GameState.Ready)
drawReadyUI();
if(state == GameState.Running)
drawRunningUI();
if(state == GameState.Paused)
drawPausedUI();
if(state == GameState.GameOver)
drawGameOverUI();
drawText(g, score, g.getWidth() / 2 - score.length()*20 / 2, g.getHeight() - 42);
}
//根据world对象来画出游戏背景和人物
private void drawWorld(World world) {
Graphics g = game.getGraphics();
Snake snake = world.snake;
SnakePart head = snake.parts.get(0);
Stain stain = world.stain;
//画出"污渍"
Pixmap stainPixmap = null;
if(stain.type == Stain.TYPE_1)
stainPixmap = Assets.stain1;
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(snake.direction == Snake.UP)
headPixmap = Assets.headUp;
if(snake.direction == Snake.LEFT)
headPixmap = Assets.headLeft;
if(snake.direction == Snake.DOWN)
headPixmap = Assets.headDown;
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);
}
//画出准备游戏界面
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);
}
//画出游戏暂停画面
private void drawPausedUI() {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.pause, 80, 100);
g.drawLine(0, 416, 480, 416, Color.BLACK);
}
//画出游戏结束画面
private void drawGameOverUI() {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.gameOver, 62, 100);
g.drawPixmap(Assets.buttons, 128, 200, 0, 128, 64, 64);
g.drawLine(0, 416, 480, 416, Color.BLACK);
}
//画出游戏的当前得分
public void drawText(Graphics g, String line, int x, int y) {
int len = line.length();
for (int i = 0; i < len; i++) {
char character = line.charAt(i);
if (character == ' ') {
x += 20;
continue;
}
int srcX = 0;
int srcWidth = 0;
if (character == '.') {
srcX = 200;
srcWidth = 10;
} else {
srcX = (character - '0') * 20;
srcWidth = 20;
}
g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, 32);
x += srcWidth;
}
}
//判断游戏状态,把状态置为暂停,并把得分保存到文件中
@Override
public void pause() {
if(state == GameState.Running)
state = GameState.Paused;
if(world.gameOver) {
Settings.addScore(world.score);
Settings.save(game.getFileIO());
}
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}