KimBreakOut

From ggc

Jump to: navigation, search

001 import fang.*;
002 import java.awt.*;
003 import java.awt.geom.*;
004 
005 /**
006  * All about my game here.
007  @author Kmooney
008  */
009 public class KimBreakOut extends GameLoop
010 {
011 
012   private Sprite ball;
013   private Sprite paddle;
014   private Sprite[] square;
015   private ProjectileTracker tracker;
016   private Sprite leftwall;
017   private Sprite topwall;
018   private Sprite rightwall;
019   private Sprite bottomwall;
020   private StringSprite wordscore;
021   private StringSprite wordlives;
022   private int score;
023   private int livesLeft;
024   private StringSprite livesSprite;
025   private StringSprite scoreSprite;
026 
027 
028   public void startGame()
029   {
030     makeSprites();
031     addSprites();
032     makeTracker();
033     score=0;
034     livesLeft=3;
035     scheduleRelative(new TimeUpdater()1);
036     toggleAudible();
037     setHelpText("Hit the bricks by bouncing the ball off the paddle. You have three chances!");
038   }
039   class TimeUpdater implements Alarm
040   {
041     public void alarm()
042     {
043       livesLeft--;
044       updateTimer();
045       if(livesLeft>0)
046       {
047         scheduleRelative(this1);
048       }
049     }
050   }
051 
052   private void makeSprites()
053   {
054     leftwall=new RectangleSprite(240);
055     leftwall.setLocation(0.0150.5);
056     leftwall.setScale(1);
057     leftwall.setColor(Color.GREEN);
058 
059     topwall=new RectangleSprite(401);
060     topwall.setLocation(0.50);
061     topwall.setScale(1);
062     topwall.setColor(Color.GREEN);
063 
064     rightwall=new RectangleSprite(240);
065     rightwall.setLocation(.9850.5);
066     rightwall.setScale(1);
067     rightwall.setColor(Color.GREEN);
068 
069     bottomwall=new RectangleSprite(401);
070     bottomwall.setLocation(0.51);
071     bottomwall.setScale(1);
072     bottomwall.setColor(Color.GREEN);
073 
074     ball=new OvalSprite(11);
075     ball.setScale(0.05);
076     ball.setLocation(0.50.9);
077     ball.setColor(Color.RED);
078 
079     paddle=new RectangleSprite(41);
080     paddle.setScale(.25);
081     paddle.setLocation(.51);
082     paddle.setColor(Color.BLUE);
083 
084 
085     int index=0;
086     square=new Sprite[50];
087     int numBricksHigh=5;
088     double startLocationY=0.1;
089     double endLocationY=.3;
090     double distanceHigh=endLocationY-startLocationY;
091     for(int j=0; j<numBricksHigh; j++)
092     {
093       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
094 
095       int numBricksAcross=10;
096       double startLocationX=0.1;
097       double endLocationX=1-startLocationX;
098       double distanceAcross=endLocationX-startLocationX;
099 
100       for(int i=0; i<numBricksAcross; i++)
101       {
102         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
103         square[i]=new RectangleSprite(21);
104         square[i].setScale(.025);
105         square[i].setLocation(x,y);
106         square[i].setColor(Color.YELLOW);
107 
108         canvas.addSprite(square[i]);
109 
110 
111         wordscore=new StringSprite("Score:" +score);
112         wordscore=new StringSprite("Score: 0");
113         wordscore.setLocation(0.90.05);
114         wordscore.setScale(0.10);
115         wordscore.setColor(Color.WHITE);
116 
117         wordlives=new StringSprite("Lives: "+livesLeft);
118         wordlives.setLocation(.15.05);
119         wordlives.setScale(0.10);
120         wordlives.setColor(Color.WHITE);
121 
122       }
123     }
124   }
125 
126   private void updateTimer()
127   {
128     livesSprite.setText("Lives: "+livesLeft);
129   }
130 
131   private void updateScore()
132   {
133     scoreSprite.setText("Score: "+score);
134   }
135 
136 
137   private void addSprites()
138   {
139     canvas.addSprite(ball);
140     canvas.addSprite(paddle);
141     canvas.addSprite(leftwall);
142     canvas.addSprite(topwall);
143     canvas.addSprite(rightwall);
144     canvas.addSprite(bottomwall);
145     canvas.addSprite(wordscore);
146     canvas.addSprite(wordlives);
147 
148   }
149 
150   private void makeTracker()
151   {
152     tracker=new ProjectileTracker(.75, -.95);
153     ball.setTracker(tracker);
154   }
155 
156 
157   private void handleCollisions()
158   {
159     Point2D.Double click=getPlayer().getMouse().getClickLocation();
160     if(click!=null)
161     {
162       for(int i=0; i<square.length; i++)
163       {
164         if(canvas.containsSprite(square[i]))
165         {
166           if(square[i].intersects(click))
167           {
168             canvas.removeSprite(square[i]);
169             score++;
170           }
171           else
172           {
173             score--;
174           }
175           updateScore();
176         }
177       }
178     }
179   }
180 
181   public void advanceFrame(double timePassed)
182   {
183     if(paddle.intersects(ball))
184     {
185       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
186       tracker.bounce(normal);
187     }
188 
189     if(leftwall.intersects(ball))
190     {
191       double normal=Sprite.getNormalVector(leftwall.getShape(), ball.getShape());
192       tracker.bounce(normal);
193     }
194     if(topwall.intersects(ball))
195     {
196       double normal=Sprite.getNormalVector(topwall.getShape(), ball.getShape());
197       tracker.bounce(normal);
198     }
199     if(rightwall.intersects(ball))
200     {
201       double normal=Sprite.getNormalVector(rightwall.getShape(), ball.getShape());
202       tracker.bounce(normal);
203     }
204 
205     if(bottomwall.intersects(ball))
206     {
207       double normal=Sprite.getNormalVector(bottomwall.getShape(), ball.getShape());
208       tracker.bounce(normal);
209     }
210     if(livesLeft>0)
211     {
212       handleCollisions();
213       toggleAudible();
214     }
215   }
216 }

Compiler Errors:
----------
1. ERROR in KimBreakOut.java (at line 54)
	leftwall=new RectangleSprite(2, 40);
	         ^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor RectangleSprite(int, int) is undefined
----------
2. ERROR in KimBreakOut.java (at line 54)
	leftwall=new RectangleSprite(2, 40);
	         ^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from RectangleSprite to Sprite
----------
12 problems (12 errors)

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