Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Entity.java:
- package monster.defense;
- import java.awt.*;
- public abstract class Entity {
- protected int x, y, w, h;
- protected boolean removed = false;
- public Entity(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public void draw(Graphics g)
- {
- }
- public int getX() {return x;}
- public int getY() {return y;}
- public int getW() {return w;}
- public int getH() {return h;}
- }
- game.java:
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package monster.defense;
- import java.awt.Graphics;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class game extends javax.swing.JPanel implements KeyListener{
- private int x = 20;
- private int y = 20;
- //private player instance;
- private int cnt = 0;
- private player[] enemy;
- private player[] towers;
- private Rectangle[] boxes;
- private int baseH=40;
- public game() {
- // Player1 = new player(100,100,16,16);
- setFocusable(true);
- addKeyListener(this);
- addMouseListener(new MouseAdapter(){
- @Override
- public void mousePressed(MouseEvent m)
- {
- if(cnt==0)
- {
- setArray();
- }
- int mx=m.getX();
- int my=m.getY();
- towers[cnt].setX(mx);
- towers[cnt].setY(my);
- boxes[cnt].x=(mx);
- boxes[cnt].y=(my);
- cnt++;
- repaint();
- }
- });
- setEnArray();
- //for(int i=0; i<enemy.length;i++)
- //{
- //enemy[i] = new player(this, 0,200);
- //}
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- setBackground(new java.awt.Color(200, 22, 0));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 907, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 521, Short.MAX_VALUE)
- );
- }// </editor-fold>//GEN-END:initComponents
- public int getEnemyNum()
- {
- return enemy.length;
- }
- public void setEnArray()
- {
- this.enemy = new player[20];
- for(int i=0;i<enemy.length;i++)
- {
- enemy[i] = new player(this,0,200);
- }
- }
- public void setArray()
- {
- //Use the global variable and don't create a local one
- this.towers = new player[30];
- this.boxes = new Rectangle[30];
- for(int i = 0; i < 30; i++)
- {
- towers[i] = new player(this, 5,5);
- boxes[i] = new Rectangle(towers[i].x,towers[i].y,100,100);
- }
- }
- public Rectangle getBox(int val)
- {
- return boxes[val];
- }
- public int getCnt()
- {
- return cnt;
- }
- public void paint(Graphics g)
- {
- g.setColor(Color.gray);
- g.fillRect(0, 0, getWidth(), getHeight()); //BACKGROUND
- g.setColor(Color.WHITE);
- g.fillRect(0,200, getWidth(), 20);
- Color myColour = new Color(80, 80, 255, 127);
- g.setColor(myColour);
- for(int i=0; i<cnt;i++)
- {
- boxes[i].setBounds(towers[i].x-40, towers[i].y-40, 100, 100); //SETS THE BOX AROUND THE TOWER
- g.fillRect(boxes[i].x,boxes[i].y,boxes[i].width,boxes[i].height); //DRAWS THE TOWER HIT BOX
- }
- for(int i = 0; i < cnt; i++) //Draws the towers
- {
- towers[i].draw(g);
- }
- for(int i=0;i<enemy.length;i++)
- {
- if(enemy[i].x==900&&!(enemy[i].getHealth()<=0))
- {
- baseH--;
- }
- System.out.println(baseH);
- g.setFont(new Font("ArialBlack", Font.BOLD, 30));
- g.drawString(""+baseH, 800, 100);
- }
- try {
- for(int i=0;i<enemy.length-1;i++)
- {
- enemy[i].drawEnemy(g);
- enemy[i+1].setX(enemy[i].x-50);
- }
- Thread.sleep(8);
- } catch (InterruptedException ex) {
- Logger.getLogger(game.class.getName()).log(Level.SEVERE, null, ex);
- }
- int ok = 1;//if(instance.getBox().x < box.x + box.width && instance.getBox().x + instance.getBox().width > box.x && instance.getBox().y < box.y + box.height && y + instance.getBox().height > box.y)
- for(int i=0;i<enemy.length; i++)
- {
- if(cnt>0)
- {
- //for(int j=0; j<boxes.length;j++)
- //{
- if(enemy[i].getEBox().intersects(boxes[i])) //INTERSECTION CHECK
- {
- g.setColor(Color.yellow);
- enemy[i].setHealth(enemy[i].getHealth()-1);
- ok=0;
- // System.out.println(enemy[i].getHealth());
- }
- else{}
- //}
- }
- if(ok==1)
- {
- g.setColor(Color.GREEN);
- }
- else{
- Color dead = new Color(0,0,0,255);
- g.setColor(dead);
- g.fillOval(x,y,16,16);
- this.x++;
- }
- g.fillOval(x,y,16,16);
- Color msColour = new Color(80, 80, 255, 127);
- g.setColor(msColour);
- }
- repaint();
- }
- @Override
- public void keyTyped(KeyEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyPressed(KeyEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyReleased(KeyEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- // End of variables declaration//GEN-END:variables
- Menu.java:
- package monster.defense;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Menu extends javax.swing.JFrame {
- public Menu() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- jLabel2 = new javax.swing.JLabel();
- jButton2 = new javax.swing.JButton();
- playername = new javax.swing.JTextField();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- jLabel1.setText("Enter your name:");
- jButton1.setFont(new java.awt.Font("Minion Pro Cond", 0, 48)); // NOI18N
- jButton1.setText("PLAY!");
- jButton1.setToolTipText("");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jLabel2.setFont(new java.awt.Font("Forte", 0, 36)); // NOI18N
- jLabel2.setText("Monster Defense");
- jButton2.setFont(new java.awt.Font("Arial Black", 0, 13)); // NOI18N
- jButton2.setText("EXIT GAME");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jButton2)
- .addGap(142, 142, 142))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(102, 102, 102))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(playername, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGap(62, 62, 62)
- .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(playername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(jButton2)
- .addGap(16, 16, 16))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- String kay;
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
- System.exit(0);
- }//GEN-LAST:event_jButton2ActionPerformed
- String clicked = "0";
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
- String name = playername.getText();
- // System.out.println(playername.getText());
- kay = "yumyum";
- try {
- this.WriteToText();
- } catch (Exception ex) {
- Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
- }
- }//GEN-LAST:event_jButton1ActionPerformed
- public String getPName()
- {
- String name = playername.getText();
- return name;
- }
- public void WriteToText() throws Exception
- {
- //BufferedWriter outF = new BufferedWriter(new FileWriter("playerdetails.txt"));
- //outF.newLine();
- //outF.write(playername.getText());
- FileWriter fw = new FileWriter("playerdetails.txt");
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(playername.getText());
- clicked = "1";
- this.opened = true;
- this.setVisible(false);
- }
- public String getClick()
- {
- return clicked;
- }
- boolean opened = false;
- public void setOpen(boolean open){
- this.opened = open;
- }
- public Boolean isOpened(){
- return opened;
- }
- public void stop()
- {
- System.exit(0);
- }
- public void run()
- {
- new Menu().setVisible(true);
- }
- public static void main(String args[]) {
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Menu().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- public javax.swing.JTextField playername;
- // End of variables declaration//GEN-END:variables
- }
- Menu.java:
- package monster.defense;
- import java.io.*;
- import javax.swing.*;
- import java.awt.*;
- public class MonsterDefense{
- public static void main(String args[]) throws Exception {
- Menu hm = new Menu();
- hm.setTitle("Monster Defense");
- hm.setLocationRelativeTo(null);
- hm.setVisible(true);
- // BufferedReader pdetails = new BufferedReader(new FileReader("playerdetails.txt"));
- // BufferedReader inKb = new BufferedReader(new InputStreamReader (System.in));
- int yum = 1;
- while(yum == 1)
- {
- if(hm.isVisible())
- {
- System.out.print("");
- }
- else if(!hm.isVisible())
- {
- System.out.println("Button clicked. Proceeding with program.");
- yum = 2;
- }
- }
- System.out.println("Game will launch now. Player name: "+hm.getPName());
- game md = new game();
- JFrame frame = new JFrame();
- frame.setTitle("Monster Defence");
- frame.add(md);
- frame.pack();
- frame.setVisible(true);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
- player.java:
- package monster.defense;
- import java.awt.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Morgan
- */
- public class player extends Entity
- {
- private game instance;
- private int xdir;
- private int ydir;
- private int health = 115;
- private boolean passed = false;
- private Rectangle box; //THIS IS THE ENEMY HITBOX
- public player(game instance, int x, int y) {
- super(x, y);
- this.instance=instance;
- w = 16; h=16;
- box = new Rectangle(x,y,w,h);
- }
- public int getHealth()
- {
- return this.health;
- }
- public void setHealth(int value)
- {
- if(value <=0)
- {
- health = 0;
- }
- else{health=value;}
- }
- public Rectangle getEBox()
- {
- return this.box;
- }
- public void draw(Graphics g)
- {
- move();
- g.setColor(Color.GREEN); //THIS IS THE ENEMY HITBOX
- //box.x=this.x-48; //THESE MAKE THE HITBOX FOLLOW THE TOWER
- //box.y=this.y-48;
- // g.fillRect(box.x, box.y, w+20, h+20);
- g.setColor(Color.RED);
- g.fillOval(x,y,w,h);
- }
- public boolean getPassed()
- {
- return passed;
- }
- public void drawEnemy(Graphics g) throws InterruptedException
- {
- if(health>0){
- g.setColor(Color.GREEN); //THIS IS THE ENEMY HITBOX
- box.x=this.x+5; //THESE MAKE THE HITBOX FOLLOW THE TOWER
- box.y=this.y+5;//-48;
- //g.setColor(Color.CYAN);
- g.fillRect(box.x, box.y, 5, 5);
- if(this.x<900)
- {
- this.x++;
- box.x++;
- }
- else if(x==900)
- {
- this.x++;
- box.x++;
- }
- else{
- this.x++;
- box.x++;
- }
- //if(!instance.getBox().intersects(box)) //INTERSECTION TEST TESTING IF THE ENEMY IS WITHIN RANGE
- int ok = 1;//if(instance.getBox().x < box.x + box.width && instance.getBox().x + instance.getBox().width > box.x && instance.getBox().y < box.y + box.height && y + instance.getBox().height > box.y)
- for(int i=0;i<instance.getCnt(); i++)
- {
- if(box.intersects(instance.getBox(i)))
- {
- g.setColor(Color.yellow);
- health--;
- ok=0;
- System.out.println(health);
- }
- }
- if(ok==1)
- {
- g.setColor(Color.GREEN);
- }
- g.fillOval(x,y,w,h);
- Color myColour = new Color(80, 80, 255, 127);
- g.setColor(myColour);
- }
- else{
- Color dead = new Color(0,0,0,255);
- g.setColor(dead);
- g.fillOval(x,y,w,h);
- this.x++;
- }
- }
- private void move()
- {
- if (this.getX()>=901&&this.getXDir()==1||this.getX()<=0&&this.getXDir()==-1)
- {
- this.setXDir(0);
- }
- if (this.getY()>=510&&this.getYDir()==1||this.getY()<=0&&this.getYDir()==-1)
- {
- this.setYDir(0);
- }
- x+=xdir;
- y+=ydir;
- }
- public void setXDir(int value)
- {
- xdir = value;
- }
- public void setYDir(int value)
- {
- ydir = value;
- }
- public int getXDir()
- {
- return xdir;
- }
- public int getYDir()
- {
- return ydir;
- }
- public void setX(int value)
- {
- x=value;
- }
- public void setY(int value)
- {
- y=value;
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- }
- SAVE THEM ALL IN A FOLDER TOGETHER TO RUN :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement