|
001 packagepackage is used to name the directory or folder a class is in cray.wackadot;
002
003 importimport means to make the classes and/or packages available in this program wiki.Wiki;
004 importimport means to make the classes and/or packages available in this program fang2.*;
005 importimport means to make the classes and/or packages available in this program java.awt.*;
006 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
007
008 /**
009 * All about my game here.
010 * @authorthis is the Javadoc tag for documenting who created the source code Cray
011 */
012
013
014 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects Wackadot extendsextends means to customize or extend the functionality of a class GameLoop
015
016
017
018 {open braces start code blocks and must be matched with a close brace
019 //private Sprite dot;
020 privateprivate is used to restrict access to the current class only ImageSprite dot;
021 privateprivate is used to restrict access to the current class only ImageSprite redDot;
022 privateprivate is used to restrict access to the current class only ImageSprite blueDot;
023 privateprivate is used to restrict access to the current class only StringSprite scoreSprite;
024 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer score;
025 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer timeLeft;
026 privateprivate is used to restrict access to the current class only StringSprite timerSprite;
027 privateprivate is used to restrict access to the current class only Sound clip=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Sound("resoures/R2.mp3");
028 privateprivate is used to restrict access to the current class only Sound clip2=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Sound("Doh.wav");
029 privateprivate is used to restrict access to the current class only Sound clip3=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Sound("Coin.wav");
030 privateprivate is used to restrict access to the current class only Sound clip4=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Sound("Airhorn.wav");
031
032
033
034
035
036
037
038 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startGame()
039 {open braces start code blocks and must be matched with a close brace
040 score=this assignment operator makes the left side equal to the right side0;
041 timeLeft=this assignment operator makes the left side equal to the right side30;
042 makeSprites();
043 addSprites();
044 scheduleRelative(newnew is used to create objects by calling the constructor TimeUpdater(), 1);
045 setHelpText("The monkey wants to eat the banana, and the banana wants to be eaten by the monkey! When you are the monkey, you must touch the banana, when you are the banana, you must touch the monkey! If the monkey gets the banana or visa versa, then you get a point! However, if You touch the banana with the banana or the monkey with the monkey, you lose a point! You have 30 seconds. ENJOY! Click 'Start' to begin. Click 'Sounds' to toggle sounds on/off.");
046 //clip=new Sound("resources/DingLower.wav");
047
048
049 }close braces end code blocks and must match an earlier open brace
050
051 classclass is a group of fields and methods used for making objects TimeUpdater implementsimplements means providing method bodies for the methods declared in the corresponding interface Alarm
052
053 {open braces start code blocks and must be matched with a close brace
054 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value alarm()
055 {open braces start code blocks and must be matched with a close brace
056 timeLeft--this is the decrement operator, which decreases the variable by 1;
057 updateTimer();
058 ifif executes the next statement only if the condition in parenthesis evaluates to true(timeLeft>0)
059 {open braces start code blocks and must be matched with a close brace
060 scheduleRelative(thisthis means the current object (the implicit parameter), 1);
061
062 }close braces end code blocks and must match an earlier open brace
063 elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (timeLeft<1)
064 {open braces start code blocks and must be matched with a close brace
065 clip4.play();
066 }close braces end code blocks and must match an earlier open brace
067 }close braces end code blocks and must match an earlier open brace
068 }close braces end code blocks and must match an earlier open brace
069
070
071 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeSprites()
072 {open braces start code blocks and must be matched with a close brace
073 //dot=new OvalSprite(1, 1);
074 dot=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ImageSprite("resources/Banana.gif");
075 dot.setScale(0.17);
076 dot.setLocation(0.5, 0.5);
077 dot.setColor(Color.RED);
078
079 //redDot=new OvalSprite(1, 1);
080 redDot=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ImageSprite("Monkey.gif");
081 redDot.setScale(0.15);
082 redDot.setLocation(
083 random.nextDouble(),
084 random.nextDouble());
085 redDot.setColor(Color.RED);
086
087 //blueDot=new OvalSprite(1, 1);
088 blueDot=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ImageSprite("Banana.gif");
089 blueDot.setScale(0.15);
090 blueDot.setLocation(
091 random.nextDouble(),
092 random.nextDouble());
093 blueDot.setColor(Color.BLUE);
094
095 scoreSprite=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Score: "+adds two numbers together or concatenates Strings togetherscore);
096 scoreSprite.setHeight(0.1);
097 scoreSprite.rightJustify();
098 scoreSprite.topJustify();
099 scoreSprite.setLocation(1, 0);
100 scoreSprite.setColor(Color.YELLOW);
101
102 timerSprite=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Timer: "+adds two numbers together or concatenates Strings togethertimeLeft);
103 timerSprite.leftJustify();
104 timerSprite.topJustify();
105 timerSprite.setHeight(0.1);
106 timerSprite.setLocation(0, 0);
107 timerSprite.setColor(Color.YELLOW);
108 }close braces end code blocks and must match an earlier open brace
109
110 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addSprites()
111 {open braces start code blocks and must be matched with a close brace
112 canvas.addSprite(dot);
113 canvas.addSprite(redDot);
114 canvas.addSprite(blueDot);
115 canvas.addSprite(scoreSprite);
116 canvas.addSprite(timerSprite);
117
118
119 }close braces end code blocks and must match an earlier open brace
120 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateTimer()
121 {open braces start code blocks and must be matched with a close brace
122 timerSprite.setText("Timer: "+adds two numbers together or concatenates Strings togethertimeLeft);
123
124 }close braces end code blocks and must match an earlier open brace
125
126
127 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value repositionRandomly(Sprite sprite)
128 {open braces start code blocks and must be matched with a close brace
129 sprite.setLocation(
130 random.nextDouble(),
131 random.nextDouble());
132 }close braces end code blocks and must match an earlier open brace
133
134 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateScore()
135 {open braces start code blocks and must be matched with a close brace
136 scoreSprite.setText("Score: "+adds two numbers together or concatenates Strings togetherscore);
137 }close braces end code blocks and must match an earlier open brace
138
139 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleCollisions()
140 {open braces start code blocks and must be matched with a close brace
141 ifif executes the next statement only if the condition in parenthesis evaluates to true(dot.intersects(blueDot))
142 {open braces start code blocks and must be matched with a close brace
143 repositionRandomly(blueDot);
144
145 ifif executes the next statement only if the condition in parenthesis evaluates to true(dot.getColor().equals(Color.BLUE))
146 {open braces start code blocks and must be matched with a close brace
147 dot.setColor(Color.RED);
148 dot.setImage("Banana.gif");
149 clip3.play();
150 score++this is the increment operator, which increases the variable by 1;
151
152
153
154 }close braces end code blocks and must match an earlier open brace
155 elseelse is what happens when the if condition is false
156 {open braces start code blocks and must be matched with a close brace
157 clip2.play();
158 score--this is the decrement operator, which decreases the variable by 1;
159
160 }close braces end code blocks and must match an earlier open brace
161 updateScore();
162 }close braces end code blocks and must match an earlier open brace
163
164 ifif executes the next statement only if the condition in parenthesis evaluates to true(dot.intersects(redDot))
165 {open braces start code blocks and must be matched with a close brace
166 repositionRandomly(redDot);
167 ifif executes the next statement only if the condition in parenthesis evaluates to true(dot.getColor().equals(Color.RED))
168 {open braces start code blocks and must be matched with a close brace
169 dot.setColor(Color.BLUE);
170 dot.setImage("Monkey.gif");
171 clip3.play();
172 score++this is the increment operator, which increases the variable by 1;
173 }close braces end code blocks and must match an earlier open brace
174 elseelse is what happens when the if condition is false
175 {open braces start code blocks and must be matched with a close brace
176 clip2.play();
177 score--this is the decrement operator, which decreases the variable by 1;
178 }close braces end code blocks and must match an earlier open brace
179 updateScore();
180 }close braces end code blocks and must match an earlier open brace
181
182 }close braces end code blocks and must match an earlier open brace
183
184
185
186 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advanceFrame(doubledouble is the type for numbers that can contain decimal fractions timePassed)
187 {open braces start code blocks and must be matched with a close brace
188 ifif executes the next statement only if the condition in parenthesis evaluates to true(timeLeft>0)
189 {open braces start code blocks and must be matched with a close brace
190
191 Point2D.Double mouse=this assignment operator makes the left side equal to the right side
192 getPlayer().getMouse().getLocation();
193 dot.setLocation(mouse);
194 handleCollisions();
195 //clip.setSoundPosition(getPlayer().getMouse().getLocation().x);
196 clip.setPan(getPlayer().getMouse().getLocation().x);
197 }close braces end code blocks and must match an earlier open brace
198
199 }close braces end code blocks and must match an earlier open brace
200
201 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args)
202 {open braces start code blocks and must be matched with a close brace
203 newnew is used to create objects by calling the constructor Wackadot().runAsApplication();
204 }close braces end code blocks and must match an earlier open brace
205
206 }close braces end code blocks and must match an earlier open brace
207
|