import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
// Note: the sound files are not provided so you won't get the sounds if
// you compile and run this
public class Shoot implements KeyListener
{
static final int WIDTH = 500;
static final int HEIGHT = 400;
NTJFrame frame = new NTJFrame("Cheesy Shoot-'Em-Up Example");
Drawing drawing = new Drawing();
Ship ship = new Ship();
Bullet bullet1 = new Bullet(), bullet2 = new Bullet(200,150);
public Shoot()
{
frame.getContentPane().add(drawing,"Center");
frame.addKeyListener(this);
frame.setLocation(100,200);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
// make the bullets and the ship start flying
ship.start();
bullet1.start();
bullet2.start();
}
// called when a key is pressed (if the key is held down it will
// get called repeatedly until another key is pressed
public void keyPressed(KeyEvent e)
{
int typed = e.getKeyCode();
if (typed == KeyEvent.VK_LEFT)
{
ship.x -= 10;
// if we fly off the left side of the screen play a sound
// and move over to the right side of the screen
if (ship.x < 0)
{
try
{
AudioClip open = Applet.newAudioClip(new
URL("file:\\x:\\courses\\dcc\\sound\\tos-power.au"));
open.play();
} catch (MalformedURLException murle)
{};
ship.x = WIDTH;
}
}
else if (typed == KeyEvent.VK_RIGHT)
{
ship.x += 10;
if (ship.x > WIDTH)
ship.x = 0;
}
else if (typed == KeyEvent.VK_UP)
{
ship.y -= 10;
if (ship.y < 0)
ship.y = HEIGHT-100;
}
else if (typed == KeyEvent.VK_DOWN)
{
ship.y += 10;
if (ship.y > HEIGHT)
ship.y = 0 + 100;
}
drawing.repaint();
}
// called when a key that was pressed is released
public void keyReleased(KeyEvent e)
{
}
// called when a key is pressed and released immediatley (typed)
public void keyTyped(KeyEvent e)
{
}
public static void main (String[] args)
{
Shoot s = new Shoot();
}
class Drawing extends JComponent
{
ImageIcon background = new ImageIcon("green_640.jpg");
public Drawing()
{
repaint();
}
public void paint(Graphics g)
{
Rectangle r = new Rectangle(ship.x, ship.y, ship.width, ship.height);
g.drawImage(background.getImage(),0,0,this);
g.setColor(Color.white);
g.fillOval(ship.x+8, ship.y+16, ship.width/6, ship.height/6);
g.drawOval(ship.x, ship.y, ship.width/2, ship.height/2);
g.drawRect(ship.x, ship.y+50, ship.width-40, ship.height-50);
g.drawRect(ship.x+15, ship.y+50, ship.width-40, ship.height-50);
g.setColor(Color.yellow);
g.fillOval(bullet1.x, bullet1.y, 10, 10);
g.fillOval(bullet2.x, bullet2.y, 10, 10);
// if a bullet hits the ship
if (r.contains(bullet1.x, bullet1.y) || r.contains(bullet2.x, bullet2.y))
{
g.setColor(Color.red);
g.fillRect(ship.x, ship.y, ship.width, ship.height+1);
// stop the ship and the bullets from moving
ship.interrupt();
bullet1.interrupt();
bullet2.interrupt();
try
{
AudioClip open = Applet.newAudioClip(new
URL("file:\\x:\\courses\\dcc\\sound\\entphoex.wav"));
open.play();
for (int i = 10000; i > 0 ; i--)
for (int j = 10000; j > 0; j--);
open = Applet.newAudioClip(new URL("file:\\x:\\courses\\dcc\\sound\\tos-deadjim.au"));
open.play();
} catch (MalformedURLException e)
{};
}
}
}
// a class for keeping track of bullet positions (note: it doesn't draw
// the bullets, the Drawing class does that)
class Bullet extends Thread
{
int x, y;
public Bullet(int x, int y)
{
this.x = x;
this.y = y;
}
public Bullet()
{
x = 30;
y = 100;
}
public void run()
{
try
{
while (true)
{
sleep(100);
x += 10;
if (x >= WIDTH)
x = 30;
drawing.repaint();
}
}
catch (InterruptedException e)
{
}
}
}
// a class for keeping track of the ship's position
class Ship extends Thread
{
int x=300, y = 300, width = 50, height = 100;
public void run()
{
try
{
while (true)
{
sleep(200);
y -= 10;
if (y <= 0)
y = HEIGHT;
drawing.repaint();
}
}
catch (InterruptedException e)
{
}
}
}
}
|