java炸弹人游戏_bomberplayer.java 源代码在线查看 - 经典游戏炸弹人游戏 资源下载 虫虫电子下载站...

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.lang.Integer;/** * File: BomberPlayer.java * Copyright: Copyright (c) 2001 * @author Sammy Leong * @version 1.0 *//** * This class creates player objects. */public class BomberPlayer extends Thread { /** game object handle */ public BomberGame game = null; /** map object handle */ private BomberMap map = null; /** player's own bomb grid (must have for synchronization) */ public boolean[][] bombGrid = null; /** input key queue */ private BomberKeyQueue keyQueue = null; /** bomb key is down or not */ private boolean bombKeyDown = false; /** direction keys down */ private byte dirKeysDown = 0x00; /** current direction key down */ private byte currentDirKeyDown = 0x00; /** sprite width */ private final int width = BomberMain.size; /** sprite height */ private final int height = 44 / (32 / BomberMain.size); /** is exploding flag */ private boolean isExploding = false; /** is dead flag */ private boolean isDead = false; /** whether a key is pressed or not */ private boolean keyPressed = false; /** the player's input keys */ private int[] keys = null; /** total bombs the player has */ public int totalBombs = 1; /** total bombs the player used */ public int usedBombs = 0; /** the player's fire strength */ public int fireLength = 2; /** if player is alive */ public boolean isActive = true; /** player position */ public int x = 0; public int y = 0; /** player's number */ private int playerNo = 0; /** user's state : default to face down */ private int state = DOWN; /** flag : whether the player is moving or not */ private boolean moving = false; /** sprite frame number */ private int frame = 0; /** clear mode flag */ private boolean clear = false; /** byte enumerations */ private static final byte BUP = 0x01; private static final byte BDOWN = 0x02; private static final byte BLEFT = 0x04; private static final byte BRIGHT = 0x08; private static final byte BBOMB = 0x10; /** number enumerations */ private static final int UP = 0; private static final int DOWN = 1; private static final int LEFT = 2; private static final int RIGHT = 3; private static final int BOMB = 4; private static final int EXPLODING = 4; /** all player sprite images */ private static Image[][][] sprites = null; /** rendering hints */ private static Object hints = null; static { /** if java runtime is Java 2 */ if (Main.J2) { /** create the rendering hints for better graphics output */ RenderingHints h = null; h = new RenderingHints(null); h.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); h.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); h.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); h.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); h.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); hints = (RenderingHints)h; } /** create the images */ sprites = new Image[4][5][5]; int[] states = { UP, DOWN, LEFT, RIGHT, EXPLODING }; Toolkit tk = Toolkit.getDefaultToolkit(); String path = new String(); /** open the files */ try { for (int p = 0; p < 4; p++) { for (int d = 0; d < 5; d++) { for (int f = 0; f < 5; f++) { /** generate file name */ path = BomberMain.RP + "Images/"; path += "Bombermans/Player " + (p + 1) + "/"; path += states[d] + "" + (f + 1) + ".gif"; /** open the file */ sprites[p][d][f] = tk.getImage( new File(path).getCanonicalPath()); } } } } catch (Exception e) { new ErrorDialog(e); } } /** * Constructs a player. * @param game game object * @param map map object * @param playerNo player's number */ public BomberPlayer(BomberGame game, BomberMap map, int playerNo) { this.game = game; this.map = map; this.playerNo = playerNo; /** create the bomb grid */ bombGrid = new boolean[17][17]; for (int i = 0; i < 17; i++) for (int j = 0; j < 17; j++) bombGrid[i][j] = false; int r = 0, c = 0; /** find player's starting position */ switch (this.playerNo) { case 1: r = c = 1; break; case 2: r = c = 15; break; case 3: r = 15; c = 1; break; case 4: r = 1; c = 15; } /** calculate position */ x = r y = c MediaTracker tracker = new MediaTracker(game); try { int counter = 0; /** load the images */ for (int p = 0; p < 4; p++) { for (int d = 0; d < 5; d++) { for (int f = 0; f < 5; f++) { tracker.addImage(sprites[p][d][f], counter++); } } } /** wait for images to finish loading */ tracker.waitForAll(); } catch (Exception e) { new ErrorDialog(e); } /** create the key queue */ keyQueue = new BomberKeyQueue(); /** create the key configurations array */ keys = new int[5]; /** load the configurations */ for (int k = BomberKeyConfig.UP; k keys[k] = BomberKeyConfig.keys[playerNo - 1][k]; /** HOG THE CPU!!! */ setPriority(Thread.MAX_PRIORITY); /** start looping */ start(); } /** * Key pressed event handler. * @param evt key event */ public void keyPressed(KeyEvent evt) { /** assume no new key is pressed */ byte newKey = 0x00; /** if player isn't exploding or dead and key pressed is in player's */ /** key list */ if (!isExploding && !isDead && evt.getKeyCode() == keys[UP] || evt.getKeyCode() == keys[DOWN] || evt.getKeyCode() == keys[LEFT] || evt.getKeyCode() == keys[RIGHT]) { /** if down key pressed */ if (evt.getKeyCode() == keys[DOWN]) { newKey = BDOWN; /** if only the up key is pressed */ if ((currentDirKeyDown & BUP) > 0 || ((currentDirKeyDown & BLEFT) == 0 && (currentDirKeyDown & BRIGHT) == 0)) currentDirKeyDown = BDOWN; } /** if up key is pressed */ else if (evt.getKeyCode() == keys[UP]) { newKey = BUP; /** if only the down key is pressed */ if ((currentDirKeyDown & BDOWN) > 0 || ((currentDirKeyDown & BLEFT) == 0 && (currentDirKeyDown & BRIGHT) == 0)) currentDirKeyDown = BUP; } /** if left key is pressed */ else if (evt.getKeyCode() == keys[LEFT]) { newKey = BLEFT; /** if only the right key is pressed */ if ((currentDirKeyDown & BRIGHT) > 0 || ((currentDirKeyDown & BUP) == 0 && (currentDirKeyDown & BDOWN) == 0)) currentDirKeyDown = BLEFT; } /** if right key is pressed */ else if (evt.getKeyCode() == keys[RIGHT]) { newKey = BRIGHT; /** if only the left is pressed */ if ((currentDirKeyDown & BLEFT) > 0 || ((currentDirKeyDown & BUP) == 0 && (currentDirKeyDown & BDOWN) == 0)) currentDirKeyDown = BRIGHT; } /** if new key isn't in the key queue */ if (!keyQueue.contains(newKey)) { /** then push it on top */ keyQueue.push(newKey); /** reset keys pressed buffer */ dirKeysDown |= newKey; keyPressed = true; /** if thread is sleeping, then wake it up */ interrupt(); } } /** if no direction key is pressed */ /** and bomb key is pressed */ if (!isExploding && !isDead && evt.getKeyCode() == keys[BOMB] && !bombKeyDown && isActive) { bombKeyDown = true; interrupt(); } } /** * Key released handler. * @param evt key event */ public void keyReleased(KeyEvent evt) { /** if a direction key is released */ if (!isExploding && !isDead && ( evt.getKeyCode() == keys[UP] || evt.getKeyCode() == keys[DOWN] || evt.getKeyCode() == keys[LEFT] || evt.getKeyCode() == keys[RIGHT])) {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值