WilliamAnderson/LightsGame

From ggc

Jump to: navigation, search

Contents

Something Important I learned:

  • Apparently underscores in the package name messes up the way the engine handles the model file. I literally spent two hours trying to figure out why my code wouldn't work with my model file correctly before I figured this out.

Lights Out


001 package WilliamAnderson;
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 William Anderson
011  */
012 public class LightsGame extends GameLoop
013 {
014   private LightsOutModel model;
015 
016   private Sprite[][] Light = new Sprite[5][5];
017   private Sprite[][] tiles = new Sprite[5][5];
018   private StringSprite youWin;
019 
020   public void startGame()
021   {
022     model=new LightsOutModel();
023     makeSprites();
024     addSprites();
025     youWin();
026     setHelpText("Click once to randomize the order of the lights, Any clicks after the first one will alternate a light and its surrounding lights between the off and on states. White squares indicate the light is on. The goal of the game is to turn off all of the lights.");
027 
028 
029   }
030 
031   private void makeSprites()
032   {
033     for (int = 0; i < tiles.length; i++)
034     {
035       for (int = 0; o < tiles.length; o++)
036       {
037         tiles[i][o] = new RectangleSprite(11);
038         tiles[i][o].setScale(.17);
039         tiles[i][o].setLocation(.1 + .2 * i, .2 * o + .1);
040         tiles[i][o].setColor(Color.BLACK);
041         canvas.addSprite(tiles[i][o]);
042         Light[i][o] = new RectangleSprite(11);
043         Light[i][o].setScale(.17);
044         Light[i][o].setLocation(.1 + .2 * i, .2 * o + .1);
045         Light[i][o].setColor(Color.WHITE);
046         canvas.addSprite(Light[i][o]);
047       }
048     }
049 
050   }
051 
052 
053 
054 
055 
056 
057 
058   private void addSprites()
059   {}
060 
061   private void youWin()
062   {
063     youWin=new StringSprite("You Win");
064     youWin.setHeight(0.1);
065     youWin.setLocation(.5.5);
066     youWin.setVisible(false);
067   }
068 
069 
070 
071   public void advanceFrame(double timePassed)
072   {
073     Point2D.Double click=getPlayer().getMouse().getClickLocation();
074     if(click!=null)
075     {
076       int row=0;
077       int col=0;
078       if(click.x> && click.x <= .2)
079       {
080         col=0;
081       }
082       else if(click.x> .2 && click.x <= .4)
083       {
084         col=1;
085       }
086       else if(click.x> .4 && click.x <= .6)
087       {
088         col=2;
089       }
090       else if (click.x> .6 && click.x <= .8)
091       {
092         col=3;
093       }
094       else if (click.x> .8 && click.x <= 1)
095       {
096         col=4;
097       }
098       if(click.y> && click.y <= .2)
099       {
100         row=0;
101       }
102       else if(click.y> .2 && click.y <= .4)
103       {
104         row=1;
105       }
106       else if (click.y> .4 && click.y <= .6)
107       {
108         row=2;
109       }
110       else if(click.y> .6 && click.y <= .8)
111       {
112         row=3;
113       }
114       else if(click.y> .8 && click.y <= 1)
115       {
116         row=4;
117       }
118 
119       model.play(row,col);
120       for(int o=0; o<5; o++)
121       {
122         for(int i=0; i<5; i++)
123         {
124 
125           youWin.setVisible(model.won());
126           Light[i][o].setVisible(model.isOn(o, i));
127         }
128       }
129 
130     }
131 
132   }
133 
134 
135 }
136 
137 


Download/View WilliamAnderson/LightsGame.java

Pages related to this Game

Credits

I received help from the following:





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