Hemendra/lights/LightsOut

From ggc

Jump to: navigation, search

001 package Hemendra.lights;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  * All about my game here.
010  @author Hpullay
011  */
012 public class LightsOut extends GameLoop
013 {
014   /**makes block*/
015   private Sprite[][] blocks;
016   /**sets up the game*/
017   private LightsOutModel model;
018   /**makes ovals*/
019   private Sprite[][] oval;
020   /**the coordinates for x and y that the game uses for spacing */
021   private double startYCoord, startXCoord;
022 
023   public void startGame()
024   {
025     model=new LightsOutModel();
026     makeBlocks();
027     makeOval();
028     addSprites();
029     setHelpText
030     ("Make all the blue lights disappear to win game.");
031 
032   }
033 
034   /**Makes the blocks*/
035 
036   private void makeOval()
037   {
038     int index=0;
039     oval=new Sprite[5][5];
040     int numBlocksHigh=5;
041     double startLocationY=.17;
042     double endLocationY=1-startLocationY;
043     double distanceHigh=endLocationY+startLocationY;
044     for(int j=0; j<numBlocksHigh; j++)
045     {
046       double y=j*distanceHigh/(numBlocksHigh+1)+startLocationY;
047 
048       int numBlocksAcross=5;
049       double startLocationX=0.17;
050       double endLocationX=1-startLocationX;
051       double distanceAcross=endLocationX+startLocationX;
052       for(int i=0; i<numBlocksAcross; i++)
053       {
054         double x=i*distanceAcross/(numBlocksAcross+1)+startLocationX;
055         oval[i][j]=new OvalSprite(1,1);
056         oval[i][j].setScale(0.15);
057         oval[i][j].setLocation(x,y);
058         oval[i][j].setColor(Color.BLUE);
059         index++;
060         /*Makes the ovals to add to the game.*/
061       }
062     }
063     for(int j=0; j<5; j++)
064     {
065       for(int i=0; i<5; i++)
066       {
067         oval[j][i].setVisible(model.isOn(i, j));
068       }
069     }
070   }
071   private void makeBlocks()
072   {
073     int index=0;
074     blocks=new Sprite[5][5];
075     int numBlocksHigh=5;
076     double startLocationY=.17;
077     double endLocationY=1-startLocationY;
078     double distanceHigh=endLocationY+startLocationY;
079     for(int j=0; j<numBlocksHigh; j++)
080     {
081       double y=j*distanceHigh/(numBlocksHigh+1)+startLocationY;
082 
083       int numBlocksAcross=5;
084       double startLocationX=0.17;
085       double endLocationX=1-startLocationX;
086       double distanceAcross=endLocationX+startLocationX;
087       for(int i=0; i<numBlocksAcross; i++)
088       {
089         double x=i*distanceAcross/(numBlocksAcross+1)+startLocationX;
090         blocks[i][j]=new RectangleSprite(1,1);
091         blocks[i][j].setScale(0.15);
092         blocks[i][j].setLocation(x,y);
093         blocks[i][j].setColor(Color.RED);
094         index++;
095         /*Makes the blocks to add to the game.*/
096       }
097     }
098   }
099 
100   /**adds the sprites to the screen*/
101   private void addSprites()
102   {
103     for(int r=0; r<blocks.length; r++)
104     {
105       for(int c=0; c<blocks[0].length; c++)
106       {
107         canvas.addSprite(blocks[r][c]);;
108       }
109     }
110     /**Adds the blocks to the game.*/
111 
112     for(int r=0; r<oval.length; r++)
113     {
114       for(int c=0; c<oval[0].length; c++)
115       {
116         canvas.addSprite(oval[r][c]);;
117       }
118     }
119     /**Adds the ovals to the game.*/
120 
121   }
122   /**handle input and game events*/
123   public void advanceFrame(double timePassed)
124   {
125     Point2D.Double click=getPlayer().getMouse().getClickLocation();
126     if(click!=null)
127     {
128       int row=0;
129       int col=0;
130       if(click.x> && click.x <= .2)
131       {
132         col=0;
133       }
134       else if(click.x> .2 && click.x <= .4)
135       {
136         col=1;
137       }
138       else if(click.x> .4 && click.x <= .6)
139       {
140         col=2;
141       }
142       else if (click.x> .6 && click.x <= .8)
143       {
144         col=3;
145       }
146       else if (click.x> .8 && click.x <= 1)
147       {
148         col=4;
149       }
150       if(click.y> && click.y <= .2)
151       {
152         row=0;
153       }
154       else if(click.y> .2 && click.y <= .4)
155       {
156         row=1;
157       }
158       else if (click.y> .4 && click.y <= .6)
159       {
160         row=2;
161       }
162       else if(click.y> .6 && click.y <= .8)
163       {
164         row=3;
165       }
166       else if(click.y> .8 && click.y <= 1)
167       {
168         row=4;
169       }
170 
171       model.play(row,col);
172       for(int j=0; j<5; j++)
173       {
174         for(int i=0; i<5; i++)
175         {
176           oval[j][i].setVisible(model.isOn(i, j));
177         }
178       }
179 
180     }
181   }
182 
183 }


Download/View Hemendra/lights/LightsOut.java /**Had help from Pavitra and Blake*/





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