
Now, do Missile Command
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class MissileCommand extends JPanel {
private ArrayList<Point> playerMissiles;
private ArrayList<Point> enemyMissiles;
private Point base;
private boolean gameOver;
private int score;
public MissileCommand() {
playerMissiles = new ArrayList<>();
enemyMissiles = new ArrayList<>();
base = new Point(400, 550);
gameOver = false;
score = 0;
Timer timer = new Timer(16, new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateGame();
repaint();
}
});
timer.start();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (!gameOver) {
playerMissiles.add(new Point(base.x, base.y));
score -= 10; // Cost to fire missile
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
if (!gameOver) {
base.x = e.getX();
}
}
});
// Spawn enemy missiles every second
new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
enemyMissiles.add(new Point((int)(Math.random()*800), 0));
}
}
}).start();
}
private void updateGame() {
// Move player missiles up
for (int i = 0; i < playerMissiles.size(); i++) {
playerMissiles.get(i).y -= 5;
}
// Move enemy missiles down
for (int i = 0; i < enemyMissiles.size(); i++) {
enemyMissiles.get(i).y += 3;
Point enemy = enemyMissiles.get(i);
// Check collision with player missiles
for (int j = 0; j < playerMissiles.size(); j++) {
Point player = playerMissiles.get(j);
if (Math.hypot(enemy.x – player.x, enemy.y – player.y) < 10) {
enemyMissiles.remove(i);
playerMissiles.remove(j);
score += 50;
i–;
break;
}
}
// Check if enemy hits base
if (Math.hypot(enemy.x – base.x, enemy.y – base.y) < 20) {
gameOver = true;
}
}
// Remove off-screen missiles
playerMissiles.removeIf(m -> m.y < 0);
enemyMissiles.removeIf(m -> m.y > 600);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 600);
// Draw base
g.setColor(Color.GREEN);
g.fillOval(base.x – 10, base.y – 10, 20, 20);
// Draw player missiles
g.setColor(Color.RED);
for (Point m : playerMissiles) {
g.fillOval(m.x – 2, m.y – 2, 4, 4);
}
// Draw enemy missiles
g.setColor(Color.WHITE);
for (Point m : enemyMissiles) {
g.fillOval(m.x – 2, m.y – 2, 4, 4);
}
// Draw score
g.setColor(Color.WHITE);
g.drawString(“Score: ” + score, 10, 20);
if (gameOver) {
g.drawString(“Game Over! Final Score: ” + score, 300, 300);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame(“Missile Command”);
MissileCommand game = new MissileCommand();
frame.add(game);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
|
||||||||||||||||||||||||
WordPress’d from my personal iPhone, 650-283-8008, number that Steve Jobs texted me on
https://www.YouTube.com/watch?v=ejeIz4EhoJ0

Duck9 is a credit score prep program that is like a Kaplan or Princeton Review test preparation service. We don't teach beating the SAT, but we do get you to a higher credit FICO score using secret methods that have gotten us on TV, Congress and newspaper articles. Say hi or check out some of our free resources before you pay for a thing. You can also text the CEO:







