import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.util.ArrayList; import javax.swing.JFrame; /* * Main class for asteroids game. */ public class Asteroids { private Ship ship; private ArrayList asteroids; private ArrayList bullets; private int score; public static void main(String[] args) { Asteroids game = new Asteroids(); AsteroidsFrame frame = new AsteroidsFrame(game); frame.setSize(600,600); frame.setTitle("Asteroids"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Asteroids() { score = 0; ship = new Ship(200,200,0,0,30,30,0,0,5); asteroids = new ArrayList(); // Add three asteroids of different sizes // Level 3 Asteroid asteroid = new Asteroid(100,100,0,0,60,60,3); asteroids.add(asteroid); // Level 2 asteroid = new Asteroid(200,100,0,0,40,40,2); asteroids.add(asteroid); // Level 1 asteroid = new Asteroid(300,100,0,0,20,20,1); asteroids.add(asteroid); bullets = new ArrayList(); Bullet bullet = new Bullet(212,180,0,0); bullets.add(bullet); } public void draw(Graphics2D g2) { // Draw the ship ship.draw(g2); // Draw the asteroids for (int i =0; i < asteroids.size(); i++) { Asteroid asteroid = asteroids.get(i); asteroid.draw(g2); } // Draw the bullets for (int i =0; i < bullets.size(); i++) { Bullet bullet = bullets.get(i); bullet.draw(g2); } // Draw the score g2.drawString("Score: "+score, 500, 20); } /** * Rotates a shape around a point. This should be the midpoint of the shape. * @param x * @param y * @param shape * @param degrees * @return */ public static Shape rotateShape(int x, int y, Shape shape, int degrees) { AffineTransform transform = new AffineTransform(); transform.rotate(degrees*Math.PI/180,x ,y); return transform.createTransformedShape(shape); } }