📄 fishcatch.java
字号:
/* * FishCatch.java * * Copyright 2000 JJKING Software, Junichi Ito <jun1@mailhost.net> * Permission to use, copy, modify, and distribute this software and its * documentation without fee for NON-COMMERCIAL is free. */ import com.sun.kjava.*;/** * This is the main application. */public class FishCatch extends Spotlet implements FrameListener{ /** * The bounds of animation field. */ static Rectangle fieldBounds = new Rectangle(2, 18, 156, 140); /** * The key code for home key. */ static final int KEY_HOME = 264; /** * The animation manager. */ AnimManager animMgr; /** * The kuma. */ Kuma kuma; /** * The score. */ int score; /** * Creates a spotlet and registers its event handlers. */ public static void main(String[] args) { (new FishCatch()).register(WANT_SYSTEM_KEYS); } /** * Create screen and start programs. */ public FishCatch() { // initialize animation manager animMgr = new AnimManager(fieldBounds); animMgr.setFrameListener(this); // init game initGame(); } /** * Initialize game. */ void initGame() { // reset animation manager animMgr.reset(); // init kuma kuma = new Kuma(); try { animMgr.add(kuma); } catch(AnimException ex) {} // reset score score = 0; // Clear and draw border. Graphics g = Graphics.getGraphics(); g.clearScreen(); g.drawBorder(fieldBounds.x , fieldBounds.y , fieldBounds.width , fieldBounds.height , g.PLAIN , g.borderType(4, 0, 1)); g.drawString("Score:", 0, 0); addScore(0); // just draw current score. } /** * Handle a key down event. */ public void keyDown(int keyCode) { switch(keyCode) { case KEY_HARD2: if (!kuma.isBusy()) { kuma.moveLeft(); animMgr.update(kuma); } break; case KEY_HARD3: if (!kuma.isBusy()) { kuma.moveRight(); animMgr.update(kuma); } break; case KEY_POWER: case KEY_HOME: System.exit(0); } } /** * invoked when the animation frame is changed. * @event evt the animation event. */ public void changeFrame(FrameEvent evt) { if (kuma == null) { return; } // reset game if (kuma.getState() == Kuma.STATE_HIDE) { initGame(); return; } // check hit AnimChar[] animChars = animMgr.getAnimChars(); for (int i = 0; i < animChars.length; i++) { if (kuma.isHit(animChars[i])) { if ((animChars[i] instanceof Bomb || animChars[i] instanceof Fire) && kuma.isLive()) { kuma.setState(Kuma.STATE_GROGGY); animChars[i].setState(AnimChar.STATE_HIDE); break; } if (animChars[i] instanceof Fish && ((Fish)animChars[i]).isEatable() && !kuma.isBusy()) { kuma.setState(Kuma.STATE_EAT); animChars[i].setState(AnimChar.STATE_HIDE); addScore(1); break; } } } // add new fish if (AnimManager.random(8) == 0) { try { Fish fish = new Fish(); animMgr.add(fish); } catch(AnimException ex) {} } // add new bomb int r = score < 50 ? 55 - score : 5; if (AnimManager.random(r) == 0) { try { Bomb bomb = new Bomb(); animMgr.add(bomb); } catch(AnimException ex) {} } } /** * Increment the score. */ void addScore(int s) { score += s; Graphics g = Graphics.getGraphics(); g.drawString(String.valueOf(score), 32, 0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -