RobertKing/wackadot/Wackadot

From ggc

Jump to: navigation, search

001 package RobertKing.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  * Fang Engine
010  @author Rking
011  */
012 public class Wackadot extends GameLoop
013 {
014   private Sprite dot;
015   private Sprite redDot;
016   private Sprite blueDot;
017   private StringSprite scoreSprite;
018   private int score;
019   private int timeLeft;
020   private StringSprite timerSprite;
021 
022   public void startGame()
023   {
024     score=0;
025     timeLeft=10;
026     makeSprites();
027     addSprites();
028     scheduleRelative(new TimeUpdater()1);
029     setHelpText("Move the dot to its same color without colliding with the opposite color");
030   }
031 
032   class TimeUpdater implements Alarm
033   {
034     public void alarm()
035     {
036       timeLeft--;
037       updateTimer();
038       if(timeLeft>0)
039       {
040         scheduleRelative(this1);
041       }
042     }
043   }
044   private void makeSprites()
045   {
046     Ellipse2D.Double circle=
047         new Ellipse2D.Double(0011);
048 
049     dot=new Sprite(circle);
050     dot.setScale(0.1);
051     dot.setLocation(0.50.5);
052     dot.setColor(Color.RED);
053 
054     redDot=new OvalSprite(11);
055     redDot.setScale(0.1);
056     redDot.setLocation(
057         random.nextDouble(),
058         random.nextDouble());
059     redDot.setColor(Color.RED);
060 
061     blueDot=new OvalSprite(11);
062     blueDot.setScale(0.1);
063     blueDot.setLocation(
064         random.nextDouble(),
065         random.nextDouble());
066     blueDot.setColor(Color.BLUE);
067 
068     scoreSprite=new StringSprite("Score: "+score);
069     scoreSprite.setHeight(0.1);
070     scoreSprite.rightJustify();
071     scoreSprite.topJustify();
072     scoreSprite.setLocation(10);
073 
074     timerSprite=new StringSprite("Timer: "+timeLeft);
075     timerSprite.leftJustify();
076     timerSprite.topJustify();
077     timerSprite.setHeight(0.1);
078     timerSprite.setLocation(00);
079   }
080 
081   private void updateScore()
082   {
083     scoreSprite.setText("Score: "+score);
084   }
085 
086   private void addSprites()
087   {
088     canvas.addSprite(dot);
089     canvas.addSprite(redDot);
090     canvas.addSprite(blueDot);
091     canvas.addSprite(scoreSprite);
092     canvas.addSprite(timerSprite);
093   }
094 
095   private void updateTimer()
096   {
097     timerSprite.setText("Timer: "+timeLeft);
098   }
099 
100   private void repositionRandomly(Sprite sprite)
101   {
102     sprite.setLocation(
103         random.nextDouble(),
104         random.nextDouble());
105   }
106 
107   private void handleCollisions()
108   {
109     if(dot.intersects(blueDot))
110     {
111       repositionRandomly(blueDot);
112       if(dot.getColor().equals(Color.BLUE))
113       {
114         dot.setColor(Color.RED);
115         score++;
116       }
117       else
118       {
119         score--;
120       }
121       updateScore();
122     }
123 
124     if(dot.intersects(redDot))
125     {
126       repositionRandomly(redDot);
127       if(dot.getColor().equals(Color.RED))
128       {
129         dot.setColor(Color.BLUE);
130         score++;
131       }
132       else
133       {
134         score--;
135       }
136       updateScore();
137     }
138   }
139 
140   public void advanceFrame(double timePassed)
141   {
142     if(timeLeft>0)
143     {
144       Point2D.Double mouse=
145           getPlayer().getMouse().getLocation();
146       dot.setLocation(mouse);
147       handleCollisions();
148     }
149   }
150 }


Download/View RobertKing/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