/** * Draws the overlay. * * @param g - Graphics */ private void paintOverlay(Graphics g) { for (int i = 0; i < player.getHealth(); i++) { g.drawImage(imgHealth, GAP + i * HEALTH_SIZE, GAP, HEALTH_SIZE, HEALTH_SIZE, this); } g.drawImage(imgCoinCount, getWidth() - COIN_SIZE, GAP, COIN_SIZE, COIN_SIZE - GAP, this); g.setFont(new Font("Verdana", Font.BOLD, FONT_SIZE)); g.drawString("" + player.getCoins(), getWidth() - COIN_STRING_POS, COIN_STRING_POS); }
/** Key handling. */ @Override public void keyPressed(final KeyEvent arg0) { switch (arg0.getKeyCode()) { case KeyEvent.VK_UP: { up = true; break; } case KeyEvent.VK_LEFT: { left = true; break; } case KeyEvent.VK_RIGHT: { right = true; break; } case KeyEvent.VK_SPACE: { setShoot(true); System.out.println("shootin"); break; } case KeyEvent.VK_ESCAPE: { player.setHealth(0); } } }
/** * Starts Game. * * @param pTUI - runs parallel TUI if true */ public void startGame(boolean pTUI) { getGraphics().drawImage(imgBackground, 0, 0, getWidth(), getHeight(), this); if (pTUI) { new TUI(landscape); } landscape.addAnObserver(this); action = ACTION_NORMAL; up = false; right = false; left = false; shoot = false; player.pause(PAUSE_LONG); requestFocus(); landscape.start(); new Thread() { public void run() { while (player.getHealth() > 0 && !player.isInGoal()) { player.pause(PAUSE_SHORT); if (up) { action = ACTION_JUMP; landscape.jump(); } if (right) { action = ACTION_RIGHT; landscape.right(); } if (left) { action = ACTION_LEFT; landscape.left(); } if (shoot) { action = ACTION_SHOOT; landscape.shoot(); } } if (player.isInGoal()) { JOptionPane.showMessageDialog( gui, "You won!", "Congratulations!", JOptionPane.INFORMATION_MESSAGE); } else if (player.getHealth() == 0) { JOptionPane.showMessageDialog(gui, "Player died!", "Game over!", JOptionPane.OK_OPTION); } main.reset(); } }.start(); }
@Override public void start(boolean arg0) { player.setHealth(0); }
/** * Draws the player. * * @param g - Graphics */ private void paintPlayer(Graphics g) { switch (action) { case ACTION_JUMP: { g.drawImage( imgPlayerJump, player.getX(), player.getY(), player.getWidth(), player.getHeight(), this); break; } case ACTION_NORMAL: { g.drawImage( imgPlayerNormal, player.getX(), player.getY(), player.getWidth(), player.getHeight(), this); break; } case ACTION_RIGHT: { g.drawImage( imgPlayerRight, player.getX(), player.getY(), player.getWidth(), player.getHeight(), this); break; } case ACTION_LEFT: { g.drawImage( imgPlayerLeft, player.getX(), player.getY(), player.getWidth(), player.getHeight(), this); break; } } }