├── workspace └── SpaceShooter │ ├── README │ ├── images │ ├── Exp1.gif │ ├── alien2.png │ ├── alien3.png │ ├── alien4.png │ ├── alien6.png │ ├── aliens.png │ ├── missile.png │ ├── explosion.jpg │ ├── explosion.png │ ├── missile2.png │ ├── spaceship.png │ └── spacecraft_explosion.gif │ ├── .classpath │ ├── .settings │ └── org.eclipse.jdt.core.prefs │ ├── .project │ └── src │ └── spaceshooter │ ├── launch │ └── Launch.java │ └── model │ ├── AlienArmy.java │ ├── Missile.java │ ├── Alien.java │ ├── Craft.java │ └── Board.java ├── README.md └── README /workspace/SpaceShooter/README: -------------------------------------------------------------------------------- 1 | My First Game. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | space-shooter 2 | ============= 3 | 4 | First Game -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | You can import it directly as an eclipse project. 2 | Launch.java is the Main class. 3 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/Exp1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/Exp1.gif -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/alien2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/alien2.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/alien3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/alien3.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/alien4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/alien4.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/alien6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/alien6.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/aliens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/aliens.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/missile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/missile.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/explosion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/explosion.jpg -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/explosion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/explosion.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/missile2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/missile2.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/spaceship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/spaceship.png -------------------------------------------------------------------------------- /workspace/SpaceShooter/images/spacecraft_explosion.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1-1/space-shooter/master/workspace/SpaceShooter/images/spacecraft_explosion.gif -------------------------------------------------------------------------------- /workspace/SpaceShooter/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sat Jan 05 21:38:46 IST 2013 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.6 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.6 13 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpaceShooter 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1357407718022 20 | images 21 | 5 22 | 23 | org.eclipse.ui.ide.multiFilter 24 | 1.0-name-matches-false-false-* 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/launch/Launch.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.launch; 5 | 6 | import javax.swing.JFrame; 7 | 8 | import spaceshooter.model.Board; 9 | 10 | /** 11 | * @author dey 12 | * 13 | */ 14 | public class Launch extends JFrame { 15 | private final static int DEFAULT_MAX_X = 600; 16 | private final static int DEFAULT_MAX_Y = 400; 17 | private final static String NAME_ON_TITLE = "SpaceShooter"; 18 | 19 | public void launch() { 20 | add(new Board(DEFAULT_MAX_X, DEFAULT_MAX_Y)); 21 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 | setSize(DEFAULT_MAX_X, DEFAULT_MAX_Y); 23 | setTitle(NAME_ON_TITLE); 24 | setLocationRelativeTo(null); 25 | setResizable(false); 26 | setVisible(true); 27 | 28 | } 29 | /** 30 | * @param args 31 | */ 32 | public static void main(String[] args) { 33 | Launch launcher = new Launch(); 34 | launcher.launch(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/model/AlienArmy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.model; 5 | 6 | import java.util.LinkedList; 7 | import java.util.List; 8 | import java.util.Random; 9 | 10 | import spaceshooter.model.Alien.ALIEN_DIR; 11 | import spaceshooter.model.Alien.ALIEN_SPEED; 12 | 13 | /** 14 | * @author dey 15 | * 16 | */ 17 | public class AlienArmy { 18 | private final static int MAX_ALIENS_PER_ITERATION = 4; 19 | private final static int RIGHT_BUFFER = 50; //in px 20 | private int it = 0; 21 | 22 | private List alienArmy; 23 | private final int MAX_X, MAX_Y; 24 | private final Random random; 25 | 26 | public AlienArmy(int MAX_X, int MAX_Y) { 27 | this.MAX_X = MAX_X; 28 | this.MAX_Y = MAX_Y; 29 | this.random = new Random(); 30 | alienArmy = new LinkedList(); 31 | addKAliens(random.nextInt(MAX_ALIENS_PER_ITERATION) + 1); 32 | } 33 | 34 | public List getAlienArmy() { 35 | return alienArmy; 36 | } 37 | 38 | public void addAlien() { 39 | addKAliens(1); 40 | } 41 | 42 | private void addKAliens(int k) { 43 | for (int i = 0; i < k && alienArmy.size() < MAX_ALIENS_PER_ITERATION; ++i) { 44 | // ALIEN_SPEED speed = ALIEN_SPEED.values()[random.nextInt(ALIEN_SPEED.values().length)]; 45 | ALIEN_SPEED speed = ALIEN_SPEED.SLOW; 46 | alienArmy.add(new Alien(random.nextInt(MAX_X - RIGHT_BUFFER), 1, MAX_X, MAX_Y, speed)); 47 | } 48 | } 49 | 50 | public void addAliens() { 51 | if (alienArmy.size() < MAX_ALIENS_PER_ITERATION) { 52 | addKAliens(random.nextInt(MAX_ALIENS_PER_ITERATION)); 53 | } 54 | } 55 | 56 | public void move() { 57 | ++it; 58 | for (Alien alien : alienArmy) { 59 | if (it % 5 == 0) { 60 | alien.move(ALIEN_DIR.DOWN); 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/model/Missile.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.model; 5 | 6 | 7 | import java.awt.Dimension; 8 | import java.awt.Image; 9 | import java.awt.Rectangle; 10 | 11 | import javax.swing.ImageIcon; 12 | 13 | /** 14 | * @author dey 15 | * 16 | */ 17 | public class Missile { 18 | private final static String MISSILE_IMAGE_LOCATION = "missile2.png"; 19 | private final static String EXPLOSION_IMAGE_LOCATION = "explosion.png"; 20 | 21 | private final static int MISSILE_SPEED = 2; 22 | private final int BOARD_MAX_X, BOARD_MAX_Y; 23 | private final int IMAGE_WIDTH, IMAGE_HEIGHT; 24 | 25 | public static enum MISSILE_DIR {UP, DOWN, LEFT, RIGHT}; 26 | 27 | private int x, y; 28 | private Image missileImage; 29 | private Image explosionImage; 30 | private boolean visible; 31 | 32 | public void setVisible(boolean visible) { 33 | this.visible = visible; 34 | } 35 | 36 | public boolean isVisible() { 37 | return visible; 38 | } 39 | 40 | public int getX() { 41 | return x; 42 | } 43 | 44 | public int getY() { 45 | return y; 46 | } 47 | 48 | public Image getMissileImage() { 49 | return missileImage; 50 | } 51 | 52 | public Image getExplosionImage() { 53 | return explosionImage; 54 | } 55 | 56 | public Missile(int x, int y, int width, int height) { 57 | missileImage = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(MISSILE_IMAGE_LOCATION)).getImage(); 58 | this.x = x; 59 | this.y = y; 60 | visible = true; 61 | BOARD_MAX_X = width; 62 | BOARD_MAX_Y = height; 63 | 64 | // explosionImage = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(EXPLOSION_IMAGE_LOCATION)).getImage(); 65 | 66 | IMAGE_WIDTH = missileImage.getWidth(null); 67 | IMAGE_HEIGHT = missileImage.getHeight(null); 68 | } 69 | 70 | public void move(MISSILE_DIR dir) { 71 | switch(dir) { 72 | case UP: 73 | y -= MISSILE_SPEED; 74 | break; 75 | case DOWN: 76 | y += MISSILE_SPEED; 77 | break; 78 | case LEFT: 79 | x -= MISSILE_SPEED; 80 | break; 81 | case RIGHT: 82 | x += MISSILE_SPEED; 83 | break; 84 | } 85 | if (x > BOARD_MAX_X || y > BOARD_MAX_Y || x < 0 || y < 0) { 86 | visible = false; 87 | } 88 | } 89 | 90 | public Rectangle getBounds() { 91 | return new Rectangle(x, y, IMAGE_WIDTH, IMAGE_HEIGHT); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/model/Alien.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.model; 5 | 6 | import java.awt.Image; 7 | import java.awt.Rectangle; 8 | import java.util.Random; 9 | 10 | import javax.swing.ImageIcon; 11 | 12 | /** 13 | * @author dey 14 | * 15 | */ 16 | public class Alien { 17 | private final static String ALIEN_IMAGE_LOCATION[] = { "alien6.png", "aliens.png", "alien4.png", "alien2.png", "alien3.png" }; 18 | private final static int BUFFER = 5; //5px; 19 | private final static Random random = new Random(); 20 | 21 | private int x, y; 22 | private Image image; 23 | 24 | private final int MAX_X, MAX_Y; 25 | private final int IMAGE_WIDTH, IMAGE_HEIGHT; 26 | 27 | private boolean visible; 28 | private int speed; 29 | 30 | public static enum ALIEN_DIR {UP, DOWN, LEFT, RIGHT} 31 | enum ALIEN_SPEED { 32 | SLOW(1), 33 | MEDIUM(2), 34 | FAST(2); 35 | 36 | private final int speed; 37 | 38 | public int getSpeed() { 39 | return speed; 40 | } 41 | 42 | ALIEN_SPEED(int val) { 43 | this.speed = val; 44 | } 45 | 46 | 47 | }; 48 | 49 | public Alien(int x, int y, int MAX_X, int MAX_Y, ALIEN_SPEED speed) { 50 | this.x = x; 51 | this.y = y; 52 | 53 | this.MAX_X = MAX_X; 54 | this.MAX_Y = MAX_Y; 55 | 56 | this.image = new ImageIcon(Thread.currentThread().getContextClassLoader() 57 | .getResource(ALIEN_IMAGE_LOCATION[random.nextInt(ALIEN_IMAGE_LOCATION.length)]) 58 | ).getImage(); 59 | this.speed = speed.getSpeed(); 60 | this.visible = true; 61 | 62 | IMAGE_WIDTH = image.getWidth(null); 63 | IMAGE_HEIGHT = image.getHeight(null); 64 | } 65 | 66 | public int getX() { 67 | return x; 68 | } 69 | 70 | public int getY() { 71 | return y; 72 | } 73 | 74 | public Image getImage() { 75 | return image; 76 | } 77 | 78 | public void setVisible(boolean visible) { 79 | this.visible = visible; 80 | } 81 | 82 | public boolean isVisible() { 83 | return visible; 84 | } 85 | 86 | public void move(ALIEN_DIR dir) { 87 | if (visible) { 88 | switch(dir) { 89 | case LEFT : x -= speed; 90 | break; 91 | case RIGHT : x += speed; 92 | break; 93 | case UP : y -= speed; 94 | break; 95 | case DOWN : y += speed; 96 | break; 97 | } 98 | 99 | if (x < 1 || y < 1 || x >= MAX_X || y >= MAX_Y) { 100 | visible = false; 101 | } 102 | } 103 | } 104 | 105 | public Rectangle getBounds() { 106 | return new Rectangle(x, y, IMAGE_WIDTH - BUFFER, IMAGE_HEIGHT - BUFFER); 107 | } 108 | 109 | public static void main(String[] args) { 110 | Alien alien = new Alien(0, 0, 0, 0, ALIEN_SPEED.SLOW); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/model/Craft.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.model; 5 | 6 | import java.awt.Image; 7 | import java.awt.Rectangle; 8 | import java.awt.event.KeyEvent; 9 | import java.awt.image.ImageObserver; 10 | import java.util.LinkedList; 11 | import java.util.List; 12 | 13 | import javax.swing.ImageIcon; 14 | 15 | /** 16 | * @author dey 17 | * 18 | */ 19 | public class Craft { 20 | private final static String craftLocation = "spaceship.png"; 21 | 22 | private final int IMAGE_WIDTH; 23 | private final int IMAGE_HEIGHT; 24 | private final int DEFAULT_MAX_X; 25 | private final int DEFAULT_MAX_Y; 26 | 27 | private int x, y, dx, dy; 28 | private Image image; 29 | private List missiles; 30 | private boolean visible; 31 | 32 | 33 | public Craft(int maxX, int maxY) { 34 | DEFAULT_MAX_X = maxX; 35 | DEFAULT_MAX_Y = maxY; 36 | 37 | x = maxX / 2; 38 | y = maxY; 39 | image = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(craftLocation)).getImage(); 40 | dx = 0; 41 | dy = 0; 42 | missiles = new LinkedList(); 43 | IMAGE_WIDTH = image.getWidth(null); 44 | IMAGE_HEIGHT = image.getHeight(null); 45 | visible = true; 46 | } 47 | 48 | public int getX() { 49 | return x; 50 | } 51 | 52 | public int getY() { 53 | return y; 54 | } 55 | 56 | public Image getImage() { 57 | return image; 58 | } 59 | 60 | public List getMissiles() { 61 | return missiles; 62 | } 63 | 64 | public void move() { 65 | if (visible) { 66 | x += dx; 67 | y += dy; 68 | if (x < 1) { 69 | x = 1; 70 | } 71 | if (y < 1) { 72 | y = 1; 73 | } 74 | 75 | if (x + IMAGE_WIDTH >= DEFAULT_MAX_X) { 76 | x = DEFAULT_MAX_X - IMAGE_WIDTH; 77 | } 78 | 79 | if (y + IMAGE_HEIGHT >= DEFAULT_MAX_Y) { 80 | y = DEFAULT_MAX_Y - IMAGE_HEIGHT; 81 | } 82 | } 83 | } 84 | 85 | public void keyPress(KeyEvent e) { 86 | int key = e.getKeyCode(); 87 | switch(key) { 88 | case KeyEvent.VK_UP : 89 | dy -= 1; 90 | break; 91 | case KeyEvent.VK_DOWN : 92 | dy += 1; 93 | break; 94 | case KeyEvent.VK_LEFT : 95 | dx -= 1; 96 | break; 97 | case KeyEvent.VK_RIGHT : 98 | dx += 1; 99 | break; 100 | case KeyEvent.VK_SPACE : 101 | fire(); 102 | break; 103 | } 104 | } 105 | 106 | 107 | public void fire() { 108 | missiles.add(new Missile(x + IMAGE_WIDTH / 4, y - IMAGE_HEIGHT / 4, DEFAULT_MAX_X, DEFAULT_MAX_Y)); 109 | 110 | } 111 | 112 | public void keyRelease(KeyEvent e) { 113 | int key = e.getKeyCode(); 114 | switch(key) { 115 | case KeyEvent.VK_UP : 116 | case KeyEvent.VK_DOWN : 117 | dy = 0; 118 | break; 119 | case KeyEvent.VK_LEFT : 120 | case KeyEvent.VK_RIGHT : 121 | dx = 0; 122 | break; 123 | } 124 | } 125 | 126 | public Rectangle getBounds() { 127 | return new Rectangle(x, y, IMAGE_WIDTH, IMAGE_HEIGHT); 128 | } 129 | 130 | 131 | public boolean isVisible() { 132 | return visible; 133 | } 134 | 135 | public void setVisible(boolean visible) { 136 | this.visible = visible; 137 | } 138 | 139 | /** 140 | * @param args 141 | */ 142 | public static void main(String[] args) { 143 | // TODO Auto-generated method stub 144 | 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /workspace/SpaceShooter/src/spaceshooter/model/Board.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package spaceshooter.model; 5 | 6 | import java.awt.Color; 7 | import java.awt.Font; 8 | import java.awt.FontMetrics; 9 | import java.awt.Graphics; 10 | import java.awt.Graphics2D; 11 | import java.awt.Image; 12 | import java.awt.Rectangle; 13 | import java.awt.Toolkit; 14 | import java.awt.event.ActionEvent; 15 | import java.awt.event.ActionListener; 16 | import java.awt.event.KeyEvent; 17 | import java.awt.event.KeyListener; 18 | import java.util.Iterator; 19 | import java.util.LinkedList; 20 | import java.util.List; 21 | 22 | import javax.swing.JPanel; 23 | import javax.swing.Timer; 24 | 25 | import spaceshooter.model.Alien.ALIEN_DIR; 26 | import spaceshooter.model.Missile.MISSILE_DIR; 27 | 28 | /** 29 | * @author dey 30 | * 31 | */ 32 | public class Board extends JPanel implements ActionListener { 33 | private final static int DELAY_MS = 5; 34 | private static final int SPACER = 15; //in px; 35 | private final int DEFAULT_MAX_X; 36 | private final int DEFAULT_MAX_Y; 37 | 38 | /** 39 | * @author dey 40 | * 41 | */ 42 | public class CraftKeyAdapter implements KeyListener { 43 | 44 | /* (non-Javadoc) 45 | * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent) 46 | */ 47 | @Override 48 | public void keyPressed(KeyEvent e) { 49 | craft.keyPress(e); 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent) 54 | */ 55 | @Override 56 | public void keyReleased(KeyEvent e) { 57 | craft.keyRelease(e); 58 | } 59 | 60 | /* (non-Javadoc) 61 | * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) 62 | */ 63 | @Override 64 | public void keyTyped(KeyEvent e) { 65 | 66 | } 67 | 68 | } 69 | 70 | 71 | private Timer timer; 72 | private Craft craft; 73 | private AlienArmy alienArmy; 74 | private boolean isRunning; 75 | private int aliensKilled; 76 | 77 | public Board(int maxX, int maxY) { 78 | addKeyListener(new CraftKeyAdapter()); 79 | setFocusable(true); 80 | setBackground(Color.BLACK); 81 | setDoubleBuffered(true); 82 | DEFAULT_MAX_X = maxX; 83 | DEFAULT_MAX_Y = maxY; 84 | 85 | craft = new Craft(DEFAULT_MAX_X, DEFAULT_MAX_Y); 86 | alienArmy = new AlienArmy(DEFAULT_MAX_X, DEFAULT_MAX_Y); 87 | 88 | isRunning = true; 89 | aliensKilled = 0; 90 | timer = new javax.swing.Timer(DELAY_MS, this); 91 | timer.start(); 92 | } 93 | 94 | @Override 95 | public void addNotify() { 96 | super.addNotify(); 97 | } 98 | 99 | @Override 100 | public void paint(Graphics g) { 101 | super.paint(g); 102 | 103 | if (isRunning) { 104 | Graphics2D g2D = (Graphics2D)g; 105 | g2D.drawImage(craft.getImage(), craft.getX(), craft.getY(),this); 106 | 107 | for (Missile missile : craft.getMissiles()) { 108 | g2D.drawImage(missile.getMissileImage(), missile.getX(), missile.getY(), this); 109 | } 110 | 111 | for (Alien alien : alienArmy.getAlienArmy()) { 112 | g2D.drawImage(alien.getImage(), alien.getX(), alien.getY(), this); 113 | } 114 | showKills(g2D); 115 | } else { 116 | String gameOver = String.format("Game Over"); 117 | Font font = new Font("Helvetica", Font.BOLD, 15); 118 | FontMetrics metrics = this.getFontMetrics(font); 119 | 120 | g.setColor(Color.WHITE); 121 | g.setFont(font); 122 | g.drawString(gameOver, DEFAULT_MAX_X / 2 , DEFAULT_MAX_Y / 2); 123 | g.drawString("Aliens Killed: " + aliensKilled, DEFAULT_MAX_X / 2, DEFAULT_MAX_Y / 2 - SPACER); 124 | } 125 | alienArmy.addAlien(); 126 | 127 | Toolkit.getDefaultToolkit().sync(); 128 | g.dispose(); 129 | } 130 | 131 | public void showKills(Graphics2D g2D) { 132 | g2D.setColor(Color.WHITE); 133 | String toDisplay = String.format("Kills: %d", aliensKilled); 134 | //top right corner 135 | g2D.drawString(toDisplay, DEFAULT_MAX_X - toDisplay.length() - 5 * SPACER, SPACER); 136 | } 137 | 138 | /* (non-Javadoc) 139 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 140 | */ 141 | @Override 142 | public void actionPerformed(ActionEvent e) { 143 | if (isRunning) { 144 | for (Iterator it = craft.getMissiles().iterator(); it.hasNext(); ) { 145 | Missile missile = it.next(); 146 | if (!missile.isVisible()) { 147 | it.remove(); 148 | } else { 149 | missile.move(MISSILE_DIR.UP); 150 | } 151 | } 152 | 153 | for (Iterator it = alienArmy.getAlienArmy().iterator(); it.hasNext(); ) { 154 | Alien alien = it.next(); 155 | if (!alien.isVisible()) { 156 | it.remove(); 157 | } else { 158 | alien.move(ALIEN_DIR.DOWN); 159 | } 160 | } 161 | craft.move(); 162 | alienArmy.move(); 163 | checkCollsions(); 164 | 165 | repaint(); 166 | } 167 | } 168 | 169 | 170 | public void checkCollsions() { 171 | //Craft collision 172 | Rectangle craftRectangle = craft.getBounds(); 173 | outer: 174 | for (Alien alien : alienArmy.getAlienArmy()) { 175 | Rectangle alienRectangle = alien.getBounds(); 176 | 177 | if (alienRectangle.intersects(craftRectangle)) { 178 | alien.setVisible(false); 179 | craft.setVisible(false); 180 | isRunning = false; 181 | break outer; 182 | } 183 | 184 | for (Missile missile : craft.getMissiles()) { 185 | if (missile.isVisible()) { 186 | Rectangle missileRectangle = missile.getBounds(); 187 | if (alien.isVisible()) { 188 | if (missileRectangle.intersects(alienRectangle)) { 189 | alien.setVisible(false); 190 | missile.setVisible(false); 191 | ++aliensKilled; 192 | } 193 | } 194 | } 195 | } 196 | } 197 | } 198 | 199 | } 200 | --------------------------------------------------------------------------------