-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
134 lines (117 loc) · 5.75 KB
/
Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// imports necessary libraries for Java swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.BadLocationException;
/**
* Game Main class that specifies the frame and widgets of the GUI
*/
public class Game implements Runnable {
public void run() {
// NOTE : recall that the 'final' keyword notes immutability even for local variables.
// Top-level frame in which game components live
// Be sure to change "TOP LEVEL FRAME" to the name of your game
final JFrame frame = new JFrame("The Escape of Blob");
// Status panel
final JPanel statusPanel = new JPanel();
frame.add(statusPanel, BorderLayout.SOUTH);
final JLabel status = new JLabel("Running...");
statusPanel.add(status);
// Pause button
final JButton pauseButton = new JButton("Pause");
// Main playing area
final GameCourt court = new GameCourt(status, pauseButton);
frame.add(court, BorderLayout.CENTER);
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.togglePause();
}
});
// Control panel
final JPanel gameControlPanel = new JPanel();
frame.add(gameControlPanel, BorderLayout.NORTH);
// Reset button
final JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.reset();
}
});
gameControlPanel.add(resetButton);
// Instructions panel
DefaultStyledDocument document = new DefaultStyledDocument();
JTextPane instructions = new JTextPane(document);
StyleContext context = new StyleContext();
Style style = context.addStyle("test", null);
instructions.setFont(new Font("sansserif", Font.PLAIN, 20));
String text = "THE BLOB NEEDS TO GET OUT\n\nBlake the blob monster doesn't know where it" +
" is. All it knows is that it is in a cold, damp, and scary place with strange" +
" noises echoing from the walls. Help Blake use its goo generation powers to" +
" navigate the labyrinthe and find the portal to escape.\n\nINSTRUCTIONS\n\nHold" +
" down arrow keys to to move\nTap the WASD keys to shoot goo in all 4 directions\n" +
"Press P to pause\n\nYou have 4 health points displayed on the upper right corner of" +
" the screen. You lose a half heart when you get shot and a full heart when you" +
" touch an enemy. When you run out of health, you lose. Enter the portal, and you" +
" win. You can't leave a room unless you have defeated all of the enemies inside." +
"\n\nTIPS\n\nThe main character will change direction smoothly when multiple arrows" +
" keys are held down simultaneously (e.g. if you hold down the left arrow key," +
" then additionally hold down the right arrow key, then release the right arrow key," +
" Blake will go left, then right, then left again). Get a feel for the controls" +
" before leaving the starting room (Blake's safe place), and try to fire rapidly!";
try {
document.insertString(0, text, style);
} catch (BadLocationException e) {
System.out.println("Instructions Document Error:" + e.getMessage());;
}
// Instructions control panel
final JPanel instructionsControlPanel = new JPanel();
// Instructions open button
final JButton instructionsButton = new JButton("Instructions");
instructionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
court.togglePause();
frame.remove(court);
frame.revalidate();
frame.remove(gameControlPanel);
frame.revalidate();
frame.add(instructions, BorderLayout.CENTER);
frame.revalidate();
frame.add(instructionsControlPanel, BorderLayout.NORTH);
frame.revalidate();
}
});
gameControlPanel.add(instructionsButton);
gameControlPanel.add(pauseButton);
// Instructions close button
final JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(instructions);
frame.revalidate();
frame.remove(instructionsControlPanel);
frame.revalidate();
frame.add(court, BorderLayout.CENTER);
frame.revalidate();
frame.add(gameControlPanel, BorderLayout.NORTH);
frame.revalidate();
court.togglePause();
}
});
instructionsControlPanel.add(closeButton);
// Put the frame on the screen
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Start game
court.reset();
}
/**
* Main method run to start and run the game. Initializes the GUI elements specified in Game
* and runs it. IMPORTANT: Do NOT delete! You MUST include this in your final submission.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Game());
}
}