diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 860ab71..e5acc5a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,7 +6,17 @@
-
+
+
+
+
+
+
+
+
+
+
+
@@ -27,21 +37,15 @@
-
+
-
-
+
+
-
-
-
-
-
-
-
-
+
+
@@ -61,7 +65,7 @@
-
+
@@ -73,7 +77,7 @@
-
+
@@ -85,7 +89,7 @@
-
+
@@ -97,12 +101,8 @@
-
-
-
-
-
-
+
+
@@ -119,14 +119,12 @@
-
+
-
-
-
-
-
+
+
+
@@ -135,7 +133,7 @@
-
+
@@ -152,6 +150,9 @@
+
+
+
-
+
-
+
@@ -203,7 +204,7 @@
-
+
@@ -310,8 +311,8 @@
+
-
@@ -604,7 +605,7 @@
-
+
@@ -621,14 +622,14 @@
-
+
-
+
@@ -647,7 +648,7 @@
file://$PROJECT_DIR$/src/Engine/GameEngine.java
- 270
+ 371
@@ -661,20 +662,42 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
@@ -721,11 +744,7 @@
-
-
-
-
-
+
@@ -743,9 +762,7 @@
-
-
-
+
@@ -765,14 +782,8 @@
-
-
-
-
-
-
-
-
+
+
@@ -819,11 +830,7 @@
-
-
-
-
-
+
@@ -841,9 +848,7 @@
-
-
-
+
@@ -863,14 +868,8 @@
-
-
-
-
-
-
-
-
+
+
@@ -917,11 +916,7 @@
-
-
-
-
-
+
@@ -939,9 +934,7 @@
-
-
-
+
@@ -961,14 +954,8 @@
-
-
-
-
-
-
-
-
+
+
@@ -1015,11 +1002,7 @@
-
-
-
-
-
+
@@ -1027,9 +1010,7 @@
-
-
-
+
@@ -1060,7 +1041,7 @@
-
+
@@ -1075,7 +1056,7 @@
-
+
@@ -1085,12 +1066,8 @@
-
-
-
-
-
-
+
+
@@ -1114,7 +1091,7 @@
-
+
@@ -1124,7 +1101,7 @@
-
+
@@ -1139,31 +1116,23 @@
-
+
-
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
diff --git a/out/artifacts/Astroids2D_jar/Astroids2D.jar b/out/artifacts/Astroids2D_jar/Astroids2D.jar
index bdad286..bfa565c 100644
Binary files a/out/artifacts/Astroids2D_jar/Astroids2D.jar and b/out/artifacts/Astroids2D_jar/Astroids2D.jar differ
diff --git a/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class b/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class
index 8e136a8..92eb2c2 100644
Binary files a/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class and b/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class differ
diff --git a/out/production/Astroids2D/Engine/GameEngine$countDownListener.class b/out/production/Astroids2D/Engine/GameEngine$countDownListener.class
index ad92904..1620bec 100644
Binary files a/out/production/Astroids2D/Engine/GameEngine$countDownListener.class and b/out/production/Astroids2D/Engine/GameEngine$countDownListener.class differ
diff --git a/out/production/Astroids2D/Engine/GameEngine$fireTimerListener.class b/out/production/Astroids2D/Engine/GameEngine$fireTimerListener.class
index 08ae85f..c2fe2d0 100644
Binary files a/out/production/Astroids2D/Engine/GameEngine$fireTimerListener.class and b/out/production/Astroids2D/Engine/GameEngine$fireTimerListener.class differ
diff --git a/out/production/Astroids2D/Engine/GameEngine.class b/out/production/Astroids2D/Engine/GameEngine.class
index e9a8b55..5a3e6b6 100644
Binary files a/out/production/Astroids2D/Engine/GameEngine.class and b/out/production/Astroids2D/Engine/GameEngine.class differ
diff --git a/out/production/Astroids2D/GamePanels/GamePanel.class b/out/production/Astroids2D/GamePanels/GamePanel.class
index 2ece5e0..966937d 100644
Binary files a/out/production/Astroids2D/GamePanels/GamePanel.class and b/out/production/Astroids2D/GamePanels/GamePanel.class differ
diff --git a/out/production/Astroids2D/Sprites/Astroid.class b/out/production/Astroids2D/Sprites/Astroid.class
index 7d4903d..ac1d59f 100644
Binary files a/out/production/Astroids2D/Sprites/Astroid.class and b/out/production/Astroids2D/Sprites/Astroid.class differ
diff --git a/out/production/Astroids2D/Sprites/Fog.class b/out/production/Astroids2D/Sprites/Fog.class
index 4ced187..a63158d 100644
Binary files a/out/production/Astroids2D/Sprites/Fog.class and b/out/production/Astroids2D/Sprites/Fog.class differ
diff --git a/out/production/Astroids2D/Sprites/LaserBlast.class b/out/production/Astroids2D/Sprites/LaserBlast.class
index 0812556..fb5f862 100644
Binary files a/out/production/Astroids2D/Sprites/LaserBlast.class and b/out/production/Astroids2D/Sprites/LaserBlast.class differ
diff --git a/out/production/Astroids2D/Sprites/Ship.class b/out/production/Astroids2D/Sprites/Ship.class
index e37eaf9..bf742f4 100644
Binary files a/out/production/Astroids2D/Sprites/Ship.class and b/out/production/Astroids2D/Sprites/Ship.class differ
diff --git a/out/production/Astroids2D/Sprites/Sprite.class b/out/production/Astroids2D/Sprites/Sprite.class
index 2f695d3..0cb774f 100644
Binary files a/out/production/Astroids2D/Sprites/Sprite.class and b/out/production/Astroids2D/Sprites/Sprite.class differ
diff --git a/src/Engine/GameEngine.java b/src/Engine/GameEngine.java
index 6d2e378..a3a343d 100644
--- a/src/Engine/GameEngine.java
+++ b/src/Engine/GameEngine.java
@@ -22,298 +22,377 @@ import java.util.Vector;
public class GameEngine implements KeyListener {
- private final int LARGE=100, MEDIUM=50, SMALL=25, NUM_OF_LIVES = 3, LASER_SPEED = 10, ASTROID_SPEED = 3;
- private final int STOP=0, UP=1, DOWN=-1;
- public boolean gameOn,fireLock, gameOver, isFirstGame;
- private Ship player;
- private Vector astroidsSprites,laserSprites;
- private int pWidth, pHeight;
- private Timer astroidTimer, fireTimer, startTime;
- private Random r;
- private int score, countDown;
- private Vector lives, fogs;
- private AudioClip laserAudioClip;
- private BufferedImage sceneImage;
+ private final int LARGE=100, MEDIUM=50, SMALL=25, NUM_OF_LIVES = 3, LASER_SPEED = 10, ASTROID_SPEED = 3, FOG_COUNT = 4, MINI_SHIP_SIZE = 30;
+ private final int STOP=0, UP=1, DOWN=-1; //Movement directions
+ public boolean gameOn,fireLock, gameOver, isFirstGame;
+ private Ship player;
+ private Vector astroidsSprites,laserSprites; //will save all laser shots and asteroids which are currently on the screen.
+ private int pWidth, pHeight; //panel dimensions
+ private Timer astroidTimer, fireTimer, startTime;
+ private Random r;
+ private int score, countDown;
+ private Vector lives, fogs;
+ private AudioClip laserAudioClip;
+ private BufferedImage sceneImage;
- private AudioClip themeAudioClip;
- private final URL laserURL= getClass().getResource("/Sounds/laser.wav");
- private final URL themeURL= getClass().getResource("/Sounds/theme.wav");
+ private AudioClip themeAudioClip;
+ private final URL laserURL= getClass().getResource("/Sounds/laser.wav");
+ private final URL themeURL= getClass().getResource("/Sounds/theme.wav");
- public GameEngine(int width, int height){
- this.isFirstGame = true;
- this.gameOver = true;
- this.pWidth = width;
- this.pHeight = height;
- try {
- laserAudioClip = Applet.newAudioClip(laserURL);
- themeAudioClip = Applet.newAudioClip(themeURL);
- }catch (Exception e){
- laserAudioClip = null;
- themeAudioClip = null;
- }
- if(themeAudioClip != null)
- themeAudioClip.loop();
- fogs = new Vector<>();
- r = new Random();
- sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
- initFogs();
- startNewGame();
- }
+ public GameEngine(int width, int height){
- private void initFogs(){
- for(int i = 0 ; i < 4 ; i++){
- fogs.add(new Fog((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,1,"Fog.png",r.nextInt(360),pWidth));
- }
- }
+ //initialize variables and load audio\image files.
+ this.isFirstGame = true;
+ this.gameOver = true;
+ this.pWidth = width;
+ this.pHeight = height;
+ try {
+ laserAudioClip = Applet.newAudioClip(laserURL);
+ themeAudioClip = Applet.newAudioClip(themeURL);
+ }catch (Exception e){
+ laserAudioClip = null;
+ themeAudioClip = null;
+ }
+ if(themeAudioClip != null)
+ themeAudioClip.loop();
+ fogs = new Vector<>();
+ r = new Random();
+ sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
- private void startNewGame(){
- score = 0;
- lives = new Vector<>();
- setupLives(NUM_OF_LIVES);
- astroidTimer = new Timer(5000, new astroidTimerListener()) ;
- fireTimer = new Timer(500, new fireTimerListener()) ;
- startTime = new Timer(1000, new countDownListener());
- fogs.remove(fogs.size()-1);
- initGame();
- }
+ initFogs(); //create fog sprites moving on screen.
+ startNewGame();
+ }
- private void initGame(){
- this.countDown = 3;
- astroidsSprites = new Vector<>();
- laserSprites = new Vector<>();
- this.player = new Ship(pWidth/2, pHeight/2,pWidth,pHeight, MEDIUM);
- gameOn = true;
- fireTimer.start();
- astroidTimer.start();
+ /**
+ * creates FOG_COUNT amount of fog sprites to roam the screen. (currently set to 4)
+ */
+ private void initFogs(){
+ for(int i = 0 ; i < FOG_COUNT ; i++){
+ fogs.add(new Fog((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,1,"Fog.png",r.nextInt(360),pWidth));
+ }
+ }
- for (int i =0 ; i<4 ; i++)
- createAstroid();
- }
+ /**
+ * initialize and reset vars and timers to "new game" configuration.
+ */
+ private void startNewGame(){
+ score = 0;
+ lives = new Vector<>();
+ setupLives(NUM_OF_LIVES);
+ astroidTimer = new Timer(5000, new astroidTimerListener()) ;
+ fireTimer = new Timer(500, new fireTimerListener()) ;
+ startTime = new Timer(1000, new countDownListener());
+ fogs.remove(fogs.size()-1);
+ initGame();
+ }
- private void setupLives(int num){
- int size = 30;
- for(int i = 0 ; i < num ; i++){
- lives.add(new Ship(pWidth - ((i+1)*size), 15, pWidth, pHeight, size));
- }
- }
+ /**
+ * clears asteroid and laser arrays, creates new asteroids, starts timers and repositions ship in center of screen.
+ */
+ private void initGame(){
+ this.countDown = 3;
+ astroidsSprites = new Vector<>();
+ laserSprites = new Vector<>();
+ this.player = new Ship(pWidth/2, pHeight/2,pWidth,pHeight, MEDIUM);
+ gameOn = true;
+ fireTimer.start();
+ astroidTimer.start();
- public int getScore(){
- return score;
- }
+ for (int i =0 ; i<4 ; i++)
+ createAstroid();
+ }
- public boolean isGameOver(){
- return this.gameOver;
- }
+ /**
+ * Creates requested amount of ships to "Lives" array (Mini ships to be presented at corner of the screen).
+ * @param num
+ * number of wanted "Lives".
+ */
+ private void setupLives(int num){
+ for(int i = 0 ; i < num ; i++){
+ lives.add(new Ship(pWidth - ((i+1)*MINI_SHIP_SIZE), 15, pWidth, pHeight, MINI_SHIP_SIZE));
+ }
+ }
- public int getCountDown(){
- return countDown;
- }
+ /**
+ * returns score
+ * @return
+ * int
+ */
+ public int getScore(){
+ return score;
+ }
- private void createAstroid(){
- astroidsSprites.add(new Astroid((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,ASTROID_SPEED,"astroid.png",r.nextInt(360),LARGE));
- }
+ /**
+ * returns gameOver flag
+ * @return
+ * boolean
+ */
+ public boolean isGameOver(){
+ return this.gameOver;
+ }
- @Override
- public void keyTyped(KeyEvent keyEvent) {
- }
+ /**
+ * returns countDown
+ * @return
+ * int
+ */
+ public int getCountDown(){
+ return countDown;
+ }
- @Override
- public void keyPressed(KeyEvent keyEvent) {
- if(gameOn) {
- switch (keyEvent.getKeyCode()) {
- case KeyEvent.VK_UP:
- player.setDirection(UP);
- break;
- case KeyEvent.VK_DOWN:
- player.setDirection(DOWN);
- break;
- case KeyEvent.VK_LEFT:
- player.turnShip(DOWN);
- break;
- case KeyEvent.VK_RIGHT:
- player.turnShip(UP);
- break;
- case KeyEvent.VK_SPACE:
- if (!fireLock && countDown <= 0) {
- if(laserAudioClip != null)
- laserAudioClip.loop();
- laserSprites.add(new LaserBlast((int) player.getLocX() + (player.getImageWidth() / 2),
- (int) player.getLocY() + (player.getImageHeight() / 2),
- pWidth,
- pHeight,
- LASER_SPEED,
- "laser2.gif",
- player.getAngle(),
- SMALL));
- fireLock = true;
- }
+ /**
+ * Create a new asteroid on a random position.
+ */
+ private void createAstroid(){
+ astroidsSprites.add(new Astroid((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,ASTROID_SPEED,"astroid.png",r.nextInt(360),LARGE));
+ }
- break;
- case KeyEvent.VK_F2:
- this.isFirstGame = false;
- gameOver = false;
- startNewGame();
- startTime.start();
- break;
+ @Override
+ public void keyTyped(KeyEvent keyEvent) {}
- default:
- break;
- }
- }
- }
+ @Override
+ public void keyPressed(KeyEvent keyEvent) {
+ if(gameOn) {
+ switch (keyEvent.getKeyCode()) {
+ case KeyEvent.VK_UP:
+ player.setDirection(UP);
+ break;
+ case KeyEvent.VK_DOWN:
+ player.setDirection(DOWN);
+ break;
+ case KeyEvent.VK_LEFT:
+ player.turnShip(DOWN); //Left
+ break;
+ case KeyEvent.VK_RIGHT:
+ player.turnShip(UP); //Right
+ break;
+ case KeyEvent.VK_SPACE: //Fire
+ if (!fireLock && countDown <= 0) {
+ if(laserAudioClip != null)
+ laserAudioClip.play(); //Play audio file
+ //create a new laser from ship towards ship direction.
+ laserSprites.add(new LaserBlast((int) player.getLocX() + (player.getImageWidth() / 2),
+ (int) player.getLocY() + (player.getImageHeight() / 2),
+ pWidth,
+ pHeight,
+ LASER_SPEED,
+ "laser2.gif",
+ player.getAngle(),
+ SMALL));
+ fireLock = true; //lock for delay in fire speed
+ }
- @Override
- public void keyReleased(KeyEvent keyEvent) {
- if(gameOn){
- switch (keyEvent.getKeyCode()){
- case KeyEvent.VK_UP:
- case KeyEvent.VK_DOWN:
- player.setDirection(STOP);
- break;
- case KeyEvent.VK_RIGHT:
- case KeyEvent.VK_LEFT:
- player.turnShip(STOP);
- break;
- case KeyEvent.VK_SPACE:
- laserAudioClip.stop();
- break;
- default:
- break;
+ break;
+ case KeyEvent.VK_F2: //new game
+ this.isFirstGame = false;
+ gameOver = false;
+ startNewGame();
+ startTime.start();
+ break;
- }
- }
+ default:
+ break;
+ }
+ }
+ }
+
+ @Override
+ public void keyReleased(KeyEvent keyEvent) {
+ if(gameOn){
+ //stop all movement activity
+ switch (keyEvent.getKeyCode()){
+ case KeyEvent.VK_UP:
+ case KeyEvent.VK_DOWN:
+ player.setDirection(STOP);
+ break;
+ case KeyEvent.VK_RIGHT:
+ case KeyEvent.VK_LEFT:
+ player.turnShip(STOP);
+ break;
+// case KeyEvent.VK_SPACE:
+// laserAudioClip.stop();
+// break;
+ default:
+ break;
+
+ }
+ }
- }
+ }
+ /**
+ * Update all sprites, including collision handling.
+ */
+ public void update(){
+ for (Sprite sprite : fogs) {
+ sprite.update();
+ }
+ if(!gameOver) {
+ if (countDown > 0)
+ return;
+ }
+ else
+ return;
- public void update(){
- for (Sprite sprite : fogs) {
- sprite.update();
- }
- if(!gameOver) {
- if (countDown > 0)
- return;
- }
- else
- return;
+ player.update();
+ for (Sprite sprite : astroidsSprites) {
+ sprite.update();
+ }
+ for (Sprite sprite : laserSprites) {
+ sprite.update();
+ }
- player.update();
- for (Sprite sprite : astroidsSprites) {
- sprite.update();
- }
- for (Sprite sprite : laserSprites) {
- sprite.update();
- }
+ // check for collisions
+ collisionHandler();
- collisionHandler();
+ }
+ /**
+ * Checks if a collision has occurred.
+ * in case of missile hitting an asteroid - they are both removed.
+ * in case of asteroid hitting the ship - round over.
+ */
+ private void collisionHandler(){
- }
+ Vector newAstroids = new Vector<>();
+ Vector removeLasers = new Vector<>();
+ Vector removeAstroids = new Vector<>();
+
+
+ /*
+ * check for "Laser vs Asteroid" collisions
+ * in case of collision both missile and asteroid are removed,
+ * and 2 smaller asteroids are created (if needed).
+ */
+ for (int i = 0; i newAstroids = new Vector<>();
- Vector removeLasers = new Vector<>();
- Vector removeAstroids = new Vector<>();
+ removeLasers.add(laserSprites.get(i));
+ removeAstroids.add(astroidsSprites.get(j));
+ }
+ }
+ }
+ /*
+ * check for "Asteroid vs Ship" collisions.
+ * if last live - game over
+ * else - 1 less life and reset vars for next round
+ */
+
+ for(int i = 0 ; i < astroidsSprites.size() ; i++){
+ if(CollisionUtil.collidesWith(astroidsSprites.get(i), player)){
+ if(lives.size() == 1){
+ gameOver = true;
+ fogs.add(new Fog(pWidth,0,pWidth,pHeight,1,"Fog.png",180,pWidth));
+ }
+ if(lives.size() > 0)
+ lives.remove(lives.size()-1);
+ initGame();
+ }
+ }
- for (int i = 0; i 0)
- lives.remove(lives.size()-1);
- initGame();
- System.out.println("==============GAME OVER==============");
- }
- }
+ /**
+ * creates an new asteroid each 5 seconds *
+ */
+ private class astroidTimerListener implements ActionListener{
+ @Override
+ public void actionPerformed(ActionEvent actionEvent) {
+ createAstroid();
+ }
+ }
+
+ /**
+ * Limits fire speed to 1 second.
+ *
+ */
+ private class fireTimerListener implements ActionListener{
+ @Override
+ public void actionPerformed(ActionEvent actionEvent) {
+ fireLock = false;
+ }
+ }
+ /**
+ * Pre game countdown.
+ *
+ */
+ private class countDownListener implements ActionListener{
+ @Override
+ public void actionPerformed(ActionEvent actionEvent) {
+ countDown--;
+ }
+ }
- astroidsSprites.addAll(newAstroids);
-
- laserSprites.removeAll(removeLasers);
- astroidsSprites.removeAll(removeAstroids);
- }
-
- public void render(JPanel panel){
- sceneImage = new BufferedImage(this.pWidth, this.pHeight, Image.SCALE_FAST);
- renderScene(sceneImage.getGraphics(), panel);
- }
-
- public void renderScene(Graphics g, JPanel panel){
- if(!gameOver)
- player.drawSprite(g, panel);
- try {
- for (Sprite sprite : astroidsSprites) {
- sprite.drawSprite(g, panel);
- }
- for (Sprite sprite : lives) {
- sprite.drawSprite(g, panel);
- }
- for (Sprite sprite : laserSprites) {
- sprite.drawSprite(g, panel);
- }
- for (Sprite sprite : fogs) {
- sprite.drawSprite(g, panel);
- }
- }catch (Exception e)
- {
-
- }
- }
-
-
-private class astroidTimerListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- createAstroid();
- }
-}
-private class fireTimerListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- fireLock = false;
- }
-}
-
- private class countDownListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- countDown--;
-
- }
- }
-
- public BufferedImage getScene(){
- return this.sceneImage;
- }
+ /**
+ * returns sceneImage
+ * @return
+ * BufferedImage
+ */
+ public BufferedImage getScene(){
+ return this.sceneImage;
+ }
}
diff --git a/src/GamePanels/GamePanel.java b/src/GamePanels/GamePanel.java
index 98fcd25..4ea761d 100644
--- a/src/GamePanels/GamePanel.java
+++ b/src/GamePanels/GamePanel.java
@@ -3,25 +3,20 @@ import Engine.*;
import javax.swing.*;
import java.awt.*;
-import java.awt.image.BufferedImage;
+//import java.awt.image.BufferedImage;
import java.net.URL;
/**
* Created by sagi on 12/18/15.
*/
public class GamePanel extends JPanel implements Runnable{
-// private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;
-
-// private final String bgPath = BASE_PATH + "Images" + File.separator + "bg.gif";
-// private final String gameOverPath = BASE_PATH + "Images" + File.separator + "gameOver.png";
-// private final String startPath = BASE_PATH + "Images" + File.separator + "start.png";
private final URL bgURL= getClass().getResource("/Images/bg2.gif");
private final URL gameOverURL= getClass().getResource("/Images/gameOver.png");
private final URL startURL= getClass().getResource("/Images/start.png");
- private int width, height;
+// private int width, height;
private GameEngine engine;
private JLabel lbl_score, lbl_countDown, lbl_gameOver, lbl_bg, lbl_start;
@@ -30,23 +25,30 @@ public class GamePanel extends JPanel implements Runnable{
-
+ /**
+ *
+ * Constructor.
+ * initializes all objects on panel and creates gameEngine
+ */
public GamePanel(int width, int height){
this.setLayout(null);
- this.width = width;
- this.height = height;
+// this.width = width;
+// this.height = height;
this.engine = new GameEngine(width, height);
this.addKeyListener(engine);
this.setFocusable(true);
this.requestFocus();
+ //set score label at top left corner
this.lbl_score = new JLabel("");
lbl_score.setBounds(15,15,width,30);
-
+
+ //set countDown label at center
this.lbl_countDown = new JLabel("");
lbl_countDown.setBounds(width/2,0,width,height);
System.out.println("URL = " +bgURL);
+ //set background
img_bg = new ImageIcon(bgURL);
Image tmp_BG = img_bg.getImage();
bg_image = tmp_BG.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
@@ -82,7 +84,8 @@ public class GamePanel extends JPanel implements Runnable{
public void run() {
while(engine.gameOn){
engine.update();
-
+
+ //check if game is over and draw labels accordingly
if(engine.isGameOver()){
this.lbl_countDown.setVisible(false);
if(!engine.isFirstGame)
@@ -94,17 +97,21 @@ public class GamePanel extends JPanel implements Runnable{
lbl_start.setVisible(false);
}
-
+
+ //render graphics
engine.render(this);
- repaint();
+
+ //sleep to slow down game
try {
- Thread.sleep(10);
+ Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
+
+ //sleep for other processes to take control
try {
- Thread.sleep(10);
+ Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
@@ -124,6 +131,7 @@ public class GamePanel extends JPanel implements Runnable{
g.drawImage(bg_image, 0, 0, this); //draw the background
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
+
//Some labels - Score & countDown
lbl_score.setText("SCORE : " + engine.getScore());
if(engine.getCountDown() > 0){
diff --git a/src/Sprites/Astroid.java b/src/Sprites/Astroid.java
index 823deb2..cb2ffd4 100644
--- a/src/Sprites/Astroid.java
+++ b/src/Sprites/Astroid.java
@@ -19,6 +19,9 @@ public class Astroid extends Sprite {
rotationSpeed = ((r.nextDouble()*10) % 3) + 0.01;
}
+ /**
+ * updates sprite location according to movement speed and angle.
+ */
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
@@ -26,7 +29,10 @@ public class Astroid extends Sprite {
outOfScreeFix();
selfAngle+=rotationSpeed;
}
-
+
+ /**
+ * draw sprite with rotation according to sprite angle
+ */
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
diff --git a/src/Sprites/Fog.java b/src/Sprites/Fog.java
index 4849962..8eff4b2 100644
--- a/src/Sprites/Fog.java
+++ b/src/Sprites/Fog.java
@@ -21,7 +21,10 @@ public class Fog extends Sprite {
rotationSpeed = 0;
alphaChannel = r.nextFloat()-0.04f;
}
-
+
+ /**
+ * updates sprite location according to movement speed and angle.
+ */
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
@@ -30,6 +33,9 @@ public class Fog extends Sprite {
selfAngle+=rotationSpeed;
}
+ /**
+ * draw sprite with rotation according to sprite angle
+ */
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
diff --git a/src/Sprites/LaserBlast.java b/src/Sprites/LaserBlast.java
index 84854fb..c9b2fab 100644
--- a/src/Sprites/LaserBlast.java
+++ b/src/Sprites/LaserBlast.java
@@ -14,6 +14,9 @@ public class LaserBlast extends Sprite {
}
+ /**
+ * updates sprite location according to movement speed and angle.
+ */
@Override
public void update() {
locX += acceleration * Math.cos(Math.toRadians(angle));
@@ -21,6 +24,9 @@ public class LaserBlast extends Sprite {
outOfScreeFix();
}
+ /**
+ * draw sprite with rotation according to sprite angle
+ */
@Override
public void drawSprite(Graphics g, JPanel p) {
Graphics2D g2d = (Graphics2D)g;
diff --git a/src/Sprites/Ship.java b/src/Sprites/Ship.java
index 677b56f..9c57408 100644
--- a/src/Sprites/Ship.java
+++ b/src/Sprites/Ship.java
@@ -1,80 +1,100 @@
package Sprites;
-import javax.imageio.ImageIO;
+//import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
-import java.awt.geom.AffineTransform;
-import java.awt.image.AffineTransformOp;
+//import java.awt.geom.AffineTransform;
+//import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
-import java.io.IOException;
+//import java.io.IOException;
/**
* Created by sagi on 12/18/15.
*/
public class Ship extends Sprite {
- private final int STOP=0, UP=1, DOWN=-1, TURN_SPEED=10;
- private final double MAX_SPEED = 6, SLOWING_FACTOR = 0.1;
- private int direction, turnDirection;
- private BufferedImage bi;
- private double selfAccel;
+ private final int STOP=0, UP=1, DOWN=-1, TURN_SPEED=10;
+ private final double MAX_SPEED = 6, SLOWING_FACTOR = 0.1;
+ private int direction, turnDirection;
+ private BufferedImage bi;
+ private double selfAccel;
- public Ship(int x, int y, int w, int h, int size) {
- super(x, y, w, h, 0, "ship2.png", 0, size);
- direction = STOP;
- turnDirection = STOP;
- selfAccel = acceleration;
+ public Ship(int x, int y, int w, int h, int size) {
+ super(x, y, w, h, 0, "ship2.png", 0, size);
+ direction = STOP;
+ turnDirection = STOP;
+ selfAccel = acceleration;
- }
+ }
- @Override
- public void update() {
- setSpeed();
- this.angle+=TURN_SPEED*turnDirection;
- locX += selfAccel * Math.cos(Math.toRadians(angle));
- locY -= selfAccel * (-1 * Math.sin(Math.toRadians(angle)));
- outOfScreeFix();
+ /**
+ * updates sprite location according to movement speed and angle.
+ */
+ @Override
+ public void update() {
+ setSpeed();
+ this.angle+=TURN_SPEED*turnDirection;
+ locX += selfAccel * Math.cos(Math.toRadians(angle));
+ locY -= selfAccel * (-1 * Math.sin(Math.toRadians(angle)));
+ outOfScreeFix();
+ }
- }
+ /**
+ * draw sprite with rotation according to sprite angle
+ */
+ public void drawSprite(Graphics g, JPanel panel) {
+ Graphics2D g2d = (Graphics2D)g;
+ g2d.rotate(Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
+ g2d.drawImage(bImage, (int)locX, (int)locY, panel);
+ g2d.rotate(-1*Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
- private void setSpeed(){
- if (direction == UP && !(selfAccel > MAX_SPEED)){
- selfAccel+=SLOWING_FACTOR*2;
- }
- else if (direction == DOWN && (selfAccel > MAX_SPEED*(-1))){
- selfAccel-=SLOWING_FACTOR*2;
- }
- else { //slowing down
- if (selfAccel > 0) {
- selfAccel -= SLOWING_FACTOR;
- if (selfAccel < 0) {
- selfAccel = 0;
- }
- }
- if (selfAccel < 0) {
- selfAccel += SLOWING_FACTOR;
- if (selfAccel > 0) {
- selfAccel = 0;
- }
- }
- }
- }
+ }
- public void setDirection(int direction){
- this.direction = direction;
- }
+ /**
+ * sets movement speed according to movement direction.
+ * if ship is not "moved" - it will slow down by slowing factor.
+ */
+ private void setSpeed(){
+ if (direction == UP && !(selfAccel > MAX_SPEED)){
+ selfAccel+=SLOWING_FACTOR*2;
+ }
+ else if (direction == DOWN && (selfAccel > MAX_SPEED*(-1))){
+ selfAccel-=SLOWING_FACTOR*2;
+ }
+ else { //slowing down
+ if (selfAccel > 0) {
+ selfAccel -= SLOWING_FACTOR;
+ if (selfAccel < 0) {
+ selfAccel = 0;
+ }
+ }
+ if (selfAccel < 0) {
+ selfAccel += SLOWING_FACTOR;
+ if (selfAccel > 0) {
+ selfAccel = 0;
+ }
+ }
+ }
+ }
- public void drawSprite(Graphics g, JPanel panel) {
- Graphics2D g2d = (Graphics2D)g;
- g2d.rotate(Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
- g2d.drawImage(bImage, (int)locX, (int)locY, panel);
- g2d.rotate(-1*Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2));
+ /**
+ * set movement direction
+ * @param direction
+ * int
+ */
+ public void setDirection(int direction){
+ this.direction = direction;
+ }
- }
- public void turnShip(int direction){
- turnDirection=direction;
- }
+ /**
+ * set turning direction
+ * @param direction
+ * int
+ */
+ public void turnShip(int direction){
+ turnDirection=direction;
+ }
}
diff --git a/src/Sprites/Sprite.java b/src/Sprites/Sprite.java
index 833c53a..4455532 100644
--- a/src/Sprites/Sprite.java
+++ b/src/Sprites/Sprite.java
@@ -4,114 +4,151 @@ import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
-import java.io.File;
+//import java.io.File;
import java.io.IOException;
-import java.net.URL;
+//import java.net.URL;
public abstract class Sprite {
- protected BufferedImage bImage;
- protected int imageWidth, imageHeight; // image dimensions
+ protected BufferedImage bImage;
+ protected int imageWidth, imageHeight; // image dimensions
- protected double locX, locY;
- protected int acceleration;
- protected int pWidth, pHeight, size; // panel's dimensions
+ protected double locX, locY;
+ protected int acceleration;
+ protected int pWidth, pHeight, size; // panel's dimensions
- protected double angle;
- private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;
+ protected double angle;
+// private final String BASE_PATH = System.getProperty("user.dir") + File.separator +"src"+ File.separator;
- public Sprite(int x, int y, int w, int h, int acc, String imgName, double angle, int size)
- {
- this.size = size;
- locX = x;
- locY = y;
- acceleration = acc;
- pWidth = w;
- pHeight = h;
- try {///home/sagi/Development/JCE/Astroids2D/src/Images/ship.gif
- bImage = ImageIO.read(getClass().getResource("/Images/"+imgName));
- }catch (IOException pin){
- pin.printStackTrace();
- bImage = null;
- }
- this.angle = angle;
+ public Sprite(int x, int y, int w, int h, int acc, String imgName, double angle, int size)
+ {
+ this.size = size;
+ locX = x;
+ locY = y;
+ acceleration = acc;
+ pWidth = w;
+ pHeight = h;
+ this.angle = angle;
- setImageDimensions();
- }
+ //load image from source files
+ try {
+ bImage = ImageIO.read(getClass().getResource("/Images/"+imgName));
+ }catch (IOException pin){
+ pin.printStackTrace();
+ bImage = null;
+ }
+
+ setImageDimensions();
+ }
- public void setImageDimensions()
- {
- Image tmp = bImage.getScaledInstance(size,size, Image.SCALE_SMOOTH);
- BufferedImage bi = new BufferedImage(size,size, BufferedImage.TYPE_INT_ARGB);
+ /*
+ * resizes image to a set size
+ */
+ private void setImageDimensions()
+ {
+ Image tmp = bImage.getScaledInstance(size,size, Image.SCALE_SMOOTH);
+ BufferedImage bi = new BufferedImage(size,size, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2d = bi.createGraphics();
- g2d.drawImage(tmp,0,0,null);
- g2d.dispose();
- bImage = bi;
- }
+ Graphics2D g2d = bi.createGraphics();
+ g2d.drawImage(tmp,0,0,null);
+ g2d.dispose();
+ bImage = bi;
+ }
+
+ /**
+ * Abstract method to update sprite.
+ */
+ public abstract void update();
- public abstract void update();
+ /**
+ * returns sprite x position
+ * @return
+ * double
+ */
+ public double getLocX() {return locX;}
+
+ /**
+ * returns sprite y position
+ * @return
+ * double
+ */
+ public double getLocY() {return locY;}
+
+ /**
+ * returns sprite acceleration
+ * @return
+ * int
+ */
+ public int getAcceleration() {return acceleration;}
+
+ /**
+ * returns sprite size
+ * @return
+ * int
+ */
+ public BufferedImage getbImage() {return bImage;}
+
+ /**
+ * returns image width
+ * @return
+ * int
+ */
+ public int getImageWidth() {return imageWidth;}
+
+ /**
+ * returns image height
+ * @return
+ * int
+ */
+ public int getImageHeight() {return imageHeight;}
+
+ /**
+ * returns sprite angle
+ * @return
+ * double
+ */
+ public double getAngle() {return angle;}
+
+ /**
+ * returns sprite size
+ * @return
+ * int
+ */
+ public int getSize() {return size;}
-// public Rectangle getBoundingBox()
-// {
-// return new Rectangle(getLocX(), getLocY(), imageWidth, imageHeight);
-// }
+
+ /**
+ * returns shape location and dimensions as a Rectangle.
+ * @return
+ * Rectangle
+ */
+ public Rectangle getBounds() {
+ return new Rectangle((int)locX, (int)locY, size, size);
+ }
- public double getLocX() {
- return locX;
- }
+
+ /**
+ * its not a bug it's a feature. actually it just moves a shape that goes beyond the screen to the other side.
+ */
+ protected void outOfScreeFix(){
+ if(locX < 0 - size)
+ locX = pWidth;
+ else if (locX > pWidth+size)
+ locX = 0-size;
- public double getLocY() {
- return locY;
- }
-
- public int getAcceleration(){
- return acceleration;
- }
-
- public BufferedImage getbImage()
- {
- return bImage;
- }
-
- public int getImageWidth() {
- return imageWidth;
- }
-
- public int getImageHeight()
- {
- return imageHeight;
- }
-
- public double getAngle(){
- return angle;
- }
-
- protected void outOfScreeFix(){ //its not a bug it's a feature
- if(locX < 0 - size)
- locX = pWidth;
- else if (locX > pWidth+size)
- locX = 0-size;
-
- if(locY < 0 - size)
- locY = pHeight;
- else if(locY > pHeight+size)
- locY = 0-size ;
- }
-
- public Rectangle getBounds() {
- Rectangle r = new Rectangle((int)locX, (int)locY, size, size);
- return r;
- }
-
- public int getSize(){
- return size;
- }
-
-
- public abstract void drawSprite(Graphics g, JPanel p);
+ if(locY < 0 - size)
+ locY = pHeight;
+ else if(locY > pHeight+size)
+ locY = 0-size ;
+ }
+ /**
+ * abstract method for drawing sprite.
+ * @param g
+ * @param p
+ */
+ public abstract void drawSprite(Graphics g, JPanel p);
}