Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.Timer;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.net.URL;
  7. import java.util.*;
  8. import java.util.TimerTask;
  9.  
  10.  
  11. /**
  12. * Basic GUI for very basic "Tower Defence" game
  13. */
  14. public class GUITowerDefence extends JFrame {
  15.  
  16. // A map that assigns a panel to each position in the game
  17. private final Map<Position, JPanel> positionPanels = new HashMap<>();
  18.  
  19. // The size of each position panel
  20. private static final int POSITION_SIZE = 100;
  21.  
  22. // A timer that will automatically advance the game each second.
  23. private final Timer timer;
  24. private static final int SPEED = 1000;
  25. private static final int PAUSE = 300;
  26.  
  27. // A representation of the complete game
  28. private TowerDefenceLevel level;
  29. static Game game;
  30.  
  31. public static void main(String[] args) {
  32.  
  33. // Change this to try out different levels
  34. TowerDefenceLevel level = TowerDefenceLevel.buildDefaultLevel();
  35. game = new Game(level);
  36. // Create the GUI and set it to be visible
  37. GUITowerDefence gui = new GUITowerDefence(level);
  38. gui.setVisible(true);
  39.  
  40. }
  41.  
  42. public GUITowerDefence(TowerDefenceLevel level) {
  43.  
  44.  
  45. this.setTitle("Tower Defence");
  46. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.  
  48. this.level = level;
  49. int levelHeight = level.getHeight();
  50. int levelWidth = level.getWidth();
  51.  
  52. this.setSize(levelWidth*POSITION_SIZE, levelHeight*POSITION_SIZE);
  53. this.setResizable(false);
  54.  
  55. // A 'main panel' that contains all other components of the GUI
  56. JPanel mainPanel = new JPanel();
  57. mainPanel.setLayout(new GridLayout(levelHeight, levelWidth));
  58. this.add(mainPanel);
  59.  
  60. for (int row = 0; row < levelHeight; row++) {
  61. for (int col = 0; col < levelWidth; col++) {
  62. JPanel positionPanel = new JPanel();
  63. positionPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  64. mainPanel.add(positionPanel);
  65. // Add the panel to the 'positionPanels' map so we can access it
  66. // later (with positionPanels.get(position)).
  67. Position position = level.getPosition(row,col);
  68. positionPanels.put(position, positionPanel);
  69. if (!level.passable[row][col]){
  70. positionPanel.setBackground(Color.green);
  71. }
  72. }
  73. }
  74.  
  75. //Position position = level.getPosition(2,2);
  76. //positionPanels.get(position).add(buildMonsterPanel(12));
  77. // Start the timer and set it to call the event loop each second
  78. EventLoop loop = new EventLoop();
  79. timer = new Timer(SPEED, loop);
  80. timer.setInitialDelay(PAUSE);
  81. timer.start();
  82.  
  83. TimerTask task = new TimerTask() {
  84. @Override
  85. public void run() {
  86.  
  87. }
  88. };
  89.  
  90. }
  91.  
  92. // ---------- Event handling --------------------
  93.  
  94. class EventLoop implements ActionListener {
  95. @Override
  96. public void actionPerformed(ActionEvent actionEvent) {
  97. //System.out.println("YOLO");
  98. game.oneRound();
  99. Position position = level.getPosition(2,2);
  100.  
  101. //positionPanels.get(position).setBackground(Color.black);
  102. // Here you can implement the logic to advance the game by one step
  103. // and update the GUI.
  104.  
  105.  
  106. boolean gameOver = false; // TODO
  107.  
  108. if (gameOver) {
  109. setTitle("Game over!");
  110. timer.stop();
  111. }
  112.  
  113. // These two commands are necessary to properly
  114. // display all the updated elements of the GUI.
  115. revalidate();
  116. repaint();
  117.  
  118. }
  119. }
  120.  
  121. // ----------- Helper methods ---------------------
  122.  
  123. // Helper method to construct a JLabel with a given image
  124. private JLabel getIconLabel(String fileName) {
  125. URL url = this.getClass().getResource(fileName);
  126. ImageIcon ii = new ImageIcon(url);
  127. return new JLabel(ii);
  128. }
  129.  
  130. // Just some examples, you can change them however you like.
  131. private JLabel buildTowerLabel() {
  132. return getIconLabel("icons/tower-icon.png");
  133. }
  134.  
  135. private JPanel buildMonsterPanel(int monsterHealth) {
  136. JPanel panel = new JPanel();
  137. panel.setBackground(Color.WHITE);
  138. panel.setLayout(new BorderLayout());
  139.  
  140. JLabel monsterIcon = getIconLabel("icons/monster10.gif");
  141. panel.add(monsterIcon, BorderLayout.CENTER);
  142.  
  143. JLabel healthLabel = new JLabel(Integer.toString(monsterHealth));
  144. healthLabel.setFont(new Font("Serif", Font.BOLD, 10));
  145. panel.add(healthLabel, BorderLayout.SOUTH);
  146.  
  147. return panel;
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement