import java.awt.*;
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 javax.swing.Timer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Core extends JPanel implements ActionListener{
public Core(){
addKeyListener(new TAdapter());
setFocusable(true);
setDoubleBuffered(true);
bg = new ImageIcon(getClass().getResource("space.png")).getImage();
menu = new ImageIcon(getClass().getResource("menu.png")).getImage();
running = true;
setSize(700,700);
player = new Player();
getButton()
;
initHeart();
timer = new Timer(5, this);
timer.start();
}
public void getButton(){
JButton shop = new JButton("Rod");
shop.setVisible(true);
add(shop);
}
public void addNotify(){
super.addNotify();
C_width = getWidth();
C_hiegth = getHeight();
}
private Timer timer;
public JButton option;
////////////SOUNDS/////////////////////
public String explosion = "explosion.wav";
////////////SOUNDS/////////////////////
////////////CLASSOBJECT/////////////////////
private JButton shop;
private Image bg;
private Image menu;
int score = 0 ;
public eHearts heart;
public Laser laser;
public Explosion explode;
////////////CLASSOBJECT/////////////////////
/////////////SPRITES//////////////////
public Player player;
/////////////SPRITES//////////////////
public Image fireHit;
private boolean running;
///////////IMAGEES////////////////////
public Image heart1;
public Image boom;
///////////IMAGEES////////////////////
public Image p;
///////////XandYintercep////////////////////
public int x;
public int y;
///////////XandYintercep////////////////////
///////////ARRAYLIST//////////////////////
private ArrayList destroy;
private ArrayList h;
///////////ARRAYLIST//////////////////////
///////////POSITION OF HEART/////////////////
private int [] [] xANDy = {{0,-200},{100,100},{300,300},{400, 200},{350,150},{240,-100},{350,-150},{450,-150},{200,150},{60,60}};
///////////POSITION OF HEART/////////////////
private int C_width;
private int C_hiegth;
public void stop(){
running = false;
}
public void run(){
}//call init and gameLoop
public void initHeart(){
/////////////////DELETEHEARTSWHENHIT/////////////
h = new ArrayList();
for (int i= 0; i < xANDy.length; i++){
h.add(new eHearts(xANDy[i][0], xANDy[i][1]));
}
/////////////////DELETEHEARTSWHENHIT/////////////
}
//////////////////////////////////CHECKINGFORCOLLISION/////////////////////////
public void checkCollision(){
Rectangle pc = player.getBounds();
for(int c = 0 ; c < h.size() ; c++)
{
eHearts eh = (eHearts) h.get(c) ;
Rectangle e = eh.getBounds();
if(pc.intersects(e)){
running = false;
}
}
ArrayList f = player.getFire();
for(int i = 0 ; i < f.size() ; i++){
Laser l = (Laser) f.get(i) ;
Rectangle p = l.getBounds();
if(l.getY() < 0){
l.setVisible(false);
}
for(int c = 0 ; c < h.size() ; c++)
{
eHearts eh = (eHearts) h.get(c) ;
Rectangle e = eh.getBounds();
if(p.intersects(e))
{
++score;
new SoundsEffects(explosion);
l.setVisible(false);
eh.setVisible(false);
ArrayList b = player.getExplode();
for(int ex = 0; i < b.size(); i++){
Explosion explode = (Explosion) b.get(ex);
explode.setX(eh.getX());
explode.setY(eh.getY());
explode.setVisible(false);
}
}
}
}
}
//////////////////////////////////CHECKINGFORCOLLISION/////////////////////////
public void actionPerformed(ActionEvent e) {
if(player.getPause()){
ArrayList f = player.getFire();
for(int i = 0 ; i < f.size() ; i++){
Laser l = (Laser) f.get(i) ;
if(l.getVisible()){
l.move();
}else{
f.remove(i);
}
}
for(int i2 = 0 ; i2 < h.size() ; i2++){
eHearts eh = (eHearts) h.get(i2) ;
if(eh.getVisible()){
eh.move();
}else{
h.remove(i2);
}
}
player.move();
checkCollision();
}//end pause
repaint();
}
public void paint(Graphics g){
super.paint(g);
if (running){
Graphics2D g1 = (Graphics2D)g;
g.drawImage(bg,0,0,null);
if(player.getVisible()){
g1.drawImage(player.getImage(), Math.round(player.getX()), Math.round(player.getY()), null);
}
ArrayList f = player.getFire();
for(int i = 0 ; i < f.size() ; i++){
Laser l = (Laser) f.get(i) ;
g1.drawImage(l.getLaser(), l.getX(), l.getY(), null);
}
////////////////////FOR BOOM////////////////////////////////////////////////
ArrayList b = player.getExplode();
for(int i = 0; i < b.size(); i++){
Explosion explode = (Explosion) b.get(i);
if(explode.getVisible() == false){
g1.drawImage(explode.getExplosion(), explode.getX(), explode.getY(), null);
}
}
////////////////////FOR BOOM////////////////////////////////////////////////
////////////////////TARGET///////////////////////////////////////////////////
for(int i = 0; i< h.size(); i++){
eHearts eh = (eHearts) h.get(i);
if(eh.getVisible()){
g1.drawImage(eh.getEnemy(), eh.getX(), eh.getY(), null);
}
}
////////////////////TARGET///////////////////////////////////////////////////
g1.setColor(Color.WHITE);
g1.drawString("Score: " + score, 5, 15);
if(player.getPause() == false){
g1.drawImage(menu, 250,150,null);
}
}//end game ender
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter{
public void keyReleased(KeyEvent e){
player.keyReleased(e);
}
public void keyPressed(KeyEvent e){
player.keyPressed(e);
}
}
}