-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerpentAquaPistolGame.java
101 lines (92 loc) · 3.03 KB
/
SerpentAquaPistolGame.java
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class SerpentAquaPistolGame extends JPanel implements ActionListener {
private final Serpent serpent;
private final ArrayList<Bullet> bullets;
private final ArrayList<Target> targets;
private final Timer timer;
public SerpentAquaPistolGame() {
serpent = new Serpent();
bullets = new ArrayList<>();
targets = new ArrayList<>();
timer = new Timer(30, this);
setBackground(Color.CYAN);
setFocusable(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT -> serpent.moveLeft();
case KeyEvent.VK_RIGHT -> serpent.moveRight();
case KeyEvent.VK_SPACE -> shoot();
}
}
});
initializeTargets();
timer.start();
}
private void initializeTargets() {
for (int i = 0; i < 5; i++) {
targets.add(new Target(i * 100 + 50, 50));
}
}
private void shoot() {
bullets.add(new Bullet(serpent.getX() + Serpent.WIDTH / 2, serpent.getY()));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
serpent.draw(g);
for (Bullet bullet : bullets) {
bullet.draw(g);
}
for (Target target : targets) {
target.draw(g);
}
}
@Override
public void actionPerformed(ActionEvent e) {
moveBullets();
checkCollisions();
repaint();
}
private void moveBullets() {
Iterator<Bullet> bulletIterator = bullets.iterator();
while (bulletIterator.hasNext()) {
Bullet bullet = bulletIterator.next();
bullet.move();
if (bullet.getY() < 0) {
bulletIterator.remove();
}
}
}
private void checkCollisions() {
Iterator<Bullet> bulletIterator = bullets.iterator();
while (bulletIterator.hasNext()) {
Bullet bullet = bulletIterator.next();
Iterator<Target> targetIterator = targets.iterator();
while (targetIterator.hasNext()) {
Target target = targetIterator.next();
if (bullet.getBounds().intersects(target.getBounds())) {
bulletIterator.remove();
targetIterator.remove();
break;
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Serpent Aqua Pistol Game");
SerpentAquaPistolGame game = new SerpentAquaPistolGame();
frame.add(game);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}