Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  I need some help, making a tower defense and unsure of what to do next  (Read 322 times)
0 Members and 2 Guests are viewing this topic.
Offline QuadraBox

JGO n00b
*

Posts: 1



« on: 2012-01-15 15:15:54 »

Ok so, I am making a tower defense game (watched a tutorial on youtube to get to this point and now I'm completely stumped)

The towers, lives, minions are all working, but I don't know how I can make the towers "attack" the minions and make the minions lose hp, does anyone know how to do this? here's some of my code so far

this is for importing the images

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
                                                                     
                                                                     
                                                                     
                                             
//Importing Images Class
package LeagueTD;

import java.awt.*;
import javax.swing.*;

public class ac extends JPanel {

   public Image back;
   public Image[] ene = new Image[200];//monster
  public Image[] style = new Image[200];
   public Image[] tower = new Image[200];
   public Image defeat;

   public boolean imagesLoaded=false;

   public ac(){
      this.setBackground(Color.BLACK);
      getImages();
   }

   public void getImages(){
      back=new ImageIcon("back.png").getImage();

      ene[0]=new ImageIcon("pinkminion.png").getImage();

      style[0]=new ImageIcon("health.png").getImage();
      style[1]=new ImageIcon("gold.png").getImage();
      style[2]=new ImageIcon("fade.png").getImage();

      tower[0]=new ImageIcon("ashe.png").getImage();

      defeat=new ImageIcon("defeat.png").getImage();


      imagesLoaded=true;
   }

   public void paintComponent(Graphics g){
      if (imagesLoaded){
         g.drawImage(back, 0, 0, aa.width-6, aa.height-28, null);
      }
   }
}


and this is for the game itself

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
                                                                     
                                                                     
                                                                     
                                             
package LeagueTD;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.*;

public class ad extends JPanel {

   public int spawnRate=200;
   public int maximumAmountOfMobs=200;
   public int[] mobKind;
   public int mobMovementSpeed=10;
   public int mobPinkMinion=0;
   public int mobSize=40;
   public int health=30;
   public int money=500;
   public int shopSize=8;
   public int sizeBetween=10;
   public int fade=shopSize+1;
   public int heldTower=0;
   public int towerID[];
   public int air=99;

   public Rectangle[] mobs;
   public Rectangle[] tower;
   public Rectangle[] shop = new Rectangle[shopSize];
   public Rectangle end;
   //public Line2D shot;
  public Point mse;

   public Timer spawn;
   public Timer mobMovement;

   public boolean gameOver = false;
   public boolean inGame = true;
   public boolean holdingTower = false;

   ClassLoader ldr=this.getClass().getClassLoader();
   java.applet.AudioClip audio=JApplet.newAudioClip(ldr.getResource("Explosion.wav"));

   public void defineObjects(){
      mobKind=new int[maximumAmountOfMobs];
      mobs=new Rectangle[maximumAmountOfMobs];
      for (int i=0;i<mobs.length;i++){
         mobs[i]=new Rectangle(-5000, -5000, 0, 0);
         mobKind[i]=0;
      }

      for(int i=0;i<shop.length;i++){
         shop[i] = new Rectangle((130)+i*72+(sizeBetween*i),12,60,80);
      }

      end=new Rectangle(680, aa.height/2, 100,100);

      tower = new Rectangle[200];
      towerID = new int[200];
      for(int i=0;i<tower.length;i++){
         tower[i] = new Rectangle(-5000, -5000, 0, 0);
         towerID[i]=air;
      }
   }

   public ad(ab f,ac bp){
      defineObjects();

      f.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent e){
            if(e.getButton()==MouseEvent.BUTTON1){
               if(!holdingTower){
                  if(shop[0].contains(mse)){
                     heldTower=mobPinkMinion;
                     holdingTower=true;
                  }
               }
               else{
                  for(int i=0;i<tower.length;i++){
                     if(towerID[i]==air){
                        tower[i]=new Rectangle(mse.x,mse.y, 90,90);
                        towerID[i]=heldTower;
                        i+=tower.length+1;

                     }
                  }
               }
            }
         }
      });

      //Fade
     f.addMouseMotionListener(new MouseMotionAdapter(){

         public void mouseMoved (MouseEvent e){
            mse=new Point(e.getX(), e.getY()-23);
            boolean nope =true;

            for(int i=0;i<shop.length;i++){
               if (shop[i].contains(mse)){
                  fade=i;
                  nope=false;
               }
            }
            if (nope){
               fade=shopSize+1;
            }
            repaint();
         }
      });

      ActionListener spawnListener = new ActionListener(){
         public void actionPerformed(ActionEvent e){
            if (inGame=true){
               for(int i=0;i<maximumAmountOfMobs;i++){
                  if (mobs[i].x <=0){
                     mobs[i]=new Rectangle(aa.width-750,aa.height/2-mobSize/2+55,mobSize,mobSize);
                     i += maximumAmountOfMobs+1;
                  }
               }
            }
            repaint();
         }
      };

      spawn=new Timer(spawnRate, spawnListener);
      spawn.start();

      ActionListener mobMovementListener = new ActionListener(){
         public void actionPerformed(ActionEvent e){
            if(inGame){
               for(int i=0;i<maximumAmountOfMobs;i++){
                  if (end.contains(mobs[i])){
                     health-=1;
                     mobs[i]=new Rectangle(-5000, -5000, 0 ,0);
                     //audio.play();
                    checkforGameOver();
                     if(gameOver){
                        //audio.stop();
                    }

                  }
                  else{
                     mobs[i].x += 1;

                  }
               }
            }
            repaint();
         }
      };
      mobMovement=new Timer(mobMovementSpeed, mobMovementListener);
      mobMovement.start();
   }

   public void checkforGameOver(){
      if (health==0){
         gameOver=true;
         inGame=false;
      }
   }

   public void paintComponent (Graphics g){
      super.paintComponent(g);


      if (aa.f.bp.imagesLoaded){
         for (int i=0;i<mobs.length;i++){
            g.drawImage(aa.f.bp.ene[mobKind[i]], mobs[i].x,mobs[i].y,mobs[i].width,mobs[i].height,null);
         }
      }

      //Panel
     g.setColor(Color.DARK_GRAY);
      g.fill3DRect(0,0,aa.width,105,true);

      //Health
     g.setFont(new Font("Arial",Font.BOLD,20));
      g.setColor(Color.WHITE);
      g.drawImage(aa.f.bp.style[0],15,15,40,40,null);
      g.drawString(health + "",66,42);

      //Money
     g.drawImage(aa.f.bp.style[1],15+2,15+48,35,35,null);
      g.drawString(money + "",66,42+43);

      //Towers
     for(int i=0;i<tower.length;i++){
         if(towerID[i] != air){
            g.drawImage(aa.f.bp.tower[towerID[i]], tower[i].x-45, tower[i].y-40,tower[i].width, tower[i].height, null);
         }
      }

      //Hand Held
     if(holdingTower){
         g.drawImage(aa.f.bp.tower[heldTower],mse.x-45,mse.y-40,90,90,null);
      }

      //Panel Component
     for (int i =0; i< shop.length;i++){
         g.setColor(Color.GRAY);
         g.fillRect(shop[i].x,shop[i].y,shop[i].width,shop[i].height);
         g.setColor(Color.WHITE);
         g.drawRect(shop[i].x-2,shop[i].y-2,shop[i].width+2,shop[i].height+2);

         if(i==fade){
            g.drawImage(aa.f.bp.style[2],shop[i].x,shop[i].y,shop[i].width,shop[i].height,null);
         }
      }
      g.drawImage(aa.f.bp.tower[0],15+105,12,80,80,null);



      if (gameOver){
         //Defeat def = new Defeat();
        g.drawImage(aa.f.bp.defeat,0,0,800,600,null);
      }
   }
}
Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3574
Medals: 44


Game Engineer


« Reply #1 on: 2012-01-15 15:26:48 »

First thing's first - read up on object-oriented programming, because your code is missing it. To make your game OO, you would have a Tower object, an Enemy object, etc. They probably are both subclasses of an Entity object. The object itself contains the hit points, the damage, the range, etc. Then instead of storing a bunch of Image's as you are instead the Entity has a reference to its own Image, and you store the Entity instead.

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #2 on: 2012-01-16 02:09:35 »

1  
 public ad(ab f,ac bp){

1  
aa.f.bp.ene

Are you serious? Why are you obfuscating your sourcecode??

If ever, you need to obfuscate your JAR, or more specifically, the bytecode it contains, using a tool like proguard, not manually.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #3 on: 2012-01-16 03:36:55 »

ROFLMAO

Offline ReBirth

JGO Wizard
****

Posts: 1275
Medals: 19



« Reply #4 on: 2012-01-16 04:40:04 »

Oh what a variable name Grin

IMO, each enemy should let the world (or particular tower) know their position so tower can decide to attack whom. There're some way to do this. If you want harder way, ask princec Grin

Offline h3ckboy

JGO Kernel
*****

Posts: 1645
Medals: 4



« Reply #5 on: 2012-01-16 13:43:22 »

hey, sorry for not replying to your question earlier, but Eli has really sound advice. If it makes you feel any better, I was once at your end of the posts with people telling me to go back to the books, and yeah I know its a bit offensive, but trust me it works miracles!
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.077 seconds with 19 queries.