Curtis/Arkanoid

From ggc

Jump to: navigation, search

001 package Curtis;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * Classic Arkanoid.
012  @author Curtis St. John
013  */
014 public class Arkanoid extends Game
015 {
016   private RectangleSprite block;
017   private ArrayList<Sprite> allBlocks;
018   private OutlineSprite paddle, wall1, wall2, wall3;
019   private OvalSprite ball, ball2, rnd1, rnd2;
020   private ArrayList<Sprite> redBullet;
021   private ProjectileTransformer projectile, projectile2, bulletTransformer;
022   private StringSprite lives, score, youWin, gameOver;
023   private int blocksLeft, startlives;
024 
025   /**sets up the game*/
026   public void setup()
027   {
028     helpScreen();
029     blocks();
030     ball();
031     paddle();
032     walls();
033     roundWalls();
034     lives();
035     score();
036     redBullet=new ArrayList<Sprite>();
037 
038     projectile=new ProjectileTransformer(.1.25);
039     projectile2=new ProjectileTransformer(.2.25);
040     ball.setTracker(projectile);
041     ball2.setTracker(projectile2);
042 
043   }
044 
045   /**The Help Screen*/
046   private void helpScreen()
047   {
048     String helpText=
049         "Use The Mouse<br>"+
050         "To Move<br>"+
051         "Right Click To Shoot<br>"+
052         "Shooting the ball<br>"+
053         "Adds an extra ball...<br>"+
054         "Only works ONCE!<br>"+
055         "By the way keep track of the<br>"+
056         "Original ball. Extra one doesn't come back";
057     setHelpText(helpText);
058   }
059 
060   /**Makes the blocks*/
061   private void blocks()
062   {
063     allBlocks=new ArrayList<Sprite>();
064     int blockshoriz=11;
065     int blocksvert=4;
066     for (int j=0; j<blocksvert; j++)
067     {
068       for(int i=1; i<=blockshoriz; i++)
069       {
070         block=new RectangleSprite(21);
071         block.setSize(.9/blockshoriz);
072         double x=.5/blockshoriz+1.0/blockshoriz*(i-1);
073         double y=.5/blockshoriz+(j+0.0)/blockshoriz;
074         block.setLocation(x, y);
075         block.setColor(getColor("white"));
076         addSprite(block);
077         allBlocks.add(block);
078       }
079     }
080   }
081   /**Makes the balls*/
082   private void ball()
083   {
084     ball=new OvalSprite(11);
085     ball.setSize(.03);
086     ball.setLocation(.7.5);
087     ball.setColor(getColor("lime green"));
088     addSprite(ball);
089 
090     ball2=new OvalSprite(11);
091     ball2.setSize(.03);
092     ball2.setLocation(.7.6);
093     ball2.setColor(getColor("lime green"));
094     ball2.setVisible(false);
095   }
096   /**Makes the paddle*/
097   private void paddle()
098   {
099     paddle=new OutlineSprite(new LineSprite(0010));
100     paddle.setSize(.25);
101     paddle.setLineThickness(.1);
102     paddle.setLocation(.5.9);
103     paddle.setColor(getColor("yellow"));
104     addSprite(paddle);
105   }
106   /**Makes straight walls*/
107   private void walls()
108   {
109     wall1=new OutlineSprite(new LineSprite(0001));
110     wall1.setSize(2);
111     wall1.setLocation(00);
112     wall1.setColor(getColor("magenta"));
113     addSprite(wall1);
114 
115     wall2=new OutlineSprite(new LineSprite(0001));
116     wall2.setSize(2);
117     wall2.setLocation(10);
118     wall2.setColor(getColor("magenta"));
119     addSprite(wall2);
120 
121     wall3=new OutlineSprite(new LineSprite(0010));
122     wall3.setSize(2);
123     wall3.setLocation(00);
124     wall3.setColor(getColor("magenta"));
125     addSprite(wall3);
126   }
127   /**Makes the round wall for extra difficulty*/
128   private void roundWalls()
129   {
130     rnd1=new OvalSprite(11);
131     rnd1.setSize(.2);
132     rnd1.setLocation(1.6);
133     addSprite(rnd1);
134 
135     rnd2=new OvalSprite(11);
136     rnd2.setSize(.2);
137     rnd2.setLocation(0.6);
138     addSprite(rnd2);
139   }
140 
141   /**Makes paddle move*/
142   private void movePaddle()
143   {
144     paddle.setX(getMouseX());
145   }
146   /**What the ball can bounce off of*/
147   private void moveBall()
148   {
149     ball.bounceOffOf(paddle);
150     ball.bounceOffOf(wall1);
151     ball.bounceOffOf(wall2);
152     ball.bounceOffOf(wall3);
153     ball.bounceOffOf(rnd1);
154     ball.bounceOffOf(rnd2);
155 
156     ball2.bounceOffOf(paddle);
157     ball2.bounceOffOf(wall1);
158     ball2.bounceOffOf(wall2);
159     ball2.bounceOffOf(wall3);
160     ball2.bounceOffOf(rnd1);
161     ball2.bounceOffOf(rnd2);
162   }
163   /**Handles the bulletts*/
164   private Sprite getAvailableBullet()
165   {
166     for(Sprite single: redBullet)
167     {
168       if(single.isVisible()==false)
169       {
170         return single;
171       }
172       if(single.getLocation().y<0)
173       {
174         return single;
175       }
176     }
177 
178     Sprite bullet=new PolygonSprite(3);
179     bullet.setSize(0.05);
180     bullet.setVisible(false);
181     bullet.setColor(getColor("red"));
182 
183     ProjectileTransformer bulletTransformer=new ProjectileTransformer(0, -0.2);
184     bullet.addTransformer(bulletTransformer);
185 
186     addSprite(bullet);
187     redBullet.add(bullet);
188 
189     return bullet;
190   }
191   /**Makes the "You Win" text*/
192   private void victory()
193   {
194     youWin=new StringSprite("You Win!");
195     youWin.setSize(1);
196     youWin.setLocation(.5.8);
197     addSprite(youWin);
198   }
199 
200   /**Makes the "Game Over" text*/
201   private void failure()
202   {
203     gameOver=new StringSprite("Game Over");
204     gameOver.setSize(1);
205     gameOver.setLocation(.5.8);
206     addSprite(gameOver);
207     setLives(0);
208   }
209 
210   /**Makes "you win" text flash different colors*/
211   public void randomColorBound()
212   {
213     youWin.setColor(new Color(random.nextInt()));
214   }
215 
216   /**Allows you to shoot by clicking*/
217   private void paddleShoot()
218   {
219     if(getClick2D()!=null)
220     {
221       Sprite available=getAvailableBullet();
222       available.setLocation(paddle.getX(), paddle.getY()-paddle.getSize()/2);
223       available.setVisible(true);
224 
225     }
226   }
227 
228   /**Shooting Blocks + 5 score*/
229   private void handleBulletblockCollision()
230   {
231     for(Sprite single: allBlocks)
232     {
233       for(Sprite bullet: redBullet)
234       {
235         if(single.isVisible() && bullet.isVisible() && single.intersects(bullet))
236         {
237           single.setVisible(false);
238           bullet.setVisible(false);
239 
240           int startscore = getScore();
241           setScore(startscore+5);
242           score.setText("Score:"+getScore());
243         }
244       }
245     }
246   }
247 
248   /**Handles the 1st ball hitting blocks + 10 score*/
249   private void handleBallblockCollision()
250   {
251     for(Sprite single: allBlocks)
252     {
253       if(single.isVisible()== true && single.intersects(ball))
254       {
255         ball.bounceOffOf(single);
256 
257         single.setVisible(false);
258 
259         int startscore = getScore();
260         setScore(startscore+10);
261         score.setText("Score:"+getScore());
262       }
263     }
264   }
265 
266   /**Handles the 2nd ball hitting blocks + 10 score*/
267   private void handleBall2blockCollision()
268   {
269     for(Sprite single: allBlocks)
270     {
271       if(single.isVisible()== true && single.intersects(ball2))
272       {
273         ball2.bounceOffOf(single);
274 
275         single.setVisible(false);
276 
277         int startscore = getScore();
278         setScore(startscore+10);
279         score.setText("Score:"+getScore());
280       }
281     }
282   }
283 
284   /**Counts how many blocks are left*/
285   private int blockCount()
286   {
287     blocksLeft = 0;
288     for(Sprite single : allBlocks)
289     {
290       if(single.isVisible() == true)
291         blocksLeft++;
292     }
293     return blocksLeft;
294   }
295 
296   /**Handles the ball hitting the bullet and giving extra ball*/
297   private void handleBallBulletCollision()
298   {
299     for(Sprite bullet: redBullet)
300     {
301       if(ball.isVisible() && bullet.isVisible() && ball.intersects(bullet))
302       {
303         ball2.setVisible(true);
304         addSprite(ball2);
305       }
306     }
307   }
308 
309   /**Displays lives*/
310   private void lives()
311   {
312     setLives(3);
313     lives=new StringSprite("Lives:"+getLives());
314     lives.setLocation(0.10.95);
315     lives.setSize(0.15);
316     lives.leftJustify();
317     lives.setColor(getColor("white"));
318     addSprite(lives);
319   }
320 
321   /**Losing a life*/
322   private void losing()
323   {
324     if(ball.getLocation().y>&& ball2.isVisible() ==false)
325     {
326       startlives = getLives();
327       setLives(startlives-1);
328       lives.setText("Lives:"+getLives());
329 
330       ball.setLocation(.4.6);
331     }
332     if(ball.getLocation().y>&& ball2.isVisible())
333     {
334       ball.setVisible(false);
335     }
336 
337     if(ball2.getLocation().y>&& ball.isVisible() ==false)
338     {
339       startlives = getLives();
340       setLives(startlives-1);
341       lives.setText("Lives:"+getLives());
342       ball.setLocation(.4.6);
343       ball.setVisible(true);
344     }
345 
346     if(ball2.getLocation().y>&& ball.isVisible())
347     {
348       ball2.setVisible(false);
349     }
350     if (getLives()<1)
351     {
352       failure();
353       paddle.setVisible(false);
354     }
355   }
356 
357   /**Display score*/
358   private void score()
359   {
360     setScore(0);
361     score=new StringSprite ("Score:"+getScore());
362     score.setLocation(0.70.95);
363     score.setSize(0.15);
364     score.setColor(getColor("white"));
365     score.rightJustify();
366     addSprite(score);
367   }
368 
369   /**handle input and game events*/
370   public void advance()
371   {
372     movePaddle();
373     moveBall();
374     paddleShoot();
375     handleBulletblockCollision();
376     handleBallblockCollision();
377     handleBall2blockCollision();
378     handleBallBulletCollision();
379 
380     if(getLives()>&& blockCount()>0)
381     {
382       losing();
383     }
384 
385     /**Displays victory text when you run out of blocks*/
386     if(blockCount()<=0)
387     {
388       victory();
389       randomColorBound();
390     }
391   }
392 }


Download/View Curtis/Arkanoid.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