alyday/fightK

From ggc

Jump to: navigation, search

001 package alyday;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 import java.util.*;
008 
009 /**
010  * All about my game here.
011  @author Alyday  
012  */
013 public class fightK extends GameLoop
014 {
015   /**These set up the ImageSprites*/
016   private ImageSprite kid;
017   private ImageSprite kaufman;
018   private ImageSprite background;
019   private ImageSprite diploma;
020   /**These set up the StringSprites*/
021   private StringSprite win;
022   private StringSprite lose;
023   private StringSprite gpa;
024   private StringSprite life;
025   /**These set up the ints that will be used in adjusting gpa and life*/
026   private int updateGPA;
027   private int lives;
028   private int clickCount;
029   /**These set up the Strings of apples and Fs*/
030   private ImageSprite[] toss;
031   private StringSprite[] fail;
032   /**These set up the tracker*/
033   private ProjectileTracker tracker;
034 
035   /**sets up the game by making the sprites and adding them, as well as setting up the initial values of gpa and life*/
036   public void startGame()
037   {
038     updateGPA=0;
039     lives=3;
040     makeSprites();
041     addSprites();
042     clickCount=0;
043   }
044 
045   /**makes the sprites*/
046   private void makeSprites()
047   {
048     /**this sets the scale, location, and image for the ImageSprite background*/
049     background=new ImageSprite("Ggc.jpg");
050     background.setScale(2.0);
051     background.setLocation(0.50.5);
052 
053     /**this sets the scale, location, and image for the ImageSprite kid, using the people method*/
054     kid=new ImageSprite("Student.jpg");
055     people(kid, 0.120.5);
056 
057     /**this sets the scale, location, and image for the ImageSprite kaufman, using the people method*/
058     kaufman=new ImageSprite ("Kaufman.jpg");
059     people(kaufman, 0.870.5);
060 
061     /**this sets the scale, location, and image for the ImageSprite[] toss, using the throwing method*/
062     toss=new ImageSprite[5];
063     throwing(toss, "Apple.gif");
064 
065     /**this sets the scale, location, and image for the StringSprite[] fail, using the throwing method*/
066     fail=new StringSprite[5];
067     throwing1(fail, "F");
068 
069     /**this sets the scale, location, and image for the StringSprite gpa, using the display method*/
070     gpa=new StringSprite("Current GPA:"+updateGPA);
071     display(gpa, 0.15);
072 
073     /**this sets the scale, location, and image for the StringSprite life, using the display method*/
074     life=new StringSprite("Lives:"+lives+"");
075     display(life, 0.08);
076 
077     /**this sets the scale, location, font, color and image for the StringSprite lose, using the end method*/
078     lose=new StringSprite("YOU LOSE!");
079     lose.setColor(Color.RED);
080     end(lose, "Chiller");
081 
082     /**this sets the scale, location, font, color and image for the StringSprite win, using the end method*/
083     win=new StringSprite("Congratulations!  You can graduate!");
084     win.setColor(Color.WHITE);
085     end(win, "Bauhaus 93");
086 
087     /**this sets the scale, location, and image for the ImageSprite diploma, using the people method*/
088     diploma=new ImageSprite("Diploma.gif");
089     people(diploma, 0.50.3);
090 
091     /** this calls on the method makeTracker, to make the tracker for the game*/
092     makeTracker();
093   }
094   /**This provides the information to make the tracker that moves the kaufman sprite*/
095   private void makeTracker()
096   {
097     tracker=new ProjectileTracker(0.0, random.nextDouble());
098     tracker.setAngularVelocity(0);
099     kaufman.setTracker(tracker);
100   }
101   /**This sets up the method, that when I call on it, it will update lives*/
102   private void refreshLives()
103   {
104     life.setText("Lives: "+lives);
105   }
106   /**This sets up the method, that when I call on it, it will update updateGPA*/
107   private void refreshUpdateGPA()
108   {
109     gpa.setText("Current GPA: "+updateGPA);
110   }
111   /**adds the sprites to the screen*/
112   private void addSprites()
113   {
114     canvas.addSprite(background);
115     canvas.addSprite(kid);
116     canvas.addSprite(kaufman);
117     canvas.addSprite(toss);
118     canvas.addSprite(fail);
119     canvas.addSprite(life);
120     canvas.addSprite(gpa);
121   }
122   /**This sets the lives and gpa StringSprites*/
123   private void display(StringSprite word, double b)
124   {
125     word.setScale(0.35);
126     word.setLocation(0.2,b);
127     word.setFontFamilyName("Bauhaus 93");
128     word.setColor(Color.GREEN);
129   }
130   /**This sets the win and lose StringSprites*/
131   private void end(StringSprite word, String font)
132   {
133     word.setScale(0.75);
134     word.setLocation(0.50.5);
135     word.setFontFamilyName(font);
136   }
137   /**This sets all that ImageSprites that use the method people*/
138   private void people(ImageSprite pic, double a, double b)
139   {
140     pic.setScale(0.25);
141     pic.setLocation(a, b);
142   }
143   /**This sets up the array for toss*/
144   private void throwing(ImageSprite[] thrown, String name)
145   {
146     for(int k=0; k<thrown.length; k++)
147     {
148       thrown[k]=new ImageSprite(name);
149       thrown[k].setScale(0.1);
150       thrown[k].setVisible(false);
151       thrown[k].setColor(Color.RED);
152     }
153   }
154   /**This sets up the array for fail*/
155   private void throwing1(StringSprite[] thrown, String name)
156   {
157     for(int k=0; k<thrown.length; k++)
158     {
159       thrown[k]=new StringSprite(name);
160       thrown[k].setScale(0.1);
161       thrown[k].setVisible(false);
162       thrown[k].setColor(Color.RED);
163     }
164   }
165   /**This method makes fail and toss go towards their targets*/
166   private void launchBullet(Sprite[] thrown, Sprite name, Sprite pic1)
167   {
168     reclaimUsedBullets(thrown);
169     for(int k=0; k<thrown.length; k++)
170     {
171       if(thrown[k].isVisible()==false)
172       {
173         Point2D.Double pic1Location=pic1.getLocation();
174         Point2D.Double nameLocation=name.getLocation();
175         double angle=Math.atan2(pic1Location.y-nameLocation.y, pic1Location.x-nameLocation.x);
176         ProjectileTracker tracker=new ProjectileTracker(00.1);
177         tracker.setVelocityDirection(angle);
178         thrown[k].setLocation(name.getLocation());
179         thrown[k].setTracker(tracker);
180         thrown[k].setVisible(true);
181         return;
182       }
183     }
184   }
185   /**This method reclaims toss and fail once they are no longer visible*/
186   private void reclaimUsedBullets(Sprite[] thrown)
187   {
188     for(int k=0; k<thrown.length; k++)
189     {
190       if(isOffScreen(thrown[k]))
191       {
192         thrown[k].setVisible(false);
193       }
194     }
195   }
196   /**This method helps determine if toss or fail is off the screen or not*/
197   private boolean isOffScreen(Sprite sprite)
198   {
199     if(sprite.getLocation().x-sprite.getBounds2D().getWidth()/2>||
200             sprite.getLocation().y-sprite.getBounds2D().getHeight()/2>||
201             sprite.getLocation().x+sprite.getBounds2D().getWidth()/2<||
202             sprite.getLocation().y+sprite.getBounds2D().getHeight()/2<0)
203     {
204       return true;
205     }
206     return false;
207   }
208   /**This method tells determines when to start the method launchBullet*/
209   private void handleKK(Sprite[] thrown, Sprite pic1, Sprite name)
210   {
211     Point2D.Double position=getPlayer().getMouse().getLocation();
212     pic1.setLocation(pic1.getLocation().x, position.y);
213     if(getPlayer().getMouse().getClickLocation()!=null)
214     {
215       launchBullet(thrown, pic1, name);
216     }
217   }
218   /**This method determines what happens when kid intersects fail*/
219   public void kidIntersection()
220   {
221     for(int k=0; k<fail.length; k++)
222     {
223       if(kid.intersects(fail[k])&& fail[k].isVisible())
224       {
225         fail[k].setVisible(false);
226         updateGPA--;
227         refreshUpdateGPA();
228         lives--;
229         refreshLives();
230       }
231     }
232   }
233   /**This method determines what happens when kaufman intersects toss*/
234   public void kaufmanIntersection()
235   {
236     for(int k=0; k<toss.length; k++)
237     {
238       if(kaufman.intersects(toss[k])&& toss[k].isVisible())
239       {
240         toss[k].setVisible(false);
241         updateGPA++;
242         refreshUpdateGPA();
243       }
244     }
245   }
246   /**This method determines what happens when lives gets to or below 0*/
247   public void affectLives()
248   {
249     if(lives<=0)
250     {
251       canvas.addSprite(lose);
252     }
253 
254   }
255   /**This method determines what happens when gpa gets to or above 4*/
256   public void affectGPA()
257   {
258     if(updateGPA>=4)
259     {
260       canvas.addSprite(diploma);
261       canvas.addSprite(win);
262     }
263 
264   }
265   /**This method determines what happens when kaufman gets below 0.0 or above 1.0*/
266   public void kaufmanLocation()
267   {
268     if(kaufman.getLocation().y<=0.0)
269     {
270       kaufman.setLocation(kaufman.getLocation().x, 1.0);
271     }
272     if(kaufman.getLocation().y>=1.0)
273     {
274       kaufman.setLocation(kaufman.getLocation().x, 0.0);
275     }
276   }
277   /**This method determines kaufmans velocity and what happens if the velocity is too slow or too fast*/
278   public void kaufmanDirection()
279   {
280     Point2D.Double direction=tracker.getVelocity();
281     direction.y+=random.nextDouble()*0.18-0.09;
282     if(direction.y<-0.5)
283     {
284       direction.y=-0.5;
285     }
286     if(direction.y>0.5)
287     {
288       direction.y=0.5;
289     }
290 
291     tracker.setVelocity(direction);
292   }
293   /**This method determines what happens when the game is started*/
294   public void stepOne()
295   {
296     if(updateGPA<&& lives>0)
297     {
298       handleKK(toss, kid, kaufman);
299       if(getPlayer().getMouse().getClickLocation()!=null)
300       {
301         launchBullet(fail, kaufman, kid);
302       }
303 
304       kaufmanDirection();
305       kaufmanLocation();
306       kidIntersection();
307       kaufmanIntersection();
308       affectLives();
309       affectGPA();
310     }
311   }
312   /**handle input and game events*/
313   public void advanceFrame(double timePassed)
314   {
315     stepOne();
316   }
317 }


Download/View alyday/fightK.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter