Rabu, 01 Februari 2023

Netbeans 8.2 Java Game, Space Invaders (Shoot from Botton to Top)

 








Alien.java

/*

 * 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 spaceinvaders;

import javax.swing.ImageIcon;

 

public class Alien extends Sprite {

 

    private Bomb bomb;

 

    public Alien(int x, int y) {

 

        initAlien(x, y);

    }

 

    private void initAlien(int x, int y) {

 

        this.x = x;

        this.y = y;

 

        bomb = new Bomb(x, y);

 

        String alienImg = "C:/Users/Acer/Pictures/Documents/NetBeansProjects/SpaceInvaders/src/spaceinvaders/alien.png";

        ImageIcon ii = new ImageIcon(alienImg);

 

        setImage(ii.getImage());

    }

 

    public void act(int direction) {

 

        this.x += direction;

    }

 

    public Bomb getBomb() {

 

        return bomb;

    }

 

    public class Bomb extends Sprite {

 

        private boolean destroyed;

 

        public Bomb(int x, int y) {

 

            initBomb(x, y);

        }

 

        private void initBomb(int x, int y) {

 

            setDestroyed(true);

 

            this.x = x;

            this.y = y;

 

            String bombImg = "C:/Users/Acer/Pictures/Documents/NetBeansProjects/SpaceInvaders/src/spaceinvaders/bomb.png";

            ImageIcon ii = new ImageIcon(bombImg);

            setImage(ii.getImage());

        }

 

        public void setDestroyed(boolean destroyed) {

 

            this.destroyed = destroyed;

        }

 

        public boolean isDestroyed() {

 

            return destroyed;

        }

    }

}

Board.java

/*

 * 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 spaceinvaders;

import spaceinvaders.Alien;

import spaceinvaders.Player;

import spaceinvaders.Shot;

 

import javax.swing.ImageIcon;

import javax.swing.JPanel;

import javax.swing.Timer;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

 

public class Board extends JPanel {

 

    private Dimension d;

    private List<Alien> aliens;

    private Player player;

    private Shot shot;

 

    private int direction = -1;

    private int deaths = 0;

 

    private boolean inGame = true;

    private String explImg = "C:/Users/Acer/Pictures/Documents/NetBeansProjects/SpaceInvaders/src/spaceinvaders/explosion.png";

    private String message = "Game Over";

 

    private Timer timer;

 

 

    public Board() {

 

        initBoard();

        gameInit();

    }

 

    private void initBoard() {

 

        addKeyListener(new TAdapter());

        setFocusable(true);

        d = new Dimension(Commons.BOARD_WIDTH, Commons.BOARD_HEIGHT);

        setBackground(Color.black);

 

        timer = new Timer(Commons.DELAY, new GameCycle());

        timer.start();

 

        gameInit();

    }

 

 

    private void gameInit() {

 

        aliens = new ArrayList<>();

 

        for (int i = 0; i < 4; i++) {

            for (int j = 0; j < 6; j++) {

 

                Alien alien = new Alien(Commons.ALIEN_INIT_X + 18 * j,

                        Commons.ALIEN_INIT_Y + 18 * i);

                aliens.add(alien);

            }

        }

 

        player = new Player();

        shot = new Shot();

    }

 

    private void drawAliens(Graphics g) {

 

        for (Alien alien : aliens) {

 

            if (alien.isVisible()) {

 

                g.drawImage(alien.getImage(), alien.getX(), alien.getY(), this);

            }

 

            if (alien.isDying()) {

 

                alien.die();

            }

        }

    }

 

    private void drawPlayer(Graphics g) {

 

        if (player.isVisible()) {

 

            g.drawImage(player.getImage(), player.getX(), player.getY(), this);

        }

 

        if (player.isDying()) {

 

            player.die();

            inGame = false;

        }

    }

 

    private void drawShot(Graphics g) {

 

        if (shot.isVisible()) {

 

            g.drawImage(shot.getImage(), shot.getX(), shot.getY(), this);

        }

    }

 

    private void drawBombing(Graphics g) {

 

        for (Alien a : aliens) {

 

            Alien.Bomb b = a.getBomb();

 

            if (!b.isDestroyed()) {

 

                g.drawImage(b.getImage(), b.getX(), b.getY(), this);

            }

        }

    }

 

    @Override

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

 

        doDrawing(g);

    }

 

    private void doDrawing(Graphics g) {

 

        g.setColor(Color.black);

        g.fillRect(0, 0, d.width, d.height);

        g.setColor(Color.green);

 

        if (inGame) {

 

            g.drawLine(0, Commons.GROUND,

                    Commons.BOARD_WIDTH, Commons.GROUND);

 

            drawAliens(g);

            drawPlayer(g);

            drawShot(g);

            drawBombing(g);

 

        } else {

 

            if (timer.isRunning()) {

                timer.stop();

            }

 

            gameOver(g);

        }

 

        Toolkit.getDefaultToolkit().sync();

    }

 

    private void gameOver(Graphics g) {

 

        g.setColor(Color.black);

        g.fillRect(0, 0, Commons.BOARD_WIDTH, Commons.BOARD_HEIGHT);

 

        g.setColor(new Color(0, 32, 48));

        g.fillRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 50);

        g.setColor(Color.white);

        g.drawRect(50, Commons.BOARD_WIDTH / 2 - 30, Commons.BOARD_WIDTH - 100, 50);

 

        Font small = new Font("Helvetica", Font.BOLD, 14);

        FontMetrics fontMetrics = this.getFontMetrics(small);

 

        g.setColor(Color.white);

        g.setFont(small);

        g.drawString(message, (Commons.BOARD_WIDTH - fontMetrics.stringWidth(message)) / 2,

                Commons.BOARD_WIDTH / 2);

    }

 

    private void update() {

 

        if (deaths == Commons.NUMBER_OF_ALIENS_TO_DESTROY) {

 

            inGame = false;

            timer.stop();

            message = "Game won!";

        }

 

        // player

        player.act();

 

        // shot

        if (shot.isVisible()) {

 

            int shotX = shot.getX();

            int shotY = shot.getY();

 

            for (Alien alien : aliens) {

 

                int alienX = alien.getX();

                int alienY = alien.getY();

 

                if (alien.isVisible() && shot.isVisible()) {

                    if (shotX >= (alienX)

                            && shotX <= (alienX + Commons.ALIEN_WIDTH)

                            && shotY >= (alienY)

                            && shotY <= (alienY + Commons.ALIEN_HEIGHT)) {

 

                        ImageIcon ii = new ImageIcon(explImg);

                        alien.setImage(ii.getImage());

                        alien.setDying(true);

                        deaths++;

                        shot.die();

                    }

                }

            }

 

            int y = shot.getY();

            y -= 4;

 

            if (y < 0) {

                shot.die();

            } else {

                shot.setY(y);

            }

        }

 

        // aliens

 

        for (Alien alien : aliens) {

 

            int x = alien.getX();

 

            if (x >= Commons.BOARD_WIDTH - Commons.BORDER_RIGHT && direction != -1) {

 

                direction = -1;

 

                Iterator<Alien> i1 = aliens.iterator();

 

                while (i1.hasNext()) {

 

                    Alien a2 = i1.next();

                    a2.setY(a2.getY() + Commons.GO_DOWN);

                }

            }

 

            if (x <= Commons.BORDER_LEFT && direction != 1) {

 

                direction = 1;

 

                Iterator<Alien> i2 = aliens.iterator();

 

                while (i2.hasNext()) {

 

                    Alien a = i2.next();

                    a.setY(a.getY() + Commons.GO_DOWN);

                }

            }

        }

 

        Iterator<Alien> it = aliens.iterator();

 

        while (it.hasNext()) {

 

            Alien alien = it.next();

 

            if (alien.isVisible()) {

 

                int y = alien.getY();

 

                if (y > Commons.GROUND - Commons.ALIEN_HEIGHT) {

                    inGame = false;

                    message = "Invasion!";

                }

 

                alien.act(direction);

            }

        }

 

        // bombs

        Random generator = new Random();

 

        for (Alien alien : aliens) {

 

            int shot = generator.nextInt(15);

            Alien.Bomb bomb = alien.getBomb();

 

            if (shot == Commons.CHANCE && alien.isVisible() && bomb.isDestroyed()) {

 

                bomb.setDestroyed(false);

                bomb.setX(alien.getX());

                bomb.setY(alien.getY());

            }

 

            int bombX = bomb.getX();

            int bombY = bomb.getY();

            int playerX = player.getX();

            int playerY = player.getY();

 

            if (player.isVisible() && !bomb.isDestroyed()) {

 

                if (bombX >= (playerX)

                        && bombX <= (playerX + Commons.PLAYER_WIDTH)

                        && bombY >= (playerY)

                        && bombY <= (playerY + Commons.PLAYER_HEIGHT)) {

 

                    ImageIcon ii = new ImageIcon(explImg);

                    player.setImage(ii.getImage());

                    player.setDying(true);

                    bomb.setDestroyed(true);

                }

            }

 

            if (!bomb.isDestroyed()) {

 

                bomb.setY(bomb.getY() + 1);

 

                if (bomb.getY() >= Commons.GROUND - Commons.BOMB_HEIGHT) {

 

                    bomb.setDestroyed(true);

                }

            }

        }

    }

 

    private void doGameCycle() {

 

        update();

        repaint();

    }

 

    private class GameCycle implements ActionListener {

 

        @Override

        public void actionPerformed(ActionEvent e) {

 

            doGameCycle();

        }

    }

 

    private class TAdapter extends KeyAdapter {

 

        @Override

        public void keyReleased(KeyEvent e) {

 

            player.keyReleased(e);

        }

 

        @Override

        public void keyPressed(KeyEvent e) {

 

            player.keyPressed(e);

 

            int x = player.getX();

            int y = player.getY();

 

            int key = e.getKeyCode();

 

            if (key == KeyEvent.VK_SPACE) {

 

                if (inGame) {

 

                    if (!shot.isVisible()) {

 

                        shot = new Shot(x, y);

                    }

                }

            }

        }

    }

}

 

Commons.java

/*

 * 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 spaceinvaders;

 

/**

 *

 * @author Acer

 */

public interface Commons {

 

    int BOARD_WIDTH = 358;

    int BOARD_HEIGHT = 350;

    int BORDER_RIGHT = 30;

    int BORDER_LEFT = 5;

 

    int GROUND = 290;

    int BOMB_HEIGHT = 5;

 

    int ALIEN_HEIGHT = 12;

    int ALIEN_WIDTH = 12;

    int ALIEN_INIT_X = 150;

    int ALIEN_INIT_Y = 5;

 

    int GO_DOWN = 15;

    int NUMBER_OF_ALIENS_TO_DESTROY = 24;

    int CHANCE = 5;

    int DELAY = 17;

    int PLAYER_WIDTH = 15;

    int PLAYER_HEIGHT = 10;

}


 

Player.java

/*

 * 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 spaceinvaders;

import spaceinvaders.Commons;

 

import javax.swing.ImageIcon;

import java.awt.event.KeyEvent;

/**

 *

 * @author Acer

 */

public class Player extends Sprite {

 

    private int width;

 

    public Player() {

 

        initPlayer();

    }

 

    private void initPlayer() {

 

        String playerImg = "C:/Users/Acer/Pictures/Documents/NetBeansProjects/SpaceInvaders/src/spaceinvaders/player.png";

        ImageIcon ii = new ImageIcon(playerImg);

 

        width = ii.getImage().getWidth(null);

        setImage(ii.getImage());

 

        int START_X = 270;

        setX(START_X);

 

        int START_Y = 280;

        setY(START_Y);

    }

 

    public void act() {

 

        x += dx;

 

        if (x <= 2) {

 

            x = 2;

        }

 

        if (x >= Commons.BOARD_WIDTH - 2 * width) {

 

            x = Commons.BOARD_WIDTH - 2 * width;

        }

    }

 

    public void keyPressed(KeyEvent e) {

 

        int key = e.getKeyCode();

 

        if (key == KeyEvent.VK_LEFT) {

 

            dx = -2;

        }

 

        if (key == KeyEvent.VK_RIGHT) {

 

            dx = 2;

        }

    }

 

    public void keyReleased(KeyEvent e) {

 

        int key = e.getKeyCode();

 

        if (key == KeyEvent.VK_LEFT) {

 

            dx = 0;

        }

 

        if (key == KeyEvent.VK_RIGHT) {

 

            dx = 0;

        }

    }

}

 

 

Shot.java

/*

 * 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 spaceinvaders;

import javax.swing.ImageIcon;

/**

 *

 * @author Acer

 */

public class Shot extends Sprite {

 

    public Shot() {

    }

 

    public Shot(int x, int y) {

 

        initShot(x, y);

    }

 

    private void initShot(int x, int y) {

 

        String shotImg = "C:/Users/Acer/Pictures/Documents/NetBeansProjects/SpaceInvaders/src/spaceinvaders/shot.png";

        ImageIcon ii = new ImageIcon(shotImg);

        setImage(ii.getImage());

 

        int H_SPACE = 6;

        setX(x + H_SPACE);

 

        int V_SPACE = 1;

        setY(y - V_SPACE);

    }

}

 

SpaceInvaders.java

/*

 * 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 spaceinvaders;

import java.awt.EventQueue;

import javax.swing.JFrame;

/**

 *

 * @author Acer

 */

public class SpaceInvaders extends JFrame  {

 

    public SpaceInvaders() {

 

        initUI();

    }

 

    private void initUI() {

 

        add(new Board());

 

        setTitle("Space Invaders");

        setSize(Commons.BOARD_WIDTH, Commons.BOARD_HEIGHT);

 

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setResizable(false);

        setLocationRelativeTo(null);

    }

 

    public static void main(String[] args) {

 

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                SpaceInvaders ex = new SpaceInvaders();

                ex.setVisible(true);

            }

        });               

    }

}

Sprite.java

/*

 * 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 spaceinvaders;

import java.awt.Image;

 

public class Sprite {

 

    private boolean visible;

    private Image image;

    private boolean dying;

 

    int x;

    int y;

    int dx;

 

    public Sprite() {

 

        visible = true;

    }

 

    public void die() {

 

        visible = false;

    }

 

    public boolean isVisible() {

 

        return visible;

    }

 

    protected void setVisible(boolean visible) {

 

        this.visible = visible;

    }

 

    public void setImage(Image image) {

 

        this.image = image;

    }

 

    public Image getImage() {

 

        return image;

    }

 

    public void setX(int x) {

 

        this.x = x;

    }

 

    public void setY(int y) {

 

        this.y = y;

    }

 

    public int getY() {

 

        return y;

    }

 

    public int getX() {

 

        return x;

    }

 

    public void setDying(boolean dying) {

 

        this.dying = dying;

    }

 

    public boolean isDying() {

 

        return this.dying;

    }

}

Tidak ada komentar: