Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package spaceinvadersreview;
- import java.io.*;
- import java.awt.*;
- import java.awt.geom.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class SpaceInvadersReview extends JFrame implements Runnable {
- static final int WINDOW_WIDTH = 600;
- static final int WINDOW_HEIGHT = 800;
- final int XBORDER = 40;
- final int YBORDER = 40;
- final int YTITLE = 25;
- boolean animateFirstTime = true;
- int xsize = -1;
- int ysize = -1;
- Image image;
- Graphics2D g;
- //Variables for the aliens.
- int numAliens = 10;
- int alienXPos[] = new int[numAliens];
- int alienYPos[] = new int[numAliens];
- int alienValue[] = new int[numAliens];
- int alienDownSpeed;
- int alienSideSpeed;
- //Variables for the cannon.
- int cannonXPos;
- int cannonYPos;
- //Variables for the cannonballs.
- int currentCannonBall;
- int numCannonBalls = 8;
- int cannonBallXPos[] = new int[numCannonBalls];
- int cannonBallYPos[] = new int[numCannonBalls];
- boolean cannonBallActive[] = new boolean[numCannonBalls];
- //Other variables.
- boolean gameOver;
- int score;
- int highScore;
- int timeCount;
- static SpaceInvadersReview frame;
- public static void main(String[] args) {
- frame = new SpaceInvadersReview();
- frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- public SpaceInvadersReview() {
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- if (e.BUTTON1 == e.getButton()) {
- //left button
- //Don't fire cannonballs when game over.
- if (gameOver)
- return;
- // location of the cursor.
- int xpos = e.getX();
- int ypos = e.getY();
- //Fire a cannonball.
- cannonBallActive[currentCannonBall] = true;
- cannonBallXPos[currentCannonBall] = cannonXPos;
- cannonBallYPos[currentCannonBall] = cannonYPos;
- currentCannonBall++;
- //Make sure we don't go off the end of the array.
- if (currentCannonBall >= numCannonBalls)
- currentCannonBall = 0;
- }
- if (e.BUTTON3 == e.getButton()) {
- //right button
- reset();
- }
- repaint();
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseDragged(MouseEvent e) {
- repaint();
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent e) {
- //Freeze the cannon when game over.
- if (gameOver)
- return;
- //Move the cannon with the mouse.
- cannonXPos = e.getX() - getX(0);
- repaint();
- }
- });
- addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- if (e.VK_UP == e.getKeyCode()) {
- } else if (e.VK_DOWN == e.getKeyCode()) {
- } else if (e.VK_LEFT == e.getKeyCode()) {
- } else if (e.VK_RIGHT == e.getKeyCode()) {
- }
- repaint();
- }
- });
- init();
- start();
- }
- Thread relaxer;
- ////////////////////////////////////////////////////////////////////////////
- public void init() {
- requestFocus();
- }
- ////////////////////////////////////////////////////////////////////////////
- public void destroy() {
- }
- ////////////////////////////////////////////////////////////////////////////
- public void paint(Graphics gOld) {
- if (image == null || xsize != getSize().width || ysize != getSize().height) {
- xsize = getSize().width;
- ysize = getSize().height;
- image = createImage(xsize, ysize);
- g = (Graphics2D) image.getGraphics();
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- }
- //fill background
- g.setColor(Color.cyan);
- g.fillRect(0, 0, xsize, ysize);
- int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
- int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
- //fill border
- g.setColor(Color.white);
- g.fillPolygon(x, y, 4);
- // draw border
- g.setColor(Color.red);
- g.drawPolyline(x, y, 5);
- if (animateFirstTime) {
- gOld.drawImage(image, 0, 0, null);
- return;
- }
- //Display the active cannonballs.
- for (int i=0;i<numCannonBalls;i++)
- {
- if (cannonBallActive[i])
- {
- g.setColor(Color.black);
- drawCannonBall(getX(cannonBallXPos[i]),getYNormal(cannonBallYPos[i]),0,1,1);
- }
- }
- //Display the cannon.
- g.setColor(Color.red);
- drawCannon(getX(cannonXPos),getYNormal(cannonYPos),0,1,1);
- //Display the aliens.
- for (int i=0;i<numAliens;i++)
- {
- g.setColor(Color.green);
- drawAlien(getX(alienXPos[i]),getYNormal(alienYPos[i]),0,1,1,alienValue[i]);
- }
- //Display the score and high score.
- g.setColor(Color.black);
- g.setFont(new Font("Arial",Font.PLAIN,16));
- g.drawString("Score: " + score, 40, 50);
- g.setColor(Color.black);
- g.setFont(new Font("Arial",Font.PLAIN,16));
- g.drawString("High Score: " + highScore, 440, 50);
- //Display game over when the game is over.
- if (gameOver)
- {
- g.setColor(Color.black);
- g.setFont(new Font("Arial",Font.PLAIN,32));
- g.drawString("Game Over", 50, 100);
- }
- gOld.drawImage(image, 0, 0, null);
- }
- ////////////////////////////////////////////////////////////////////////////
- public void drawAlien(int xpos,int ypos,double rot,
- double xscale,double yscale,int value)
- {
- g.translate(xpos,ypos);
- g.rotate(rot * Math.PI/180.0);
- g.scale( xscale , yscale );
- int xval[] = {10,-10,-10,-5,-10,0,10,5,10};
- int yval[] = {-20,-20,-5,-5,10,0,10,-5,-5};
- g.fillPolygon(xval,yval,xval.length);
- g.setColor(Color.black);
- g.setFont(new Font("Arial",Font.PLAIN,16));
- g.drawString("" + value, 0, 0);
- g.scale( 1.0/xscale,1.0/yscale );
- g.rotate(-rot * Math.PI/180.0);
- g.translate(-xpos,-ypos);
- }
- ////////////////////////////////////////////////////////////////////////////
- public void drawCannon(int xpos,int ypos,double rot,
- double xscale,double yscale)
- {
- g.translate(xpos,ypos);
- g.rotate(rot * Math.PI/180.0);
- g.scale( xscale , yscale );
- int xvals[] = {0,10,10,-10,-10,0};
- int yvals[] = {-15,-10,10,10,-10,-15};
- g.fillPolygon(xvals,yvals,xvals.length);
- g.scale( 1.0/xscale,1.0/yscale );
- g.rotate(-rot * Math.PI/180.0);
- g.translate(-xpos,-ypos);
- }
- ////////////////////////////////////////////////////////////////////////////
- public void drawCannonBall(int xpos,int ypos,double rot,double xscale,double yscale)
- {
- g.translate(xpos,ypos);
- g.rotate(rot * Math.PI/180.0);
- g.scale( xscale , yscale );
- g.fillOval(-10,-10,20,20);
- g.scale( 1.0/xscale,1.0/yscale );
- g.rotate(-rot * Math.PI/180.0);
- g.translate(-xpos,-ypos);
- }
- ////////////////////////////////////////////////////////////////////////////
- // needed for implement runnable
- public void run() {
- while (true) {
- animate();
- repaint();
- double seconds = .01; //time that 1 frame takes.
- int miliseconds = (int) (1000.0 * seconds);
- try {
- Thread.sleep(miliseconds);
- } catch (InterruptedException e) {
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////
- public void reset() {
- score = 0;
- gameOver = false;
- //Initialize the values of the cannonballs.
- currentCannonBall = 0;
- for (int i=0;i<numCannonBalls;i++)
- cannonBallActive[i] = false;
- //Initialize the values of the cannon.
- cannonYPos = 0;
- //Initialize the values of the aliens.
- alienSideSpeed = 2;
- alienDownSpeed = 1;
- for (int i=0;i<numAliens;i++) {
- alienXPos[i] = (int)(Math.random()*getWidth2()/2+getWidth2()/4);
- alienYPos[i] = (int)(Math.random()*getHeight2()/2+getHeight2()/2);
- alienValue[i] = (int)(Math.random()*4+2);
- }
- timeCount = 0;
- }
- /////////////////////////////////////////////////////////////////////////
- public void animate() {
- if (animateFirstTime) {
- animateFirstTime = false;
- if (xsize != getSize().width || ysize != getSize().height) {
- xsize = getSize().width;
- ysize = getSize().height;
- }
- reset();
- //Position the cannon at the center when the program starts.
- cannonXPos = getWidth2()/2;
- highScore = 0;
- }
- //Freeze the game when game over.
- if (gameOver)
- return;
- //Move the cannonballs up and check to see if a cannonball has hit an alien.
- for (int j=0;j<numCannonBalls;j++)
- {
- if (cannonBallActive[j])
- {
- for (int i=0;i<numAliens;i++) {
- if (alienXPos[i] > cannonBallXPos[j] - 10 &&
- alienXPos[i] < cannonBallXPos[j] + 10 &&
- alienYPos[i] > cannonBallYPos[j] - 10 &&
- alienYPos[i] < cannonBallYPos[j] + 10)
- {
- score+=alienValue[i];
- if (score > highScore)
- highScore = score;
- alienXPos[i] = (int)(Math.random()*getWidth2()/2+getWidth2()/4);
- alienYPos[i] = getHeight2();
- alienValue[i] = (int)(Math.random()*4+2);
- cannonBallActive[j] = false;
- }
- }
- if (cannonBallYPos[j] > getHeight2())
- cannonBallActive[j] = false;
- cannonBallYPos[j] += 5;
- }
- }
- //Move the aliens sideways and determine if we need to switch the direction of the aliens.
- boolean switchDir = false;
- for (int i=0;i<numAliens;i++) {
- alienXPos[i] += alienSideSpeed;
- if (alienXPos[i] > getWidth2() || alienXPos[i] < 0)
- switchDir = true;
- }
- if (switchDir)
- alienSideSpeed = -alienSideSpeed;
- //Move the aliens down.
- if (timeCount % 3 == 2)
- {
- for (int i=0;i<numAliens;i++)
- {
- if (alienYPos[i] <= 0)
- gameOver = true;
- alienYPos[i] -= alienDownSpeed;
- }
- }
- timeCount++;
- }
- ////////////////////////////////////////////////////////////////////////////
- public void start() {
- if (relaxer == null) {
- relaxer = new Thread(this);
- relaxer.start();
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- public void stop() {
- if (relaxer.isAlive()) {
- relaxer.stop();
- }
- relaxer = null;
- }
- /////////////////////////////////////////////////////////////////////////
- public int getX(int x) {
- return (x + XBORDER);
- }
- public int getY(int y) {
- return (y + YBORDER + YTITLE);
- }
- public int getYNormal(int y) {
- return (-y + YBORDER + YTITLE + getHeight2());
- }
- public int getWidth2() {
- return (xsize - getX(0) - XBORDER);
- }
- public int getHeight2() {
- return (ysize - getY(0) - YBORDER);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement