Jam/ShapeMatcher

From ggc

Jump to: navigation, search

001 package Jam;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  * This game is to teach kids about the
010  * names of shapes and words for numbers.
011  @author Jam Jenkins
012  */
013 public class ShapeMatcher extends GameLoop
014 {
015   /**background behind shape to match*/
016   private RectangleSprite highlight;
017   /**the shape to match click on*/
018   private PolygonSprite shapeToMatch;
019   /**how many sides are on the shape to match*/
020   private int sides;
021   /**a regular three-sided polygon*/
022   private PolygonSprite triangle;
023   /**a regular four-sided polygon*/
024   private PolygonSprite square;
025   /**a regular five-sided polygon*/
026   private PolygonSprite pentagon;
027   /**a regular six-sided polygon*/
028   private PolygonSprite hexagon;
029   /**name of the shape*/
030   private StringSprite shapeLabel;
031   /**number of sides as a word*/
032   private StringSprite sidesLabel;
033 
034   /**right in a row*/
035   private int rightInRow;
036 
037   /**sets up the game*/
038   public void startGame()
039   {
040     rightInRow=0;
041     sides=random.nextInt(4)+3;
042     makeSprites();
043     addSprites();
044     setHelpText("<h1>Match the Shape</h1>"+
045                 "Click on the shape that matches the words "+
046                 "on the top and left and the shape in the "+
047                 "upper left corner.<br><br>As you get better "+
048                 "the game gets harder.  Have fun!");
049   }
050 
051   private void makeRandomShapes()
052   {
053     triangle=new PolygonSprite(3);
054     triangle.setScale(shapeToMatch.getScale()*2);
055     triangle.setLocation(random.nextDouble()*0.6+0.2, random.nextDouble()*0.6+0.2);
056     triangle.setColor(new Color(random.nextFloat()*0.8f+0.2f,
057                                 random.nextFloat()*0.8f+0.2f,
058                                 random.nextFloat()*0.8f+0.2f0.8f));
059 
060     square=new PolygonSprite(4);
061     square.setScale(shapeToMatch.getScale()*2);
062     square.setLocation(random.nextDouble()*0.6+0.2, random.nextDouble()*0.6+0.2);
063     square.setColor(new Color(random.nextFloat()*0.8f+0.2f,
064                               random.nextFloat()*0.8f+0.2f,
065                               random.nextFloat()*0.8f+0.2f0.8f));
066 
067     pentagon=new PolygonSprite(5);
068     pentagon.setScale(shapeToMatch.getScale()*2);
069     pentagon.setLocation(random.nextDouble()*0.6+0.2, random.nextDouble()*0.6+0.2);
070     pentagon.setColor(new Color(random.nextFloat()*0.8f+0.2f,
071                                 random.nextFloat()*0.8f+0.2f,
072                                 random.nextFloat()*0.8f+0.2f0.8f));
073 
074     hexagon=new PolygonSprite(6);
075     hexagon.setScale(shapeToMatch.getScale()*2);
076     hexagon.setLocation(random.nextDouble()*0.6+0.2, random.nextDouble()*0.6+0.2);
077     hexagon.setColor(new Color(random.nextFloat()*0.8f+0.2f,
078                                random.nextFloat()*0.8f+0.2f,
079                                random.nextFloat()*0.8f+0.2f0.8f));
080   }
081 
082   private void makeTargetShapes()
083   {
084     highlight=new RectangleSprite(11);
085     highlight.setScale(0.15);
086     highlight.setLocation(0.10.1);
087     highlight.setColor(Color.YELLOW);
088 
089     shapeToMatch=new PolygonSprite(sides);
090     shapeToMatch.setScale(highlight.getScale()*0.9);
091     shapeToMatch.setLocation(highlight.getLocation());
092     shapeToMatch.setColor(Color.RED);
093 
094     shapeLabel=new StringSprite("triangle");
095     if(sides==4)
096       shapeLabel.setText("square");
097     else if(sides==5)
098       shapeLabel.setText("pentagon");
099     else if(sides==6)
100       shapeLabel.setText("hexagon");
101     shapeLabel.rightJustify();
102     shapeLabel.setLineHeight(highlight.getScale());
103     shapeLabel.topJustify();
104     shapeLabel.setLocation(0.950.05);
105 
106     sidesLabel=new StringSprite("three");
107     if(sides==4)
108       sidesLabel.setText("four");
109     else if(sides==5)
110       sidesLabel.setText("five");
111     else if(sides==6)
112       sidesLabel.setText("six");
113     sidesLabel.leftJustify();
114     sidesLabel.setLineHeight(highlight.getScale());
115     sidesLabel.topJustify();
116     sidesLabel.setLocation(0.050.95);
117     sidesLabel.rotateRevolutions(-0.25);
118   }
119 
120   /**makes all the sprites*/
121   private void makeSprites()
122   {
123     makeTargetShapes();
124     makeRandomShapes();
125   }
126 
127   /**adds the sprites to the screen*/
128   private void addSprites()
129   {
130     canvas.addSprite(highlight, shapeToMatch);
131     canvas.addSprite(triangle, square, pentagon, hexagon);
132     canvas.addSprite(sidesLabel, shapeLabel);
133   }
134 
135   private void handleTriangle()
136   {
137     Point2D.Double click=getPlayer().getMouse().getClickLocation();
138     if(sides==3)
139     {
140       if(triangle.intersects(click))
141       {
142         rightInRow=rightInRow+1;
143       }
144       else
145       {
146         rightInRow=0;
147       }
148     }
149   }
150 
151   private void handleSquare()
152   {
153     Point2D.Double click=getPlayer().getMouse().getClickLocation();
154     if(sides==4)
155     {
156       if(square.intersects(click))
157       {
158         rightInRow=rightInRow+1;
159       }
160       else
161       {
162         rightInRow=0;
163       }
164     }
165 
166   }
167 
168   private void handlePentagon()
169   {
170     Point2D.Double click=getPlayer().getMouse().getClickLocation();
171     if(sides==5)
172     {
173       if(pentagon.intersects(click))
174       {
175         rightInRow=rightInRow+1;
176       }
177       else
178       {
179         rightInRow=0;
180       }
181     }
182 
183   }
184 
185   private void handleHexagon()
186   {
187     Point2D.Double click=getPlayer().getMouse().getClickLocation();
188     if(sides==6)
189     {
190       if(hexagon.intersects(click))
191       {
192         rightInRow=rightInRow+1;
193       }
194       else
195       {
196         rightInRow=0;
197       }
198     }
199 
200   }
201 
202   /**handle input and game events*/
203   public void advanceFrame(double timePassed)
204   {
205     Point2D.Double click=getPlayer().getMouse().getClickLocation();
206     if(click!=null)
207     {
208       handleTriangle();
209       handleSquare();
210       handlePentagon();
211       handleHexagon();
212     }
213     if(rightInRow>=3)
214     {
215       canvas.removeSprite(shapeToMatch);
216     }
217     else
218     {
219       canvas.addSprite(shapeToMatch);
220     }
221 
222   }
223 }


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