ChrisGarrett/wackadot/Wackadot

From ggc

Jump to: navigation, search

001 package ChrisGarrett.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 Christopher Garrett
011  */
012 
013 public class Wackadot extends GameLoop
014 {
015         private Sprite dot3;
016   private Sprite dot;
017         private Sprite dot2;
018         private Sprite redDot;
019         private Sprite blueDot;
020         private StringSprite scoreSprite;
021         private int score;
022         private int timeLeft;
023         private StringSprite timerSprite;
024 
025 
026    public void startGame()
027    {
028        timeLeft=25;
029        score=0;
030        makeSprites();
031        addSprites();
032        scheduleRelative(new TimeUpdater()1);
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       }
044    }
045 }
046     
047    private void makeSprites()
048    {
049        Ellipse2D.Double circle=
050            new Ellipse2D.Double(0011);
051 
052        dot3=new Sprite(circle);
053        dot3.setScale(0.2);
054        dot3.setLocation(
055             random.nextDouble(),
056             random.nextDouble());
057        dot3.setColor(Color.ORANGE);
058 
059        dot2=new Sprite(circle);
060        dot2.setScale(0.3);
061        dot2.setLocation(
062             random.nextDouble(),
063             random.nextDouble());
064        dot2.setColor(Color.GREEN);
065 
066        dot=new Sprite(circle);
067        dot.setScale(0.1);
068        dot.setLocation(0.50.5);
069        dot.setColor(Color.RED);
070 
071        redDot=new OvalSprite(11);
072        redDot.setScale(0.1);
073        redDot.setLocation(
074             random.nextDouble(),
075             random.nextDouble());
076        redDot.setColor(Color.RED);
077 
078        blueDot=new OvalSprite(11);
079        blueDot.setScale(0.5);
080        blueDot.setLocation(
081             random.nextDouble(),
082             random.nextDouble());
083        blueDot.setColor(Color.BLUE);
084    scoreSprite=new StringSprite("Score: "+score);
085    scoreSprite.setHeight(0.1);
086    scoreSprite.rightJustify();
087    scoreSprite.topJustify();
088    scoreSprite.setLocation(10);
089    timerSprite=new StringSprite("Timer: "+timeLeft);
090    timerSprite.leftJustify();
091    timerSprite.topJustify();
092    timerSprite.setHeight(0.1);
093    timerSprite.setLocation(00);
094    }
095 
096     
097    private void addSprites()
098    {
099        canvas.addSprite(blueDot);
100        canvas.addSprite(dot);
101        canvas.addSprite(dot2);
102        canvas.addSprite(dot3);
103        canvas.addSprite(redDot);
104        canvas.addSprite(scoreSprite);
105        canvas.addSprite(timerSprite);
106    }
107 
108 private void updateTimer()
109 {
110    timerSprite.setText("Timer: "+timeLeft);
111 }
112 
113    public void advanceFrame(double timePassed)
114    {
115    if(timeLeft>0)
116    {
117    Point2D.Double mouse=
118        getPlayer().getMouse().getLocation();
119    dot.setLocation(mouse);
120    handleCollisions();
121      }
122     }
123    private void repositionRandomly(Sprite sprite)
124    {    
125    sprite.setLocation(
126        random.nextDouble()
127        random.nextDouble())
128     }
129 private void updateScore()
130 {
131    scoreSprite.setText("Score: "+score);
132 }
133 
134    private void handleCollisions()
135    {    
136      if(dot.intersects(blueDot))
137      {
138        repositionRandomly(blueDot);  
139            if(dot.getColor().equals(Color.BLUE))
140        {
141            dot.setColor(Color.GREEN);
142            score++;
143        }  
144       else
145       {
146          score--;
147       }
148       updateScore();
149      }    
150      if(dot.intersects(redDot))    
151      {
152        repositionRandomly(redDot)
153           if(dot.getColor().equals(Color.RED))
154        {
155            dot.setColor(Color.BLUE);
156            score++;
157        }
158      else
159       {
160          score--;
161       }
162       updateScore();
163      }
164      if(dot.intersects(dot2))
165      {
166        repositionRandomly(dot2);  
167            if(dot.getColor().equals(Color.GREEN))
168        {
169            dot.setColor(Color.ORANGE);
170            score++;
171        }  
172       else
173       {
174          score--;
175       }
176       updateScore();
177      }    
178  if(dot.intersects(dot3))
179      {
180        repositionRandomly(dot3);  
181            if(dot.getColor().equals(Color.ORANGE))
182        {
183            dot.setColor(Color.RED);
184            score++;
185        }  
186       else
187       {
188          score--;
189       }
190       updateScore();
191      }    
192  }
193 }
194 


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