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