Última atividade 1719509743

Revisão 66e9db5871b00ae596df605fcc2e84a7c726a9ff

SpaceInvadersGame.java Bruto
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
5import java.awt.event.KeyEvent;
6import java.awt.event.KeyListener;
7import java.util.ArrayList;
8import java.util.List;
9import java.util.Random;
10
11// Read the code, and then explain it!
12// Do so WITHOUT running it to see what it does.
13
14public class SpaceInvadersGame extends JPanel implements KeyListener, ActionListener {
15 private int playerX, playerY;
16 private List<Alien> aliens;
17 private List<Bullet> bullets;
18 private boolean isGameOver;
19 private int score;
20
21 private SpaceInvadersGame() {
22 playerX = 300;
23 playerY = 480;
24 isGameOver = false;
25 score = 0;
26
27 aliens = new ArrayList<>();
28 bullets = new ArrayList<>();
29
30 // Initialize aliens
31 for (int i = 0; i < 6; i++) {
32 aliens.add(new Alien(i * 100, 0));
33 }
34
35 Timer timer = new Timer(15, this);
36 timer.start();
37
38 addKeyListener(this);
39 setFocusable(true);
40 setFocusTraversalKeysEnabled(false);
41 }
42
43 public void paintComponent(Graphics g) {
44 super.paintComponent(g);
45 // Draw background
46 g.setColor(Color.black);
47 g.fillRect(0, 0, 800, 600);
48
49 // Draw player
50 g.setColor(Color.blue);
51 g.fillRect(playerX, playerY, 50, 50);
52
53 // Draw aliens
54 for (Alien alien : aliens) {
55 g.setColor(Color.green);
56 g.fillRect(alien.x, alien.y, 50, 50);
57 }
58
59 // Draw bullets
60 for (Bullet bullet : bullets) {
61 g.setColor(Color.red);
62 g.fillRect(bullet.x, bullet.y, 10, 20);
63 }
64
65 // Display score
66 g.setColor(Color.white);
67 g.setFont(new Font("Arial", Font.PLAIN, 20));
68 g.drawString("Score: " + score, 10, 30);
69
70 if (isGameOver) {
71 g.setFont(new Font("Arial", Font.PLAIN, 50));
72 g.drawString("Game Over", 300, 300);
73 }
74 }
75
76 public void actionPerformed(ActionEvent e) {
77 if (!isGameOver) {
78 // Move aliens
79 for (Alien alien : aliens) {
80 alien.y += 2;
81 }
82
83 // Move bullets
84 for (Bullet bullet : bullets) {
85 bullet.y -= 5;
86 }
87
88 // Check for collisions
89 checkCollisions();
90
91 // Remove off-screen aliens and bullets
92 aliens.removeIf(alien -> alien.y > 600);
93 bullets.removeIf(bullet -> bullet.y < 0);
94
95 // Spawn new aliens
96 if (aliens.isEmpty()) {
97 for (int i = 0; i < 6; i++) {
98 aliens.add(new Alien(i * 100, 0));
99 }
100 }
101
102 repaint();
103 }
104 }
105
106 private void checkCollisions() {
107 for (Alien alien : new ArrayList<>(aliens)) {
108 for (Bullet bullet : new ArrayList<>(bullets)) {
109 if (alien.getBounds().intersects(bullet.getBounds())) {
110 aliens.remove(alien);
111 bullets.remove(bullet);
112 score += 10;
113 }
114 }
115
116 if (alien.getBounds().intersects(new Rectangle(playerX, playerY, 50, 50))) {
117 isGameOver = true;
118 }
119 }
120 }
121
122 public void keyTyped(KeyEvent e) {}
123
124 public void keyPressed(KeyEvent e) {
125 int key = e.getKeyCode();
126
127 if (key == KeyEvent.VK_LEFT && playerX > 0) {
128 playerX -= 20;
129 }
130
131 if (key == KeyEvent.VK_RIGHT && playerX < 750) {
132 playerX += 20;
133 }
134
135 if (key == KeyEvent.VK_SPACE) {
136 if (!isGameOver) {
137 bullets.add(new Bullet(playerX + 20, playerY));
138 } else {
139 // Restart the game
140 isGameOver = false;
141 playerX = 300;
142 score = 0;
143 aliens.clear();
144 bullets.clear();
145 for (int i = 0; i < 6; i++) {
146 aliens.add(new Alien(i * 100, 0));
147 }
148 }
149 }
150 }
151
152 public void keyReleased(KeyEvent e) {}
153
154 public static void main(String[] args) {
155 JFrame frame = new JFrame("Space Invaders");
156 SpaceInvadersGame game = new SpaceInvadersGame();
157 frame.add(game);
158 frame.setSize(800, 600);
159 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
160 frame.setVisible(true);
161 }
162
163 private class Alien {
164 int x, y;
165
166 Alien(int x, int y) {
167 this.x = x;
168 this.y = y;
169 }
170
171 Rectangle getBounds() {
172 return new Rectangle(x, y, 50, 50);
173 }
174 }
175
176 private class Bullet {
177 int x, y;
178
179 Bullet(int x, int y) {
180 this.x = x;
181 this.y = y;
182 }
183
184 Rectangle getBounds() {
185 return new Rectangle(x, y, 10, 20);
186 }
187 }
188}