|
| 1 | +import games.Game; |
| 2 | +import games.crosstheroad.CrossTheRoadGame; |
| 3 | +import games.flappybird.FlappyBirdGame; |
| 4 | +import games.jumpinggame.JumpingGame; |
| 5 | +import games.pong.PongGame; |
| 6 | +import games.snake.SnakeGame; |
| 7 | +import games.tetris.board.TetrisGame; |
| 8 | +import games.tictactoe.TicTacToe; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.awt.event.KeyEvent; |
| 13 | +import java.awt.event.KeyListener; |
| 14 | +import java.util.ArrayList; |
| 15 | + |
| 16 | +public class GUI extends JFrame { |
| 17 | + private final JPanel jPanel = new Menu(); |
| 18 | + |
| 19 | + private int frameWidth = 400; |
| 20 | + private int frameHeight = 800; |
| 21 | + |
| 22 | + |
| 23 | + public GUI() { |
| 24 | + |
| 25 | + this.setBounds(600, 200, frameWidth, frameHeight); |
| 26 | + |
| 27 | + jPanel.setBackground(Color.BLACK); |
| 28 | + jPanel.setLayout(new GridLayout(0, 1)); |
| 29 | + jPanel.setVisible(true); |
| 30 | + addKeyListener((KeyListener) jPanel); |
| 31 | + this.add(jPanel); |
| 32 | + |
| 33 | + |
| 34 | + this.setVisible(true); |
| 35 | + } |
| 36 | + |
| 37 | + private class Menu extends JPanel implements KeyListener { |
| 38 | + |
| 39 | + private final ArrayList<GameInfo> games = new ArrayList<>(); |
| 40 | + |
| 41 | + private int gameNum = 0; |
| 42 | + private int selectedGame = 1; |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + public Menu() { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + private void addGame(GameInfo game) { |
| 51 | + games.add(game); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void paintComponent(Graphics g) { |
| 56 | + super.paintComponent(g); |
| 57 | + games.clear(); |
| 58 | + gameNum = 0; |
| 59 | + |
| 60 | + g.drawLine(frameWidth / 2, 0, frameWidth / 2, frameHeight); |
| 61 | + |
| 62 | + g.setColor(Color.RED); |
| 63 | + g.drawString("Chose a game to play", (frameWidth / 2) - (g.getFontMetrics().stringWidth("Chose a game to play") / 2), 20); |
| 64 | + |
| 65 | + g.setColor(Color.RED); |
| 66 | + |
| 67 | + addGame(new GameInfo("Snake", new SnakeGame())); |
| 68 | + addGame(new GameInfo("Pong", new PongGame())); |
| 69 | + addGame(new GameInfo("FlappyBird", new FlappyBirdGame())); |
| 70 | + addGame(new GameInfo("TicTacToe", new TicTacToe())); |
| 71 | + addGame(new GameInfo("JumpingGame", new JumpingGame())); |
| 72 | + addGame(new GameInfo("Tetris", new TetrisGame())); |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + gameNum = games.size(); |
| 77 | + |
| 78 | + drawGames(g); |
| 79 | + |
| 80 | + //Draw title |
| 81 | + g.drawRect(0, (int) (frameHeight * (0.1 * (selectedGame)) - 25), frameWidth, 40); |
| 82 | + |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + private void drawGames(Graphics g) { |
| 87 | + for (int i = 1; i <= games.size(); i++) |
| 88 | + g.drawString(games.get(i - 1).getName(), 10, (int) (frameHeight * (0.1 * i))); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void keyTyped(KeyEvent e) { |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void keyPressed(KeyEvent e) { |
| 98 | + if (e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) { |
| 99 | + if (selectedGame == gameNum) |
| 100 | + selectedGame = 0; |
| 101 | + selectedGame++; |
| 102 | + repaint(); |
| 103 | + } else if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) { |
| 104 | + if (selectedGame == 1) |
| 105 | + selectedGame = gameNum + 1; |
| 106 | + selectedGame--; |
| 107 | + repaint(); |
| 108 | + } else if (e.getKeyChar() == KeyEvent.VK_ENTER) { |
| 109 | + games.get(selectedGame - 1).getGame().startGame(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void keyReleased(KeyEvent e) { |
| 115 | + |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private class GameInfo { |
| 120 | + private final String name; |
| 121 | + private final Game game; |
| 122 | + |
| 123 | + public GameInfo(String name, Game game) { |
| 124 | + this.name = name; |
| 125 | + this.game = game; |
| 126 | + } |
| 127 | + |
| 128 | + public String getName() { |
| 129 | + return name; |
| 130 | + } |
| 131 | + |
| 132 | + public Game getGame() { |
| 133 | + return game; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
| 138 | + |
0 commit comments