我正在创建一个炸弹人游戏,我需要能够拥有多人游戏。用户应该能够同时移动他们的角色,但我不知道如何切换KeyPressed方法的焦点。这是我的两个类:Jframe Bomberman类和Character类。
public class Bomberman extends JFrame implements ActionListener, MouseListener,
KeyListener {
private JButton singleplayer, multiplayer, howtoplay, highscores, goBack;
public Map[] game;
private JLabel scoreLabel, armor;
private JComboBox numEnemies;
private Highscore highscore = new Highscore();
private int score, armorscore;
public Bomberman() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
Color red = new Color(178, 0, 0);
getContentPane().setBackground(red);
addKeyListener(this);
setStartScreen();
setVisible(true);
}
private void setHighScores() {
Font subtitle = new Font("Myriad Pro", Font.PLAIN, 32);
setSize(1000, 750);
goBack = new JButton();
goBack.setBounds(300, 620, 240, 70);
goBack.setBackground(Color.YELLOW);
goBack.setForeground(Color.RED);
goBack.setFont(subtitle);
goBack.setText("Return");
goBack.addActionListener(this);
add(goBack);
}
private void setSingleplayer(int numEnemies) {
setSize(1000, 800);
repaint();
score = 0;
randomMap(numEnemies, true);
JLabel playerScore, armorCount;
scoreLabel = new JLabel();
scoreLabel.setBounds(125, 15, 60, 30);
scoreLabel.setForeground(Color.YELLOW);
scoreLabel.setFont(new Font("Arial Black", Font.PLAIN, 12));
scoreLabel.setText(getScore());
add(scoreLabel);
armor = new JLabel();
armor.setBounds(80, 40, 10, 30);
armor.setForeground(Color.YELLOW);
armor.setFont(new Font("Arial Black", Font.PLAIN, 12));
armor.setText(String.valueOf(getArmorscore()));
add(armor);
playerScore = new JLabel();
playerScore.setBounds(10, 15, 110, 30);
playerScore.setForeground(Color.BLACK);
playerScore.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore.setText("Player Score:");
add(playerScore);
armorCount = new JLabel();
armorCount.setBounds(10, 40, 70, 30);
armorCount.setForeground(Color.BLACK);
armorCount.setFont(new Font("Arial Black", Font.PLAIN, 15));
armorCount.setText("Armor: ");
add(armorCount);
}
private void setMultiplayer(int numEnemies) {
setSize(1000, 800);
repaint();
score = 0;
JLabel playerScore, score, playerScore2, score2, playerScore3, score3, playerScore4, score4;
if (numEnemies == 0) {
score = new JLabel();
score.setBounds(130, 5, 150, 40);
score.setForeground(Color.YELLOW);
score.setFont(new Font("Arial Black", Font.PLAIN, 12));
score.setText("bomber.getScore()");
add(score);
playerScore = new JLabel();
playerScore.setBounds(10, 5, 120, 40);
playerScore.setForeground(Color.BLACK);
playerScore.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore.setText("Player Score:");
add(playerScore);
score2 = new JLabel();
score2.setBounds(830, 5, 150, 40);
score2.setForeground(Color.YELLOW);
score2.setFont(new Font("Arial Black", Font.PLAIN, 12));
score2.setText("bomber2.getScore()");
add(score2);
playerScore2 = new JLabel();
playerScore2.setBounds(700, 5, 120, 40);
playerScore2.setForeground(Color.BLACK);
playerScore2.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore2.setText("Player2 Score:");
add(playerScore2);
randomMap(2, false);
} else if (numEnemies == 1) {
score = new JLabel();
score.setBounds(130, 5, 150, 40);
score.setForeground(Color.YELLOW);
score.setFont(new Font("Arial Black", Font.PLAIN, 12));
score.setText("bomber.getScore()");
add(score);
playerScore = new JLabel();
playerScore.setBounds(10, 5, 120, 40);
playerScore.setForeground(Color.BLACK);
playerScore.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore.setText("Player Score:");
add(playerScore);
score2 = new JLabel();
score2.setBounds(830, 5, 150, 40);
score2.setForeground(Color.YELLOW);
score2.setFont(new Font("Arial Black", Font.PLAIN, 12));
score2.setText("bomber2.getScore()");
add(score2);
playerScore2 = new JLabel();
playerScore2.setBounds(700, 5, 120, 40);
playerScore2.setForeground(Color.BLACK);
playerScore2.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore2.setText("Player2 Score:");
add(playerScore2);
score3 = new JLabel();
score3.setBounds(130, 720, 150, 40);
score3.setForeground(Color.YELLOW);
score3.setFont(new Font("Arial Black", Font.PLAIN, 12));
score3.setText("bomber3.getScore()");
add(score3);
playerScore3 = new JLabel();
playerScore3.setBounds(10, 720, 120, 40);
playerScore3.setForeground(Color.BLACK);
playerScore3.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore3.setText("Player3 Score:");
add(playerScore3);
randomMap(3, false);
} else if (numEnemies == 2) {
score = new JLabel();
score.setBounds(130, 5, 150, 40);
score.setForeground(Color.YELLOW);
score.setFont(new Font("Arial Black", Font.PLAIN, 12));
score.setText("bomber.getScore()");
add(score);
playerScore = new JLabel();
playerScore.setBounds(10, 5, 120, 40);
playerScore.setForeground(Color.BLACK);
playerScore.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore.setText("Player Score:");
add(playerScore);
score2 = new JLabel();
score2.setBounds(830, 5, 150, 40);
score2.setForeground(Color.YELLOW);
score2.setFont(new Font("Arial Black", Font.PLAIN, 12));
score2.setText("bomber2.getScore()");
add(score2);
playerScore2 = new JLabel();
playerScore2.setBounds(700, 5, 120, 40);
playerScore2.setForeground(Color.BLACK);
playerScore2.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore2.setText("Player2 Score:");
add(playerScore2);
score3 = new JLabel();
score3.setBounds(140, 720, 150, 40);
score3.setForeground(Color.YELLOW);
score3.setFont(new Font("Arial Black", Font.PLAIN, 12));
score3.setText("bomber3.getScore()");
add(score3);
playerScore3 = new JLabel();
playerScore3.setBounds(10, 720, 120, 40);
playerScore3.setForeground(Color.BLACK);
playerScore3.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore3.setText("Player3 Score:");
add(playerScore3);
score4 = new JLabel();
score4.setBounds(830, 720, 150, 40);
score4.setForeground(Color.YELLOW);
score4.setFont(new Font("Arial Black", Font.PLAIN, 12));
score4.setText("bomber4.getScore()");
add(score4);
playerScore4 = new JLabel();
playerScore4.setBounds(700, 720, 120, 40);
playerScore4.setForeground(Color.BLACK);
playerScore4.setFont(new Font("Arial Black", Font.PLAIN, 15));
playerScore4.setText("Player4 Score:");
add(playerScore4);
randomMap(4, false);
}
}
private void setHowtoplay() {
try {
Font subtitle = new Font("Myriad Pro", Font.PLAIN, 32);
goBack = new JButton();
goBack.setBounds(300, 620, 240, 70);
goBack.setBackground(Color.YELLOW);
goBack.setForeground(Color.RED);
goBack.setFont(subtitle);
goBack.setText("Return");
goBack.addActionListener(this);
add(goBack);
Image howtoplay;
howtoplay = ImageIO.read(new File("src/pics/howtoplay.png"));
ImageIcon setPic = new ImageIcon(howtoplay);
JLabel putHowto = new JLabel();
putHowto.setBounds(0, 0, 1000, 600);
putHowto.setIcon(setPic);
add(putHowto);
} catch (IOException e) {
System.out.println("Error, problem reading image");
}
}
private void randomMap(int numEnemies, boolean isSingleplayer) {
try {
Image floor = ImageIO.read(new File("src/pics/floor.fw.png"));
System.out.println("Loading Game with " + numEnemies + " bombers.");
game = new Map[361];
Point cornerTopLeft, cornerTopRight, cornerBotLeft, cornerBotRight, topLeftRight, topLeftBot, topRightLeft, topRightBot, botLeftTop, botLeftRight, botRightLeft, botRightTop;
cornerTopLeft = new Point(160, 65);
cornerTopRight = new Point(790, 65);
cornerBotLeft = new Point(160, 695);
cornerBotRight = new Point(790, 695);
topLeftRight = new Point(195, 65);
topLeftBot = new Point(160, 100);
topRightLeft = new Point(755, 65);
topRightBot = new Point(790, 100);
botLeftTop = new Point(160, 660);
botLeftRight = new Point(195, 695);
botRightLeft = new Point(755, 695);
botRightTop = new Point(790, 660);
Random generator = new Random();
int j = 0;
int k = 0;
for (int i = 0; i < game.length; i++) {
boolean l = generator.nextBoolean();
if ((i % 19) == 0) {
k = k + 35;
j = 0;
} else {
k = k + 0;
if (i > 0) {
j = j + 35;
}
}
Point o = new Point((160 + j), (30 + k));
if (isSingleplayer == true) {
if (numEnemies == 0 && i == 0) {
game[360] = new Character(true, this, "Player");
add(game[360]);
game[0] = new CharacterAI(this, "Bot 1", 0);
add(game[0]);
System.out.println("2 bombers called");
} else if (numEnemies == 1 && i == 0) {
game[360] = new Character(true, this, "Player");
add(game[360]);
game[0] = new CharacterAI(this, "Bot 1", 0);
add(game[0]);
game[18] = new CharacterAI(this, "Bot 2", 18);
add(game[18]);
System.out.println("3 bombers called");
} else if (numEnemies == 2 && i == 0) {
game[360] = new Character(true, this,"Player");
add(game[360]);
game[0] = new CharacterAI(this,"Bot 1", 0);
add(game[0]);
game[18] = new CharacterAI(this, "Bot 2", 18);
add(game[18]);
game[342] = new CharacterAI(this, "Bot 3", 342);
add(game[342]);
System.out.println("4 bombers called");
}
} else {
if (numEnemies == 2 && i == 0) {
game[0] = new Character(false, this,
"Player1");
add(game[0]);
game[18] = new Character(false, this,
"Player2");
add(game[18]);
System.out.println("2 bombers called");
} else if (numEnemies == 3 && i == 0) {
game[0] = new Character(false, this,
"Player1");
add(game[0]);
game[18] = new Character(false, this,
"Player2");
add(game[18]);
game[342] = new Character(false, this,
"Player3");
add(game[342]);
System.out.println("3 bombers called");
} else if (numEnemies == 4 && i == 0) {
game[0] = new Character(false, this,
"Player1");
add(game[0]);
game[18] = new Character(false, this,
"Player2");
add(game[18]);
game[342] = new Character(false, this,
"Player3");
add(game[342]);
game[360] = new Character(false, this,
"Player4");
add(game[360]);
System.out.println("4 bombers called");
}
}
if (i > 18 && i < 38 || i > 56 && i < 76 || i > 94 && i < 114
|| i > 133 && i < 152 || i > 170 && i < 190 || i > 208
&& i < 228 || i > 246 && i < 266 || i > 284 && i < 304
|| i > 322 && i < 342) {
if (i % 2 == 0) {
game[i] = new IndestructibleObject(160 + j, 30 + k);
add(game[i]);
} else {
if (l == true
&& DestroyableObject.getNumDestroyableObject() < 141
&& !o.equals(cornerTopLeft)
&& !o.equals(cornerTopRight)
&& !o.equals(cornerBotRight)
&& !o.equals(cornerBotLeft)
&& !o.equals(topLeftRight)
&& !o.equals(topLeftBot)
&& !o.equals(topRightLeft)
&& !o.equals(botLeftTop)
&& !o.equals(topRightBot)
&& !o.equals(botLeftRight)
&& !o.equals(botRightLeft)
&& !o.equals(botRightTop)) {
game[i] = new DestroyableObject(160 + j, 30 + k,
this);
add(game[i]);
} else {
game[i] = new Map(i + "", 0);
game[i].setBounds((160 + j), (30 + k), 35, 35);
game[i].setText("");
game[i].setIcon(new ImageIcon(floor));
game[i].setOpaque(false);
add(game[i]);
}
}
} else {
if (l == true
&& DestroyableObject.getNumDestroyableObject() < 141
&& !o.equals(cornerTopLeft)
&& !o.equals(cornerTopRight)
&& !o.equals(cornerBotRight)
&& !o.equals(cornerBotLeft)
&& !o.equals(topLeftRight) && !o.equals(topLeftBot)
&& !o.equals(topRightLeft) && !o.equals(botLeftTop)
&& !o.equals(topRightBot)
&& !o.equals(botLeftRight)
&& !o.equals(botRightLeft)
&& !o.equals(botRightTop)) {
game[i] = new DestroyableObject(160 + j, 30 + k, this);
add(game[i]);
} else {
game[i] = new Map(i + "", 0);
game[i].setBounds((160 + j), (30 + k), 35, 35);
game[i].setText("");
game[i].setIcon(new ImageIcon(floor));
game[i].setOpaque(false);
add(game[i]);
}
}
}
} catch (IOException e) {
System.out.println("Error, problem reading image");
}
}
public void increaseScore(int type) {
if (type == 1) {
score = score + 100;
scoreLabel.setText(getScore());
} else if (type == 2) {
score = score + 50;
scoreLabel.setText(getScore());
} else if (type == 3) {
score = score + 1000;
scoreLabel.setText(getScore());
}
}
public String getScore() {
String strScore = String.valueOf(score);
return strScore;
}
public int getArmorscore() {
return armorscore;
}
public void setArmorscore(int armorscore) {
this.armorscore = armorscore;
armor.setText(String.valueOf(getArmorscore()));
}
public void gameEnd(boolean isSingleplayer) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
if (isSingleplayer == true) {
DestroyableObject.reset();
Font title = new Font("Myriad Pro", Font.PLAIN, 80);
Font subtitle = new Font("Myriad Pro", Font.PLAIN, 26);
setSize(1000, 750);
JLabel gameOver = new JLabel();
gameOver.setBounds(250, 50, 500, 70);
gameOver.setFont(title);
gameOver.setText("GAME OVER");
gameOver.setForeground(Color.YELLOW);
add(gameOver);
JLabel score = new JLabel();
score.setBounds(200, 250, 500, 70);
score.setFont(subtitle);
score.setText("Your Score: " + getScore());
score.setForeground(Color.YELLOW);
add(score);
JButton subHighscore = new JButton();
subHighscore.setBounds(180, 560, 260, 70);
subHighscore.setBackground(Color.YELLOW);
subHighscore.setForeground(Color.RED);
subHighscore.setFont(subtitle);
subHighscore.setText("Submit Highscore");
subHighscore.addActionListener(this);
add(subHighscore);
goBack = new JButton();
goBack.setBounds(500, 560, 240, 70);
goBack.setBackground(Color.YELLOW);
goBack.setForeground(Color.RED);
goBack.setFont(subtitle);
goBack.setText("Return");
goBack.addActionListener(this);
add(goBack);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == howtoplay) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
setHowtoplay();
} else if (e.getSource() == goBack) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
setStartScreen();
} else if (e.getSource() == highscores) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
setHighScores();
} else if (e.getSource() == singleplayer) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
String[] list = { "1", "2", "3" };
numEnemies = new JComboBox(list);
JOptionPane.showMessageDialog(null, numEnemies,
"Select the number of opponents: ",
JOptionPane.QUESTION_MESSAGE);
setSingleplayer(numEnemies.getSelectedIndex());
} else if (e.getSource() == multiplayer) {
getContentPane().removeAll();
getContentPane().revalidate();
getContentPane().repaint();
String[] list = { "2", "3", "4" };
numEnemies = new JComboBox(list);
JOptionPane.showMessageDialog(null, numEnemies,
"Select the number of players: ",
JOptionPane.QUESTION_MESSAGE);
setMultiplayer(numEnemies.getSelectedIndex());
} else if (e.getSource() == game[360]) {
if (game[360].getType() == 0) {
System.out.println("Game over");
}
}
}
}
这是我的角色类:
public class Character extends Map implements KeyListener {
private static int numCharacter = 0;
private int moveUp, moveDown, moveLeft, moveRight, placeBomb, moveSpeed,
direction, source, armor;
public int maxBombs, numBombs;
private int bombExplodeTime, bombRange;
private Bomberman host;
private boolean isSingleplayer;
private Bomb bomb;
public Character(boolean ifSingleplayer, Bomberman game, String name) {
super("Character", 3);
host = game;
this.setName(name);
isSingleplayer = ifSingleplayer;
maxBombs = 1;
numBombs = 0;
bombExplodeTime = 1000;
bombRange = 0;
armor = 0;
try {
Image moveDownImage = ImageIO.read(new File(
"src/pics/Character-Down.fw.png"));
if (ifSingleplayer == true){
if (numCharacter == 0) {
addKeyListener(this);
setBounds(790, 695, 35, 35);
setSource(360);
moveUp = KeyEvent.VK_UP;
moveDown = KeyEvent.VK_DOWN;
moveLeft = KeyEvent.VK_LEFT;
moveRight = KeyEvent.VK_RIGHT;
placeBomb = KeyEvent.VK_SPACE;
setFocusable(true);
}
if (numCharacter == 1) {
setBounds(160, 65, 35, 35);
setSource(0);
}
if (numCharacter == 2) {
setBounds(160, 695, 35, 35);
setSource(18);
}
if (numCharacter == 3) {
setBounds(790, 65, 35, 35);
setSource(342);
}
}else{
if (numCharacter == 0) {
setBounds(160, 65, 35, 35);
setSource(0);
moveUp = KeyEvent.VK_W;
moveDown = KeyEvent.VK_S;
moveLeft = KeyEvent.VK_A;
moveRight = KeyEvent.VK_D;
placeBomb = KeyEvent.VK_C;
setFocusable(true);
addKeyListener(this);
}
if (numCharacter == 1) {
setBounds(790, 65, 35, 35);
setSource(18);
moveUp = KeyEvent.VK_Y;
moveDown = KeyEvent.VK_H;
moveLeft = KeyEvent.VK_G;
moveRight = KeyEvent.VK_J;
placeBomb = KeyEvent.VK_M;
setFocusable(true);
addKeyListener(this);
}
if (numCharacter == 2) {
setBounds(160, 695, 35, 35);
setSource(342);
moveUp = KeyEvent.VK_P;
moveDown = KeyEvent.VK_SEMICOLON;
moveLeft = KeyEvent.VK_L;
moveRight = KeyEvent.VK_QUOTE;
placeBomb = KeyEvent.VK_SLASH;
setFocusable(true);
addKeyListener(this);
}
if (numCharacter == 3) {
setBounds(790, 695, 35, 35);
setSource(360);
moveUp = KeyEvent.VK_UP;
moveDown = KeyEvent.VK_DOWN;
moveLeft = KeyEvent.VK_LEFT;
moveRight = KeyEvent.VK_RIGHT;
placeBomb = KeyEvent.VK_CONTROL;
setFocusable(true);
addKeyListener(this);
}
}
setOpaque(false);
setIcon(new ImageIcon(moveDownImage));
numCharacter++;
} catch (IOException error) {
System.out.println("Error, problem reading image");
}
}
public int getBombExplodeTime() {
return bombExplodeTime;
}
public void setBombExplodeTime(int bombExplodeTime) {
this.bombExplodeTime = bombExplodeTime;
}
public void checkEnd() {
if (isSingleplayer == true && getNumCharacter() == 0) {
host.gameEnd(isSingleplayer);
} else if (isSingleplayer == false && getNumCharacter() == 1) {
host.gameEnd(isSingleplayer);
}
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
public void increaseMaxBombs(){
maxBombs++;
}
public int getBombRange() {
return bombRange;
}
public void setBombRange(int bombRange) {
this.bombRange = bombRange;
}
public void runUp(int source) {
// direction up = 1
try {
Image moveUpImage = ImageIO.read(new File(
"src/pics/Character-Up.fw.png"));
switchButtons(source, source - 19);
} catch (IOException error) {
System.out.println("Error, problem reading image");
}
}
public void runDown(int source) {
// direction down = 2
try {
Image moveDownImage = ImageIO.read(new File(
"src/pics/Character-Down.fw.png"));
setIcon(new ImageIcon(moveDownImage));
switchButtons(source, source + 19);
} catch (IOException error) {
System.out.println("Error, problem reading image");
}
}
public void runLeft(int source) {
// direction left = 3
try {
Image moveLeftImage = ImageIO.read(new File(
"src/pics/Character-Left.fw.png"));
setIcon(new ImageIcon(moveLeftImage));
switchButtons(source, source - 1);
} catch (IOException error) {
System.out.println("Error, problem reading image");
}
}
public void runRight(int source) {
// direction right = 4
try {
Image moveRightImage = ImageIO.read(new File(
"src/pics/Character-Right.fw.png"));
setIcon(new ImageIcon(moveRightImage));
switchButtons(source, source + 1);
} catch (IOException error) {
System.out.println("Error, problem reading image");
}
}
public int getMoveSpeed() {
return moveSpeed;
}
public void pickUpMoveSpeed() {
moveSpeed++;
}
public int getArmor() {
return armor;
}
public void pickUpArmor(int i) {
armor = i;
}
@Override
public void keyTyped(KeyEvent e) {
}
public static int getNumCharacter() {
return numCharacter;
}
public static void setNumCharacter(int numCharacter) {
Character.numCharacter = numCharacter;
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
/**
if(key == KeyEvent.VK_Y){
System.out.println("#");
host.game[18].requestFocus();
}
else if(key == KeyEvent.VK_W){
System.out.println("3");
host.game[0].requestFocus();
}
**/
try {
Image moveRightImage = ImageIO.read(new File(
"src/pics/Character-Right.fw.png"));
Image moveUpImage = ImageIO.read(new File(
"src/pics/Character-Up.fw.png"));
Image moveDownImage = ImageIO.read(new File(
"src/pics/Character-Down.fw.png"));
Image moveLeftImage = ImageIO.read(new File(
"src/pics/Character-Left.fw.png"));
if (key == moveLeft) {
try {
if (getDirection() != 3) {
setIcon(new ImageIcon(moveLeftImage));
setDirection(3);
} else if (this.getY() == host.game[source - 1].getY()
&& this.getX() - 35 == host.game[source - 1].getX()
&& getDirection() == 3
&& host.game[source - 1].getType() == 0) {
if (host.game[source - 1].getName() == "Destroyed" ||
host.game[source - 1].getName() == "IncreaseRange" ||
host.game[source - 1].getName() == "IncreaseSpeed" ||
host.game[source - 1].getName() == "IncreaseArmor" ||
host.game[source - 1].getName() == "IncreaseBomb" ||
host.game[source - 1].getName() == "IncreaseExplodeSpeed") {
if (((DestroyableObject) host.game[source - 1]).getIsPowerup() == true) {
((DestroyableObject) host.game[source - 1]).toGrass(source-1, this);
runLeft(source);
source = source - 1;
}
else {
runLeft(source);
source = source - 1;
}
}
else {
runLeft(source);
source = source - 1;
}
}
} catch (NullPointerException f) {
System.out.println("I can't move there");
}
}
if (key == moveRight) {
try {
if (getDirection() != 4) {
setIcon(new ImageIcon(moveRightImage));
setDirection(4);
} else if (this.getY() == host.game[source + 1].getY()
&& this.getX() + 35 == host.game[source + 1].getX()
&& getDirection() == 4
&& host.game[source + 1].getType() == 0) {
if (host.game[source + 1].getName() == "Destroyed" ||
host.game[source + 1].getName() == "IncreaseRange" ||
host.game[source + 1].getName() == "IncreaseSpeed" ||
host.game[source + 1].getName() == "IncreaseArmor" ||
host.game[source + 1].getName() == "IncreaseBomb" ||
host.game[source + 1].getName() == "IncreaseExplodeSpeed") {
if (((DestroyableObject) host.game[source + 1]).getIsPowerup() == true) {
((DestroyableObject) host.game[source + 1]).toGrass(source+1, this);
runRight(source);
source = source + 1;
}
else {
runRight(source);
source = source + 1;
}
}
else {
runRight(source);
source = source + 1;
}
}
} catch (NullPointerException f) {
System.out.println("I can't move there");
}
}
if (key == moveUp) {
try {
if (getDirection() != 1) {
setIcon(new ImageIcon(moveUpImage));
setDirection(1);
} else if (this.getY() - 35 == host.game[source - 19]
.getY()
&& this.getX() == host.game[source - 19].getX()
&& getDirection() == 1
&& host.game[source - 19].getType() == 0) {
if (host.game[source - 19].getName() == "Destroyed" ||
host.game[source - 19].getName() == "IncreaseRange" ||
host.game[source - 19].getName() == "IncreaseSpeed" ||
host.game[source - 19].getName() == "IncreaseArmor" ||
host.game[source - 19].getName() == "IncreaseBomb" ||
host.game[source - 19].getName() == "IncreaseExplodeSpeed") {
if (((DestroyableObject) host.game[source - 19]).getIsPowerup() == true) {
((DestroyableObject) host.game[source - 19]).toGrass(source-19, this);
runUp(source);
source = source - 19;
}
else {
runUp(source);
source = source - 19;
}
}
else {
runUp(source);
source = source - 19;
}
}
} catch (NullPointerException f) {
System.out.println("I can't move there");
}
}
if (key == moveDown) {
try {
if (getDirection() != 2) {
setIcon(new ImageIcon(moveDownImage));
setDirection(2);
} else if (this.getY() + 35 == host.game[source + 19]
.getY()
&& this.getX() == host.game[source + 19].getX()
&& getDirection() == 2
&& host.game[source + 19].getType() == 0) {if (host.game[source + 19].getName() == "Destroyed" ||
host.game[source + 19].getName() == "IncreaseRange" ||
host.game[source + 19].getName() == "IncreaseSpeed" ||
host.game[source + 19].getName() == "IncreaseArmor" ||
host.game[source + 19].getName() == "IncreaseBomb" ||
host.game[source + 19].getName() == "IncreaseExplodeSpeed") {
if (((DestroyableObject) host.game[source + 19]).getIsPowerup() == true) {
((DestroyableObject) host.game[source + 19]).toGrass(source+19, this);
runDown(source);
source = source + 19;
}
else {
runDown(source);
source = source + 19;
}
}
else {
runDown(source);
source = source + 19;
}
}
} catch (NullPointerException f) {
System.out.println("I can't move there");
}
}
if (key == placeBomb) {
bomb = new Bomb(this.getX(), this.getY(), getDirection(),
source, this, host, bombExplodeTime, bombRange);
add(bomb);
}
} catch (ArrayIndexOutOfBoundsException f) {
System.out.println("I can't move there");
} catch (IOException g) {
System.out.println("Error, problem reading image");
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
private void switchButtons(int one, int two) {
Rectangle temp = this.getBounds();
this.setBounds(host.game[two].getBounds());
host.game[two].setBounds(temp);
Map tempOne = host.game[two];
host.game[two] = this;
host.game[one] = tempOne;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public Bomberman getHost() {
return host;
}
}