import java.awt.*;
import java.awt.event.ActionEvent;
public class MouseTrapGame extends java.awt.Frame
implements java.awt.event.ActionListener {
private PFigureList listOfFigures = new PFigureList();
private Mouse mouse;
private MouseTrap mouseTrap;
private Broom broom;
private Cheese[] cheeses = new Cheese[5];
private Dialog winMsg = new Dialog(this);
private Dialog loseMsg = new Dialog(this);
private int cheeseLeft = 5;
private javax.swing.Timer moveTimer = new javax.swing.Timer(200, this);
/**
* Creates new form MouseTrapGame
*/
public MouseTrapGame()
{
initComponents();
setSize(1000, 800);
setLocationRelativeTo(null);
spawnCheese();
spawnTraps();
spawnBrooms();
mouse = new Mouse(gamePanel);
gamePanel.requestFocus();
moveTimer.start();
}
private void displayWin()
{
winMsg.add(new Label("Congratulations! You won!"));
winMsg.setSize(200, 100);
winMsg.setLocationRelativeTo(null);
winMsg.setVisible(true);
}
private void displayLose()
{
loseMsg.add(new Label("You lose..."));
loseMsg.setSize(150, 100);
loseMsg.setLocationRelativeTo(null);
loseMsg.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if (listOfFigures.checkCollision(mouse))
{
moveTimer.stop();
displayLose();
}
if (cheeseLeft == 0)
{
moveTimer.stop();
displayWin();
}
mouse.draw();
for (int i = 0; i < 5; i++)
cheeses[i].draw();
listOfFigures.hideAll();
listOfFigures.moveAll();
listOfFigures.drawAll();
cheeseLeftField.setText(String.valueOf(cheeseLeft));
}
private void spawnTraps()
{
int startX;
int startY;
for (int i = 0; i < 4; i++)
{
startX = 200 + (int) (Math.random() * 700);
startY = 50 + (int) (Math.random() * 650);
mouseTrap = new MouseTrap(startX, startY, gamePanel);
listOfFigures.add(mouseTrap);
}
}
private void spawnBrooms()
{
int startX;
int startY;
for (int i = 0; i < 4; i++)
{
startX = 200 + (int) (Math.random() * 700);
startY = 50 + (int) (Math.random() * 650);
broom = new Broom(startX, startY, gamePanel);
listOfFigures.add(broom);
}
}
private void spawnCheese()
{
int startX;
int startY;
for (int i = 0; i < 5; i++)
{
startX = 20 + (int) (Math.random() * 750);
startY = 20 + (int) (Math.random() * 600);
cheeses[i] = new Cheese(startX, startY, gamePanel);
System.out.println(startX);
System.out.println(startY);
}
}
/**
* This method is called from within the constructor to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
gamePanel = new java.awt.Panel();
cheeseLeftLabel = new java.awt.Label();
cheeseLeftField = new java.awt.TextField();
setMinimumSize(new java.awt.Dimension(1000, 800));
setTitle("Mouse Trap Game 243");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
setLayout(null);
gamePanel.setBackground(new java.awt.Color(0, 153, 153));
gamePanel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
mouseMover(evt);
}
});
add(gamePanel);
gamePanel.setBounds(0, 0, 1000, 750);
cheeseLeftLabel.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
cheeseLeftLabel.setText("Cheese Left:");
add(cheeseLeftLabel);
cheeseLeftLabel.setBounds(375, 750, 120, 30);
cheeseLeftField.setName(""); // NOI18N
add(cheeseLeftField);
cheeseLeftField.setBounds(510, 760, 70, 20);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* Exit the Application
*/
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
private void mouseMover(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mouseMover
mouse.hide();
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_DOWN)
mouse.move(0, 10);
else if ( evt.getKeyCode() == java.awt.event.KeyEvent.VK_UP )
mouse.move(0, -10);
else if ( evt.getKeyCode() == java.awt.event.KeyEvent.VK_LEFT )
mouse.move(-10, 0);
else if ( evt.getKeyCode() == java.awt.event.KeyEvent.VK_RIGHT )
mouse.move(10, 0);
mouse.draw();
for (int i = 0; i < cheeseLeft; i++)
if (mouse.collidedWith(cheeses[i]))
{
cheeses[i].hide();
cheeseLeft--;
}
}//GEN-LAST:event_mouseMover
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MouseTrapGame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.TextField cheeseLeftField;
private java.awt.Label cheeseLeftLabel;
private java.awt.Panel gamePanel;
// End of variables declaration//GEN-END:variables
}