Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.34 KB | None | 0 0
  1. package whackajavamole;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Image;
  6. import java.awt.Point;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.FileReader;
  15. import java.io.FileWriter;
  16. import java.io.IOException;
  17. import java.util.Random;
  18.  
  19. import javax.swing.ImageIcon;
  20. import javax.swing.JButton;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.SwingConstants;
  26. import javax.swing.Timer;
  27. import javax.swing.border.EmptyBorder;
  28.  
  29. public class Game extends JFrame {
  30.  
  31.     private JPanel panel;
  32.     private JLabel[] holes = new JLabel[16];
  33.     private int[] board = new int[16];
  34.  
  35.     private int score = 0;
  36.     private int timeLeft = 30;
  37.     private int highscore = 0;
  38.  
  39.     private JLabel lblScore;
  40.     private JLabel lblTimeLeft;
  41.     private JLabel lblHighscore;
  42.  
  43.     private JButton btnStart;
  44.     private Timer timer;
  45.  
  46.     private void loadHighscore() {
  47.         BufferedReader br = null;
  48.         String line = "";
  49.         try {
  50.             br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/highscore.txt"));
  51.             line = br.readLine();
  52.             br.close();
  53.         } catch (IOException e) {
  54.             line = "";
  55.         }
  56.  
  57.         if (line != "") {
  58.             highscore = Integer.parseInt(line);
  59.             lblHighscore.setText("Highscore: " + highscore);
  60.         }
  61.     }
  62.  
  63.     private void saveHighscore() {
  64.         BufferedWriter bw = null;
  65.         try {
  66.             bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/highscore.txt", false)); //append - set to false
  67.             bw.write("" + highscore);
  68.             bw.flush();
  69.             bw.close();
  70.         } catch (IOException e) {
  71.             JOptionPane.showMessageDialog(this, e.getMessage(), "Error while saving highscore", JOptionPane.ERROR_MESSAGE);
  72.         }
  73.     }
  74.  
  75.     private void gameOver() {
  76.         btnStart.setEnabled(true);
  77.         if (score > highscore) {
  78.             highscore = score;
  79.             lblHighscore.setText("Highscore: " + highscore);
  80.             JOptionPane.showMessageDialog(this, "Your final score is: " + score, "You beat the high score!", JOptionPane.INFORMATION_MESSAGE);
  81.         } else {
  82.             JOptionPane.showMessageDialog(this, "Your final score is: " + score, "Game Over!", JOptionPane.INFORMATION_MESSAGE);
  83.         }
  84.         score = 0;
  85.         timeLeft = 30;
  86.         lblScore.setText("Score: 0");
  87.         lblTimeLeft.setText("30");
  88.  
  89.         clearBoard();
  90.  
  91.         saveHighscore();
  92.     }
  93.  
  94.     private void pressedButton(int id) {
  95.         int val = board[id];
  96.  
  97.         //if val is 1 = mole
  98.         //if val is 0 = empty hole
  99.         if (val == 1) {
  100.             score++;
  101.         } else { //val==0
  102.             score--;
  103.         }
  104.  
  105.         lblScore.setText("Score: " + score); //update the score
  106.  
  107.         clearBoard();
  108.  
  109.         genRandMole();
  110.     }
  111.  
  112.     private void initEvents() {
  113.         for (int i = 0; i < holes.length; i++) {
  114.             holes[i].addMouseListener(new MouseAdapter() {
  115.                 public void mouseClicked(MouseEvent e) {
  116.                     JLabel lbl = (JLabel) e.getSource();
  117.                     int id = Integer.parseInt(lbl.getName());
  118.                     pressedButton(id);
  119.                 }
  120.             });
  121.         }
  122.  
  123.         btnStart.addActionListener(new ActionListener() {
  124.             public void actionPerformed(ActionEvent e) {
  125.                 btnStart.setEnabled(false);
  126.                 clearBoard();
  127.                 genRandMole();
  128.                 timer.start();
  129.             }
  130.         });
  131.  
  132.         timer = new Timer(1000, new ActionListener() {
  133.             public void actionPerformed(ActionEvent evt) {
  134.                 if (timeLeft == 0) {
  135.                     lblTimeLeft.setText("" + timeLeft);
  136.                     timer.stop();
  137.                     gameOver();
  138.                 }
  139.                 lblTimeLeft.setText("" + timeLeft);
  140.                 timeLeft--;
  141.             }
  142.         });
  143.     }
  144.  
  145.     private void initGUI() {
  146.         setTitle("Whack A Mole");
  147.         setResizable(false);            // DONT TOUCH FALSE LANG DAPAT        
  148.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  149.         setBounds(100, 100, 608, 720);              // 100, 100, 608, 720
  150.  
  151.         JPanel contentPane = new JPanel();
  152.         contentPane = new JPanel();
  153.         contentPane.setBackground(new Color(0, 51, 51));        //0, 51, 51 background
  154.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));         // 5, 5, 5, 5
  155.         contentPane.setLayout(null);
  156.  
  157.         JLabel lblTitle = new JLabel("Whack A Mole");
  158.         lblTitle.setForeground(new Color(153, 204, 0));     //color ng title    153, 204, 0
  159.         lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
  160.         lblTitle.setFont(new Font("Century Gothic", Font.BOLD, 20));
  161.         lblTitle.setBounds(0, 0, 602, 47);          // 0, 0, 602, 47  
  162.         contentPane.add(lblTitle);
  163.  
  164.         panel = new JPanel();
  165.         panel.setBackground(new Color(0, 102, 0));
  166.         panel.setBounds(32, 105, 535, 546);             // 32, 105, 535, 546
  167.         panel.setLayout(null);
  168.         panel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
  169.                 loadImage("/hammer.png").getImage(),
  170.                 new Point(0, 0), "custom cursor1"));
  171.         contentPane.add(panel);
  172.  
  173.         holes[0] = new JLabel("0");
  174.         holes[0].setName("0");
  175.         holes[0].setBounds(0, 396, 132, 132);
  176.         panel.add(holes[0]);
  177.  
  178.         holes[1] = new JLabel("1");
  179.         holes[1].setName("1");
  180.         holes[1].setBounds(132, 396, 132, 132);
  181.         panel.add(holes[1]);
  182.  
  183.         holes[2] = new JLabel("2");
  184.         holes[2].setName("2");
  185.         holes[2].setBounds(264, 396, 132, 132);
  186.         panel.add(holes[2]);
  187.  
  188.         holes[3] = new JLabel("3");
  189.         holes[3].setName("3");
  190.         holes[3].setBounds(396, 396, 132, 132);
  191.         panel.add(holes[3]);
  192.  
  193.         holes[4] = new JLabel("4");
  194.         holes[4].setName("4");
  195.         holes[4].setBounds(0, 264, 132, 132);
  196.         panel.add(holes[4]);
  197.  
  198.         holes[5] = new JLabel("5");
  199.         holes[5].setName("5");
  200.         holes[5].setBounds(132, 264, 132, 132);
  201.         panel.add(holes[5]);
  202.  
  203.         holes[6] = new JLabel("6");
  204.         holes[6].setName("6");
  205.         holes[6].setBounds(264, 264, 132, 132);
  206.         panel.add(holes[6]);
  207.  
  208.         holes[7] = new JLabel("7");
  209.         holes[7].setName("7");
  210.         holes[7].setBounds(396, 264, 132, 132);
  211.         panel.add(holes[7]);
  212.  
  213.         holes[8] = new JLabel("8");
  214.         holes[8].setName("8");
  215.         holes[8].setBounds(0, 132, 132, 132);
  216.         panel.add(holes[8]);
  217.  
  218.         holes[9] = new JLabel("9");
  219.         holes[9].setName("9");
  220.         holes[9].setBounds(132, 132, 132, 132);
  221.         panel.add(holes[9]);
  222.  
  223.         holes[10] = new JLabel("10");
  224.         holes[10].setName("10");
  225.         holes[10].setBounds(264, 132, 132, 132);
  226.         panel.add(holes[10]);
  227.  
  228.         holes[11] = new JLabel("11");
  229.         holes[11].setName("11");
  230.         holes[11].setBounds(396, 132, 132, 132);
  231.         panel.add(holes[11]);
  232.  
  233.         holes[12] = new JLabel("12");
  234.         holes[12].setName("12");
  235.         holes[12].setBounds(0, 0, 132, 132);
  236.         panel.add(holes[12]);
  237.  
  238.         holes[13] = new JLabel("13");
  239.         holes[13].setName("13");
  240.         holes[13].setBounds(132, 0, 132, 132);
  241.         panel.add(holes[13]);
  242.  
  243.         holes[14] = new JLabel("14");
  244.         holes[14].setName("14");
  245.         holes[14].setBounds(264, 0, 132, 132);
  246.         panel.add(holes[14]);
  247.  
  248.         holes[15] = new JLabel("15");
  249.         holes[15].setName("15");
  250.         holes[15].setBounds(396, 0, 132, 132);
  251.         panel.add(holes[15]);
  252.  
  253.         lblScore = new JLabel("Score: 0");
  254.         lblScore.setHorizontalAlignment(SwingConstants.TRAILING);
  255.         lblScore.setFont(new Font("Cambria", Font.BOLD, 14));
  256.         lblScore.setForeground(new Color(135, 206, 250));
  257.         lblScore.setBounds(423, 54, 144, 33);       // 423, 54, 144, 33
  258.         contentPane.add(lblScore);
  259.  
  260.         lblTimeLeft = new JLabel("30");
  261.         lblTimeLeft.setHorizontalAlignment(SwingConstants.CENTER);
  262.         lblTimeLeft.setForeground(new Color(240, 128, 128));
  263.         lblTimeLeft.setFont(new Font("Cambria Math", Font.BOLD, 24));
  264.         lblTimeLeft.setBounds(232, 54, 144, 33);     // 232, 54, 144, 33
  265.         contentPane.add(lblTimeLeft);
  266.  
  267.         lblHighscore = new JLabel("Highscore: 0");
  268.         lblHighscore.setHorizontalAlignment(SwingConstants.TRAILING);
  269.         lblHighscore.setForeground(new Color(255, 255, 0));
  270.         lblHighscore.setFont(new Font("Cambria", Font.BOLD, 14));
  271.         lblHighscore.setBounds(433, 18, 134, 33);
  272.         contentPane.add(lblHighscore);
  273.  
  274.         btnStart = new JButton("Start");
  275.         btnStart.setBackground(Color.WHITE);
  276.         btnStart.setBounds(32, 60, 110, 33);
  277.         contentPane.add(btnStart);
  278.  
  279.         setContentPane(contentPane);
  280.     }
  281.  
  282.     private void clearBoard() {
  283.         for (int i = 0; i < 16; i++) {              // i < 16
  284.             holes[i].setIcon(loadImage("/moleIn.png"));
  285.             board[i] = 0;
  286.         }
  287.     }
  288.  
  289.     private void genRandMole() {
  290.         Random rnd = new Random(System.currentTimeMillis()); //seeding random with current time
  291.         int moleID = rnd.nextInt(16);
  292.         board[moleID] = 1;
  293.         holes[moleID].setIcon(loadImage("/moleOut.png"));
  294.        
  295.         int moleID2 = rnd.nextInt(16);
  296.         board[moleID2] = 1;
  297.         holes[moleID2].setIcon(loadImage("/moleOut2.png"));
  298.     }
  299.  
  300.     private ImageIcon loadImage(String path) {
  301.         Image image = new ImageIcon(this.getClass().getResource(path)).getImage();
  302.         Image scaledImage = image.getScaledInstance(132, 132, java.awt.Image.SCALE_SMOOTH);
  303.         return new ImageIcon(scaledImage);
  304.     }
  305.  
  306.     public Game() {
  307.         initGUI();
  308.         clearBoard();
  309.         initEvents();
  310.         loadHighscore();
  311.     }
  312.  
  313.     public static void main(String[] args) {
  314. //        Game frame = new Game();
  315. //        frame.setVisible(true);
  316.  
  317.         Menu menu = new Menu();
  318.         menu.setVisible(true);
  319.  
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement