rendered paste bodyimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
public class myJPanel extends JPanel implements ActionListener, KeyListener
{
//testing the new alien
alien alien1;
//ship Info
private int shipX=0;
private int shipY=490;
private String shipImage = "blank.gif";
private int shipLives= 3;
//shot timer and position
private Timer shotTimer;
private int shotDelay=4;
private int shotX=-20;
private int shotY=530;
private String shotStatus="avaliable";
//alien shot timer and position
private Timer alienShotTimer;
private int alienShotDelay = 50;
private int alienShotX =-20;
private int alienShotY =-20;
private String alienShotStatus="available";
//Alien Timer and Position
private Timer alienTimer;
private int alienDelay=100;
private int alienX=10;
private int alienY=10;
//Alien's image
String alienImage = "blank.gif";
String alienImage1 = "blank.gif";
String alienImage2 = "blank.gif";
String alienImage3 = "blank.gif";
String alienImage4 = "blank.gif";
private String DirectionMoving="right";
//Alien Life
public String alienLife="dead";
public String alienLife1="dead";
public String alienLife2="dead";
public String alienLife3="dead";
public String alienLife4="dead";
//Current Game state info
private int level=1;
private int shipsLeft=5;
private int currentScore = 0;
private int pointsToAdd = 100;
//Menu Items
private JLabel b;
private JButton restart;
private JLabel playerLives;
private JLabel score;
private String[] difficultySettings = {"Easy", "Medium", "Hard"};
private JComboBox difficulties;
//Panel Settings
private Color bgcolor = Color.black;
private String backgroundImage = "title.gif";
//ship hit timer
private Timer shipHitTimer;
private int shipHitDelay=10;
private int noTimes = 0;
//keys actions
private Timer movementsTimer;
private int movementsDelay = 2;
private int leftKey = 0;
private int rightKey = 0;
private int spaceBar = 0;
//Status Image
private String statusImage = "blank.gif";
private JLabel livesIcon = new JLabel();
//Sounds
Sampled alienShot = new Sampled("alienShot.wav");
Sampled shipShot = new Sampled("shipShot.wav");
Sampled explosion = new Sampled("explosion.wav");
Sampled advanceLevel = new Sampled("advanceLevel.wav");
Sampled startGame = new Sampled("startGame.wav");
Sampled miniHit = new Sampled("miniHit.wav");
Sampled alienHit = new Sampled("alienHit.wav");
public myJPanel()
{
//define settings for the panel
super();
setLayout(new BorderLayout());
setBackground(bgcolor);
//movements timer
movementsTimer = new Timer(movementsDelay, this);
movementsTimer.addActionListener(this);
//Shot timer
shotTimer = new Timer(shotDelay, this);
shotTimer.addActionListener(this);
//shipHit Timer
shipHitTimer = new Timer(shipHitDelay, this);
shipHitTimer.addActionListener(this);
//alien Tim er
alienTimer = new Timer(alienDelay, this);
alienTimer.addActionListener(this);
//Alien Shot Timer
alienShotTimer = new Timer(alienShotDelay, this);
alienShotTimer.addActionListener(this);
//restart button
restart = new JButton("Start");
restart.addActionListener(this);
//lives
playerLives = new JLabel("3");
playerLives.setForeground(Color.white);
Font playingFont = playerLives.getFont();
Font playerFont = playingFont.deriveFont(25.0f);
playerLives.setFont(playerFont);
playerLives.setVisible(true);
//score
score = new JLabel("0");
score.setForeground(Color.white);
Font bigFont = score.getFont();
Font largeFont = bigFont.deriveFont(25.0f);
score.setFont(largeFont);
score.setVisible(true);
//level
b = new JLabel("1");
b.setForeground(Color.white);
Font bigFont2 = b.getFont();
Font largeFont2 = bigFont2.deriveFont(25.0f);
b.setFont(largeFont2);
b.setVisible(true);
//difficulty level dropdown box
difficulties= new JComboBox(difficultySettings);
difficulties.setSelectedIndex(0);
difficulties.addActionListener(this);
//menu
JPanel menu = new JPanel();
menu.setBackground(Color.black);
menu.setFocusable(true);
menu.addKeyListener(this);
menu.requestFocus();
//Lives
ImageIcon image1 = new ImageIcon("lives.gif");
livesIcon.setIcon(image1);
menu.add(livesIcon);
menu.add(playerLives);
//Scores
ImageIcon image2 = new ImageIcon("score.gif");
JLabel scoresIcon = new JLabel();
scoresIcon.setIcon(image2);
menu.add(scoresIcon);
menu.add(score);
//Level
ImageIcon image3 = new ImageIcon("level.gif");
JLabel levelIcon = new JLabel();
levelIcon.setIcon(image3);
menu.add(levelIcon);
menu.add(b);
//difficulty
menu.add(difficulties);
menu.add(restart);
//add the menu
add(menu, BorderLayout.SOUTH);
}
/*****************
* Paint
*****************/
public void paintComponent(Graphics g)
{
//background Image
super.paintComponent(g);
try
{
g.drawImage(ImageIO.read(new File(backgroundImage)), 0,0, this);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the ship
try
{
g.drawImage(ImageIO.read(new File(shipImage)), shipX, shipY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the ship shot
g.setColor(Color.green);
g.fillRect(shotX, shotY, 3, 10);
//draw the Alien's shot
g.setColor(Color.red);
g.fillRect(alienShotX, alienShotY, 3, 10);
//draw alien1
try
{
g.drawImage(ImageIO.read(new File(alienImage)), alienX, alienY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the alien 2
try
{
g.drawImage(ImageIO.read(new File(alienImage1)), alienX+40, alienY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the alien 2
try
{
g.drawImage(ImageIO.read(new File(alienImage2)), alienX+80, alienY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the alien 3
try
{
g.drawImage(ImageIO.read(new File(alienImage3)), alienX+120, alienY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//draw the alien 4
try
{
g.drawImage(ImageIO.read(new File(alienImage4)), alienX+160, alienY, null);
}
catch (Exception e)
{
e.printStackTrace();
}
//Game Over
try
{
g.drawImage(ImageIO.read(new File(statusImage)), 0,0, this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/***********************
* Regular Actions
***********************/
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
//START/RESTART BUTTON
if(obj==restart)
{
//testing the alien
alien1 = new alien("ship.gif", 40, 40);
//play the sound that you're going to start!
startGame.playSound();
//level settings
String difficulty= (String)difficulties.getSelectedItem();
if(difficulty == "Easy")
{
alienDelay = 150;
alienTimer.setDelay(alienDelay);
b.requestFocusInWindow();
pointsToAdd=100;
}
else if(difficulty == "Medium")
{
alienDelay = 100;
alienTimer.setDelay(alienDelay);
b.requestFocusInWindow();
pointsToAdd=150;
}
else if(difficulty == "Hard")
{
alienDelay = 75;
alienTimer.setDelay(alienDelay);
shotDelay = 2;
shotTimer.setDelay(shotDelay);
b.requestFocusInWindow();
pointsToAdd=200;
}
//set the background:
backgroundImage = "starfield.gif";
//show game status things
b.setVisible(true);
b.requestFocusInWindow();
playerLives.setVisible(true);
score.setVisible(true);
level=1;
b.setText("" + level);
currentScore = 0;
score.setText("" + currentScore);
//reset the alien's position and lives
alienX=10;
alienY=10;
alienLife = "alive";
alienLife1 = "alive";
alienLife2 = "alive";
alienLife3 = "alive";
alienLife4 = "alive";
alienImage = "alien.gif";
alienImage1 = "alien.gif";
alienImage2 = "alien.gif";
alienImage3 = "alien.gif";
alienImage4 = "alien.gif";
shipX = 0;
shipY= 490;
shipsLeft=5;
shotStatus="available";
//reset the ship
shipImage = "ship.gif";
shipLives=3;
playerLives.setText(""+shipLives);
//remove gameOver
statusImage = "blank.gif";
repaint();
//set the button the reset
restart.setText("Restart");
//start the movement
movementsTimer.start();
//set the focus to the right button
alienTimer.start();
difficulties.setVisible(false);
restart.setVisible(false);
//show stats
playerLives.setVisible(true);
livesIcon.setVisible(true);
}
//SHIP MOVEMENTS
if(obj == movementsTimer)
{
//moving left and shooting
if (leftKey == 1 && spaceBar == 1)
{
//move left
if (shipX>=0)
shipX-=10;
repaint();
//shoot
if(shotStatus.equals("available"))
{
shipShot.playSound();
shotY=520;
shotX = shipX+20;
shotTimer.start();
if (shotStatus == "available")
{
currentScore -= 10;
score.setText("" + currentScore);
}
shotStatus="unavailable";
}
}
//moving right and shooting
else if (rightKey == 1 && spaceBar == 1)
{
//move right
if (shipX<=460)
shipX+=10;
repaint();
//shoot
if(shotStatus.equals("available"))
{
shipShot.playSound();
shotY=520;
shotX = shipX+20;
shotTimer.start();
if (shotStatus == "available")
{
currentScore -= 10;
score.setText("" + currentScore);
}
shotStatus="unavailable";
}
}
//left key
else if (leftKey == 1)
{
if (shipX>=0)
shipX-=10;
repaint();
}
//right key
else if (rightKey == 1)
{
if (shipX<=448)
shipX+=10;
repaint();
}
//space bar
else if (spaceBar == 1)
{
if(shotStatus.equals("available"))
{
shipShot.playSound();
shotY=520;
shotX = shipX+20;
shotTimer.start();
if (shotStatus == "available")
{
currentScore -= 10;
score.setText("" + currentScore);
}
shotStatus="unavailable";
}
}
}
//SHIP SHOTS
if(obj == shotTimer)
{
//shot missed
if (shotY<=0)
{
shotY=600;
shotTimer.stop();
if (shipLives > 0)
shotStatus = "available";
}
//Check for hits
else
{
//alien hit
if(shotX>=alienX && shotX <= alienX+40 && shotY == alienY && alienLife.equals("alive"))
{
//kill the alien
alienImage = "blank.gif";
alienLife="dead";
//count the kill
shipsLeft--;
//add the points to the score
currentScore += pointsToAdd;
score.setText("" + currentScore);
//reset the shot
shotY=600;
shotTimer.stop();
shotStatus="available";
alienHit.playSound();
}
//alien1 hit
else if(shotX>=alienX+40 && shotX <= alienX+80 && shotY == alienY && alienLife1.equals("alive"))
{
//kill alien
alienImage1 = "blank.gif";
alienLife1="dead";
shipsLeft--;
//add to score
currentScore += pointsToAdd;
score.setText("" + currentScore);
//reset the shot
shotY=600;
shotTimer.stop();
shotStatus="available";
alienHit.playSound();
}
//alien2 hit
else if(shotX>=alienX+80 && shotX <= alienX+120 && shotY == alienY && alienLife2.equals("alive"))
{
//kill the alien
alienImage2 = "blank.gif";
alienLife2="dead";
shipsLeft--;
//add the score
currentScore += pointsToAdd;
score.setText("" + currentScore);
//reset the shot
shotY=600;
shotTimer.stop();
shotStatus="available";
alienHit.playSound();
}
//alien3 hit
else if(shotX>=alienX+120 && shotX <= alienX+160 && shotY == alienY && alienLife3.equals("alive"))
{
//kill the alien
alienImage3 = "blank.gif";
alienLife3="dead";
shipsLeft--;
//add the score
currentScore += pointsToAdd;
score.setText("" + currentScore);
//reset the shot
shotY=600;
shotTimer.stop();
shotStatus="available";
alienHit.playSound();
}
//alien4 hit
else if(shotX>=alienX+160 && shotX <= alienX+200 && shotY == alienY && alienLife4.equals("alive"))
{
//kill the alien
alienImage4 = "blank.gif";
alienLife4="dead";
shipsLeft--;
//add score
currentScore += pointsToAdd;
score.setText("" + currentScore);
//reset the shot
shotY=600;
shotTimer.stop();
shotStatus="available";
alienHit.playSound();
}
//Advance Level
if(shipsLeft == 0)
{
//add score
currentScore += 200;
score.setText("" + currentScore);
//show level marker
level++;
b.setText("" + level);
//change background color
if(level % 2 == 0) // even background
backgroundImage = "starfield2.gif";
else if(level % 2 == 1)
backgroundImage = "starfield.gif";
setBackground(bgcolor);
//reset the aliens
alienX=10;
alienY=10;
alienLife = "alive";
alienLife1 = "alive";
alienLife2 = "alive";
alienLife3 = "alive";
alienLife4 = "alive";
alienImage = "alien.gif";
alienImage1 = "alien.gif";
alienImage2 = "alien.gif";
alienImage3 = "alien.gif";
alienImage4 = "alien.gif";
shipsLeft=5;
//restart everything
repaint();
restart.setText("Restart");
//reset the alien's timer and placement
alienDelay-=10;
alienTimer.setDelay(alienDelay);
alienTimer.start();
//Let the Player Know they've won!
advanceLevel.playSound();
}
//move the object
shotY-=10;
}
//redraw the window
repaint();
}
//SHIP HIT TIMER
if (obj==shipHitTimer)
{
//shot missed
if (noTimes <= 8)
{
if (noTimes % 2 == 0)
{
shipImage = "ship.gif";
}
if (noTimes % 2 == 1)
{
shipImage = "blank.gif";
}
noTimes ++;
}
else
{
noTimes = 0;
if (shipLives > 0)
shipImage = "ship.gif";
if (shipLives <= 0)
shipImage = "explosion.gif";
shipHitTimer.stop();
}
repaint();
}
//ALIEN'S SHOT
if (obj==alienShotTimer)
{
//shot missed
if (alienShotY>=600)
{
alienShotX=-10;
alienShotY= -20;
alienShotTimer.stop();
alienShotStatus = "available";
}
//Check for hits
else
{
//Ship hit
if(alienShotX >= shipX+10 && alienShotX <= shipX+30 && alienShotY >= shipY && alienShotY <= 520)
{
alienShotStatus = "available";
shipLives--;
playerLives.setText("" + shipLives);
shipHitTimer.start();
alienShotX=-10;
alienShotY= -20;
alienShotTimer.stop();
alienShotStatus = "available";
miniHit.playSound();
}
if (shipLives <= 0)
{
statusImage = "gameOver.gif";
shipImage = "explosion.gif";
explosion.playSound();
shotStatus="unavailable";
movementsTimer.stop();
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
}
}
//move the object
alienShotY+=10;
repaint();
}
//ALIENS
if(obj == alienTimer)
{
//alien's movement lefe and right
//right
if(DirectionMoving.equals("right"))
{
alienX+=10;
if (alienX>=310)
{
DirectionMoving="left";
alienY+=40;
}
//alien shoot at ship
if(alienX == shipX && alienShotStatus == "available"&& alienLife == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 40 == shipX && alienShotStatus == "available"&& alienLife1 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 80 == shipX && alienShotStatus == "available"&& alienLife2 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 120 == shipX && alienShotStatus == "available"&& alienLife3 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 160 == shipX && alienShotStatus == "available"&& alienLife4 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
}
//left
if (DirectionMoving.equals("left"))
{
alienX-=10;
if(alienX<=0)
{
DirectionMoving="right";
alienY+=40;
}
//alien shoot at ship
if(alienX == shipX && alienShotStatus == "available"&& alienLife == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 40 == shipX && alienShotStatus == "available"&& alienLife1 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 80 == shipX && alienShotStatus == "available"&& alienLife2 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 120 == shipX && alienShotStatus == "available"&& alienLife3 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
//alien shoot at ship
if(alienX + 160 == shipX && alienShotStatus == "available"&& alienLife4 == "alive")
{
alienShotStatus="unavailable";
alienShotX=shipX + 20;
alienShotY=alienY;
alienShot.playSound();
alienShotTimer.start();
}
}
//Check for loss from alien
if(alienY >= 490 && alienLife.equals("alive"))
{
alienLife = "dead";
alienImage="blank.gif";
shipImage="explosion.gif";
explosion.playSound();
shotStatus="unavailable";
statusImage = "gameOver.gif";
shipLives = 0;
playerLives.setText("" + 0);
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
movementsTimer.stop();
}
//Check for loss from alien1
if(alienY >= 490 && alienLife1.equals("alive"))
{
alienLife1= "dead";
alienImage1="blank.gif";
shipImage="explosion.gif";
explosion.playSound();
shotStatus="unavailable";
statusImage = "gameOver.gif";
shipLives = 0;
playerLives.setText("" + 0);
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
movementsTimer.stop();
}
//Check for loss from alien2
if(alienY >= 490 && alienLife2.equals("alive"))
{
alienLife2= "dead";
alienImage2="blank.gif";
shipImage="explosion.gif";
explosion.playSound();
shotStatus="unavailable";
statusImage = "gameOver.gif";
shipLives = 0;
playerLives.setText("" + 0);
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
movementsTimer.stop();
}
//Check for loss from alien3
if(alienY >= 490 && alienLife3.equals("alive"))
{
alienLife3= "dead";
alienImage3="blank.gif";
shipImage="explosion.gif";
explosion.playSound();
shotStatus="unavailable";
statusImage = "gameOver.gif";
b.setText("" + level);
shipLives = 0;
playerLives.setText("" + 0);
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
movementsTimer.stop();
}
//Check for loss from alien4
if(alienY >= 490 && alienLife4.equals("alive"))
{
alienLife4= "dead";
alienImage4="blank.gif";
shipImage="explosion.gif";
explosion.playSound();
statusImage = "gameOver.gif";
shotStatus="unavailable";
shipLives = 0;
playerLives.setText("" + 0);
alienTimer.stop();
difficulties.setVisible(true);
restart.setVisible(true);
playerLives.setVisible(false);
livesIcon.setVisible(false);
movementsTimer.stop();
}
//redraw the window
repaint();
}
}
/**********************
* Key Mapping
**********************/
public void keyPressed(KeyEvent e)
{
//turn on the keys pressed
int x = e.getKeyCode();
if (x == e.VK_LEFT)
{
leftKey = 1;
}
if (x == e.VK_RIGHT)
{
rightKey = 1;
}
if (x == e.VK_SPACE)
{
spaceBar = 1;
}
}
public void keyReleased(KeyEvent e)
{
//turn off the keys pressed
int x = e.getKeyCode();
if (x == e.VK_LEFT)
{
leftKey = 0;
}
if (x == e.VK_RIGHT)
{
rightKey = 0;
}
if (x == e.VK_SPACE)
{
spaceBar = 0;
}
}
public void keyTyped(KeyEvent e) { }
}