Schwarz/Lightsout

From ggc

Jump to: navigation, search

001 package Schwarz;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import fang2.core.*;
007 import fang2.sprites.*;
008 import fang2.transformers.*;
009 import fang2.attributes.*;
010 import java.awt.*;
011 import java.awt.geom.*;
012 
013 /**
014  * This is the Lights out game where the objective is to get the lights to all go out.
015  * much of this code was from help in the help sesions.
016  @author Drew Schwarz
017  */
018 
019 public class Lightsout extends Game
020 {
021   private Sprite [][] totalBricks;
022   private Sprite [][] lights;
023   private int numberOfRows = 5;
024   private int numberOfColumns = 5;
025   private RectangleSprite brick;
026   private OvalSprite lightbulb;
027   private int row = 0, column = 0;
028   private StringSprite win;
029   
030   /**sets up the game*/
031   public void setup()
032   {
033     removeAllSprites();
034     createBricks();
035     createLights();
036     randomeClicks();
037     
038     String helpText =
039         "Hello this page teaches you the rulls of the game.<br>" +
040         "The objective is get all the lights to turn off.<br>" +
041         "Each click however changes not only the clicked light but also the ones around it in a + shape.<br>" +
042         "Select r to restart the game.";
043     setHelpTexthelpText );
044     
045   }
046   
047   /** creates the grid for the lights on the game bord space invaders game code*/
048   private void createBricks()
049   {
050     totalBricks = new RectangleSprite [ ][ ];
051     
052     for int = 0; i < numberOfRows; i++ )
053     {
054       for int = 0; j < numberOfColumns; j++ )
055       {
056         brick = new RectangleSprite1);
057         totalBricks[ ][ ] = brick;
058         brick.setColorgetColor"Blue" ) );
059         brick.setSize0.9 / numberOfColumns );
060         double = .5 / numberOfColumns + + 0.0 / numberOfColumns;
061         double = .5 / numberOfRows + + 0.0 / numberOfRows;
062         brick.setLocationx, y );
063         brick.setVisibletrue );
064         addSpritebrick );
065       }
066     }
067   }
068   
069   /** Creates the lightbulbs in the game space invaders game code*/
070   private void createLights()
071   {
072     lights = new OvalSprite[ ][ ];
073     
074     for int = 0; i < numberOfRows; i++ )
075     {
076       for int = 0; j < numberOfColumns; j++ )
077       {
078         lightbulb = new OvalSprite1);
079         lights[ ][ ] = lightbulb;
080         lightbulb.setColorgetColor"white" ) );
081         lightbulb.setSize0.9 / numberOfColumns );
082         double = .5 / numberOfColumns + + 0.0 / numberOfColumns;
083         double = .5 / numberOfRows + + 0.0 / numberOfRows;
084         lightbulb.setLocationx, y );
085         lightbulb.setVisiblefalse );
086         addSpritelightbulb );
087       }
088     }
089   }
090   
091   /**Roher helped me out by sugesting this; keeps track of wether or not thier is a click
092   *then changes the the lightbulb to true or flase depending on its position also combined with Joseph Argumedo
093   *code to help get this to work*/
094   public void checkClick()
095   {
096     Point2D.Double click = getPlayer().getMouse().getClickLocation();
097     
098     if click != null )
099     {
100       for int = 0; i < lights.length; i++ )
101       {
102         for int = 0; j < lights.length; j++ )
103         {
104         
105           if lights[ ][ ].intersectsclick ) )
106           
107           {
108             row = 10 * i + j 10;
109             column = 10 * i + j % 10;
110             changeLightsrow, column );
111             lightRowChangerow, column );
112             lightColumnChangerow, column );
113           }
114         }
115       }
116     }
117   }
118   
119   /**changes the lights when you click from false to true or visaversa*/
120   private void changeLightsint row, int column )
121   {
122     if lights[ row ][ column ].isVisible() && lights[ row ][ column ].intersectsgetClick2D() ) )
123     {
124       lights[ row ][ column ].setVisiblefalse );
125     }
126     
127     else if lights[ row ][ column ].isVisible() == false && lights[ row ][ column ].intersectsgetClick2D() ) )
128     {
129       lights[ row ][ column ].setVisibletrue );
130     }
131   }
132   
133   /**changes the light above and below the secelcted lightbulb had help from rohers program to start this this
134   * then modified it to make it so that it would not click outside the row's length noticed Joseph Argumedo used
135   if statments to do his so i borowd this idea*/
136   private void lightRowChangeint row, int column )
137   {
138     if row != )
139     {
140       lights[ row + ][ column ].setVisible!lights[ row + ][ column ].isVisible() );
141     }
142     
143     if row != )
144     {
145       lights[ row - ][ column ].setVisible!lights[ row - ][ column ].isVisible() );
146     }
147   }
148   
149   /**changes the lights to the left and right of the selected lightbulb had help from rohers program to start this this
150   * then modified it to make it so that it would not click outside the row's lengthnoticed Joseph Argumedo used
151   if statments to do his so i borowd this idea*/
152   public void lightColumnChangeint row, int column )
153   {
154     if column != )
155     {
156       lights[ row ][ column + ].setVisible!lights[ row ][ column + ].isVisible() );
157     }
158     
159     if column != )
160     {
161       lights[ row ][ column - ].setVisible!lights[ row ][ column - ].isVisible() );
162     }
163   }
164   
165   /**Randomly clicks to turn on lights within the game*/
166   public void randomeClicks()
167   {
168     for int = 0; r < 5; r++ )
169     {
170       row = int ) ( Math.random() );
171       column = int ) ( Math.random() );
172       lightColumnChangerow, column );
173       lightRowChangerow, column );
174     }
175   }
176   
177   /**handle input and game events*/
178   public void advance()
179   {
180     checkClick();
181     
182     if getKeyPressed() == 'r' )
183     {
184       removeAllSprites();
185       setup();
186     }
187   }
188 }


Download/View Schwarz/Lightsout.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