Wilson/lightsOutGUI

From ggc

Jump to: navigation, search

01 package Wilson;
02 
03 import java.util.*;
04 import fang.*;
05 import java.awt.*;
06 
07 /**
08  * All about my game.
09  @author Wilson Green
10  */
11 public class lightsOutGUI extends Game
12 {
13   RectangleSprite singleBox;
14   RectangleSprite[][] box = new RectangleSprite[10][10];
15   int x;
16   /**This calls the display() method to set up all of the squares*/
17   public void setup()
18   {
19     display();
20   }
21   /**
22   *This method sets up all the squares in an array, it has two for loops for rows and colums
23   *Also randomizes a number between 1 and 2 and if its 1 then set the block to be blue, otherwise white
24   */
25   private void display()
26   {
27     for(int i=0; i<10; i++)
28     {
29       for(int l=0; l<10; l++)
30       {
31         x=(int)(Math.random()*3);
32         singleBox = new RectangleSprite(1,1);
33         singleBox.setSize(.098);
34         if(x==1)
35           singleBox.setColor(getColor("BLUE"));
36         else
37           singleBox.setColor(getColor("WHITE"));
38         singleBox.setLocation((i+.5)/10,(l+.5)/10);
39         box[i][l]=singleBox;
40         addSprite(singleBox);
41       }
42     }
43   }
44   /**
45   *This method makes it so you can click on a box and change its color and the boxes up,down,left, and right of it
46   *When wanting to change the color it calls the invertBlock() method
47   */
48   private void clickBox()
49   {
50     for(int j=0; j<10; j++)
51     {
52       for(int k=0; k<10; k++)
53       {
54         if(getClick2D() != null && getClick2D().intersects(box[j][k]))
55         {
56           invertBlock(box[j][k]);
57           if (j->=0)
58             invertBlock(box[j-1][k]);
59           if (j+<= 9)
60             invertBlock(box[j+1][k]);
61           if (k->= 0)
62             invertBlock(box[j][k-1]);
63           if (k+<= 9)
64             invertBlock(box[j][k+1]);
65         }
66       }
67     }
68   }
69   /**
70   *This method inverts the RectangleSprite that you tell it to
71   *@param block - this is the rectangle sprite that you want to invert
72   */
73   private void invertBlock(RectangleSprite block)
74   {
75     if(getColorName(block.getColor()) == getColorName(getColor("BLUE")))
76       block.setColor(getColor("WHITE"));
77     else
78       block.setColor(getColor("BLUE"));
79 
80   }
81 
82   /**handle input and game events
83   *Calls the clickBox() method to change the color of the blocks.
84   */
85   public void advance()
86   {
87     clickBox();
88   }
89 }


Download/View Wilson/lightsOutGUI.javat auto-imports import java.util.*; //end auto-imports

import fang.*; import java.awt.*; import java.awt.geom.*;

/**

* All about my game.
* @author Wilson
*/

public class lightsOutGUI extends Game { LightsOutModel model= new LightsOutModel(); RectangleSprite singleBox; RectangleSprite[][] box = new RectangleSprite[5][5]; /**sets up the game*/ public void setup() { display(); }

private void display() { for(int i=0; i<5; i++) { for(int l=0; l<5; l++) { singleBox = new RectangleSprite(1,1); singleBox.setSize(.19); singleBox.setColor(getColor("BLUE")); singleBox.setLocation((i+.5)/5,(l+.5)/5); box[i][l]=singleBox; addSprite(singleBox); } } }

private void clickBox() { for(int j=0; j<5; j++) { for(int k=0; k<5; k++) { if(getClick2D() != null && getClick2D().intersects(box[j][k])) { invertBlock(box[j][k]); } } } }

private void invertBlock(RectangleSprite block) { if(getColorName(block.getColor()) == getColorName(getColor("BLUE"))) block.setColor(getColor("WHITE")); else block.setColor(getColor("BLUE")); }

/**handle input and game events*/ public void advance() { clickBox(); } }





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