📄 screen.java~54~
字号:
package pinball;import javax.microedition.lcdui.*;public class Screen extends Canvas { private Font font; //字体 public static int width; //宽度 public static int height; //高度 private static final int TL_ANCHOR = Graphics.TOP | Graphics.LEFT; private static final int TH_ANCHOR = Graphics.TOP | Graphics.HCENTER; private Image img; public static Graphics GRAPHICS; public static int VDIVIDER; public static final int BACKGROUND = ThreeDColor.gray.getRGB(); private int nLastState; //状态 private int nLastScore; //得分 private int nLastLives; //剩余生命数 private Engine engine; //游戏引擎 private EngineState state; //游戏引擎状态 private int nFontVoff; public Screen() { width = getWidth(); //屏幕宽度 height = getHeight(); //屏幕高度 font = height < 150 ? Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL) : Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM); //选用字体 nFontVoff = font.getHeight(); img = Image.createImage(width, height - (nFontVoff + 5)); VDIVIDER = img.getHeight(); GRAPHICS = img.getGraphics(); state = new EngineState(); nLastState = -1; nLastScore = -1; nLastLives = -1; } public void setEngine(Engine engine) { this.engine = engine; } public void keyPressed(int keycode) { engine.keyPressed(keycode, getGameAction(keycode)); } public void keyReleased(int keycode) { engine.keyReleased(keycode, getGameAction(keycode)); } /** * 绘制背景 */ private void paintBackground(Graphics g) { g.setColor(BACKGROUND); g.fillRect(0, 0, width, height); } /** * 绘制标题 */ private void paintTitle(Graphics g) { engine.getState(state); if (nLastState != Engine.TITLE) { paintBackground(GRAPHICS); state.bricks.paintShadow(GRAPHICS); state.bricks.paint(GRAPHICS); } g.drawImage(img, 0, 0, TL_ANCHOR); g.setColor(BACKGROUND); g.fillRect(0, height - (nFontVoff + 5), width, nFontVoff + 5); g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE)); g.setColor(ThreeDColor.black.getRGB()); g.drawString("PinBall", width / 2 + 1, height / 2 + 20, TH_ANCHOR); g.setColor(ThreeDColor.red.getRGB()); g.drawString("PinBall", width / 2, height / 2 + 19, TH_ANCHOR); } /** * 绘制游戏结束信息 */ private void paintOver(Graphics g) { paintBackground(g); g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE)); g.setColor(ThreeDColor.black.getRGB()); g.drawString("Game Over", width / 2 + 1, height / 3 - 10 + 1, TH_ANCHOR); g.setColor(ThreeDColor.red.getRGB()); g.drawString("Game Over", width / 2, height / 3 - 10, TH_ANCHOR); } public void paint(Graphics g) { boolean bFullRepaint = engine.levelStarted(); engine.getState(state); g.setFont(font); if (state.state == Engine.TITLE) { //处于标题状态 paintTitle(g); } else if (state.state == Engine.OVER) { //游戏结束 paintOver(g); } else { //游戏正在运行 if (bFullRepaint) { paintBackground(GRAPHICS); state.bricks.paintShadow(GRAPHICS); state.bricks.paint(GRAPHICS); } g.drawImage(img, 0, 0, TL_ANCHOR); if (state.state == Engine.PLAY) { //游戏进行当中 if (state.score != nLastScore || bFullRepaint) { g.setColor(BACKGROUND); g.fillRect(width / 3, height - (nFontVoff + 5), width * 2 / 3, nFontVoff + 5); g.setColor(ThreeDColor.black.getRGB()); g.drawString("分数: " + state.score, width * 1 / 3 + 1, height - (nFontVoff + 3), TL_ANCHOR); g.drawString("最高分: " + state.highscore, width * 2 / 3 + 1, height - (nFontVoff + 3), TL_ANCHOR); g.setColor(ThreeDColor.orange.getRGB()); g.drawString("分数: " + state.score, width * 1 / 3, height - (nFontVoff + 5), TL_ANCHOR); g.drawString("最高分: " + state.highscore, width * 2 / 3, height - (nFontVoff + 5), TL_ANCHOR); } if (state.lives != nLastLives || bFullRepaint) { g.setColor(BACKGROUND); g.fillRect(0, height - (nFontVoff + 5), width / 3, nFontVoff + 5); for (int i = 0; i < state.lives; i++) { g.setColor(ThreeDColor.black.getRGB()); g.fillArc(BrickList.XOFFSET + 1 + 3 * Ball.RADIUS * i, height - (nFontVoff - 1), Math.max(4, height / 50), Math.max(4, height / 50), 0, 360); g.setColor(ThreeDColor.red.getRGB()); g.fillArc(BrickList.XOFFSET + 3 * Ball.RADIUS * i, height - nFontVoff, Math.max(4, height / 50), Math.max(4, height / 50), 0, 360); } } } state.ball.paintShadow(g); state.board.paintShadow(g); state.ball.paint(g); state.board.paint(g); } nLastState = state.state; nLastScore = state.score; nLastLives = state.lives; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -