-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimationGame.java
More file actions
277 lines (226 loc) · 6.7 KB
/
Copy pathAnimationGame.java
File metadata and controls
277 lines (226 loc) · 6.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This is a template for an animation game using SWING.
// There are things that need fixing (see comments)
// Other classes have been added at the end of this file. This is a BAD THING.
//They should be in separate files or else inner classes. I've done it this way because it's easier for you to download.
/* Use WASD to move player. Use SPACE to fire laser.
* Game stops when all enemies are off the screen. */
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.*;
//This version handles multiple keys at the same time
//Wow! This is lightning fast, especially if you reduce the timer time.
public class AnimationGame implements ActionListener {
public static void main(String[] args) {
new AnimationGame();
}
static int panW = 800, panH = 500;
JFrame window;
JPanel panel;
Player player;
Timer timerPlayer, timerMain;
ArrayList<Enemy> enemyList = new ArrayList<Enemy>();
ArrayList<Laser> laserList = new ArrayList<Laser>();
MyKL mainKL = new MyKL();
AnimationGame(){
//make game objects
player = new Player();
for (int i = 0; i < Enemy.MAXENEMIES; i++) {
enemyList.add(new Enemy()); //make them at random locations
}
//setup GUI
window = new JFrame("Starting animation game");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new DrawingPanel();
window.add(panel); // the panel will control the size
window.pack(); //therefore we need pack
window.setLocationRelativeTo(null);
window.setVisible(true);
timerPlayer = new Timer(10, this);
timerMain = new Timer (10, new mainAL());
timerPlayer.start();
timerMain.start();
}
/** This controls the game, except for player interaction. **/
class mainAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
moveEnemies();
moveLasers();
checkCollisions();
checkWin();
}
}
void moveEnemies() {
for (Enemy enemy : enemyList) {
enemy.y += enemy.speed;
}
//remove them when they go off the bottom of the screen
for (Enemy enemy : enemyList) {
if (enemy.y > panH) {
enemyList.remove(enemy);
break;
}
}
}
void moveLasers() {
for (Laser z: laserList) {
z.y += z.speed;
}
//remove them when they go off the top of the screen
for (Laser z: laserList) {
if (z.y < 0) {
laserList.remove(z);
break;
}
}
}
private long lastShotTime = System.currentTimeMillis();
void shoot() {
if (laserList.size() >= 7) return;
//has more than SHOTDELAY time passed since last shot?
if (System.currentTimeMillis() - lastShotTime < Laser.SHOTDELAY) return;
lastShotTime = System.currentTimeMillis();
laserList.add(new Laser(player));
}
void checkCollisions() {
//see if enemy hits player:
for (int i = 0; i < enemyList.size(); i++) {
Enemy en = enemyList.get(i);
if (en.intersects(player)) {
player.health -= en.dmg;
enemyList.remove(i);
i--;
}
}
}
/* Purpose: determine if the game is over
* Called from: EnemyAL (which moves the enemy)
*/
void checkWin() {
if (player.health <= 0) {
//pop up message that the game is over
System.out.println("game over!"); //TEMPORARY FIX
panel.repaint(); //to update player health bar
timerPlayer.stop();
timerMain.stop();
}
if (enemyList.size() == 0) {
System.out.println("YOU WIN!"); //TEMPORARY FIX
panel.repaint();
timerPlayer.stop();
timerMain.stop();
}
}
//for Player timer
@Override
public void actionPerformed(ActionEvent e) {
//get keys and move player
if (mainKL.isKeyDown('A') && player.x > 0) player.x-=player.speed;
if (mainKL.isKeyDown('D') && player.x+player.width < panW) player.x+=player.speed;
if (mainKL.isKeyDown('W') && player.y > 0) player.y-=player.speed;
if (mainKL.isKeyDown('S') && player.y+player.height < panH) player.y+=player.speed;
//shoot
if (mainKL.isKeyDown(' ')) shoot();
panel.repaint();
}
private class DrawingPanel extends JPanel {
DrawingPanel(){
//this.setBackground(new Color(60,60,60));
this.setPreferredSize(new Dimension(panW,panH));
this.addKeyListener(mainKL);
this.setFocusable(true); // need something like this to get focus
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.ORANGE);
g.fillRect(player.x, player.y, player.width, player.height);
//** Draw health bar **
g.setColor(Color.GREEN);
if (player.health < 45) g.setColor(Color.RED);
g.fillRect(player.x, player.y-10, player.width*player.health/100, 10);
g.setColor(Color.GRAY);
g.drawRect(player.x, player.y-10, player.width, 10);
//draw lasers
g.setColor(Laser.c);
for (Laser z: laserList) {
g.fillRect(z.x, z.y, z.width, z.height);
}
//enemies
g2.setStroke(new BasicStroke(3));
g.setColor(Color.BLUE);
for (Enemy e: enemyList) {
g.drawOval(e.x, e.y, e.width, e.height);
}
}
} //end of drawing panel
class MyKL implements KeyListener {
private boolean[] keys = new boolean[256]; //hopefully default=false
boolean isKeyDown(int n) {
return keys[n];
}
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
@Override
public void keyTyped(KeyEvent e) {} //slow!!!
}
}
class Enemy extends Rectangle {
static final int MAXENEMIES = 50;
int speed = 3;
int dmg = 15; //damage
//This is needed for the player class????
Enemy(int x, int y, int w, int h) {
this.x = x;
this.y = y;
width = w;
height = h;
}
// They start above the top of screen (-50 to -150)
Enemy() {
this.x = (int) (Math.random()*(AnimationGame.panW-15)) + 30;
this.y = (int) (Math.random()*200) - 250;
width = height = 15;
speed = (int) (Math.random()*4)+1;
}
}
class Player extends Enemy {
int health = 100;
Player(){
this(AnimationGame.panW/2 , AnimationGame.panH-200 , 100, 60);
}
Player(int x, int y, int w, int h) {
super(x, y, w, h);
//change speed here:
}
}
class Laser extends Rectangle {
static int speed = -3;
static Color c = Color.BLACK;
static final int MAXSHOTS = 7; //how many on screen
static final int SHOTDELAY = 100; //ms
Laser(Player p) {
x = p.x+p.width/2;
y = p.y - 10;
width = 2;
height = 10;
}
}