/** * Invaders * Copyright (C) 2003 Michael Birken * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * http://www.meatfighter.com */ package invaders; import gamingtools.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Invaders extends FrameBuilder { private class Monster { private int x; private int y; private int imageIndex; private int counter; private int minX; private int maxX; private boolean movingLeft; private Random random; public Monster(int x, int y, boolean movingLeft, Random random) { this.x = x; this.y = y; this.movingLeft = movingLeft; this.random = random; counter = random.nextInt(FPS / 2) + FPS / 2; maxX = 10 + random.nextInt(WIDTH - imageWidth - 20); minX = 10 + random.nextInt(Math.max(1, maxX - 10)); } public void move() { if (movingLeft && x <= minX) { movingLeft = false; minX = 10 + random.nextInt(Math.max(1, maxX - 10)); } else if (!movingLeft && x >= maxX) { movingLeft = true; maxX = 10 + random.nextInt(WIDTH - imageWidth - 20); } if (movingLeft) x--; else x++; if (--counter <= 0) { counter = random.nextInt(FPS / 2) + FPS / 2; imageIndex = (imageIndex == 0) ? 1 : 0; } } public int getImageIndex() { return imageIndex; } } public static final int WIDTH = 640; public static final int HEIGHT = 480; public static final int STARS = 20; public static final int MONSTERS = 8; public static final int FPS = 240; private Image[] invaders; private Point[] stars; private Monster[] monsters; private int imageWidth; public Invaders() { GraphicsSource.enterDisplayMode(WIDTH, HEIGHT); Frame frame = GraphicsSource.getFrame(); frame.setTitle("Invaders"); frame.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { GraphicsSource.stopAnimation(); GraphicsSource.exitDisplayMode(); System.exit(0); } } }); GraphicsSource.setRenderListener(this); GraphicsSource.startAnimation(FPS); } public void init() { invaders = ImageSource.getImages( new String[] { "invader1", "invader2" }, ".gif"); imageWidth = invaders[0].getWidth(null); Random random = new Random(); stars = new Point[STARS]; for(int i = STARS - 1; i >= 0; i--) stars[i] = new Point(random.nextInt(WIDTH), random.nextInt(HEIGHT)); monsters = new Monster[MONSTERS]; for(int i = MONSTERS - 1, j = 0; i >= 0; i--, j += HEIGHT / MONSTERS) monsters[i] = new Monster(10 + random.nextInt(WIDTH - imageWidth - 20), j, random.nextBoolean(), random); } public void renderBackground( Graphics g, int x, int y, int width, int height) { g.setColor(Color.BLACK); g.fillRect(x, y, width + 1, height + 1); g.setColor(Color.WHITE); for(int i = STARS - 1; i >= 0; i--) if (stars[i].x >= x && stars[i].y >= y && stars[i].x <= (x+width) && stars[i].y <= (y+height)) g.fillRect(stars[i].x, stars[i].y, 1, 1); } public void renderBackground(Graphics g) { g.setColor(Color.BLACK); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.WHITE); for(int i = STARS - 1; i >= 0; i--) g.fillRect(stars[i].x, stars[i].y, 1, 1); } public boolean isBackgroundSame() { return true; } public void updateState() { for(int i = MONSTERS - 1; i >= 0; i--) monsters[i].move(); } public void renderForeground(Graphics g) { for(int i = MONSTERS - 1; i >= 0; i--) drawSprite(g, invaders[monsters[i].getImageIndex()], monsters[i].x, monsters[i].y); } public static void main(String[] args) { new Invaders(); } }