Below is the working code for Bullet Shooting Game developed using Java.
This game project includes 5 java files:
This game project includes 5 java files:
- Window.java
package balloone_Shooting;
import javax.swing.JFrame;
public class Window {
/**
* @param args
*/
public static void main(String[] args) {
Combine c = new Combine();
JFrame f = new JFrame("Cloud Shooting");
f.add(c);
f.setResizable(false);
f.setSize(800,700);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
}
}
import javax.swing.JFrame;
public class Window {
/**
* @param args
*/
public static void main(String[] args) {
Combine c = new Combine();
JFrame f = new JFrame("Cloud Shooting");
f.add(c);
f.setResizable(false);
f.setSize(800,700);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
}
}
- Paddle.java
package balloone_Shooting;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Paddle {
private int x, y;
private boolean left = false, right = false;;
Image volcano;
ImageIcon big = new ImageIcon(this.getClass()
.getResource("volcano_big.png"));
ImageIcon small = new ImageIcon(this.getClass().getResource(
"volcano_small.png"));
public Paddle() {
x = 395;
y = 630;
volcano = small.getImage();
}
public void setLeft(boolean left) {
this.left = left;
}
public void setRight(boolean right) {
this.right = right;
}
public boolean getLeft() {
return left;
}
public boolean getRight() {
return right;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return volcano;
}
public void left() {
x -= 2;
if (x <= 0)
x = 0;
}
public void right() {
x += 2;
if (x >= 706)
x = 706;
}
public void volcano_Pressed() {
y = 630;
volcano = small.getImage();
}
public void volcano_Release() {
y = 620;
volcano = big.getImage();
}
}
import java.awt.Image;
import javax.swing.ImageIcon;
public class Paddle {
private int x, y;
private boolean left = false, right = false;;
Image volcano;
ImageIcon big = new ImageIcon(this.getClass()
.getResource("volcano_big.png"));
ImageIcon small = new ImageIcon(this.getClass().getResource(
"volcano_small.png"));
public Paddle() {
x = 395;
y = 630;
volcano = small.getImage();
}
public void setLeft(boolean left) {
this.left = left;
}
public void setRight(boolean right) {
this.right = right;
}
public boolean getLeft() {
return left;
}
public boolean getRight() {
return right;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return volcano;
}
public void left() {
x -= 2;
if (x <= 0)
x = 0;
}
public void right() {
x += 2;
if (x >= 706)
x = 706;
}
public void volcano_Pressed() {
y = 630;
volcano = small.getImage();
}
public void volcano_Release() {
y = 620;
volcano = big.getImage();
}
}
- FireBall.java
package balloone_Shooting;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class FireBall {
private int x, y;
private Image image;
boolean visible;
private int width, height;
private int dy;
public FireBall(int x) {
dy = 20;
y = 605;
ImageIcon ii = new ImageIcon(this.getClass()
.getResource("fireball.png"));
image = ii.getImage();
visible = true;
width = image.getWidth(null);
height = image.getHeight(null);
this.x = x;
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void move() {
y = y - dy;
}
}
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class FireBall {
private int x, y;
private Image image;
boolean visible;
private int width, height;
private int dy;
public FireBall(int x) {
dy = 20;
y = 605;
ImageIcon ii = new ImageIcon(this.getClass()
.getResource("fireball.png"));
image = ii.getImage();
visible = true;
width = image.getWidth(null);
height = image.getHeight(null);
this.x = x;
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void move() {
y = y - dy;
}
}
- Cloudball.java
package balloone_Shooting;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Cloudball {
private int x, y, width, height;
private Image img;
public Cloudball(int x) {
ImageIcon ic = new ImageIcon(this.getClass().getResource(
"cloudball.png"));
img = ic.getImage();
this.x = x;
y = 0;
width = img.getWidth(null);
height = img.getHeight(null);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void move() {
y++;
}
public Image getImage()
{
return img;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Cloudball {
private int x, y, width, height;
private Image img;
public Cloudball(int x) {
ImageIcon ic = new ImageIcon(this.getClass().getResource(
"cloudball.png"));
img = ic.getImage();
this.x = x;
y = 0;
width = img.getWidth(null);
height = img.getHeight(null);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void move() {
y++;
}
public Image getImage()
{
return img;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
- Combine.java
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Combine extends JPanel implements ActionListener {
Image back_Ground;
Timer timer;
Paddle paddle;
ArrayList<FireBall> fireballs_array;
ArrayList<Cloudball> cloud_array;
FireBall fireBall;
private boolean fire;
public boolean isFire() {
return fire;
}
public void setFire(boolean fire) {
this.fire = fire;
}
public Combine() {
addKeyListener(new Action());
ImageIcon ii = new ImageIcon(this.getClass().getResource(
"skybackground.png"));
fireballs_array = new ArrayList<FireBall>();
cloud_array = new ArrayList<Cloudball>();
paddle = new Paddle();
setFocusable(true);
back_Ground = ii.getImage();
timer = new Timer(70, this);
timer.start();
Thread startCloud = new Thread(new Runnable() {
public void run() {
while (true)
try {
cloudBalls();
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
startCloud.start();
Thread startFire = new Thread(new Runnable() {
public void run() {
while (true)
try {
//System.out.println("fire " + fire);
if (fire == true) {
fire();
}
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
startFire.start();
Thread movePaddle = new Thread(new Runnable() {
public void run() {
while (true)
try {
if (paddle.getLeft() == true) {
paddle.left();
}
if (paddle.getRight() == true) {
paddle.right();
}
Thread.sleep(9);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
movePaddle.start();
}
public void actionPerformed(ActionEvent arg0) {
for (int k = 0; k < fireballs_array.size(); k++) {
fireballs_array.get(k).move();
if (fireballs_array.get(k).getY() < 1) {
fireballs_array.remove(k);
}
}
for (int i = 0; i < cloud_array.size(); i++) {
cloud_array.get(i).move();
if (cloud_array.get(i).getY() > 600) {
cloud_array.remove(i);
}
}
Thread collide = new Thread(new Runnable() {
public void run() {
collision();
}
});
collide.start();
repaint();
}
public void paintComponent(Graphics g) {
g.drawImage(back_Ground, 0, 0, null);
g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(), null);
if (!cloud_array.isEmpty()) {
for (int i = 0; i < cloud_array.size(); i++) {
g.drawImage(cloud_array.get(i).getImage(), cloud_array.get(i)
.getX(), cloud_array.get(i).getY(), null);
}
}
if (!fireballs_array.isEmpty()) {
for (int k = 0; k < fireballs_array.size(); k++) {
g.drawImage(fireballs_array.get(k).getImage(), fireballs_array
.get(k).getX(), fireballs_array.get(k).getY(), null);
}
}
}
public void cloudBalls() {
Random rn = new Random();
int ran = rn.nextInt(730);
Cloudball ball = new Cloudball(ran);
cloud_array.add(ball);
}
public void fire() {
fireBall = new FireBall(paddle.getX() + 35);
// fireBall.setY(20);
fireballs_array.add(fireBall);
}
public void collision() {
for (int k = 0; k < fireballs_array.size(); k++) {
for (int j = 0; j < cloud_array.size(); j++) {
Rectangle r1;
if(!fireballs_array.isEmpty()){
r1 = fireballs_array.get(k).getBounds();
Rectangle r2 = cloud_array.get(j).getBounds();
if (r1.intersects(r2)) {
fireballs_array.remove(k);
cloud_array.remove(j);
}
}
}
}
}
private class Action extends KeyAdapter {
public void keyPressed(KeyEvent e) {
paddle.volcano_Pressed();
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
fire = true;
}
if (keyCode == KeyEvent.VK_LEFT) {
paddle.setLeft(true);
}
if (keyCode == KeyEvent.VK_RIGHT) {
paddle.setRight(true);
}
}
public void keyReleased(KeyEvent e) {
paddle.volcano_Release();
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
fire = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
paddle.setLeft(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
paddle.setRight(false);
}
}
}
}
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Combine extends JPanel implements ActionListener {
Image back_Ground;
Timer timer;
Paddle paddle;
ArrayList<FireBall> fireballs_array;
ArrayList<Cloudball> cloud_array;
FireBall fireBall;
private boolean fire;
public boolean isFire() {
return fire;
}
public void setFire(boolean fire) {
this.fire = fire;
}
public Combine() {
addKeyListener(new Action());
ImageIcon ii = new ImageIcon(this.getClass().getResource(
"skybackground.png"));
fireballs_array = new ArrayList<FireBall>();
cloud_array = new ArrayList<Cloudball>();
paddle = new Paddle();
setFocusable(true);
back_Ground = ii.getImage();
timer = new Timer(70, this);
timer.start();
Thread startCloud = new Thread(new Runnable() {
public void run() {
while (true)
try {
cloudBalls();
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
startCloud.start();
Thread startFire = new Thread(new Runnable() {
public void run() {
while (true)
try {
//System.out.println("fire " + fire);
if (fire == true) {
fire();
}
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
startFire.start();
Thread movePaddle = new Thread(new Runnable() {
public void run() {
while (true)
try {
if (paddle.getLeft() == true) {
paddle.left();
}
if (paddle.getRight() == true) {
paddle.right();
}
Thread.sleep(9);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
movePaddle.start();
}
public void actionPerformed(ActionEvent arg0) {
for (int k = 0; k < fireballs_array.size(); k++) {
fireballs_array.get(k).move();
if (fireballs_array.get(k).getY() < 1) {
fireballs_array.remove(k);
}
}
for (int i = 0; i < cloud_array.size(); i++) {
cloud_array.get(i).move();
if (cloud_array.get(i).getY() > 600) {
cloud_array.remove(i);
}
}
Thread collide = new Thread(new Runnable() {
public void run() {
collision();
}
});
collide.start();
repaint();
}
public void paintComponent(Graphics g) {
g.drawImage(back_Ground, 0, 0, null);
g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(), null);
if (!cloud_array.isEmpty()) {
for (int i = 0; i < cloud_array.size(); i++) {
g.drawImage(cloud_array.get(i).getImage(), cloud_array.get(i)
.getX(), cloud_array.get(i).getY(), null);
}
}
if (!fireballs_array.isEmpty()) {
for (int k = 0; k < fireballs_array.size(); k++) {
g.drawImage(fireballs_array.get(k).getImage(), fireballs_array
.get(k).getX(), fireballs_array.get(k).getY(), null);
}
}
}
public void cloudBalls() {
Random rn = new Random();
int ran = rn.nextInt(730);
Cloudball ball = new Cloudball(ran);
cloud_array.add(ball);
}
public void fire() {
fireBall = new FireBall(paddle.getX() + 35);
// fireBall.setY(20);
fireballs_array.add(fireBall);
}
public void collision() {
for (int k = 0; k < fireballs_array.size(); k++) {
for (int j = 0; j < cloud_array.size(); j++) {
Rectangle r1;
if(!fireballs_array.isEmpty()){
r1 = fireballs_array.get(k).getBounds();
Rectangle r2 = cloud_array.get(j).getBounds();
if (r1.intersects(r2)) {
fireballs_array.remove(k);
cloud_array.remove(j);
}
}
}
}
}
private class Action extends KeyAdapter {
public void keyPressed(KeyEvent e) {
paddle.volcano_Pressed();
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
fire = true;
}
if (keyCode == KeyEvent.VK_LEFT) {
paddle.setLeft(true);
}
if (keyCode == KeyEvent.VK_RIGHT) {
paddle.setRight(true);
}
}
public void keyReleased(KeyEvent e) {
paddle.volcano_Release();
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
fire = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
paddle.setLeft(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
paddle.setRight(false);
}
}
}
}