/*
* Hit the ship
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
class HitMe extends Canvas
{
public static final int FRAME_WIDTH = 800;
public static final int FRAME_HEIGHT = 600;
public static final int FRAME_X_ORIGIN = 50;
public static final int FRAME_Y_ORIGIN = 50;
public static final int BUTTON_WIDTH = 250;
public static final int BUTTON_HEIGHT = 25;
public BufferedImage bi1, bi2, bi3;
public BufferStrategy bs;
public int iPosX, iPosY;
public int mPosX, mPosY;
public int firePosX, firePosY;
public int hitShipCtr;
public int rnd;
public static int scoreCtr;
public boolean up, down, left, right, space, launch, endGame, hit;
public HitMe( )
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(0,0);
addKeyListener(new KeyStroke());
}
public static void main(String[] args)
{
JFrame j = new JFrame();
Container contentPane = j.getContentPane();
j.setTitle ("Move Me");
j.setSize (FRAME_WIDTH, FRAME_HEIGHT);
j.setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
j.setResizable(false);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HitMe c = new HitMe();
contentPane.add(c);
c.init();
c.requestFocus();
c.gameLoop();
}
public void init()
{
iPosX=390;
iPosY=530;
mPosX=-10;
mPosY=20;
firePosX=0;
firePosY=0;
hitShipCtr=0;
hit=false;
scoreCtr=0;
rnd=10;
createBufferStrategy(2);
bs = getBufferStrategy();
try
{
bi1 = ImageIO.read(new File("shiphit.png"));
bi2 = ImageIO.read(new File("ship.gif"));
bi3 = ImageIO.read(new File("canon.png"));
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, "error");
}
}
public void gameLoop()
{
while(!endGame)
{
gameLogic();
//put this at the top for clearing screen
Graphics g = bs.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT);
// Draw your objects on the screen
// g.drawImage(bi1,10,10,bi1.getWidth(),bi1.getHeight(),null);
// Color myColor = new Color(100, 125, 250);
g.setColor(Color.YELLOW);
g.fillRect(0,0,FRAME_WIDTH,70);
g.drawImage(bi3,iPosX,iPosY,bi3.getWidth(),bi3.getHeight(),null);
//target hit
if(firePosX>=mPosX-2 && firePosX<=mPosX+98 && firePosY==mPosY+50)
{
g.drawImage(bi1,mPosX,mPosY,bi1.getWidth(),bi1.getHeight(),null);
hitShipCtr++;
launch = false;
mPosX = mPosX - 1; //offset ctr to make ship stop
scoreCtr++;
if(hitShipCtr==10)
{
mPosX = 999;
hit = false;
firePosX=0;
firePosY=0;
hitShipCtr=0;
}
}
else
{
g.drawImage(bi2,mPosX,mPosY,bi2.getWidth(),bi2.getHeight(),null);
}
g.setColor(Color.BLACK);
g.fillRect(mPosX,mPosY+50,98,2);
if(launch)
{
g.setColor(Color.BLACK);
g.fillRect(firePosX,firePosY,10,10);
firePosY = firePosY - 1;
if(firePosY < 65)
{
launch=false;
}
}
if(mPosX > 810)
{
mPosX = -10;
rnd = (int) Math.floor(Math.random() * 10) + 5;
}
else
{
mPosX++;
}
try
{
Thread.sleep(rnd);
}
catch(Exception e)
{
}
finally
{
// put this at the bottom for showing
bs.show();
}
}
JOptionPane.showMessageDialog(null, "The Game has ended. Thank you for playing.");
System.exit(0);
}
public void gameLogic()
{
if(left)
{
if(iPosX>0)
{
iPosX--;
}
}
if(right)
{
if(iPosX<750)
{
iPosX++;
}
}
if(space)
{
launch = true;
firePosX = iPosX+20;
firePosY = 525;
space = false;
}
// if(up)
// {
// iPosY = iPosY - 1;
// }
// if(down)
// {
// iPosY = iPosY + 1;
// }
}
class KeyStroke implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ESCAPE)
{
endGame = true;
}
if (e.getKeyCode()==KeyEvent.VK_UP)
{
up = true;
}
if (e.getKeyCode()==KeyEvent.VK_DOWN)
{
down = true;
}
if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
left = true;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT)
{
right = true;
}
if (e.getKeyCode()==KeyEvent.VK_SPACE)
{
space = true;
}
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_UP)
{
up = false;
}
if (e.getKeyCode()==KeyEvent.VK_DOWN)
{
down = false;
}
if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
left = false;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT)
{
right = false;
}
}
public void keyTyped(KeyEvent e)
{
}
}
}