Anam/assignment6prac

From ggc

Jump to: navigation, search

001 package Anam;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * This game allows you to interact with shapes and images.
009  * by Anam Mughal
010  */
011 public class assignment6prac extends Game
012 {
013   private StringSprite atitle;
014   private StringSprite aname;
015   private PolygonSprite shape1;
016   private ImageSprite belle1;
017   private StringSprite clickb;
018   private StringSprite clicko;
019   private OvalSprite shape4;
020   private StringSprite helping;
021         private Sound ring1=new Sound("Ring1.mp3");
022   
023 
024   /**sets up the game*/
025   public void setup()
026   {
027     /* This is the method body that tells the program
028     to do whatever is told in the private void below. 
029     This will post the sprites on the screen.*/
030     makeTexts();
031     makeShapes();
032     makeBelle();
033     makeHelpbox();
034 ring1();
035     
036   }
037 /*This adds sound to the game*/
038 public void ring1()
039 {
040    playSoundImmediately();
041    ring1.setVolume(0.8);
042    ring1.loop();
043 }
044 
045   private void makeHelpbox()
046   {
047     /* This makes the help box.
048     When the user clicks help, a help box pops up 
049     for additional help & details about the game.
050     The text in purple is what will appear in 
051     the help box*/
052 
053     String helpbox=
054         "Click on Belle's image and it will start blinking.<br>"+
055                     "Press letter 'a' key and the instructions will disappear.<br>"+
056                     "Then click on the oval twice and it will change color after 10 seconds.<br>"+
057                     "Click Sound for sound effects.<br>";
058     setHelpText(helpbox);
059   }
060   private void makeTexts()
061   {
062     /* This makes all the Sprites that include instructions and texts.
063     However, without the method body above, the sprites will not
064     appear on the screen, regardless of the addSprite in the following 
065     code. For example, all of the sprites below are instructed
066     to be added to the screen, but if it is not told to makeTexts 
067     in the method body, sprites will not appear on the screen*/
068 
069     /* This makes the title*/
070     atitle=new StringSprite("Interactive Shapes");
071     atitle.setSize(0.60);
072     atitle.setColor(Palette.getColor("Peachpuff"));
073     atitle.setLocation(0.50.06);
074     addSprite(atitle);
075 
076     /*This makes the instructions for help box*/
077     helping=new StringSprite("Click Help for additional help");
078     helping.setSize(0.35);
079     helping.setColor(Palette.getColor("Light Green"));
080     helping.setLocation(0.200.98);
081     addSprite(helping);
082 
083     /* This makes the author's name*/
084     aname=new StringSprite("Anam Mughal");
085     aname.setSize(0.30);
086     aname.setColor(Palette.getColor("SILVER"));
087     aname.setLocation(0.820.97);
088     addSprite(aname);
089 
090     /*This makes the instructions to
091     click on belle and the oval*/
092     clickb=new StringSprite("Click on the Image");
093     clickb.setSize(0.20);
094     clickb.setLocation(0.380.42);
095     clickb.setColor(Palette.getColor("Light Blue"));
096     addSprite(clickb);
097     clickb.setVisible(true);
098 
099     clicko=new StringSprite("First Press 'a' then Click Me");
100     clicko.setSize(0.35);
101     clicko.setLocation(0.500.60);
102     clicko.setColor(Color.BLACK);
103     clicko.setVisible(true);
104   }
105   private void makeBelle()
106   {
107     /* Makes the image (Belle), but the image will
108     not appear on the screen until instructed as makeBelle
109     in the method body.*/
110 
111     belle1=new ImageSprite("Belle.jpg");
112     belle1.setSize(0.18);
113     belle1.setLocation(0.380.30);
114     addSprite(belle1);
115   }
116   private void makeShapes()
117   {
118     /* Makes all the shapes, but has to be
119     instructed in the method body as makeShapes
120     for it to appear on the screen.*/
121 
122     /*Makes the Pentagon*/
123     shape1=new PolygonSprite(5);
124     shape1.setSize(0.22);
125     shape1.setLocation(0.150.30);
126     shape1.setColor(Palette.getColor("Sandy Brown"));
127     addSprite(shape1);
128 
129     /*Makes the Oval*/
130     shape4=new OvalSprite(21);
131     shape4.setSize(0.40);
132     shape4.setLocation(0.500.60);
133     shape4.setColor(Palette.getColor("SILVER"));
134     addSprite(shape4);
135     addSprite(clicko);
136 
137     /* This attaches the spinning/rotating
138      to the shape it is assigned to, 
139     in this case to shape1(which is a polygon). 
140     This does not need to be instructed in the method 
141     body since this is already part of the class
142     makeShapes*/
143 
144     Spinner spins;
145     spins=new Spinner(0);
146     spins.setRotationDegrees(175);
147 
148     shape1.addTransformer(spins);
149 
150     /* this attaches the translation
151     to shape1.*/
152     OutlineTransformer moving;
153     moving=new OutlineTransformer(0.400.150.300.30.90.70.50.80.100.150.30);
154     moving.setLooping(true);
155     shape1.addTransformer(moving);
156     shape1.setLocation(moving.getCurrentPoint());
157   }
158 
159 
160   class Blinker extends TimedAction
161   {
162     /* This makes the image of belle
163     blink when clicked*/
164     public void act()
165     {
166       boolean vis=belle1.isVisible();
167       belle1.setVisible(!vis);
168       schedule(this.5);
169     }
170   }
171   class DelayedInter extends TimedAction
172   {
173     /*This makes the oval change
174     color*/
175     public void act()
176     {
177       shape4.setColor(getColor("purple"));
178     }
179   }
180 
181 
182   /**handle input and game events*/
183 
184   private void interaction()
185   {
186     /*This makes the "click on the image"
187     disappear and start the image to blink
188     when clicked*/
189     if(getClick2D()!=null && belle1.intersects(getClick2D()))
190     {
191       schedule (new Blinker()0.2);
192       clickb.setVisible(false);
193     }
194 
195   }
196   private void changeColor()
197   {
198     /*This makes the oval change color
199     after 10 seconds when it is clicked*/
200     if(getClick2D()!=null && shape4.intersects(getClick2D()))
201     {
202       schedule(new DelayedInter()10);
203     }
204   }
205   public void advance()
206   {
207     /* This makes the instructions
208     "first press 'a' then click me"
209     disappear when 'a' is pressed.*/
210     if(getKeyPressed()== 'a')
211     {
212       clicko.setVisible(false);
213     }
214     {
215       changeColor();
216       interaction();
217     }
218   }
219 }


Download/View Anam/assignment6prac.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