cray/LightsGraphic

From ggc

Jump to: navigation, search

001 package cray;
002 
003 import cray.LightsModel.*;
004 import grid.*;
005 import wiki.Wiki;
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * All about my game here.
012  @author Cray
013  */
014 public class LightsGraphic extends GameLoop
015 {
016 
017   private Sprite grid;
018   private Sprite[][] dots;
019   private LightsModel lightsModel;
020   private StringSprite victory;
021   private Sound clip=new Sound("Pong.wav");
022   private Sound clip2=new Sound("Kidscheer.wav");
023   private ProjectileTracker tracker;
024 
025 
026   /**sets up the game*/
027   public void startGame()
028   {
029     lightsModel=new LightsModel();
030     makeSprites();
031     addSprites();
032     makeDots();
033   }
034 
035   /**makes the sprites*/
036   private void makeSprites()
037   {
038     grid=new Grid(550.05);
039     grid.setScale(1);
040     grid.setLocation(0.50.5);
041 
042     tracker=new ProjectileTracker(
043                 random.nextDouble()-0.5,
044                 random.nextDouble()-0.5);
045     tracker.setAngularVelocity(1);
046 
047     victory= new StringSprite("Epic \nWin!");
048     victory.setHeight(.25);
049     victory.setColor(Color.RED);
050     victory.setLocation(0.5,0.5);
051     victory.setFontFamilyName("Castellar");
052   }
053 
054   private void makeDots()
055   {
056     dots=new OvalSprite[5][5];
057     for (int a=0; a<5; a++)
058     {
059       for (int b=0; b<5; b++)
060       {
061         double x=(b+0.5)/5;
062         double y=(a+0.5)/5;
063         dots[a][b] =new OvalSprite (1,1);
064         dots[a][b].setScale((1.0/7)/1.2);
065         dots[a][b].setLocation(x, y);
066         dots[a][b].setColor(Color.GREEN);
067         canvas.addSprite(dots[a][b]);
068       }
069 
070     }
071   }
072   public Point getCell(Point2D.Double point)
073   {
074     int x=(int)Math.min(4, point.x*5);
075     int y=(int)Math.min(4, point.y*5);
076     return new Point(x, y);
077   }
078 
079   /**adds the sprites to the screen*/
080   private void addSprites()
081   {
082     canvas.addSprite(grid);
083   }
084 
085   /**handle input and game events*/
086   public void advanceFrame(double timePassed)
087   {
088     Point2D.Double leftClick=getPlayer().getMouse().getLeftClickLocation();
089     if(lightsModel.won()==false)
090     {
091       if(leftClick!=null)
092       {
093         Point cell=getCell(leftClick);
094         lightsModel.play(cell.y, cell.x);
095         clip.play();
096 
097       }
098       for(int r=0; r<dots.length; r++)
099       {
100         for(int c=0; c<dots[0].length; c++)
101         {
102           dots[r][c].setVisible(lightsModel.isOn(r, c));
103         }
104       }
105     }
106     else if(lightsModel.won()==true)
107     {
108       canvas.removeSprite(grid);
109       canvas.addSprite(victory);
110       victory.setTracker(tracker);
111       if(victory.getLocation().y>|| victory.getLocation().y<0)
112       {
113         Point2D.Double direction=tracker.getVelocity();
114         direction.y*=-1;
115         tracker.setVelocity(direction);
116         tracker.setAngularVelocity(-tracker.getAngularVelocity());
117       }
118       if(victory.getLocation().x>|| victory.getLocation().x<0)
119       {
120         Point2D.Double direction=tracker.getVelocity();
121         direction.x*=-1;
122         tracker.setVelocity(direction);
123         tracker.setAngularVelocity(-tracker.getAngularVelocity());
124       }
125 
126 
127     }
128   }
129 }


Download/View cray/LightsGraphic.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