patternjavaMinor
"Destroy the Asteroid" Game
Viewed 0 times
asteroidgamethedestroy
Problem
I made this game for my computer science class.
Here is my professor's requirements:
continuous stream of torpedoes generate when the space bar is held
down, you can use the PApplet’s keyReleased method instead of the
keyPressed method.
around when they cross one of the window’s boundaries.
easier.) When the asteroids are destroyed, create a Particle System
centered at the center of the asteroid to make it look like there was
an explosion.
What kind of improvement should I make?
Main:
```
import java.util.ArrayList;
import java.util.Iterator;
import processing.core.PApplet;
public class spaceMain extends PApplet {
ArrayList TorpedoesList = new ArrayList();
ArrayList AsteroidsList = new ArrayList();
ArrayList theList = new ArrayList();
ArrayList smallList = new ArrayList();
float width = 1000;
float height = 600;
float angle;
float numAsteroids = 10;
float rXPos;
float rYPos;
int score;
boolean sAlive = true;
boolean gameIsOver = false;
SpaceShip theSpaceSh
Here is my professor's requirements:
- Write a Processing program that draws a triangle in the middle of the window.
- Rotate the triangle when the left and right arrow keys are pressed using the translate and rotate methods.
- When the spacebar is hit, create a torpedo that appears to shoot out one of the vertices of the triangle. Note: if you do not want a
continuous stream of torpedoes generate when the space bar is held
down, you can use the PApplet’s keyReleased method instead of the
keyPressed method.
- Create some number of large ellipses to represent asteroids. Have the asteroids move in random directions around the window, wrapping
around when they cross one of the window’s boundaries.
- Have the torpedoes destroy the asteroids when they collide. (Using an ellipse for the torpedo will probably make the collision detection
easier.) When the asteroids are destroyed, create a Particle System
centered at the center of the asteroid to make it look like there was
an explosion.
- Keep track of points based on the number of asteroids destroyed and display the total points in the window.
- When an asteroid collides with the spaceship, destroy the spaceship and create a Particle System at the center of the colliding ship.
- Include a button to restart the game when the spaceship is destroyed.
What kind of improvement should I make?
Main:
```
import java.util.ArrayList;
import java.util.Iterator;
import processing.core.PApplet;
public class spaceMain extends PApplet {
ArrayList TorpedoesList = new ArrayList();
ArrayList AsteroidsList = new ArrayList();
ArrayList theList = new ArrayList();
ArrayList smallList = new ArrayList();
float width = 1000;
float height = 600;
float angle;
float numAsteroids = 10;
float rXPos;
float rYPos;
int score;
boolean sAlive = true;
boolean gameIsOver = false;
SpaceShip theSpaceSh
Solution
Formatting & look:
Use proper access modifiers
All you methods and fields use the default package level visibility. Decreasing the scope of items to the lowest possible scope is recommended for clearer code. This is as simple of marking you methods and fields private or public where needed.
Start field/variable names with a lowercase letter
In java, fields should start with a lowercase letter
Marking fields final when they are write once
By marking fields final, you cover the meaning that they should be edited using the methods only, as opposite to reinitiating them.
Class names should start with a uppercase
A common practise is to start class names with uppercase letters
Logic issues
Manual clearing of Collection
The above can be made smaller using:
Updating in the draw loop
You are updating your entities in your main draw loop, this will cause jitter in the movement of the entities as they are affected by the main fps.
Don't use boxed objects if unboxed exist
When using boolean with a uppercase letter, you are boxing the results, this means that a object is used instead a primitive. Using objects is slower than primitives, and should be avoided.
Distances should be compared using the squared version
Doing
Use proper access modifiers
All you methods and fields use the default package level visibility. Decreasing the scope of items to the lowest possible scope is recommended for clearer code. This is as simple of marking you methods and fields private or public where needed.
Start field/variable names with a lowercase letter
ArrayList TorpedoesList = new ArrayList();
ArrayList AsteroidsList = new ArrayList();
...
Iterator Titerator = TorpedoesList.iterator();
...
Iterator Aiterator = AsteroidsList.iterator();In java, fields should start with a lowercase letter
ArrayList torpedoesList = new ArrayList();
ArrayList asteroidsList = new ArrayList();
...
Iterator tIterator = torpedoesList.iterator();
...
Iterator aIterator = asteroidsList.iterator();Marking fields final when they are write once
ArrayList TorpedoesList = new ArrayList();
ArrayList AsteroidsList = new ArrayList();
ArrayList theList = new ArrayList();
ArrayList smallList = new ArrayList();
...
ArrayList particlesList = new ArrayList();By marking fields final, you cover the meaning that they should be edited using the methods only, as opposite to reinitiating them.
Class names should start with a uppercase
public class spaceMain extends PApplet {
...
PApplet.main("spaceMain");A common practise is to start class names with uppercase letters
Logic issues
Manual clearing of Collection
Iterator Asiterator = AsteroidsList.iterator();
while(Asiterator.hasNext()){
Asiterator.next();
Asiterator.remove();
}//end while
Iterator Toiterator = TorpedoesList.iterator();
while(Toiterator.hasNext()){
Toiterator.next();
Toiterator.remove();
}//end whileThe above can be made smaller using:
AsteroidsList.clear();
TorpedoesList.clear();Updating in the draw loop
You are updating your entities in your main draw loop, this will cause jitter in the movement of the entities as they are affected by the main fps.
Don't use boxed objects if unboxed exist
Boolean collided = false;When using boolean with a uppercase letter, you are boxing the results, this means that a object is used instead a primitive. Using objects is slower than primitives, and should be avoided.
Distances should be compared using the squared version
if(distance(theAsteroid.getXPos(), theAsteroid.getYPos(), width/2, height/2) <= theAsteroid.getLength() ){
...
if(distance(theAsteroid.getXPos(), theAsteroid.getYPos(), theTorpedo.getXPos(), theTorpedo.getYPos()) <= 50){Doing
number * number is a cheap operation, doing sqrt(number) is a heavy operation. Avoid sqrt in favor of pow.Code Snippets
ArrayList<Torpedoes> TorpedoesList = new ArrayList<Torpedoes>();
ArrayList<Asteroids> AsteroidsList = new ArrayList<Asteroids>();
...
Iterator<Torpedoes> Titerator = TorpedoesList.iterator();
...
Iterator<Asteroids> Aiterator = AsteroidsList.iterator();ArrayList<Torpedoes> torpedoesList = new ArrayList<Torpedoes>();
ArrayList<Asteroids> asteroidsList = new ArrayList<Asteroids>();
...
Iterator<Torpedoes> tIterator = torpedoesList.iterator();
...
Iterator<Asteroids> aIterator = asteroidsList.iterator();ArrayList<Torpedoes> TorpedoesList = new ArrayList<Torpedoes>();
ArrayList<Asteroids> AsteroidsList = new ArrayList<Asteroids>();
ArrayList<ParticleSystems> theList = new ArrayList<ParticleSystems>();
ArrayList<Asteroids> smallList = new ArrayList<Asteroids>();
...
ArrayList<Particles> particlesList = new ArrayList<Particles>();public class spaceMain extends PApplet {
...
PApplet.main("spaceMain");Iterator<Asteroids> Asiterator = AsteroidsList.iterator();
while(Asiterator.hasNext()){
Asiterator.next();
Asiterator.remove();
}//end while
Iterator<Torpedoes> Toiterator = TorpedoesList.iterator();
while(Toiterator.hasNext()){
Toiterator.next();
Toiterator.remove();
}//end whileContext
StackExchange Code Review Q#123491, answer score: 3
Revisions (0)
No revisions yet.