|
001 packagepackage is used to name the directory or folder a class is in intro;
002 //start auto-imports
003 importimport means to make the classes and/or packages available in this program java.util.*;
004 importimport means to make the classes and/or packages available in this program fang.*;
005 //end auto-imports
006
007 /**
008 * All about my game.
009 * @authorthis is the Javadoc tag for documenting who created the source code My Name Here
010 */
011 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 SpaceInvaders extendsextends means to customize or extend the functionality of a class Game
012 {open braces start code blocks and must be matched with a close brace
013 privateprivate is used to restrict access to the current class only ArrayList<Sprite> allEnemies;
014 privateprivate is used to restrict access to the current class only ProjectileTransformer enemyTransformer;
015 privateprivate is used to restrict access to the current class only ProjectileTransformer bulletTransformer;
016 privateprivate is used to restrict access to the current class only ArrayList<Sprite> heroBullet;
017 privateprivate is used to restrict access to the current class only ArrayList<Sprite> enemyBullet;
018 privateprivate is used to restrict access to the current class only StringSprite score;
019 privateprivate is used to restrict access to the current class only StringSprite lives;
020
021 privateprivate is used to restrict access to the current class only Sprite hero;
022
023 /**sets up the game*/
024 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
025 {open braces start code blocks and must be matched with a close brace
026 setScore(0);
027 heroBullet=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<Sprite>();
028 enemyBullet=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<Sprite>();
029
030 hero=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("me");
031 hero.setSize(0.1);
032 hero.setLocation(0.5, 0.9);
033 hero.setColor(getColor("yellow"));
034 addSprite(hero);
035
036 allEnemies=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<Sprite>();
037 intint is the type for whole numbers and it is short for integer enemiesAcross=this assignment operator makes the left side equal to the right side11;
038 intint is the type for whole numbers and it is short for integer enemiesDown=this assignment operator makes the left side equal to the right side4;
039 doubledouble is the type for numbers that can contain decimal fractions enemyWidth=this assignment operator makes the left side equal to the right side0.8;
040 enemyTransformer=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTransformer(0.05, 0);
041 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer j=this assignment operator makes the left side equal to the right side0; j<enemiesDown; j++this is the increment operator, which increases the variable by 1)
042 {open braces start code blocks and must be matched with a close brace
043 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side1; i<=this evaluates to true if the left side is not more than the right sideenemiesAcross; i++this is the increment operator, which increases the variable by 1)
044 {open braces start code blocks and must be matched with a close brace
045 RectangleSprite enemy=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(1, 1);
046 enemy.setSize(0.9/enemiesAcross);
047 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right side0.5/enemiesAcross+adds two numbers together or concatenates Strings together1.0/enemiesAcross*(i-1);
048 x=this assignment operator makes the left side equal to the right sidex*enemyWidth;
049 enemy.scale(enemyWidth);
050 doubledouble is the type for numbers that can contain decimal fractions y=this assignment operator makes the left side equal to the right side0.5/enemiesAcross+adds two numbers together or concatenates Strings together(j+adds two numbers together or concatenates Strings together0.0)/enemiesAcross;
051 y=this assignment operator makes the left side equal to the right sidey*enemyWidth;
052 enemy.setLocation(x, y);
053 addSprite(enemy);
054 enemy.addTransformer(enemyTransformer);
055 allEnemies.add(enemy);
056 }close braces end code blocks and must match an earlier open brace
057 }close braces end code blocks and must match an earlier open brace
058
059 score=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("0");
060 score.setScale(0.2);
061 score.setColor(getColor("white"));
062 score.setLocation(0.9, 0.1);
063 addSprite(score);
064
065 setLives(3);
066
067 lives=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite(""+adds two numbers together or concatenates Strings togethergetLives());
068 lives.setScale(0.2);
069 lives.setColor(getColor("white"));
070 lives.setLocation(0.1, 0.1);
071 addSprite(lives);
072 }close braces end code blocks and must match an earlier open brace
073
074 privateprivate is used to restrict access to the current class only Sprite getAvailableBullet(ArrayList<Sprite> bullets,
075 String name, doubledouble is the type for numbers that can contain decimal fractions direction)
076 {open braces start code blocks and must be matched with a close brace
077 forfor is a looping structure for repeatedly executing a block of code(Sprite single: bullets)
078 {open braces start code blocks and must be matched with a close brace
079 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.isVisible()==this is the comparison operator which evaluates to true if both sides are the samefalsefalse is a value for the boolean type and means not true)
080 {open braces start code blocks and must be matched with a close brace
081 returnreturn means to provide the result of the method and/or cease execution of the method immediately single;
082 }close braces end code blocks and must match an earlier open brace
083 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.getLocation().y<0)
084 {open braces start code blocks and must be matched with a close brace
085 returnreturn means to provide the result of the method and/or cease execution of the method immediately single;
086 }close braces end code blocks and must match an earlier open brace
087 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.getLocation().y>1)
088 {open braces start code blocks and must be matched with a close brace
089 returnreturn means to provide the result of the method and/or cease execution of the method immediately single;
090 }close braces end code blocks and must match an earlier open brace
091 }close braces end code blocks and must match an earlier open brace
092 Sprite bullet=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite(name);
093 bullet.setSize(0.05);
094 bullet.setVisible(falsefalse is a value for the boolean type and means not true);
095 bullet.setColor(getColor("white"));
096
097 bullets.add(bullet);
098
099 Spinner spinner=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Spinner(0);
100 spinner.setRotationDegrees(50);
101 bullet.addTransformer(spinner);
102
103 ProjectileTransformer bulletTransformer=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTransformer(0, direction);
104 bullet.addTransformer(bulletTransformer);
105
106 addSprite(bullet);
107
108 returnreturn means to provide the result of the method and/or cease execution of the method immediately bullet;
109 }close braces end code blocks and must match an earlier open brace
110
111 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value moveEnemiesDown()
112 {open braces start code blocks and must be matched with a close brace
113 forfor is a looping structure for repeatedly executing a block of code(Sprite single: allEnemies)
114 {open braces start code blocks and must be matched with a close brace
115 single.translate(0, 0.05);
116 }close braces end code blocks and must match an earlier open brace
117 }close braces end code blocks and must match an earlier open brace
118
119 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value moveEnemies()
120 {open braces start code blocks and must be matched with a close brace
121 forfor is a looping structure for repeatedly executing a block of code(Sprite single: allEnemies)
122 {open braces start code blocks and must be matched with a close brace
123 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.getLocation().x>1 &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) single.isVisible())
124 {open braces start code blocks and must be matched with a close brace
125 moveEnemiesDown();
126 enemyTransformer.setVector2D(-0.05, 0);
127 breakbreak terminates the loop immediately;
128 }close braces end code blocks and must match an earlier open brace
129 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.getLocation().x<0 &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) single.isVisible())
130 {open braces start code blocks and must be matched with a close brace
131 moveEnemiesDown();
132 enemyTransformer.setVector2D(0.05, 0);
133 breakbreak terminates the loop immediately;
134 }close braces end code blocks and must match an earlier open brace
135 }close braces end code blocks and must match an earlier open brace
136 }close braces end code blocks and must match an earlier open brace
137
138 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value moveHero()
139 {open braces start code blocks and must be matched with a close brace
140 hero.setX(getMouseX());
141 }close braces end code blocks and must match an earlier open brace
142
143 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value heroShoot()
144 {open braces start code blocks and must be matched with a close brace
145 ifif executes the next statement only if the condition in parenthesis evaluates to true(getClick2D()!=this is the not equals operator which evaluates to true if both sides are differentnullnull is the value used to refer to a non-existant object)
146 {open braces start code blocks and must be matched with a close brace
147 Sprite available=this assignment operator makes the left side equal to the right sidegetAvailableBullet(heroBullet, "Java", -0.2);
148 available.setLocation(hero.getX(), hero.getY()-hero.getSize()/2);
149 available.setVisible(truetrue is the boolean value that is the opposite of false);
150
151 }close braces end code blocks and must match an earlier open brace
152 }close braces end code blocks and must match an earlier open brace
153
154 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleBulletEnemyCollision()
155 {open braces start code blocks and must be matched with a close brace
156 forfor is a looping structure for repeatedly executing a block of code(Sprite single: allEnemies)
157 {open braces start code blocks and must be matched with a close brace
158 forfor is a looping structure for repeatedly executing a block of code(Sprite bullet: heroBullet)
159 {open braces start code blocks and must be matched with a close brace
160 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.isVisible() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) bullet.isVisible() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) single.intersects(bullet))
161 {open braces start code blocks and must be matched with a close brace
162 single.setVisible(falsefalse is a value for the boolean type and means not true);
163 bullet.setVisible(falsefalse is a value for the boolean type and means not true);
164 intint is the type for whole numbers and it is short for integer original=this assignment operator makes the left side equal to the right sidegetScore();
165 setScore(original+adds two numbers together or concatenates Strings together5);
166 Sprite available=this assignment operator makes the left side equal to the right sidegetAvailableBullet(enemyBullet, "bug", 0.1);
167 available.setLocation(single.getX(), single.getY()+adds two numbers together or concatenates Strings togethersingle.getSize()/2);
168 available.setVisible(truetrue is the boolean value that is the opposite of false);
169 }close braces end code blocks and must match an earlier open brace
170 }close braces end code blocks and must match an earlier open brace
171 }close braces end code blocks and must match an earlier open brace
172 }close braces end code blocks and must match an earlier open brace
173
174 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleBulletHeroCollision()
175 {open braces start code blocks and must be matched with a close brace
176 forfor is a looping structure for repeatedly executing a block of code(Sprite bullet: enemyBullet)
177 {open braces start code blocks and must be matched with a close brace
178 ifif executes the next statement only if the condition in parenthesis evaluates to true(hero.isVisible() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) bullet.isVisible() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) hero.intersects(bullet))
179 {open braces start code blocks and must be matched with a close brace
180 bullet.setVisible(falsefalse is a value for the boolean type and means not true);
181 intint is the type for whole numbers and it is short for integer original=this assignment operator makes the left side equal to the right sidegetLives();
182 setLives(original-1);
183 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives()==this is the comparison operator which evaluates to true if both sides are the same0)
184 hero.setVisible(falsefalse is a value for the boolean type and means not true);
185 }close braces end code blocks and must match an earlier open brace
186 }close braces end code blocks and must match an earlier open brace
187 }close braces end code blocks and must match an earlier open brace
188
189
190 /**handle input and game events*/
191 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
192 {open braces start code blocks and must be matched with a close brace
193 moveEnemies();
194 moveHero();
195 heroShoot();
196 handleBulletEnemyCollision();
197 handleBulletHeroCollision();
198 score.setText(""+adds two numbers together or concatenates Strings togethergetScore());
199 ifif executes the next statement only if the condition in parenthesis evaluates to true(rightPressed())
200 {open braces start code blocks and must be matched with a close brace
201 setLives(getLives()-1);
202 }close braces end code blocks and must match an earlier open brace
203 lives.setText(""+adds two numbers together or concatenates Strings togethergetLives());
204 }close braces end code blocks and must match an earlier open brace
205 }close braces end code blocks and must match an earlier open brace
|