-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
203 lines (150 loc) · 5.27 KB
/
GameWindow.java
File metadata and controls
203 lines (150 loc) · 5.27 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameWindow extends JFrame implements ActionListener, KeyListener, MouseListener{
private JButton start;
private JButton pause;
private JButton resume;
private JButton exit;
private boolean started;
private boolean isJumping;
private boolean won;
private boolean lost;
private Container c;
private JPanel mainPanel;
private GamePanel gamePanel;
private JPanel infoPanel;
private JLabel score;
private JLabel instructionsLabel;
@SuppressWarnings({"unchecked"})
public GameWindow(){
setTitle("Jumpy Monkey");
setSize(700, 700);
mainPanel = new JPanel();
FlowLayout flowLayout = new FlowLayout();
mainPanel.setLayout(flowLayout);
started = false;
isJumping = false;
lost = false;
won= false;
// add mainPanel to window surface
c = getContentPane();
c.add(mainPanel);
// create the gamePanel for game entities
gamePanel = new GamePanel();
gamePanel.setPreferredSize(new Dimension(600, 600));
gamePanel.createGameEntities();
instructionsLabel = new JLabel("<html> Press Start to begin! <br> Press SPACE to make Baby Monkey jump. <br> Watch out for Mommy Monkey! <html>", SwingConstants.CENTER);
instructionsLabel.setFont(new Font("Arial", Font.BOLD, 16));
// add gamePanel to mainPanel
mainPanel.add(gamePanel);
gamePanel.add(instructionsLabel);
//make sure it focuses on space key
mainPanel.addKeyListener(this);
mainPanel.setFocusable(true);
mainPanel.requestFocusInWindow();
//create buttons
start = new JButton("Start");
pause = new JButton("Pause");
resume = new JButton("Resume");
exit = new JButton("Exit");
//add action listeners to buttons
start.addActionListener(this);
pause.addActionListener(this);
resume.addActionListener(this);
exit.addActionListener(this);
//create buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
//Label
score = new JLabel();
score.setText("Score: " + gamePanel.getSleepBarNumber());
//create info panel
infoPanel = new JPanel();
infoPanel.setLayout(new FlowLayout());
//add score to infopanel
infoPanel.add(score);
//add buttons to buttonPanel
buttonPanel.add(start);
buttonPanel.add(pause);
buttonPanel.add(resume);
buttonPanel.add(exit);
// add mainPanel to window surface
c = getContentPane();
c.add(mainPanel);
mainPanel.add(buttonPanel);
mainPanel.add(infoPanel);
// set properties of window
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == start){
gamePanel.createGameEntities();
gamePanel.drawGameEntities();
mainPanel.requestFocus();
gamePanel.setPause(false);
gamePanel.runMommy();
started = true;
lost = false;
won = false;
}
if (e.getSource() == pause){
gamePanel.setPause(true);
gamePanel.pauseGameEntities();
score.setText("Score: " + gamePanel.getSleepBarNumber()+ " (Paused)");
mainPanel.requestFocus();
}
if(e.getSource() == resume){
if(!lost && !won){
gamePanel.setPause(false);
gamePanel.resumeGameEntities();
score.setText("Score: " + gamePanel.getSleepBarNumber());
mainPanel.requestFocus();
}
}
if(e.getSource()==exit){
System.exit(0);
}
}
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_SPACE && !isJumping && started){
gamePanel.jump();
score.setText("Score: " + gamePanel.getSleepBarNumber());
if(!gamePanel.getPause())
isJumping = true;
if((isJumping && gamePanel.getInRoomStatus()) && !won){
lost = true;
gamePanel.caught(true);
gamePanel.printLoss();
gamePanel.setPause(true);
}
if((gamePanel.getSleepBarNumber()==150) && !lost){
won = true;
gamePanel.printWin();
gamePanel.pauseGameEntities();
gamePanel.setPause(true);
}
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_SPACE){
isJumping = false;
}
}
public void keyTyped(KeyEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}