📄 slugsscreen.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: SlugsScreen.java,v 1.2 2003/05/29 22:39:01 wildcard Exp $ */package com.sun.j2me.blueprints.slugs.client;import com.sun.j2me.blueprints.slugs.shared.*;import com.sun.j2me.blueprints.slugs.shared.Item;import javax.microedition.lcdui.*;/** * Client visualization component for the slugs game */public class SlugsScreen extends Screen implements PlayerActions { // so we don't have to use raw hex // numbers to indicate colors private static final int white = 0xffffff; private static final int black = 0x000000; private static final int blue = 0x0000ff; private static final int red = 0xff0000; private static final int orange = 0xffc800; private static final int yellow = 0xffff00; private static final int green = 0x00ff00; private static final int grass_green = 0x00c000; private static final int gray = 0x808080; private static final int purple = 0x8000c0; private static final int pink = 0xffafaf; private static final int lightGray = 0xc0c0c0; private static final int darkGray = 0x404040; // cell image patterns for use by pixelator private static final String[] IMG_PATTERN = { "" + "gGgGg" + "!GG!!" + "G!GGG" + "GGG!G" + "gG!Gg", "" + "rrgrr" + "rgbgr" + "gbbbg" + "rgbgr" + "rrgrr", "" + "rrgrr" + "rgygr" + "gyyyg" + "rgygr" + "rrgrr", "" + "gyyyg" + "yXyXy" + "yyyyy" + "yXXXy" + "gyyyg", "" + "####B" + "#gggB" + "#gggB" + "#gggB" + "#BBBB", "" + "@@@@Y" + "@gggY" + "@gggY" + "@gggY" + "@YYYY", "" + "ggggg" + "ggggg" + "ggggg" + "ggggg" + "ggggg", "" + "gnnng" + "NnNNN" + "NnNnN" + "NnnnN" + "gNNNg", "" + "ggrgg" + "grrrg" + "grrrg" + "ggrgg" + "grgrg", "" + "ggggg" + "g##Bg" + "g#bBg" + "g#BBg" + "ggggg", "" + "ggggg" + "g@@Yg" + "g@yYg" + "g@YYg" + "ggggg", "" + "####B" + "#bbbB" + "#bbbB" + "#bbbB" + "#BBBB", "" + "@@@@Y" + "@yyyY" + "@yyyY" + "@yyyY" + "@YYYY", "" + "g...g" + ".g.g." + "g...g" + "g.g.g" + "ggggg", "" + "gnnng" + "ggngg" + "ggngg" + "gaaag" + "ggagg", "" + "YYAKK" + "YYAKK" + "AAAAA" + "KKAYY" + "KKAYY", }; private static final int BOOSTER = 0; private static final int DIG1 = 1; private static final int DIG2 = 2; private static final int EXTRA_LIFE = 3; private static final int FLY1 = 4; private static final int FLY2 = 5; private static final int GRASS = 6; private static final int HOUSE = 7; private static final int JET_PACK = 8; private static final int PATH1 = 9; private static final int PATH2 = 10; private static final int PLAYER1 = 11; private static final int PLAYER2 = 12; private static final int POISON = 13; private static final int SHOVEL = 14; private static final int WALL = 15; private Font font; private int font_offset; private long last; private int fps; private int x_offset; private int y_offset; private int level_width; private int level_height; private EventClient client; private Image buf; private Graphics buf_g; private Image[] img_list; private String life_string; private String shovel_string; private String jetpack_string; private String score_string; private int player_id; private int lives; private String fps_string; private boolean showTitle; /** * Create a new instance of this class */ public SlugsScreen() { int i; font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); font_offset = font.getHeight() + 2; fps = 0; fps_string = "fps: " + fps; showTitle = true; buf = Image.createImage(getWidth(), getHeight()); buf_g = buf.getGraphics(); buf_g.setColor(black); buf_g.fillRect(0, 0, getWidth(), getHeight()); String color_key = "g!GrybX@Y#BnN.aKA"; int[] color_value = new int[] { 0x00c000, 0x78d000, 0x006818, 0xff0000, 0xffff00, 0x0000ff, 0x000000, 0xf8f098, 0xf0b000, 0x00ffff, 0x000080, 0xbe7e1e, 0x5f3f1e, 0xffffff, 0xc0c0c0, 0x800000, 0x808080 }; Pixelator.setColors(color_key, color_value); img_list = new Image[IMG_PATTERN.length]; for (i = 0; i < IMG_PATTERN.length; i++) { img_list[i] = Pixelator.createImage(IMG_PATTERN[i], 5); } setLives(0); setShovels(0); setJetpacks(0); setScore(0); } /** * Initialize this instance for use with event client 'client' * and player id 'player_id'. Since the constructor for this * class is called by Class.foName().newInstance() (which * requires a no-arg constructor), we need to do the set-up * separately. */ public void initialize(EventClient client, int player_id) { this.client = client; this.player_id = player_id; } private void setLives(int lives) { this.lives = lives; life_string = "" + lives; } private void setShovels(int shovels) { shovel_string = "" + shovels; } private void setJetpacks(int jetpacks) { jetpack_string = "" + jetpacks; } private void setScore(int score) { score_string = "Score: " + score; } /** * MIDP event key event handling */ public void keyPressed(int keycode) { int game_action = getGameAction(keycode); int player_action = -1; if (true /* lives > 0 */) { switch (game_action) { case Canvas.UP: player_action = PlayerActions.UP; break; case Canvas.DOWN: player_action = PlayerActions.DOWN; break; case Canvas.LEFT: player_action = PlayerActions.LEFT; break; case Canvas.RIGHT: player_action = PlayerActions.RIGHT; break; default: // do nothing } switch (keycode) { case Canvas.KEY_NUM1: player_action = PlayerActions.JUMP; break; case Canvas.KEY_NUM3: player_action = PlayerActions.DIG; break; default: // do nothing } if (player_action >= 0) { client.handleEvent(Message.CLIENT_STATUS, player_action); } } } /** * MIDP event key event handling */ public void keyReleased(int keycode) {} /** * Update the game diaply at ('x', 'y') with tag 'tag' */ public void update(int tag, int x, int y) { int index, id; index = -1; id = -1; if ((tag & PlayerActions.PLAYER_BITS) != 0) { id = tag & 1; tag &= ~1; if ((tag & BURROW) != 0) { index = id == 0 ? DIG1 : DIG2; } else if ((tag & FLY) != 0) { index = id == 0 ? FLY1 : FLY2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -