-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceInvaders
204 lines (178 loc) · 5.39 KB
/
SpaceInvaders
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
204
// utility
import java.util.ArrayList;
import java.util.Random;
// graphics
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
// events
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
// swing
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SpaceInvaders extends JPanel implements ActionListener, KeyListener, Runnable {
private final int canvasWidth;
private final int canvasHeight;
private final Color backgroundColor;
private final int framesPerSecond = 25;
private final int msPerFrame = 1000 / framesPerSecond;
private Timer timer;
private int frame = 0;
// FIXME list your game objects here
/* Constructor for a Space Invaders game
*/
public SpaceInvaders() {
// fix the window size and background color
this.canvasWidth = 600;
this.canvasHeight = 400;
this.backgroundColor = Color.WHITE;
setPreferredSize(new Dimension(this.canvasWidth, this.canvasHeight));
// set the drawing timer
this.timer = new Timer(msPerFrame, this);
// FIXME initialize your game objects
}
/* Start the game
*/
@Override
public void run() {
// show this window
display();
// set a timer to redraw the screen regularly
this.timer.start();
}
/* Create the window and display it
*/
private void display() {
JFrame jframe = new JFrame();
jframe.addKeyListener(this);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setContentPane(this);
jframe.pack();
jframe.setVisible(true);
}
/* Run all timer-based events
*
* @param e An object describing the timer
*/
@Override
public void actionPerformed(ActionEvent e) {
// update the game objects
update();
// draw every object (this calls paintComponent)
repaint(0, 0, this.canvasWidth, this.canvasHeight);
// increment the frame counter
this.frame++;
}
/* Paint/Draw the canvas.
*
* This function overrides the paint function in JPanel. This function is
* automatically called when the panel is made visible.
*
* @param g The Graphics for the JPanel
*/
@Override
protected void paintComponent(Graphics g) {
// clear the canvas before painting
clearCanvas(g);
if (hasWonGame()) {
paintWinScreen(g);
} else if (hasLostGame()) {
paintLoseScreen(g);
} else {
paintGameScreen(g);
}
}
/* Clear the canvas
*
* @param g The Graphics representing the canvas
*/
private void clearCanvas(Graphics g) {
Color oldColor = g.getColor();
g.setColor(this.backgroundColor);
g.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
g.setColor(oldColor);
}
/* Respond to key release events
*
* A key release is when you let go of a key
*
* @param e An object describing what key was released
*/
public void keyReleased(KeyEvent e) {
// you can leave this function empty
}
/* Respond to key type events
*
* A key type is when you press then let go of a key
*
* @param e An object describing what key was typed
*/
public void keyTyped(KeyEvent e) {
// you can leave this function empty
}
/* Respond to key press events
*
* A key type is when you press then let go of a key
*
* @param e An object describing what key was typed
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
// FIXME what happens when left arrow is pressed
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
// FIXME what happens when right arrow is pressed
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// FIXME what happens when space bar is pressed
}
}
/* Update the game objects
*/
private void update() {
// FIXME update game objects here
}
/* Check if the player has lost the game
*
* @returns true if the player has lost, false otherwise
*/
private boolean hasLostGame() {
return false; // FIXME delete this when ready
}
/* Check if the player has won the game
*
* @returns true if the player has won, false otherwise
*/
private boolean hasWonGame() {
return false; // FIXME delete this when ready
}
/* Paint the screen during normal gameplay
*
* @param g The Graphics for the JPanel
*/
private void paintGameScreen(Graphics g) {
// FIXME draw game objects here
}
/* Paint the screen when the player has won
*
* @param g The Graphics for the JPanel
*/
private void paintWinScreen(Graphics g) {
// FIXME draw the win screen here
}
/* Paint the screen when the player has lost
*
* @param g The Graphics for the JPanel
*/
private void paintLoseScreen(Graphics g) {
// FIXME draw the game over screen here
}
public static void main(String[] args) {
SpaceInvaders invaders = new SpaceInvaders();
EventQueue.invokeLater(invaders);
}
}