Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.geom.*;
- import java.awt.event.*;
- import java.applet.*;
- import javax.swing.*;
- import java.io.*;
- import javax.imageio.*;
- import java.net.*;
- class rectangle
- {
- private int xpos, ypos, height, width, speed, ydir, xdir;
- private Rectangle c;
- public rectangle(int y,int x, int h, int w, int s, int yd)
- {
- ypos = y;
- xpos = x;
- height = h;
- width = w;
- speed = s;
- ydir = yd;
- c = new Rectangle(xpos, ypos, width, height);
- }
- public void move(Bullet b)
- {
- xpos = xpos + speed*xdir;
- ypos = ypos + speed*ydir;
- c.setFrame(xpos, ypos, width, height);
- }
- public int getX() { return xpos; }
- public int getY() { return ypos; }
- public int getHeight() { return height; }
- public int getWidth() { return width; }
- public int getXDir() { return xdir; }
- public int getYDir() { return ydir; }
- public void setDir(int x, int y)
- {
- xdir = x;
- ydir = y;
- }
- public boolean intersects(double xpos2, double ypos2, double height2, double width2)
- {
- return c.intersects(xpos2, ypos2, height2, width2);
- }
- public void Kill()
- {
- width = width /2;
- }
- public void paint(Graphics2D g)
- {
- g.fill(c);
- }
- }
- class Alien
- {
- private double xpos, ypos, xdir, ydir,score;
- private double speed;
- private Image alienPic;
- private Component parent;
- // private Ellipse2D.Double c;
- public Alien(double x, double y, double xd, double yd, double s,double S, Image alienPic, Component parent)
- {
- xpos = x;
- ypos = y;
- xdir = xd;
- ydir = yd;
- speed = s;
- score = S;
- this.alienPic = alienPic;
- this.parent = parent;
- }
- public void Speed(double As)
- {
- speed = As;
- }
- public void move(Score s, Bullet b)
- {
- xpos = xpos + speed*xdir;
- ypos = ypos + speed*ydir;
- c.setFrame(xpos, ypos, width, height);
- if(b.intersects(xpos, ypos))
- {
- ypos = 1;
- xpos = 458;
- xdir = 0;
- ydir = 0;
- s.newS();
- b.Kill();
- }
- }
- public double getX() { return xpos; }
- public double getY() { return ypos; }
- public double getXDir() { return xdir; }
- public double getYDir() { return ydir; }
- public double getSpeed() {return speed; }
- public double getS(){return score;}
- public void setY (double yD)
- {
- ypos = yD;
- }
- public void setHW()
- {
- ypos = -1000;
- xpos = -1000;
- }
- public void setDir(double x, double y)
- {
- xdir = x;
- ydir = y;
- }
- public void shoot(Bullet b)
- {
- b = new Bullet(100,200 ,10, 10 ,1, 1);
- }
- public boolean intersects(int x, int y, int w, int h)
- {
- return c.intersects(x, y, w, h);
- }
- public void paint(Graphics2D g)
- {
- g2.drawImage(alienPic,xpos,ypos,null);
- }
- }
- class Bullet
- {
- private double xpos, ypos, height, width, speed, xdir, ydir;
- private Ellipse2D.Double c;
- public Bullet(int x, int y, int w, int h, int s, int yd)
- {
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- speed = s;
- ydir = yd;
- c = new Ellipse2D.Double(xpos, ypos, width, height);
- }
- public void move()
- {
- xpos = xpos + speed*xdir;
- ypos = ypos + speed*ydir;
- c.setFrame(xpos, ypos, width, height);
- }
- public double getX() { return xpos; }
- public double getY() { return ypos; }
- public double getHeight() { return height; }
- public double getWidth() { return width; }
- public double getXDir() { return xdir; }
- public double getYDir() { return ydir; }
- public void setDir(int x, int y)
- {
- xdir = x;
- ydir = y;
- }
- public boolean intersects(double x, double y, double w, double h)
- {
- return c.intersects(x, y, w, h);
- }
- public void Kill()
- {
- width = 0;
- height = 0;
- }
- public void paint(Graphics2D g)
- {
- g.fill(c);
- }
- }
- class AlienBullet
- {
- private double xpos, ypos, height, width, speed, xdir, ydir;
- private Ellipse2D.Double c;
- public AlienBullet(double x, double y, double w, double h, int s, int yd)
- {
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- speed = s;
- ydir = yd;
- c = new Ellipse2D.Double(xpos, ypos, width, height);
- }
- public void move(rectangle r2, rectangle r3, rectangle r4)
- {
- xpos = xpos + speed*xdir;
- ypos = ypos + speed*ydir;
- c.setFrame(xpos, ypos, width, height);
- if(r2.intersects(xpos, ypos, height,width))
- {
- width = 0;
- height = 0;
- ypos = 1;
- xpos = 300;
- r2.Kill();
- }
- if(r3.intersects(xpos, ypos, height,width))
- {
- width = 0;
- height = 0;
- ypos = 1;
- xpos = 300;
- r3.Kill();
- }
- if(r4.intersects(xpos, ypos, height,width))
- {
- width = 0;
- height = 0;
- ypos = 1;
- xpos = 300;
- r4.Kill();
- }
- }
- public double getX() { return xpos; }
- public double getY() { return ypos; }
- public double getHeight() { return height; }
- public double getWidth() { return width; }
- public double getXDir() { return xdir; }
- public double getYDir() { return ydir; }
- public void setDir(int x, int y)
- {
- xdir = x;
- ydir = y;
- }
- public boolean intersects(double x, double y, double w, double h)
- {
- return c.intersects(x, y, w, h);
- }
- public void WH(double x, double y)
- {
- xpos = x;
- ypos = y;
- height = 7;
- width = 5;
- }
- public void Kill()
- {
- width = 0;
- height = 0;
- }
- public void paint(Graphics2D g)
- {
- g.fill(c);
- }
- }
- class Score
- {
- private int score;
- public Score(int s)
- {
- score = s;
- }
- public void newS()
- {
- score = score + 10;
- }
- public void newSMother()
- {
- score = score + 30;
- }
- public int getScore(){return score;}
- }
- class Mother
- {
- private double xpos, ypos, height, width, xdir, ydir,score;
- private double speed;
- private Ellipse2D.Double c;
- public Mother(double x, double y, double w, double h, double xd, double yd, double s,double S)
- {
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- xdir = xd;
- ydir = yd;
- speed = s;
- score = S;
- c = new Ellipse2D.Double(xpos, ypos, width, height);
- }
- public void Speed(double As)
- {
- speed = As;
- }
- public void move(Score s, Bullet b)
- {
- xpos = xpos + speed*xdir;
- ypos = ypos + speed*ydir;
- c.setFrame(xpos, ypos, width, height);
- if(b.intersects(xpos, ypos, height,width))
- {
- width = 0;
- height = 0;
- ypos = 1;
- xpos = 458;
- xdir = 0;
- ydir = 0;
- s.newSMother();
- b.Kill();
- }
- }
- public double getX() { return xpos; }
- public double getY() { return ypos; }
- public double getHeight() { return height; }
- public double getWidth() { return width; }
- public double getXDir() { return xdir; }
- public double getYDir() { return ydir; }
- public double getSpeed() {return speed; }
- public double getS(){return score;}
- public void setY (double yD)
- {
- ypos = yD;
- }
- public void setHW()
- {
- height = 0;
- width = 0;
- ypos = -1000;
- xpos = -1000;
- }
- public void setDir(double x, double y)
- {
- xdir = x;
- ydir = y;
- }
- public void shoot(Bullet b)
- {
- b = new Bullet(100,200 ,10, 10 ,1, 1);
- }
- public boolean intersects(int x, int y, int w, int h)
- {
- return c.intersects(x, y, w, h);
- }
- public void paint(Graphics2D g)
- {
- g.fill(c);
- }
- }
- public class SpaceInvade extends Applet implements KeyListener{
- int roll;
- Image img;
- private int roll1 = 0;
- int roll2 = 0;
- int lives = 3;
- private Alien a;
- private rectangle r;
- private rectangle r2;
- private rectangle r3;
- private rectangle r4;
- private rectangle r5;
- private rectangle r6;
- private Mother m1;
- private Score s1;
- private Timer t;
- private Bullet b;
- AlienBullet[]Alienb = new AlienBullet[10];
- Alien[]aliens = new Alien [10];
- Alien[]aliens2 = new Alien [10];
- Alien[]aliens3 = new Alien [10];
- Alien[]aliens4 = new Alien [10];
- Alien[]aliens5 = new Alien [10];
- int XDIR = 1;
- public void init() {
- r = new rectangle(400, 1, 20, 20, 7, 0);
- b = new Bullet (-10, -123,10, 10 ,0, 0);
- r2 = new rectangle(330,50 , 20, 100, 7, 0);
- r3 = new rectangle(330,350 , 20, 100, 7, 0);
- r4 = new rectangle(330,650 , 20, 100, 7, 0);
- r5 = new rectangle(85,500,30,30,0,0);
- m1 = new Mother(100,100, 100, 100, 7, 1,1,0);
- s1 = new Score(0);
- addKeyListener(this);
- setFocusable(true);
- int score1 = s1.getScore();
- for (int i = 0; i <aliens.length ;i++)
- {
- aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
- aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
- aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
- aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
- aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
- }
- for (int b = 0; b <Alienb.length ;b++)
- {
- Alienb[b] = new AlienBullet(20*b,20,0,0,1,1);
- }
- ActionListener s = new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- r.move(b);
- r2.move(b);
- r3.move(b);
- r4.move(b);
- int ALIEN = 0;
- b.move();
- int FONT = 0;
- roll = (int)(Math.random()*200 + 1);
- if (roll <= 3)
- {
- roll1 = (int)(Math.random()*9 + 1);
- if(Alienb[roll1].getHeight() == 0 && Alienb[roll1].getWidth() == 0 && aliens[roll1].getHeight() > 0 && aliens[roll1].getWidth() > 0)
- {
- Alienb[roll1].WH(aliens[roll1].getX(), aliens[roll1].getY());
- Alienb[roll1].WH(aliens2[roll1].getX(), aliens2[roll1].getY());
- Alienb[roll1].WH(aliens3[roll1].getX(), aliens3[roll1].getY());
- Alienb[roll1].WH(aliens4[roll1].getX(), aliens4[roll1].getY());
- Alienb[roll1].WH(aliens5[roll1].getX(), aliens5[roll1].getY());
- }
- }
- if(roll2 != 10)
- {
- roll2 = (int)(Math.random()*20 + 2);
- System.out.println(roll2);
- }
- if (roll2 == 10)
- {
- roll2 = 1;
- r6 = new rectangle(100,500,30,50,2,-1);
- r6.move(b);
- System.out.println(roll2);
- }
- for (int i = 0; i < aliens.length ;i++)
- {
- if (aliens3[i].getY() >= r2.getY())
- {
- aliens3[i].setHW();
- FONT = 1;
- }
- if (aliens4[i].getY() >= r2.getY())
- {
- aliens4[i].setHW();
- FONT = 1;
- }
- if (aliens5[i].getY() >= r2.getY())
- {
- aliens5[i].setHW();
- FONT = 1;
- }
- if (aliens2[i].getY() >= r2.getY())
- {
- aliens2[i].setHW();
- FONT = 1;
- }
- if (aliens[i].getY() >= r2.getY())
- {
- aliens[i].setHW();
- FONT = 1;
- }
- if ((aliens3[i].getX() + aliens3[i].getWidth() >= getWidth()) || (aliens3[i].getX() < 0))
- {
- ALIEN = 1;
- }
- if ((aliens4[i].getX() + aliens4[i].getWidth() >= getWidth()) || (aliens4[i].getX() < 0))
- {
- ALIEN = 1;
- }
- if ((aliens5[i].getX() + aliens5[i].getWidth() >= getWidth()) || (aliens5[i].getX() < 0))
- {
- ALIEN = 1;
- }
- if ((aliens2[i].getX() + aliens2[i].getWidth() >= getWidth()) || (aliens2[i].getX() < 0))
- {
- ALIEN = 1;
- }
- if ((aliens[i].getX() + aliens[i].getWidth() >= getWidth()) || (aliens[i].getX() < 0))
- {
- ALIEN = 1;
- }
- if (ALIEN == 1)
- {
- for (i = 0; i < aliens2.length ;i++)
- {
- aliens5[i].setY(aliens5[i].getY() + 20);
- aliens5[i].setDir(-aliens5[i].getXDir(),aliens5[i].getYDir());
- aliens5[i].Speed(aliens5[i].getSpeed() + 0.1);
- }
- }
- if (ALIEN == 1)
- {
- for (i = 0; i < aliens2.length ;i++)
- {
- aliens4[i].setY(aliens4[i].getY() + 20);
- aliens4[i].setDir(-aliens4[i].getXDir(),aliens4[i].getYDir());
- aliens4[i].Speed(aliens4[i].getSpeed() + 0.1);
- }
- }
- if (ALIEN == 1)
- {
- for (i = 0; i < aliens2.length ;i++)
- {
- aliens3[i].setY(aliens3[i].getY() + 20);
- aliens3[i].setDir(-aliens3[i].getXDir(),aliens3[i].getYDir());
- aliens3[i].Speed(aliens3[i].getSpeed() + 0.1);
- }
- }
- if (ALIEN == 1)
- {
- for (i = 0; i < aliens2.length ;i++)
- {
- aliens2[i].setY(aliens2[i].getY() + 20);
- aliens2[i].setDir(-aliens2[i].getXDir(),aliens2[i].getYDir());
- aliens2[i].Speed(aliens2[i].getSpeed() + 0.1);
- }
- }
- if (ALIEN == 1)
- {
- for (i = 0; i < aliens.length ;i++)
- {
- aliens[i].setY(aliens[i].getY() + 20);
- aliens[i].setDir(-aliens[i].getXDir(),aliens[i].getYDir());
- aliens[i].Speed(aliens[i].getSpeed() + 0.1);
- }
- }
- }
- for (int i = 0; i < aliens.length;i++)
- {
- aliens[i].move(s1, b);
- aliens2[i].move(s1,b);
- aliens3[i].move(s1,b);
- aliens4[i].move(s1,b);
- aliens5[i].move(s1,b);
- }
- for (int b = 0; b < Alienb.length;b++)
- {
- Alienb[b].move(r2,r3,r4);
- }
- repaint();
- if (b.intersects(r2.getX(), r2.getY(), r2.getWidth(), r2.getHeight()))
- {
- b.Kill();
- r2.Kill();
- }
- if (b.intersects(r4.getX(), r4.getY(), r4.getWidth(), r4.getHeight()))
- {
- b.Kill();
- r4.Kill();
- }
- if (b.intersects(r3.getX(), r3.getY(), r3.getWidth(), r3.getHeight()))
- {
- b.Kill();
- r3.Kill();
- }
- if (r.getX() <= 0)
- {
- r.setDir(0, 0);
- }
- if (r.getX() >= getWidth() - 30)
- {
- r.setDir( 0, 0);
- }
- for (int a = 0; a <= Alienb.length-1; a++)
- {
- if (Alienb[a].intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight()))
- {
- lives--;
- Alienb[a].Kill();
- }
- }
- }
- };
- t=new Timer(10, s);
- t.start();
- }
- public void keyPressed(KeyEvent e)
- {
- if (e.getKeyCode() == e.VK_LEFT && (r.getX() >= 0))
- {
- r.setDir(-1,0);
- }
- if (e.getKeyCode() == e.VK_RIGHT && (r.getX() <= getWidth() - 30))
- {
- r.setDir(1,0);
- }
- if ((e.getKeyCode() == e.VK_SPACE) && (b.getY() < 0))
- {
- b = new Bullet (r.getX(), 390, 5, 7 ,7, 0);
- b.setDir (0,-1); //change direction of the paddle to positive
- }
- }
- public void keyReleased(KeyEvent e)
- {
- if (e.getKeyCode() == e.VK_LEFT)
- {
- r.setDir(0,0); //change direction of the paddle to positive
- }
- if (e.getKeyCode() == e.VK_RIGHT)
- {
- r.setDir(0,0); //change direction of the paddle to negative
- }
- }
- public void keyTyped(KeyEvent e) {} //not needed but has to be here
- public void paint(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- setBackground ( Color.red);
- for (int i = 0; i < aliens.length ;i++)
- {
- if (aliens[i].getY() >= r2.getY() || lives == 0)
- {
- Font font = new Font("Arial",Font.BOLD,45);
- g.setFont(font);
- FontMetrics fm = g.getFontMetrics();
- g.setColor(Color.BLACK);
- int y = fm.getHeight();
- g.drawString("Game Over",320,225);
- g.drawString("Try Again",320,275);
- t.stop();
- }
- Font font = new Font("Helevetica",Font.BOLD,25);
- g.setFont(font);
- FontMetrics fm = g.getFontMetrics();
- g.setColor(Color.BLACK);
- int y = fm.getHeight();
- g.drawString("Score = " +s1.getScore(),70,500);
- g.drawString("Lives = " +lives,300,500);
- }
- r.paint(g2);
- b.paint(g2);
- r2.paint(g2);
- r3.paint(g2);
- r4.paint(g2);
- m1.paint(g2);
- if (roll1 == 1)
- {
- //g2.setColor(Color.GREEN);
- //r6.paint(g2);
- }
- g2.setColor(Color.BLACK);
- for (int i = 0; i < aliens.length;i++)
- {
- aliens[i].paint(g2);
- aliens2[i].paint(g2);
- aliens3[i].paint(g2);
- aliens4[i].paint(g2);
- aliens5[i].paint(g2);
- }
- for (int b = 0; b < Alienb.length;b++)
- {
- Alienb[b].paint(g2);
- }
- }
- }
Add Comment
Please, Sign In to add comment