?? gamepanel.java
字號:
package dungeonsanddragons.game;import dungeonsanddragons.model.Castle;import dungeonsanddragons.model.Door;import dungeonsanddragons.model.Exit;import dungeonsanddragons.model.Player;import dungeonsanddragons.model.Room;import dungeonsanddragons.model.Showable;import dungeonsanddragons.model.Thing;import dungeonsanddragons.model.monsters.Monster;import dungeonsanddragons.model.weapons.Weapon;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.IOException;import java.util.Date;import javax.swing.JOptionPane;import javax.swing.JPanel;/** * The panel showing the game board * and controlling the game, the movements and the drawing of the board * @author Sandra Nilsson */public class GamePanel extends JPanel implements KeyListener { /** * The castle that decribes this current board */ private Castle c; /** * The current player */ private Player p; /** * The status panel */ private GameStatus statusPanel; private Date start; /** * Creates a new memory panel, reads the images from file and creates a new board */ public GamePanel(GameStatus gs) { this.addKeyListener(this); this.setFocusable(true); this.statusPanel = gs; createBoard(); setIgnoreRepaint(true); start = new Date(); } /** * Method to paint on the panel * This method is called automatically by the system when we tell the paint to repaint itself. * Overrides paint in the Canvas object * @param graphics the graphics object to paint on */ public void paintComponent(Graphics graphics) { //Do not change this one draw(); } /** * Method to draw the game board according to the current state of the game **/ public void draw() { try { //Get the graphics object of this panel Graphics g = this.getGraphics(); //Draw the background image (the rooms); g.drawImage(c.getCastleImage(), 0, 0, GameFrame.ROOM_SIZE * GameFrame.SIZE, GameFrame.ROOM_SIZE * GameFrame.SIZE, this); //Draw all room items Room[][] rooms = c.getRooms(); for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { if (rooms[i][j].getItem() != null) { rooms[i][j].getItem().draw(g); } } } //Draw the player p.draw(g); } catch (IOException ex) { System.out.println("Could not draw image"); ex.printStackTrace(); } } /** * Method to create a new board (start the game) * @throws java.io.IOException */ private void createBoard() { c = new Castle(); p = new Player(); p.setCurrentRoom(c.getStartRoom()); statusPanel.updateStatus(p); randomizeBoard(); } /** * Method to reset and start the game again */ private void restart(){ statusPanel.reset(); createBoard(); } /** * Method to end the game * @param win true if the game ends by winning, false otherwise */ private void endGame(boolean win) { System.out.println("Ended game"); Date end=new Date(); long seconds = (end.getTime()-start.getTime())/1000; int total = p.calculateTotalPoints(seconds); if (win) { int option = JOptionPane.showConfirmDialog(this, "Game has ended. It took" +seconds+ "seconds and you got " + total + " points. Play again?", "Exited", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.OK_OPTION) { restart(); } } else { int option = JOptionPane.showConfirmDialog(this, "Game has ended, you have died.I took "+total+" points. Play again?", "LOST", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.OK_OPTION) { restart(); } else { this.removeKeyListener(this); } } } /** * Mehtod to randomly distribute all items over the board */ private void randomizeBoard() { //Randomize monsters, treasures and weapons c.randomizeMonsters(); c.randomizeTreasures(); c.randomizeWeapons(); c.randomizeAidKit(); } /** * Method called when key is typed. * Is not needed for this application, therefore empty * @param e the KeyEvent generated */ public void keyTyped(KeyEvent e) { } /** * Method called when any key is pressed * Is not needed for this application, therefore empty * @param e the KeyEvent generated */ public void keyPressed(KeyEvent e) { } /** * Method called when any key is released. * This we use for moving the player * @param e the KeyEvent generated */ public void keyReleased(KeyEvent e) { System.out.println("Key released "); if (isMoving(e.getKeyCode())) { Door d = p.move(e.getKeyCode()); if (d instanceof Exit) { endGame(true); } else if (d != null) { draw(); Showable item = p.getCurrentRoom().getItem(); if (item != null) { if (item instanceof Monster) { if (p.canBeBitten((Monster) item)) { p.bitten((Monster) item); } else { ((Monster) item).kill(); } statusPanel.updateStatus(p); } else if (item instanceof Thing) { ((Thing) item).giveTo(p); statusPanel.updateStatus(p); if (item instanceof Weapon) { statusPanel.updateWeapons(p); } } } if (p.getHealthStatus() <= 0) { endGame(false); } } } } /** * Method to check if it was any of the keys that is moving the player that is typed * @param keyCode the key code of the key press to check * @return true if any of the keys arrow up, arrow down, arrow left or arrow right */ private boolean isMoving(int keyCode) { return keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_KP_LEFT || keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_KP_RIGHT || keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_KP_DOWN; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -