Subscribe NOW

Enter your email address:

Text Message our CEO:

650-283-8008

or on twitter

Free Resources

Click Here to learn more

In The Media

Missile Command

by Larry Chiang on February 19, 2025

image0.png
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);
    }
}

 
 
Shibetoshi Nakamoto
⁦‪@BillyM2k‬⁩
people were saying the grok 3 ai made asteroids clone was lame because it didn’t have the asteroids break into smaller asteroids and there was no score, so i had grok add those features by asking it to

the whole process took like 5 minutes

we are all software engineers now pic.x.com/OKYOvKH0IU

 
2/19/25, 9:17 AM
 
 


WordPress’d from my personal iPhone, 650-283-8008, number that Steve Jobs texted me on

https://www.YouTube.com/watch?v=ejeIz4EhoJ0

Leave a Comment

Previous post:

Next post: