ChrisHull/LightOutGame

From ggc

Jump to: navigation, search

001 package ChrisHull;
002 import fang.*;
003 import java.awt.*;
004 import java.awt.geom.*;
005 
006 /**
007  * LightsOut.
008  @author   Chull
009  
010  
011  */
012 
013 public class LightOutGame extends GameLoop
014 {
015   /**the coordinates for x and y that the game uses for spacing */
016   private double startYCoord, startXCoord;
017   /**an  block*/
018   private Sprite[][] block;
019   /**sets up the game*/
020   private StringSprite youWin;
021   private LightsOutModel model;
022   public void startGame()
023   {
024 
025     model=new LightsOutModel();
026     makeBricks();
027     addSprites();
028     youWin();
029     canvas.addSprite(youWin);
030   }
031   private void youWin()
032   {
033     youWin=new StringSprite("You Win");
034     youWin.setHeight(0.1);
035     youWin.setLocation(.5.5);
036     youWin.setVisible(false);
037   }
038 
039   /**Makes the bricks*/
040   private void makeBricks()
041   {
042 
043     int index=0;
044     block=new Sprite[5][5] ;
045     int numBricksHigh=5;
046     double startLocationY=0.25;
047     double endLocationY=3.25-startLocationY;
048     double distanceHigh=endLocationY-startLocationY;
049     for(int j=0; j<numBricksHigh; j++)
050     {
051       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
052       int numBricksAcross=5;
053       double startLocationX=0.1;
054       double endLocationX=1-startLocationX;
055       double distanceAcross=endLocationX-startLocationX;
056       for(int i=0; i<numBricksAcross; i++)
057 
058       {
059         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
060         block[i][j]=new RectangleSprite(21);
061         block[i][j].setScale(0.15);
062         block[i][j].setLocation(x,y/3.5);
063         block[i][j].setColor(Color.RED);
064 
065         index++;
066       }
067     }
068     for(int j=0; j<5; j++)
069     {
070       for(int i=0; i<5; i++)
071       {
072 
073 
074         block[j][i].setVisible(model.isOn(i, j));
075       }
076     }
077   }
078 
079 
080   /**adds the sprites to the screen*/
081   private void addSprites()
082   {
083     for(int r=0; r<block.length; r++)
084     {
085       for(int c=0; c<block[0].length; c++)
086       {
087         canvas.addSprite(block[r][c]);;
088       }
089     }
090 
091   }
092   /**handle input and game events*/
093   public void advanceFrame(double timePassed)
094   {
095 
096     Point2D.Double click=getPlayer().getMouse().getClickLocation();
097     if(click!=null)
098     {
099       int row=0;
100       int col=0;
101       if(click.x> && click.x <= .2)
102       {
103         col=0;
104       }
105       else if(click.x> .2 && click.x <= .4)
106       {
107         col=1;
108       }
109       else if(click.x> .4 && click.x <= .6)
110       {
111         col=2;
112       }
113       else if (click.x> .6 && click.x <= .8)
114       {
115         col=3;
116       }
117       else if (click.x> .8 && click.x <= 1)
118       {
119         col=4;
120       }
121       if(click.y> && click.y <= .2)
122       {
123         row=0;
124       }
125       else if(click.y> .2 && click.y <= .4)
126       {
127         row=1;
128       }
129       else if (click.y> .4 && click.y <= .6)
130       {
131         row=2;
132       }
133       else if(click.y> .6 && click.y <= .8)
134       {
135         row=3;
136       }
137       else if(click.y> .8 && click.y <= 1)
138       {
139         row=4;
140       }
141 
142       model.play(row,col);
143       for(int j=0; j<5; j++)
144       {
145         for(int i=0; i<5; i++)
146         {
147 
148           youWin.setVisible(model.won());
149           block[j][i].setVisible(model.isOn(i, j));
150         }
151       }
152 
153     }
154 
155   }
156 
157 }


Download/View ChrisHull/LightOutGame.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