-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePane.java
More file actions
133 lines (132 loc) · 4.97 KB
/
Copy pathGamePane.java
File metadata and controls
133 lines (132 loc) · 4.97 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
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
import java.util.ConcurrentModificationException;
class GamePane extends JPanel implements KeyListener {
private Dimension windowDimensions;
static ArrayList<ArrayList<GameObject>> gameObjects;
static ArrayList<GameObject> ships;
static ArrayList<GameObject> asteroids;
static ArrayList<GameObject> bullets;
ScoreBoard scoreboard;
PlayerShip playerShip;
GamePane(Dimension windowDimensions) {
super();
setBackground(new Color(0,0,0));
this.windowDimensions = windowDimensions;
scoreboard = new ScoreBoard(5,10);
ships = new ArrayList<GameObject>() {{
Random random = new Random();
int height = (int) windowDimensions.getHeight();
int width = (int) windowDimensions.getWidth();
add(new PlayerShip(random.nextInt(width),random.nextInt(height)));
add(new AlienShip(random.nextInt(width),random.nextInt(height)));
}};
asteroids = new ArrayList<GameObject>() {{
Random random = new Random();
int height = (int) windowDimensions.getHeight();
int width = (int) windowDimensions.getWidth();
add(new Asteroid(random.nextInt(width),random.nextInt(height),Size.LARGE));
add(new Asteroid(random.nextInt(width),random.nextInt(height),Size.MEDIUM));
add(new Asteroid(random.nextInt(width),random.nextInt(height),Size.SMALL));
}};
bullets = new ArrayList<GameObject>();
gameObjects = new ArrayList<ArrayList<GameObject>>() {{
add(ships);
add(asteroids);
add(bullets);
}};
playerShip = (PlayerShip)gameObjects.get(0).get(0);
}
@Override
protected void paintComponent(Graphics g) {
Random random = new Random();
super.paintComponent(g);
g.setColor(Color.GREEN);
scoreboard.paint(g);
try {
for(GameObject asteroid : asteroids) {
for(GameObject bullet : bullets) {
if(bullet.hitbox.intersects(asteroid.hitbox)) {
scoreboard.updateScore(asteroid.size.getPoints());
if(asteroid.size != Size.SMALL) {
Size newSize = (asteroid.size == Size.LARGE) ? Size.MEDIUM : Size.SMALL;
int numCreated = random.nextInt(1)+2;
for(int i = 0; i < numCreated; i++) {
asteroids.add(new Asteroid(asteroid.x,asteroid.y,newSize));
}
asteroids.remove(asteroid);
} else {
asteroids.remove(asteroid);
}
bullets.remove(bullet);
}
}
if(asteroid.hitbox.intersects(playerShip.hitbox)) {
ships.remove(playerShip);
System.exit(0);
}
}
} catch(ConcurrentModificationException cme) {
}
for (GameObject ship : ships) {
if(ship instanceof AlienShip) {
for (GameObject bullet : bullets) {
if(bullet.hitbox.intersects(ship.hitbox)) {
scoreboard.updateScore(ship.size.getPoints());
ships.remove(ship);
bullets.remove(bullets);
}
}
if(ship.hitbox.intersects(playerShip.hitbox)) {
ships.remove(playerShip);
System.exit(0);
}
}
}
for (ArrayList<GameObject> objectList : gameObjects) {
for (GameObject gObj : objectList) {
Graphics2D g2D = (Graphics2D) g.create();
gObj.update();
gObj.paint(g2D);
g2D.dispose();
}
}
}
@Override
public void keyPressed(KeyEvent ke) {
switch(ke.getKeyCode()) {
case KeyEvent.VK_SPACE:
playerShip.updateTrajectory();
repaint();
break;
case KeyEvent.VK_B:
bullets.add(new Bullet(playerShip.x,playerShip.y,playerShip.trajectory));
repaint();
break;
case KeyEvent.VK_LEFT:
playerShip.rotateShip(-1);
repaint();
break;
case KeyEvent.VK_RIGHT:
playerShip.rotateShip(1);
repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent ke) {
//NOT USED
}
@Override
public void keyTyped(KeyEvent ke) {
//NOT USED
}
}