Jayson/deal/Deal

From ggc

Jump to: navigation, search

001 //package jayson_deal;
002 
003 package Jayson.deal;
004 
005 import wiki.Wiki;
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * Author:  Jayson Park
012  * Let's Make a Deal game with a reset button.
013  */
014 public class Deal extends GameLoop
015 {
016   /**Image Sprites for the three doors.*/
017   private ImageSprite door1, door2, door3;
018   /**Image for the explosion when a door is opened.*/
019   private ImageSprite explosion;
020   /**Image for the first lose prize.*/
021   private ImageSprite lose;
022   /**Image for the second lose prize.*/
023   private ImageSprite lose1;
024   /**Image for the first win prize.*/
025   private ImageSprite win;
026   /**Sprite for dot that appears when a door is chosen.*/
027   private Sprite chosendot;
028   /**Text for the reset message.*/
029   private StringSprite reset;
030   /**Text for the different stages of the game.*/
031   private StringSprite doormsg;
032   /**Sound for when a door is opened.*/
033   private Sound explode;
034   /**Integer for the randomization of which door is opened on click.*/
035   private int doorint;
036   /**Integer to hold the door number chosen.*/
037   private int doorchose = 0;
038   /**Door that is sent for the final win or lose stage.*/
039   private int lastdoor = 0;
040   /**Integer to hold the door that was opened.*/
041   private int dooropen = 0;
042   /**Integer for the stages of the game.*/
043   private int firstclick = 0;
044   /**Integer to hold the door not opened/chosen.*/
045   private int doornot = 0;
046 
047   //Declaring main method just so my IDE can work.
048   public static void main(String[] args)
049   {
050     //Constructor
051     Deal deal = new Deal();
052     //Special method to fix some errors.
053     deal.runAsApplication();
054   }
055 
056   /**Sets up the game*/
057   public void startGame()
058   {
059     makeSprites();
060     addSprites();
061   }
062 
063   /**Makes the sprites*/
064   private void makeSprites()
065   {
066     //Chosen dot to mark door.
067     chosendot = new OvalSprite(11);
068     chosendot.setScale(.1);
069     chosendot.setColor(Color.GREEN);
070 
071     //Reset Instructions
072     reset = new StringSprite("Press R to reset.");
073     reset.setScale(.8);
074     reset.setLocation(.5.8);
075 
076     //Lose and Win Images/scales
077     lose = new ImageSprite(("Loser0000.JPG"));
078     lose.setScale(.25);
079     lose1 = new ImageSprite(("Loser0000.JPG"));
080     lose1.setScale(.25);
081     win = new ImageSprite(("Loser0000.JPG"));
082     win.setScale(.25);
083 
084     //Sound File;
085     explode = new Sound(Wiki.getMedia("Explosion3.wav"));
086 
087     //Image for the explosion effect.
088     explosion = new ImageSprite(("Loser0000.JPG"));
089     explosion.setScale(.25);
090 
091     //Image and location of the first door.
092     door1 = new ImageSprite(("Doora.gif"));
093     door1.setLocation(.200.5);
094     door1.setScale(.25);
095 
096     //Image and location of the second door.
097     door2 = new ImageSprite(("Doora.gif"));
098     door2.setLocation(.50.5);
099     door2.setScale(.25);
100 
101     //Image and location of the third door.
102     door3 = new ImageSprite(("Doora.gif"));
103     door3.setLocation(.800.5);
104     door3.setScale(.25);
105   }
106 
107   /**Adds the sprites to the screen*/
108   private void addSprites()
109   {
110     canvas.addSprite(door1);
111     canvas.addSprite(door2);
112     canvas.addSprite(door3);
113     canvas.addSprite(reset);
114   }
115 
116   /**Runs the gif once and sets Alarm to remove the sprite after 1 second.*/
117   public void explosion()
118   {
119     explosion.setLooping(false);
120     explosion.startAnimationNow();
121     canvas.addSprite(explosion);
122     scheduleRelative(new Boom(explosion)1);
123   }
124 
125   /**Sets an alarm to add the lose graphic to the canvas in half a second.*/
126   public void loseSprite()
127   {
128     scheduleRelative(new Lose(lose).5);
129   }
130 
131   /**Adds a dot to the selected door
132    * Displays a message about door chosen and door clicked.
133    * Plays Sound for the explosion.*/
134   public void clickedDoor()
135   {
136     canvas.addSprite(chosendot);
137     doormsg = new StringSprite("You have chosen Door #" + doorchose +
138                                ".\nGood thing you didn't pick Door #" +
139                                dooropen +
140                                ".\nPress Q to stay or W to switch choice.");
141     doormsg.scale(.7);
142     doormsg.setLocation(.5.20);
143     canvas.addSprite(doormsg);
144     explode.play();
145     firstclick++;
146   }
147 
148   /**Opens either door 2 or 3, calls the clickedDoor()*/
149   public void doorOne(int doorint)
150   {
151     chosendot.setLocation(door1.getLocation());
152     doorchose = 1;
153     if (doorint == 0)
154     {
155       openDoor(doorint, door2);
156       dooropen = 2;
157       doornot = 3;
158     }
159     if (doorint == 1)
160     {
161       openDoor(doorint, door3);
162       dooropen = 3;
163       doornot = 2;
164     }
165     clickedDoor();
166   }
167 
168   /**Opens either door 1 or 3, calls the clickedDoor()*/
169   public void doorTwo(int doorint)
170   {
171     chosendot.setLocation(door2.getLocation());
172     doorchose = 2;
173     if (doorint == 0)
174     {
175       openDoor(doorint, door1);
176       dooropen = 1;
177       doornot = 3;
178     }
179     if (doorint == 1)
180     {
181       openDoor(doorint, door3);
182       dooropen = 3;
183       doornot = 1;
184     }
185     clickedDoor();
186   }
187 
188   /**Opens either door 1 or 2, calls the clickedDoor()*/
189   public void doorThree(int doorint)
190   {
191     chosendot.setLocation(door3.getLocation());
192     doorchose = 3;
193     if (doorint == 0)
194     {
195       dooropen = 1;
196       doornot = 2;
197       openDoor(doorint, door1);
198     }
199     if (doorint == 1)
200     {
201       dooropen = 2;
202       doornot = 1;
203       openDoor(doorint, door2);
204     }
205     clickedDoor();
206   }
207 
208   /**Removes the door sent, calls the explosion() and loseSprite()*/
209   public void openDoor(int a, Sprite dooropened)
210   {
211     lose.setLocation(dooropened.getLocation());
212     explosion.setLocation(dooropened.getLocation());
213     explosion();
214     canvas.removeSprite(dooropened);
215     loseSprite();
216   }
217 
218   /**Code to reset the game to replay without having to refresh the page.*/
219   public void resetGame()
220   {
221     doornot = 0;
222     doorchose = 0;
223     dooropen = 0;
224     firstclick = 0;
225     doorint = 0;
226     canvas.addSprite(door1);
227     canvas.addSprite(door2);
228     canvas.addSprite(door3);
229     canvas.removeSprite(lose);
230     canvas.removeSprite(win);
231     canvas.removeSprite(lose1);
232     canvas.removeSprite(doormsg);
233     canvas.removeSprite(chosendot);
234   }
235 
236   /**Sets the location of the 2nd lose graphic if won.*/
237   public void notDoor(int notthedoor)
238   {
239     if (notthedoor == 1)
240     {
241       lose1.setLocation(door1.getLocation());
242     }
243     if (notthedoor == 2)
244     {
245       lose1.setLocation(door2.getLocation());
246     }
247     if (notthedoor == 3)
248     {
249       lose1.setLocation(door3.getLocation());
250     }
251   }
252 
253   /**Sets the location of the win graphic if lost.*/
254   public void isDoor(int dooropen)
255   {
256     if (dooropen == 1)
257     {
258       win.setLocation(door1.getLocation());
259     }
260     if (dooropen == 2)
261     {
262       win.setLocation(door2.getLocation());
263     }
264     if (dooropen == 3)
265     {
266       win.setLocation(door3.getLocation());
267     }
268   }
269 
270   /**Removes all of the Doors from the canvas.*/
271   public void removeAllDoors()
272   {
273     canvas.removeSprite(door1);
274     canvas.removeSprite(door2);
275     canvas.removeSprite(door3);
276   }
277 
278   /**If won, sets the chosen door as win and other door as lose.*/
279   public void win(ImageSprite windoor)
280   {
281     win.setLocation(windoor.getLocation());
282     notDoor(doornot);
283     removeAllDoors();
284     canvas.addSprite(lose1);
285     canvas.addSprite(win);
286   }
287 
288   /**If lost, sets the chosen door as lose and other door as win*/
289   public void lose(ImageSprite losedoor)
290   {
291     lose1.setLocation(losedoor.getLocation());
292     isDoor(doornot);
293     removeAllDoors();
294     canvas.addSprite(lose1);
295     canvas.addSprite(win);
296   }
297 
298   /**Last choice, sets conditions for winning*/
299   public void finalChoice(int doorchosen)
300   {
301     int lastnumber = random.nextInt(2);
302     if (lastnumber == 1)
303     {
304       doormsg.setText("YOU WIN!");
305       if (doorchosen == 1)
306       {
307         win(door1);
308       }
309       if (doorchosen == 2)
310       {
311         win(door2);
312       }
313       if (doorchosen == 3)
314       {
315         win(door3);
316       }
317     }
318     if (lastnumber == 0)
319     {
320       doormsg.setText("YOU LOSE!");
321       if (doorchosen == 1)
322       {
323         lose(door1);
324       }
325       if (doorchosen == 2)
326       {
327         lose(door2);
328       }
329       if (doorchosen == 3)
330       {
331         lose(door3);
332       }
333     }
334   }
335 
336   /**Switches the door chosen when the w key is pressed.*/
337   public void switchChoice()
338   {
339     if (getPlayer().getKeyboard().getLastKey() == 'w')
340     {
341       if (firstclick == 1)
342       {
343         firstclick++;
344         lastdoor = doornot;
345         doornot = doorchose;
346         finalChoice(lastdoor);
347         canvas.removeSprite(chosendot);
348       }
349     }
350   }
351 
352   /**Stays on the door chosen when the q key is pressed.*/
353   public void stayChoice()
354   {
355     if (getPlayer().getKeyboard().getLastKey() == 'q')
356     {
357       if (firstclick == 1)
358       {
359         firstclick++;
360         lastdoor = doorchose;
361         finalChoice(lastdoor);
362         canvas.removeSprite(chosendot);
363       }
364     }
365   }
366 
367   /**Watches for clicks on doors.*/
368   public void doorClick()
369   {
370     Point2D.Double click = getPlayer().getMouse().getClickLocation();
371     //did they click at all
372     if (click != null && firstclick == 0)
373     {
374       doorint = random.nextInt(2);
375       if (door1.intersects(click))
376       {
377         doorOne(doorint);
378       }
379       if (door2.intersects(click))
380       {
381         doorTwo(doorint);
382       }
383       if (door3.intersects(click))
384       {
385         doorThree(doorint);
386       }
387     }
388   }
389 
390   /**Calls to the resetGame method when the r key is pressed*/
391   public void resetGameKey()
392   {
393     if (getPlayer().getKeyboard().getLastKey() == 'r')
394     {
395       resetGame();
396     }
397   }
398 
399   /**Handle input and game events*/
400   public void advanceFrame(double timePassed)
401   {
402     switchChoice();
403     stayChoice();
404     doorClick();
405     resetGameKey();
406   }
407 
408   /**Alarm class for the explosion Sprite removal.*/
409   private class Boom implements Alarm
410   {
411     private Sprite spriteremove;
412 
413     public Boom(Sprite sprite)
414     {
415       spriteremove = sprite;
416     }
417 
418     public void alarm()
419     {
420       canvas.removeSprite(spriteremove);
421     }
422   }
423 
424   /**Alarm class for the lose sprite addition.*/
425   private class Lose implements Alarm
426   {
427     private Sprite loseadd;
428     public Lose(Sprite sprite)
429     {
430       loseadd = sprite;
431     }
432 
433     public void alarm()
434     {
435       canvas.addSprite(loseadd);
436     }
437   }
438 }


Download/View Jayson/deal/Deal.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