Stephanie/wackadot/Wackadot

From ggc

Jump to: navigation, search

001 package Stephanie.wackadot;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**This is a fun, simple game I made using
009  * the FANG Engine.
010  @author Stephanie
011  */
012 public class Wackadot extends GameLoop
013 {
014   private ImageSprite dot;
015   private ImageSprite redDot;
016   private ImageSprite blueDot;
017   private StringSprite scoreSprite;
018   private int score;
019   private int timeLeft;
020   private StringSprite timerSprite;
021   private Sound sound=new Sound("DingLower.wav");
022   private Sound sound2=new Sound("Drums.wav");
023 
024   public void startGame()
025   {
026     score=0;
027     timeLeft=10;
028     makeSprites();
029     addSprites();
030     scheduleRelative(new TimeUpdater()1);
031     setHelpText("Try to get the same images hit. If you make the right ones hit, you get a point. Watch out, if you hit the wrong image you lose a point.");
032   }
033 
034   class TimeUpdater implements Alarm
035   {
036     public void alarm()
037     {
038       timeLeft--;
039       updateTimer();
040       if(timeLeft>0)
041       {
042         scheduleRelative(this1);
043         sound.play();
044       }
045       if(timeLeft>5)
046       {
047         sound2.play();
048       }
049     }
050   }
051 
052   private void makeSprites()
053   {
054     dot=new ImageSprite("Heart1.gif");
055     dot.setScale(0.1);
056     dot.setLocation(0.50.5);
057     dot.setColor(Color.RED);
058 
059     redDot=new ImageSprite("Heart1.gif");
060     redDot.setScale(0.1);
061     redDot.setLocation(
062         random.nextDouble(),
063         random.nextDouble());
064     redDot.setColor(Color.RED);
065 
066     blueDot=new ImageSprite("Smiley.jpg");
067     blueDot.setScale(0.1);
068     blueDot.setLocation(
069         random.nextDouble(),
070         random.nextDouble());
071     blueDot.setColor(Color.BLUE);
072 
073 
074     scoreSprite=new StringSprite("Score: "+score);
075     scoreSprite.setHeight(0.1);
076     scoreSprite.rightJustify();
077     scoreSprite.topJustify();
078     scoreSprite.setLocation(10);
079     timerSprite=new StringSprite("Timer: "+timeLeft);
080     timerSprite.leftJustify();
081     timerSprite.topJustify();
082     timerSprite.setHeight(0.1);
083     timerSprite.setLocation(00);
084 
085   }
086 
087   private void addSprites()
088   {
089     canvas.addSprite(dot);
090     canvas.addSprite(redDot);
091     canvas.addSprite(blueDot);
092     canvas.addSprite(scoreSprite);
093     canvas.addSprite(timerSprite);
094   }
095   private void updateTimer()
096   {
097     timerSprite.setText("Timer: "+timeLeft);
098   }
099 
100 
101   private void repositionRandomly(Sprite sprite)
102   {
103     sprite.setLocation(
104         random.nextDouble(),
105         random.nextDouble());
106   }
107 
108   private void updateScore()
109   {
110     scoreSprite.setText("Score: "+score);
111   }
112 
113   private void handleCollisions()
114   {
115     if(dot.intersects(blueDot))
116     {
117       repositionRandomly(blueDot);
118       if(dot.getColor().equals(Color.BLUE))
119       {
120         dot.setImage("Heart1.gif");
121         dot.setColor(Color.RED);
122         score++;
123       }
124       else
125       {
126         score--;
127       }
128       updateScore();
129 
130     }
131     if(dot.intersects(redDot))
132     {
133       repositionRandomly(redDot);
134       if(dot.getColor().equals(Color.RED))
135       {
136         dot.setImage("Smiley.jpg");
137         dot.setColor(Color.BLUE);
138         score++;
139       }
140       else
141       {
142         score--;
143       }
144       updateScore();
145 
146     }
147   }
148 
149 
150   public void advanceFrame(double timePassed)
151   {
152     if(timeLeft>0)
153     {
154       Point2D.Double mouse=
155           getPlayer().getMouse().getLocation();
156       dot.setLocation(mouse);
157       handleCollisions();
158     }
159   }
160 }


Download/View Stephanie/wackadot/Wackadot.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