jay/Whackadot

From ggc

Jump to: navigation, search

001 package jay;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * My version of Whackadot!
009  @author Jay
010  */
011 public class Whackadot extends Game
012 {
013   private Sprite dot;
014   private Sprite redDot;
015   private Sprite blueDot;
016   private StringSprite scoreSprite;
017   private int score;
018   private int timeLeft;
019   private ImageSprite sprite;
020   private StringSprite timerSprite;
021 
022   public void setup()
023   {
024     score=0;
025     timeLeft=10;
026     makeSprites();
027     addSprites();
028     schedule(new TimeUpdater()1);
029     setHelpText("Lots of help text here later");
030   }
031   class TimeUpdater extends TimedAction
032   {
033     public void act()
034     {
035       timeLeft--;
036       updateTimer();
037       if(timeLeft>0)
038       {
039         schedule(this1);
040       }
041     }
042   }
043   private void makeSprites()
044   {
045     dot=new OvalSprite(11);
046     dot.setScale(0.1);
047     dot.setLocation(0.50.5);
048     dot.setColor(Color.RED);
049   }
050   {
051     redDot=new OvalSprite(11);
052     redDot.setScale(0.1);
053     redDot.setLocation(
054         randomDouble(),
055         randomDouble());
056     redDot.setColor(getColor("red"));
057 
058     blueDot=new OvalSprite(11);
059     blueDot.setScale(0.1);
060     blueDot.setLocation(
061         randomDouble(),
062         randomDouble());
063     blueDot.setColor(getColor("blue"));
064 
065     scoreSprite=new StringSprite("Score: 0");
066     scoreSprite.setHeight(0.1);
067     scoreSprite.rightJustify();
068     scoreSprite.topJustify();
069     scoreSprite.setLocation(10);
070     timerSprite=new StringSprite("Timer: "+timeLeft);
071     timerSprite.leftJustify();
072     timerSprite.topJustify();
073     timerSprite.setHeight(0.1);
074     timerSprite.setLocation(00);
075     sprite=new ImageSprite("HNI_0011.JPG");
076     sprite.setScale(1.0);
077     sprite.setLocation(0.5,0.5);
078 
079     addSprite(sprite);
080   }
081   private void addSprites()
082   {
083     addSprite(dot);
084     addSprite(redDot);
085     addSprite(blueDot);
086     addSprite(scoreSprite);
087     addSprite(timerSprite);
088   }
089 
090   private void updateTimer()
091   {
092     timerSprite.setText("Timer: "+timeLeft);
093   }
094   private void repositionRandomly(Sprite sprite)
095   {
096     sprite.setLocation(
097         randomDouble(),
098         randomDouble());
099   }
100   private void updateScore()
101   {
102     scoreSprite.setText("Score: "+score);
103   }
104 
105   private void handleCollisions()
106   {
107     if(dot.intersects(blueDot))
108     {
109       repositionRandomly(blueDot);
110       if(dot.getColor().equals(getColor("blue")))
111       {
112         dot.setColor(getColor("red"));
113         score++;
114       }
115       else
116       {
117         score--;
118       }
119       updateScore();
120 
121     }
122     if(dot.intersects(redDot))
123     {
124       repositionRandomly(redDot);
125       if(dot.getColor().equals(getColor("red")))
126       {
127         dot.setColor(getColor("blue"));
128         score++;
129       }
130       else
131       {
132         score--;
133       }
134       updateScore();
135 
136     }
137   }
138 
139   public void advance()
140   {
141     if(timeLeft>0)
142     {
143       Location2D mouse=
144           getMouse2D();
145       dot.setLocation(mouse);
146       handleCollisions();
147     }
148   }
149 
150 
151 }


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