Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I need some help, making a tower defense and unsure of what to do next  (Read 629 times)
0 Members and 1 Guest are viewing this topic.
Offline QuadraBox

Innocent Bystander





« Posted 2012-01-15 20: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2012-01-15 20: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-01-16 07: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-16 08:36:55 »

ROFLMAO

Offline ReBirth
« Reply #4 - Posted 2012-01-16 09: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 Coder


Medals: 5



« Reply #5 - Posted 2012-01-16 18: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!