-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
253 lines (215 loc) · 8.48 KB
/
Copy pathGamePanel.java
File metadata and controls
253 lines (215 loc) · 8.48 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
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import java.util.List;
public class GamePanel extends JPanel implements KeyListener, ActionListener{
static final int PANEL_WIDTH = 700;
static final int PANEL_HEIGHT = 700;
private int spaceShipX = 325; //initial x position of the spaceship
private int spaceShipY = 560; //initial y position of the spaceship
private int spaceShipHealth = 3;
private final int spaceshipWidth = 50; // width of the spaceship
private final int spaceshipHeight = 50; // height of the spaceship
private int spaceShipSpeed = 10;
private List<Rectangle> bullets;
public static List<Alien> aliens;
public static final Object syncObject = new Object();
public static int score = 0;
public static boolean gameOver = false;
int delay = 2000;
int level = 1;
static String activeUser;
Image backgroundImage = new ImageIcon("images/spaceImage.png").getImage();
public List<Alien> getAliens(){
return aliens;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
//spaceship
Image spaceshipImage = new ImageIcon("images/spaceship.png").getImage();
int spaceshipCenterX = spaceShipX + spaceshipWidth / 2;
int spaceshipImageCenterX = spaceshipImage.getWidth(null) / 2;
int spaceshipOffsetX = spaceshipCenterX - spaceshipImageCenterX;
// Draw the spaceship with the adjusted x position
g.drawImage(spaceshipImage, spaceshipOffsetX, spaceShipY, null);
//bullets
g.setColor(Color.WHITE);
for(Rectangle bullet : bullets){
g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
//aliens
for(Alien alien : aliens){
alien.draw(g);
}
//score
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.valueOf(score), 350, 20);
//spaceship health
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("\u2665"+" x"+ spaceShipHealth,150, 20);
//level
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Level: "+ level,40, 20);
}
public GamePanel() {
this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
setFocusable(true);
addKeyListener(this);
bullets = new ArrayList<>();
aliens = new ArrayList<>();
Timer timer = new Timer(20, this);
timer.start();
spawnAliens();
}
private void nextLevel() { //Score her 500 arttığında level artışı ve uzaylıların çıkış hızının artışı
if (score % 500 == 0 && score >= 500) {
level++;
Alien.alienSpeed += 0.5;
delay -= 300;
System.out.println(Alien.alienSpeed);
System.out.println(level);
System.out.println(delay);
}
}
private void spawnAliens(){
Timer alienSpawnTimer = new Timer(delay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!GamePanel.gameOver){
synchronized (syncObject){
aliens.add(new Alien());
}
}
}
});
alienSpawnTimer.start();
}
private void updateCharacterPosition(){
if (isKeyPressed(KeyEvent.VK_UP)) {
spaceShipY -= spaceShipSpeed; // Move character up
} else if (isKeyPressed(KeyEvent.VK_DOWN)) {
spaceShipY += spaceShipSpeed; // Move character down
} else if (isKeyPressed(KeyEvent.VK_LEFT)) {
spaceShipX -= spaceShipSpeed; // Move character left
} else if (isKeyPressed(KeyEvent.VK_RIGHT)) {
spaceShipX += spaceShipSpeed; // Move character right
}
spaceShipX = Math.max(0, Math.min(spaceShipX, getWidth() - spaceshipWidth));
spaceShipY = Math.max(0, Math.min(spaceShipY, getWidth() - spaceshipHeight));
repaint(); // Redraw the panel to update the character's position
}
private void updateBulletPosition(){
Iterator<Rectangle> bulletIterator = bullets.iterator();
while(bulletIterator.hasNext()){
Rectangle bullet = bulletIterator.next();
bullet.y -= 5;
if(bullet.y + bullet.height < 0 ){ //remove bullets that go off the top of the panel
bulletIterator.remove();
}
}
}
private void checkCollisions(){
Iterator<Rectangle> bulletIterator = bullets.iterator();
while(bulletIterator.hasNext()){
Rectangle bullet = bulletIterator.next();
Iterator<Alien> alienIterator = aliens.iterator();
while(alienIterator.hasNext()){
Alien alien = alienIterator.next();
if(bullet.intersects(alien.getX(), alien.getY(), Alien.alienWidth, Alien.alienHeight)){
alien.takeDamage();
bulletIterator.remove();
if(alien.isDestroyed()){
if(alien.alienImage.equals(Alien.alienImages.get(0).getImage())||
alien.alienImage.equals(Alien.alienImages.get(1).getImage())||
alien.alienImage.equals(Alien.alienImages.get(2).getImage())){
alienIterator.remove();
score+=10; //increase the score by 10 if one of the purple aliens
}else {
alienIterator.remove();
score+=5; //increase the score by 5 if one of the green aliens
}
nextLevel();
}
}
}
}
Rectangle spaceShipRectangle = new Rectangle(spaceShipX, spaceShipY, spaceshipWidth, spaceshipHeight);
for(Alien alien : aliens){
if(spaceShipRectangle.intersects(alien.getX(), alien.getY(), Alien.alienWidth, Alien.alienHeight))
gameOver=true;
else if(alien.hasCollision(spaceShipRectangle)){
spaceShipHealth--;
if(spaceShipHealth == 0)
gameOver=true;
}
}
}
private void checkGameOver() throws IOException {
if(gameOver){
for (Alien a: aliens) {
a.stop();
}
synchronized (syncObject){
aliens.clear();
}
bullets.clear();
MyFrame frame = (MyFrame) SwingUtilities.getWindowAncestor(this);
frame.getContentPane().remove(this); // Remove the current GamePanel from the frame
GameOverPanel gameOverPanel = new GameOverPanel();
frame.setContentPane(gameOverPanel); // Set the GameOverPanel as the new content pane
frame.pack();
frame.revalidate();
frame.repaint();
FileUtil.updateScore(activeUser, StringUtil.padRight(String.valueOf(score), 5, ' '));
}
}
private boolean isKeyPressed(int keyCode){
return keyPressedSet.contains(keyCode);
}
private final HashSet<Integer> keyPressedSet = new HashSet<>();
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
keyPressedSet.add(keyCode);
if(keyCode==KeyEvent.VK_SPACE){
int bulletX = spaceShipX + spaceshipWidth / 2 - 2;
Rectangle bullet = new Rectangle(bulletX, spaceShipY, 4, 10);
bullets.add(bullet);
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
keyPressedSet.remove(keyCode);
}
@Override
public void actionPerformed(ActionEvent e) {
if(!gameOver){
updateCharacterPosition();
updateBulletPosition();
synchronized (syncObject){
for(Alien alien: aliens){
alien.updateAlienPosition();
}
}
checkCollisions();
try {
checkGameOver();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
repaint();
}
}
}