SpaceInvadersGame.java
· 4.8 KiB · Java
Brut
import javax.swing.*;
import java.awt.*;
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 java.util.List;
import java.util.Random;
// Read the code, and then explain it!
// Do so WITHOUT running it to see what it does.
public class SpaceInvadersGame extends JPanel implements KeyListener, ActionListener {
private int playerX, playerY;
private List<Alien> aliens;
private List<Bullet> bullets;
private boolean isGameOver;
private int score;
private SpaceInvadersGame() {
playerX = 300;
playerY = 480;
isGameOver = false;
score = 0;
aliens = new ArrayList<>();
bullets = new ArrayList<>();
// Initialize aliens
for (int i = 0; i < 6; i++) {
aliens.add(new Alien(i * 100, 0));
}
Timer timer = new Timer(15, this);
timer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw background
g.setColor(Color.black);
g.fillRect(0, 0, 800, 600);
// Draw player
g.setColor(Color.blue);
g.fillRect(playerX, playerY, 50, 50);
// Draw aliens
for (Alien alien : aliens) {
g.setColor(Color.green);
g.fillRect(alien.x, alien.y, 50, 50);
}
// Draw bullets
for (Bullet bullet : bullets) {
g.setColor(Color.red);
g.fillRect(bullet.x, bullet.y, 10, 20);
}
// Display score
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.PLAIN, 20));
g.drawString("Score: " + score, 10, 30);
if (isGameOver) {
g.setFont(new Font("Arial", Font.PLAIN, 50));
g.drawString("Game Over", 300, 300);
}
}
public void actionPerformed(ActionEvent e) {
if (!isGameOver) {
// Move aliens
for (Alien alien : aliens) {
alien.y += 2;
}
// Move bullets
for (Bullet bullet : bullets) {
bullet.y -= 5;
}
// Check for collisions
checkCollisions();
// Remove off-screen aliens and bullets
aliens.removeIf(alien -> alien.y > 600);
bullets.removeIf(bullet -> bullet.y < 0);
// Spawn new aliens
if (aliens.isEmpty()) {
for (int i = 0; i < 6; i++) {
aliens.add(new Alien(i * 100, 0));
}
}
repaint();
}
}
private void checkCollisions() {
for (Alien alien : new ArrayList<>(aliens)) {
for (Bullet bullet : new ArrayList<>(bullets)) {
if (alien.getBounds().intersects(bullet.getBounds())) {
aliens.remove(alien);
bullets.remove(bullet);
score += 10;
}
}
if (alien.getBounds().intersects(new Rectangle(playerX, playerY, 50, 50))) {
isGameOver = true;
}
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && playerX > 0) {
playerX -= 20;
}
if (key == KeyEvent.VK_RIGHT && playerX < 750) {
playerX += 20;
}
if (key == KeyEvent.VK_SPACE) {
if (!isGameOver) {
bullets.add(new Bullet(playerX + 20, playerY));
} else {
// Restart the game
isGameOver = false;
playerX = 300;
score = 0;
aliens.clear();
bullets.clear();
for (int i = 0; i < 6; i++) {
aliens.add(new Alien(i * 100, 0));
}
}
}
}
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Space Invaders");
SpaceInvadersGame game = new SpaceInvadersGame();
frame.add(game);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class Alien {
int x, y;
Alien(int x, int y) {
this.x = x;
this.y = y;
}
Rectangle getBounds() {
return new Rectangle(x, y, 50, 50);
}
}
private class Bullet {
int x, y;
Bullet(int x, int y) {
this.x = x;
this.y = y;
}
Rectangle getBounds() {
return new Rectangle(x, y, 10, 20);
}
}
}
1 | import javax.swing.*; |
2 | import java.awt.*; |
3 | import java.awt.event.ActionEvent; |
4 | import java.awt.event.ActionListener; |
5 | import java.awt.event.KeyEvent; |
6 | import java.awt.event.KeyListener; |
7 | import java.util.ArrayList; |
8 | import java.util.List; |
9 | import java.util.Random; |
10 | |
11 | // Read the code, and then explain it! |
12 | // Do so WITHOUT running it to see what it does. |
13 | |
14 | public 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 | } |