Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Zero Volt (posted 2014-07-17 23:47:54, viewed 69 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  
package pkgnew;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends JFrame {

    public Window() {
        add(new Screen());
        setTitle("Space Invaders");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900, 560);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);  
    }

    public static void main(String[] args) {
        new Window();
    }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183  
package pkgnew;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javafx.scene.shape.Line;
import javax.swing.*;

public class Screen extends JPanel implements ActionListener {

    private SpaceShip spaceShip;
    private Timer timer;
    private ArrayList aliens;
    private boolean ingame;

    private int[][] pos;
    private int S_WIDTH;
    private int S_HEIGHT;

    public Screen() {
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLUE);
        setDoubleBuffered(true);
        ingame = true;
        pos = new int[100][2];

        S_WIDTH = 900;
        S_HEIGHT = 560;
        spaceShip = new SpaceShip();
        timer = new Timer(5, this);
        timer.start();

        initAliens();
    }

    public void initAliens() {
        aliens = new ArrayList();

        int x = 0;
        int y = 0;

        for (int i = 0; i < pos.length; i++) {
            // min + (int)(Math.random()*(max - min) + 1)
            x = 900 + (int) (Math.random() * (2000 - 900) + 1);
            y = (int) (Math.random() * 500);
            pos[i][0] = x;
            pos[i][1] = y;
            aliens.add(new AlienShip(pos[i][0], pos[i][1]));
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g);
        if (ingame) {
            if (spaceShip.isVisible()) {
                drawSpaceShip(g);
            }
            drawAlienShip(g);

            g2d.setColor(Color.white);
            g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
        } else {
            String msg = "Game Over";
            Font small = new Font("Viner Hand ITC", Font.BOLD, 36);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (S_WIDTH - metr.stringWidth(msg)) / 2,
                    S_HEIGHT / 2 - metr.getHeight());
        }
        g.dispose();
    }

    private void drawSpaceShip(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(Color.CYAN);
        g2d.draw(spaceShip.rect);

        ArrayList bs = spaceShip.getBullets();
        for (int i = 0; i < bs.size(); ++i) {
            Bullet b = (Bullet) bs.get(i);
            if (b.isVisible()) {
                g2d.draw(b.rect);
            }
        }
    }

    private void drawAlienShip(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(Color.YELLOW);
        for (int i = 0; i < aliens.size(); i++) {
            AlienShip a = (AlienShip) aliens.get(i);
            if (a.isVisible()) {
                g2d.draw(a.rect);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (aliens.size() == 0) {
            ingame = false;
        }

        ArrayList bs = spaceShip.getBullets();

        for (int i = 0; i < aliens.size(); i++) {
            AlienShip a = (AlienShip) aliens.get(i);
            if (a.isVisible()) {
                a.move();
            } else {
                aliens.remove(a);
            }
        }

        for (int i = 0; i < bs.size(); i++) {
            Bullet b = (Bullet) bs.get(i);
            if (b.isVisible()) {
                b.move();
            } else {
                bs.remove(b);
            }
        }

        spaceShip.move();
        checkCollisions();
        repaint();
    }

    public void checkCollisions() {

        Rectangle r3 = spaceShip.getBounds();

        for (int j = 0; j < aliens.size(); j++) {
            AlienShip a = (AlienShip) aliens.get(j);
            Rectangle r2 = a.getBounds();

            if (r3.intersects(r2)) {
                spaceShip.setVisible(false);
                a.setVisible(false);
                ingame = false;
            }
        }

        ArrayList ms = spaceShip.getBullets();

        for (int i = 0; i < ms.size(); i++) {
            Bullet b = (Bullet) ms.get(i);

            Rectangle r1 = b.getBounds();

            for (int j = 0; j < aliens.size(); j++) {
                AlienShip a = (AlienShip) aliens.get(j);
                Rectangle r2 = a.getBounds();

                if (r1.intersects(r2)) {
                    b.setVisible(false);
                    a.setVisible(false);

                }
            }
        }
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            spaceShip.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            spaceShip.keyPressed(e);
        }
    }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106  
package pkgnew;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.*;

public class SpaceShip {

    public int x;
    public int y;
    public int dx;
    public int dy;
    public Rectangle2D rect;
    public ArrayList bullets;
    private boolean visible;

    public SpaceShip() {
        x = 30;
        y = 30;
        visible = true;
        rect = new Rectangle2D.Double(x, y, 20, 20);
        bullets = new ArrayList();
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isVisible() {
        return visible;
    }

    public void move() {
        x += dx;
        y += dy;

        if (x < 1) {
            x = 1;
        }

        if (y < 1) {
            y = 1;
        }

        rect.setRect(x, y, 20, 20);
    }

    public ArrayList getBullets() {
        return bullets;
    }

    public void fire() {
        bullets.add(new Bullet((int) rect.getX() + 20, (int) rect.getY()
                + 20 / 2));
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 20, 20);
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 3;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -3;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 3;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -3;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }
    }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41  
package pkgnew;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javafx.scene.shape.Line;

public class Bullet {

    public int x, y;
    public Rectangle2D rect;
    boolean visible;
    private int S_WIDTH = 900;
    
    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
        visible = true;
        rect = new Rectangle2D.Double(this.x, this.y, 10, 1);
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }
    
    public Rectangle getBounds() {
        return new Rectangle(x, y, 10, 1);
    }
    
    public void move() {
        x += 5;
        rect.setRect(x, y, 10, 1);
        if (x > S_WIDTH)
            visible = false;
    }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52  
package pkgnew;

import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

public class AlienShip {

    private int x;
    private int y;
    private int width;
    private int height;
    private int S_WIDTH;
    private int S_HEIGHT;
    public Rectangle2D rect;
    private boolean visible;

    public AlienShip(int x, int y) {
        this.S_WIDTH = 900;
        this.S_HEIGHT = 560;
        this.x = x;
        this.y = y;
        visible = true;
        rect = new Rectangle2D.Double(this.x, this.y, 20, 20);
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 20, 20);
    }
    
    public void move() {
        if (x < 0) 
            x = S_WIDTH;
        x -= 2;
        rect.setRect(this.x, this.y, 20, 20);
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }
    
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (48 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!