Charles/BearHockey

From ggc

Jump to: navigation, search

001 package Charles;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * All about my game.
009  @author Charles Perez
010  */
011 /**the timed action in the advanced was created with the help of Kevins code*/
012 public class BearHockey extends Game
013 {
014   /**movePuck is used to manipulate the pucks motion*/
015   private ProjectileTransformer movePuck;
016   private StringSprite logo1, logo2, p1, p1score, p2, p2score, clock, stage, gOMessage;
017   /**plyr is an array that indexs the two players in the order they connect to the game */
018   private Sprite[] plyr;
019   private ImageSprite puck;
020   private LineSprite topC, bottomC, lGoal, rGoal;
021   /** s1 and s2 are used to represent the players scores, and level is used to represent stages of the game where aspects get modified*/
022   private int s1, s2, level, winner;
023   private double b, count;
024   private String helptxt;
025 
026 
027   /**sets up the game*/
028   public void setup()
029   {
030     level=1;
031     int s1=0;
032     int s2=0;
033     count=90;
034     gameLogos();
035     displayStage();
036     makePlayerIcons();
037     makePuck();
038     makeHelp();
039     locatePlayer();
040     makeScoreBoard();
041     makeTimeClock();
042     gameArea();
043     gameEnd();
044     plyr[0].rotateDegrees(180);
045 
046   }
047   /**creates the help message for the player to read once game is loaded*/
048   private void makeHelp()
049   {
050     helptxt=
051         "*To begin click start.<br>"+
052         "*Player one will be the first player who joined while player two will be the second.<br>"+
053         "*Move your bear up by using W and down by using S.<br>"+
054         "*The object of the game is to keep the G puck out of your goal and score more than your opponent before the time is up!<br>"+
055         "*Once you've completed level one, the game will advance and become more difficult!<br>"+
056         "You have 90 seconds Good Luck!<br>";
057     setHelpText(helptxt);
058   }
059   /**Contains the design as well as adds the border for the game's action to be contained in*/
060   private void gameArea()
061   {
062     topC=new LineSprite(-0.1,0.51.10.5);
063     topC.setLocation(0.5,0.3);
064     topC.setColor(getColor("Green"));
065     topC.setLineThickness(0.05);
066     addSprite(topC);
067 
068     bottomC=new LineSprite(-0.1,0.51.10.5);
069     bottomC.setLocation(0.5,0.98);
070     bottomC.setColor(getColor("Green"));
071     bottomC.setLineThickness(0.05);
072     addSprite(bottomC);
073 
074     lGoal=new LineSprite(0.00.00.00.62);
075     lGoal.setLineThickness(0.03);
076     lGoal.setLocation(0.020.64);
077     lGoal.setColor(getColor("White"));
078     addSprite(lGoal);
079 
080     rGoal=new LineSprite(0.00.00.00.62);
081     rGoal.setLineThickness(0.03);
082     rGoal.setLocation(0.980.64);
083     rGoal.setColor(getColor("White"));
084     addSprite(rGoal);
085   }
086   /**creates an incomplete string that is filled in based on the players scores at the end of the game*/
087   private void gameEnd()
088   {
089     gOMessage=new StringSprite("");
090     gOMessage.setLocation(0.5,0.5);
091     gOMessage.setVisible(false);
092     addSprite(gOMessage);
093   }
094   /**creates the level display at the top of the game screen so the player knows what point of the game they are in*/
095   private void displayStage()
096   {
097     stage=new StringSprite("Level:"+level);
098     stage.setScale(0.2);
099     stage.setLocation(0.50.1);
100     stage.setColor(getColor("Red"));
101     addSprite(stage);
102   }
103   /**creates the score board that indicates the players number and displayes each players individual score*/
104   private void makeScoreBoard()
105   {
106     p1=new StringSprite("Player 1");
107     p1.setLocation(0.150.1);
108     p1.setScale(0.2);
109     p1.setColor(getColor("Yellow"));
110     addSprite(p1);
111 
112     p1score=new StringSprite("Score:"+s1);
113     p1score.setLocation(0.150.2);
114     p1score.setScale(0.2);
115     p1score.setColor(getColor("Yellow"));
116     addSprite(p1score);
117 
118     p2=new StringSprite("Player 2");
119     p2.setLocation(0.80.1);
120     p2.setScale(0.2);
121     p2.setColor(getColor("Yellow"));
122     addSprite(p2);
123 
124     p2score=new StringSprite("Score:"+s2);
125     p2score.setLocation(0.80.2);
126     p2score.setScale(0.2);
127     p2score.setColor(getColor("Yellow"));
128     addSprite(p2score);
129   }
130   /**creates the clock display for the time left in the game.*/
131   private void makeTimeClock()
132   {
133     clock=new StringSprite("Time Left:"+count);
134     clock.setLocation(0.50.25);
135     clock.setScale(0.25);
136     clock.setColor(getColor("White"));
137     addSprite(clock);
138   }
139   /**creates two strings as logos on the side of the game table with the bottom one upside down*/
140   private void gameLogos()
141   {
142     logo1=new StringSprite("BearHockey");
143     logo1.setLocation(0.50.3);
144     logo1.setHeight(0.04);
145     logo1.setColor(getColor("Yellow"));
146     addSprite(logo1);
147 
148     logo2=new StringSprite("BearHockey");
149     logo2.setLocation(0.50.98);
150     logo2.setHeight(0.04);
151     logo2.setColor(getColor("Yellow"));
152     logo2.setRotationDegrees(180);
153     addSprite(logo2);
154   }
155   /**Creates the images used to represent the players in the game area via a bear that i created in art to go with the GGC theme*/
156   private void makePlayerIcons()
157   {
158     plyr=new Sprite[getNumberOfPlayers()];
159     for(int i=0; i<plyr.length; i++)
160     {
161       plyr[i]=new ImageSprite("Bear2.jpg");
162       plyr[i].setScale(0.14);
163       addSprite(plyr[i]);
164     }
165   }
166 
167   /**creates and adds the image that represents the puck*/
168   private void makePuck()
169   {
170     puck=new ImageSprite("Puck.jpg");
171     puck.setScale(0.06);
172     puck.setLocation(0.50.65);
173     movePuck=new ProjectileTransformer(0.10.25);
174     puck.setTracker(movePuck);
175     addSprite(puck);
176   }
177   /**instructes the puck to bounce off of certain objects.*/
178   private void handlePuckMotion()
179   {
180     puck.bounceOffOf(topC);
181     puck.bounceOffOf(bottomC);
182     for(int i=0; i<plyr.length; i++)
183     {
184       puck.bounceOffOf(plyr[i]);
185     }
186   }
187   /** places player one on the left side of the feild and player 2 on the right side*/
188   private void locatePlayer()
189   {
190     for(int i=0; i<plyr.length; i++)
191     {
192       if(i%2==0)
193       {
194         plyr[i].setLocation(0.080.65);
195       }
196       else
197       {
198         plyr[i].setLocation(0.920.65);
199       }
200     }
201   }
202   /** allows the players to move thier icons up and down*/
203   private void movePlayer()
204   {
205     for(int i=0; i<plyr.length; i++)
206     {
207       double y=plyr[i].getY();
208       if(getKeyPressed(i)=='w' && plyr[i].getY()>=0.35)
209       {
210         plyr[i].setY(y-0.035);
211       }
212       else if(getKeyPressed(i)=='s' && plyr[i].getY()<=0.93)
213       {
214         plyr[i].setY(y+0.035);
215       }
216     }
217   }
218   /**creates the speed and angle for the puck to be released during level1*/
219   private void puckSpeed1()
220   {
221     int a=random.nextInt(2);
222     if(a==0)
223     {
224       movePuck.setVector2D(0.250.0);
225     }
226     else if(a==1)
227     {
228       movePuck.setVector2D(-0.25,0.0);
229     }
230   }
231   /**creates the speed and angle for the puck to be released during level2*/
232   private void puckSpeed2()
233   {
234     int a=random.nextInt(4);
235     if(a==0)
236     {
237       movePuck.setVector2D(0.3, -0.1);
238     }
239     else if(a==1)
240     {
241       movePuck.setVector2D(-0.3,-0.1);
242     }
243     else if(a==2)
244     {
245       movePuck.setVector2D(0.3,0.1);
246     }
247     else if(a==3)
248     {
249       movePuck.setVector2D(-0.3,0.1);
250     }
251   }
252   /**creates the speed and angle for the puck to be released during level3*/
253   private void puckSpeed3()
254   {
255     int a=random.nextInt(4);
256     if(a==0)
257     {
258       movePuck.setVector2D(0.4, -0.15);
259     }
260     else if(a==1)
261     {
262       movePuck.setVector2D(-0.4,-0.15);
263     }
264     else if(a==2)
265     {
266       movePuck.setVector2D(0.4,0.15);
267     }
268     else if(a==3)
269     {
270       movePuck.setVector2D(-0.4,0.15);
271     }
272   }
273   /** when the puck crosses player 2's goal player 1 scores and the puck is reset and released based on the level*/
274   private void player1Scores()
275   {
276     if(puck.getX()>= 1.0 &&puck.getX()<=1.02 )
277     {
278       s1++;
279       p1score.setText("Score:"+s1);
280       puck.setLocation(0.5,0.65);
281       if(level==1)
282       {
283         puckSpeed1();
284       }
285       if(level==2)
286       {
287         puckSpeed2();
288       }
289       if(level==3)
290       {
291         puckSpeed3();
292       }
293     }
294   }
295   /** when the puck crosses player 1's goal player 2 scores and the puck is reset and released based on the level*/
296   private void player2Scores()
297   {
298     if(puck.getX()<= 0.0 && puck.getX()>=-0.02 )
299     {
300       s2++;
301       p2score.setText("Score:"+s2);
302       puck.setLocation(0.5,0.65);
303       if(level==1)
304       {
305         puckSpeed1();
306       }
307       if(level==2)
308       {
309         puckSpeed2();
310       }
311       if(level==3)
312       {
313         puckSpeed3();
314       }
315     }
316   }
317   /** advances the game to level 2 at 70 seconds*/
318   private void advanceToLevel2()
319   {
320     if(level==&& count<=70 && count>=30)
321     {
322       level=2;
323       stage.setText("Level:"+level);
324       for(int i=0; i<plyr.length; i++)
325       {
326         plyr[i].setScale(0.1);
327       }
328     }
329   }
330   /** advances the game to level 3 at 30 seconds*/
331   private void advanceToLevel3()
332   {
333     if(level==&& count<30)
334     {
335       level=3;
336       stage.setText("Level:"+level);
337       for(int i=0; i<plyr.length; i++)
338       {
339         plyr[0].setX(0.1);
340         plyr[1].setX(0.9);
341       }
342     }
343   }
344   /** displayes the winner of the game at 0 seconds based on the return value of findWinner(), and stops the pucks movement.*/
345   private void endGameMessage()
346   {
347     if(level==&& count<0.5)
348     {
349       level=4;
350       movePuck.setVector2D(0.0,0.0);
351       if(winner==|| winner==2)
352       {
353         gOMessage.setText("Player:"+winner +" is the WINNER!!");
354         gOMessage.setSize(0.7);
355       }
356       else if(winner==0)
357       {
358         gOMessage.setText("Tie");
359         gOMessage.setScale(0.2);
360       }
361       gOMessage.setVisible(true);
362     }
363   }
364   /**recieves the two int parameters for both players' scores and returns an int that determines if player 1 or player is the winner or a tie */
365   private int findWinner(int s1, int s2)
366   {
367     if(s1>s2)
368     {
369       winner=1;
370     }
371     else if(s1<s2)
372     {
373       winner=2;
374     }
375     else if(s1==s2)
376     {
377       winner=0;
378     }
379     return winner;
380   }
381   /**handle input and game events*/
382   public void advance()
383   {
384     handlePuckMotion();
385     movePlayer();
386     player1Scores();
387     player2Scores();
388     advanceToLevel2();
389     advanceToLevel3();
390     findWinner(s1,s2);
391     endGameMessage();
392   }
393   /**once game starts this reduces the timer by 1 every second until it reaches 0. I recieved help from Kevin Rohrer to get this to count properly.*/
394   class DelayedResponse extends TimedAction
395   {
396     public void act()
397     {
398       count--;
399       clock.setText("Time Left:"+count);
400       if(count>0.0)
401         schedule(this,1.0);
402     }
403   }
404   {
405     schedule(new DelayedResponse(),1.0);
406   }
407 }


Download/View Charles/BearHockey.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