This repository was archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInvadersKeys.html
More file actions
73 lines (63 loc) · 2.99 KB
/
InvadersKeys.html
File metadata and controls
73 lines (63 loc) · 2.99 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
<!DOCTYPE html>
<html>
<title>League Invaders - Drawing Shapes</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://league-central.github.io/curriculum/style/style.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<script src="https://league-central.github.io/curriculum/script/headerAndFooter.js"></script>
<body>
<div id ="wrap">
<div id ="main">
<div id = "moduleIndex">
<h1>Game Controls</h1>
<h2>Enter and Arrow Keys</h2>
<br>
<br>
<p class="normal">
1. The game will change states each time the ENTER key is pressed. This means the <code>GamePanel</code> class must implement the <code>KeyListener</code> interface (and all its unimplemented methods). The GamePanel class should now look like this:</p>
<pre>
public class GamePanel extends JPanel
implements ActionListener, KeyListener{ }
</pre>
<br>
<p class="normal">
2. In the <code>keyPressed()</code> method, if ENTER is pressed, we want the game to change to the next state as shown below:
</p>
<pre>
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
if (currentState == END) {
currentState = MENU;
} else {
currentState++;
}
}
</pre>
<br>
<p class="normal">
3. We also want to be notified if any of the arrow keys are pressed, since they will eventually be controlling the movement of our game character. For now, just add print statements to verify that the arrow keys are "working". See below for an example of how this would work for the "up arrow" key. NOTE: Only check the arrow keys when the game is in GAME state.
<pre>
if (e.getKeyCode()==KeyEvent.VK_UP) {
System.out.println("UP");
}
</pre>
<br>
<p class="normal">
4. Add code for the other 3 arrow keys on the keyboard (DOWN, LEFT, RIGHT).</p>
<br>
<h2>LeagueInvaders</h2>
<p class="normal">
5. In the <code>LeagueInvaders</code> class. Add a <code>KeyListener</code> to your <code>JFrame</code>. The <code>GamePanel</code> object is the listener, so the code might look something like this:</p>
<pre>
frame.addKeyListener(panel);
</pre>
<h3><u>TESTING</u></h3>
<p class="normal">
6. Run your program. Every time the ENTER key is pressed, you should see the game window change color (blue, black, red, blue, black, red, etc.) When the window is red, you should now see your END game text. Each of the arrow keys should print a message to the console (the timer events are still printing "action"). <br>
<u>Do not continue until you have this working</u>.
</p>
</div></div></div>
<div id="footer">
<script>addFooter();</script>
</div>
</body></html>