alyday/LightsOut

From ggc

Jump to: navigation, search

001 package alyday;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 import grid.*;
008 
009 /**
010  * All about my game here.
011  @author Alyday    
012  */
013 public class LightsOut extends GameLoop
014 {
015   /**an oval*/
016   private Sprite space;
017   private ImageSprite[][] oval=new ImageSprite[5][5];
018   private RectangleSprite background;
019   private LightsOutModel lightsOut;
020   private StringSprite fin;
021 
022   /**sets up the game*/
023   public void startGame()
024   {
025     lightsOut=new LightsOutModel();
026     makeSprites();
027     addSprites();
028   }
029 
030   /**makes the sprites*/
031   private void makeSprites()
032   {
033     for (int a=0; a<5; a++)
034     {
035       for (int b=0; b<5; b++)
036       {
037         double x=(b+0.5)/5;
038         double y=(a+0.5)/5;
039         oval[a][b] =new ImageSprite ("Hamster.jpg");
040         oval[a][b].setScale((1.0/5)/1.2);
041         oval[a][b].setLocation(x, y);
042       }
043     }
044     space=new Grid(550.05);
045     space.setScale(1.0);
046     space.setLocation(0.50.5);
047     space.setColor(Color.YELLOW);
048 
049     background=new RectangleSprite (2,1);
050     background.setScale(2.0);
051     background.setLocation(0.50.5);
052 
053     fin=new StringSprite ("You Won!");
054     fin.setScale(1.0);
055     fin.setLocation(0.50.5);
056     fin.setColor(new Color(180,0,0));
057     fin.setFontFamilyName("Bauhaus 93");
058 
059   }
060 
061   /**adds the sprites to the screen*/
062   private void addSprites()
063   {
064     canvas.addSprite(background);
065     canvas.addSprite(space);
066     for (int a=0; a<5; a++)
067     {
068       for (int b=0; b<5; b++)
069       {
070         canvas.addSprite(oval[a][b]);
071       }
072     }
073 
074   }
075 
076   public Point getCell(Point2D.Double point)
077   {
078     int x=(int)Math.min(4, point.x*5);
079     int y=(int)Math.min(4, point.y*5);
080     return new Point(x, y);
081   }
082 
083   /**handle input and game events*/
084   public void advanceFrame(double timePassed)
085   {
086     if(getPlayer().getMouse().getClickLocation()!=null)
087     {
088       Point cell=getCell(getPlayer().getMouse().getClickLocation());
089       lightsOut.play(cell.y, cell.x);
090     }
091     for(int r=0; r<oval.length; r++)
092     {
093       for(int c=0; c<oval[0].length; c++)
094       {
095         oval[r][c].setVisible(lightsOut.isOn(r, c));
096       }
097     }
098     if (lightsOut.won()==true)
099     {
100       canvas.removeSprite(space);
101       canvas.removeSprite(background);
102       canvas.addSprite(fin);
103     }
104 
105   }
106 }


Download/View alyday/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