Rohrer/Final Project

From ggc

Jump to: navigation, search

001 package Rohrer;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * the name of my game is Herding Cats
009  * the object of my game is to use your cursor to scare the cats in the correct direction to get them into the fence in the middle of the screen
010  @author Kevin Rohrer
011  */
012 
013 public class Final_Project extends Game
014 {
015   private OutlineSprite screenOutline;
016   private Sprite[] cats = new Sprite[9];
017   private Sprite[] fence = new Sprite[4];
018   private ProjectileTransformer catsWander;
019   private int time = 60;
020   private StringSprite timeDisplay = new StringSprite(":"+time);
021   private StringSprite lose = new StringSprite("You Lose");
022   private StringSprite win = new StringSprite("YOU WIN!!!");
023 
024   /**runs the methods below to make or begin vareous aspects of the game*/
025   public void setup()
026   {
027     addFence();
028     screenOutline();
029     makeCats();
030     startWander();
031     setTimer();
032     helpScreen();
033   }
034   /**
035    * runs the boundarys method to keep the cats on the screen
036    * runs the push away method to push the cats away from cursor
037    * runs the winLose method when time hits 0
038    */
039   public void advance()
040   {
041     boundarys();
042     pushAway();
043     if(time == 0)
044     {
045       winLose();
046     }
047   }
048   /** finds ut if the player wins or looses and displays the apropreate message*/
049   private void winLose()
050   {
051     int catsOut = 0;
052     for(int i=0; i<cats.length; i++)
053     {
054       double = cats[i].getX();
055       double = cats[i].getY();
056       if((x<0.7 && x>0.3&& (y<0.7 && y>0.3))
057       {
058         catsOut++;
059       }
060     }
061     fence[1].setColor(getColor("Saddle Brown"));
062     if(catsOut < 9// lose
063     {
064       lose.setLocation(.5.3);
065       lose.setWidth(.7);
066       lose.setColor(getColor("White"));
067       addSprite(lose);
068     }
069     if(catsOut == 9// win
070     {
071       win.setLocation(.5.3);
072       win.setWidth(.7);
073       win.setColor(getColor("White"));
074       addSprite(win);
075     }
076   }
077   /** adds the countdown clock string sprite and schedules the timed action to count down the "time" variable*/
078   private void setTimer()
079   {
080     timeDisplay.setWidth(.1);
081     timeDisplay.leftJustify();
082     timeDisplay.setLocation(.008.05);
083     timeDisplay.setColor(getColor("White"));
084     addSprite(timeDisplay);
085 
086     schedule(new countDown()1);
087   }
088   /** counts down "time" every second and sets the text of the string sprite to show the countdown clock*/
089   class countDown extends TimedAction
090   {
091     public void act()
092     {
093       time--;
094       timeDisplay.setText(":"+time);
095       if(time >0)
096         schedule(this,1);
097     }
098   }
099   /** finds out if the cats are close to cursor and returns true if they are in correct distance and false if they are not*/
100   private boolean outlinePush(int i, Location2D mousePosition, Location2D catPosition)
101   {
102     if((mousePosition.distance(catPosition.1 && i<3||
103             (mousePosition.distance(catPosition.15 && i>&& i<6||
104             (mousePosition.distance(catPosition.2 && i>5))
105     {
106       return true;
107     }
108     else
109     {
110       return false;
111     }
112   }
113   /** checks to see if a cat is in contact with any of the fences and returns true if there are collisions and the fence is brown and false if there is no collisions or the fence is green*/
114   private boolean fencePush(int i)
115   {
116     if(cats[i].intersects(fence[0]== true && getColorName(fence[0].getColor())=="Saddle Brown" ||
117             cats[i].intersects(fence[1]== true && getColorName(fence[1].getColor())=="Saddle Brown" ||
118             cats[i].intersects(fence[2]== true && getColorName(fence[2].getColor())=="Saddle Brown" ||
119             cats[i].intersects(fence[3]== true && getColorName(fence[3].getColor())=="Saddle Brown")
120     {
121       return true;
122     }
123     else
124     {
125       return false;
126     }
127   }
128   /** changes transformer so that the cats repel from the cursor when it gets close to the cats this also speeds up the cats when the cursor repels them*/
129   private void pushAway()   //  to solve problem of pushing them out of screen make so that it only pushes away if they are not in colision of outline
130   {
131     for(int i=0; i<cats.length; i++)
132     {
133       for(int j=0; j<fence.length; j++)
134       {
135         Location2D mousePosition = getMouse2D();
136         Location2D catPosition = cats[i].getLocation();
137         catsWander = (ProjectileTransformer)cats[i].getTransformer();
138         Vector2D toCat = new Vector2D(getMouse2D(), cats[i].getLocation());
139         Vector2D originalDirection = catsWander.getVector2D();
140 
141         if(outlinePush(i, mousePosition, catPosition== true && cats[i].intersects(screenOutline== false && fencePush(i== false)
142         {
143           originalDirection.setDirectionDegrees(toCat.getDirectionDegrees() + (100.0*Math.random()-50.0));
144           originalDirection.setSpeed(.2);
145           catsWander.setVector2D(originalDirection);
146         }
147         else
148         {
149           originalDirection.setSpeed(.05);
150         }
151       }
152     }
153   }
154   /** adds transformer to cat and schedules the timed action that keeps the cat wandering */
155   private void startWander()
156   {
157     for(int i=0; i<cats.length; i++)
158     {
159       catsWander = new ProjectileTransformer((20.0*Math.random()-10.0)/100.0(20.0*Math.random()-10.0)/100.0);
160       cats[i].addTransformer(catsWander);
161     }
162     schedule(new changeDirection().1);
163   }
164   /** changes the direction of the cats slightly multiple times a second to give the image that the cat is wandering */
165   class changeDirection extends TimedAction
166   {
167     public void act()
168     {
169       for(int i=0; i<cats.length; i++)
170       {
171         catsWander = (ProjectileTransformer)cats[i].getTransformer();
172         Vector2D direction = catsWander.getVector2D();
173         direction.turn((50.0*Math.random()-25.0));
174         direction.setSpeed(.05);
175         catsWander.setVector2D(direction);
176       }
177       schedule(this,.1);
178     }
179   }
180   /**creates and adds the screen outline */
181   private void screenOutline()
182   {
183     PolygonSprite screen = new PolygonSprite(0,01,01,10,1);
184     screenOutline = new OutlineSprite(screen);
185     screenOutline.setSize(1.2);
186     screenOutline.setLineThickness(0.1);
187     screenOutline.setLocation(0.50.5);
188     screenOutline.setColor(getColor("Grey"));
189     addSprite(screenOutline);
190   }
191   /** Creates and adds all four sides of the fence the cats will be herded into */
192   private void addFence()     // redesign to use Paramiters
193   {
194     fence[0] = new RectangleSprite(10,1);
195     fence[0].setLocation(.5,.375);
196 
197     fence[1] = new RectangleSprite(10,1);
198     fence[1].setLocation(.5,.625);
199 
200     fence[2] = new RectangleSprite(1,10);
201     fence[2].setLocation(.625,.5);
202 
203     fence[3] = new RectangleSprite(1,10);
204     fence[3].setLocation(.375,.5);
205 
206     for(int i=0; i<fence.length; i++)
207     {
208       fence[i].setSize(.3);
209       fence[i].setColor(getColor("Saddle Brown"));
210 
211       addSprite(fence[i]);
212     }
213     fence[1].setColor(getColor("Green"));
214   }
215   /** makes and adds 3 of each color cats to the screen at random places outside of the fence */
216   private void makeCats()
217   {
218     for(int = 0; i<cats.length; i++)
219     {
220       if(i<3)
221       {
222         cats[i] = new ImageSprite("Green_Cat_Icon.jpg");
223       }
224       if(i>&& i<6)
225       {
226         cats[i] = new ImageSprite("Orange_Cat_Icon.jpg");
227       }
228       if(i>5)
229       {
230         cats[i] = new ImageSprite("Red_Cat_Icon.jpg");
231       }
232       cats[i].setSize(.05);
233       double =(1+(double)(8*Math.random()))/10.0;
234       double =(1+(double)(8*Math.random()))/10.0;
235       while((x<0.7 && x>0.3&& (y<0.7 && y>0.3))
236       {
237         =(1+(double)(8*Math.random()))/10.0;
238         =(1+(double)(8*Math.random()))/10.0;
239       }
240       cats[i].setLocation(x,y);
241 
242       addSprite(cats[i]);
243     }
244   }
245   /** sets the boundarys to keep the cats in the screen cats also bounce off of the fence only if it is brown */
246   private void boundarys()
247   {
248     for(int i=0; i<cats.length; i++)
249     {
250       cats[i].bounceOffOf(screenOutline);
251       for(int j=0; j<fence.length; j++)
252       {
253         if(getColorName(fence[j].getColor())=="Saddle Brown")
254           cats[i].bounceOffOf(fence[j]);
255       }
256     }
257   }
258   /** creates the text to go into the help screen*/
259   private void helpScreen()
260   {
261     String helpText=
262         "the object of the game is to herd all 9 cats into the fence in the middle of the window before the time runs out<br>"+
263         "you have 60 seconds to get the cats into the fence, the cats can only pass over the green fence<br>"+
264         "after the 60 seconds have passed the green fence will turn brown and the game will dicide if you have won or lost<br>"+
265         "you win if all the cats are inside the fence when the timer runs out<br>"+
266         "you lose if the timer runs out before you get all the cats inside the fence<br>"+
267         "if you get all the cats into the fence before the time runs out you must keep them in the fence untill the timer reaches 0<br>"+
268         "be carefull of the corners of the screen,  if you push the cats into the corners than they will glitch and be pushed off the screen<br>"+
269         "Enjoy.";
270     setHelpText(helpText);
271   }
272 }

Compiler Errors:
----------
1. ERROR in Rohrer/Final_Project.java (at line 137)
	catsWander = (ProjectileTransformer)cats[i].getTransformer();
	                                            ^^^^^^^^^^^^^^
The method getTransformer() is undefined for the type Sprite
----------
2. ERROR in Rohrer/Final_Project.java (at line 143)
	originalDirection.setDirectionDegrees(toCat.getDirectionDegrees() + (100.0*Math.random()-50.0));
	                                            ^^^^^^^^^^^^^^^^^^^
The method setSpeed(double) is undefined for the type Vector2D
----------
7 problems (7 errors)

Download/View Rohrer/Final_Project.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