commit 0d27c26c0f3b5042de38cb6366836f7892389a32 Author: Sagi Dayan Date: Mon Dec 21 19:54:44 2015 +0200 First Commit diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..de34422 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Astroids2D \ No newline at end of file diff --git a/.idea/artifacts/Astroids2D_jar.xml b/.idea/artifacts/Astroids2D_jar.xml new file mode 100644 index 0000000..9d9c38f --- /dev/null +++ b/.idea/artifacts/Astroids2D_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/Astroids2D_jar + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7e725c2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fe80aaf --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..860ab71 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1261 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1450456019624 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/src/Engine/GameEngine.java + 270 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Astroids2D:jar + + + + + + + + No facets are configured + + + + + + + + jogl + + + + + + + + 1.7 + + + + + + + + Astroids2D + + + + + + + + 1.7 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Astroids2D.iml b/Astroids2D.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Astroids2D.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/artifacts/Astroids2D_jar/Astroids2D.jar b/out/artifacts/Astroids2D_jar/Astroids2D.jar new file mode 100644 index 0000000..bdad286 Binary files /dev/null and b/out/artifacts/Astroids2D_jar/Astroids2D.jar differ diff --git a/out/production/Astroids2D/Engine/CollisionUtil.class b/out/production/Astroids2D/Engine/CollisionUtil.class new file mode 100644 index 0000000..5cac485 Binary files /dev/null and b/out/production/Astroids2D/Engine/CollisionUtil.class differ diff --git a/out/production/Astroids2D/Engine/GameEngine$1.class b/out/production/Astroids2D/Engine/GameEngine$1.class new file mode 100644 index 0000000..25c820c Binary files /dev/null and b/out/production/Astroids2D/Engine/GameEngine$1.class differ diff --git a/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class b/out/production/Astroids2D/Engine/GameEngine$astroidTimerListener.class new file mode 100644 index 0000000..8e136a8 Binary files /dev/null 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 new file mode 100644 index 0000000..ad92904 Binary files /dev/null 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 new file mode 100644 index 0000000..08ae85f Binary files /dev/null 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 new file mode 100644 index 0000000..e9a8b55 Binary files /dev/null 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 new file mode 100644 index 0000000..2ece5e0 Binary files /dev/null and b/out/production/Astroids2D/GamePanels/GamePanel.class differ diff --git a/out/production/Astroids2D/Images/Fog.png b/out/production/Astroids2D/Images/Fog.png new file mode 100644 index 0000000..b74affd Binary files /dev/null and b/out/production/Astroids2D/Images/Fog.png differ diff --git a/out/production/Astroids2D/Images/astroid.png b/out/production/Astroids2D/Images/astroid.png new file mode 100644 index 0000000..d44526a Binary files /dev/null and b/out/production/Astroids2D/Images/astroid.png differ diff --git a/out/production/Astroids2D/Images/bg2.gif b/out/production/Astroids2D/Images/bg2.gif new file mode 100644 index 0000000..94c093e Binary files /dev/null and b/out/production/Astroids2D/Images/bg2.gif differ diff --git a/out/production/Astroids2D/Images/gameOver.png b/out/production/Astroids2D/Images/gameOver.png new file mode 100644 index 0000000..5683036 Binary files /dev/null and b/out/production/Astroids2D/Images/gameOver.png differ diff --git a/out/production/Astroids2D/Images/laser2.gif b/out/production/Astroids2D/Images/laser2.gif new file mode 100644 index 0000000..795e213 Binary files /dev/null and b/out/production/Astroids2D/Images/laser2.gif differ diff --git a/out/production/Astroids2D/Images/ship.png b/out/production/Astroids2D/Images/ship.png new file mode 100644 index 0000000..4bb54bc Binary files /dev/null and b/out/production/Astroids2D/Images/ship.png differ diff --git a/out/production/Astroids2D/Images/ship2.png b/out/production/Astroids2D/Images/ship2.png new file mode 100644 index 0000000..742306c Binary files /dev/null and b/out/production/Astroids2D/Images/ship2.png differ diff --git a/out/production/Astroids2D/Images/start.png b/out/production/Astroids2D/Images/start.png new file mode 100644 index 0000000..5891d9e Binary files /dev/null and b/out/production/Astroids2D/Images/start.png differ diff --git a/out/production/Astroids2D/META-INF/MANIFEST.MF b/out/production/Astroids2D/META-INF/MANIFEST.MF new file mode 100644 index 0000000..37197ef --- /dev/null +++ b/out/production/Astroids2D/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/out/production/Astroids2D/Main.class b/out/production/Astroids2D/Main.class new file mode 100644 index 0000000..5c4de9d Binary files /dev/null and b/out/production/Astroids2D/Main.class differ diff --git a/out/production/Astroids2D/Sounds/laser.wav b/out/production/Astroids2D/Sounds/laser.wav new file mode 100644 index 0000000..d65446a Binary files /dev/null and b/out/production/Astroids2D/Sounds/laser.wav differ diff --git a/out/production/Astroids2D/Sounds/theme.wav b/out/production/Astroids2D/Sounds/theme.wav new file mode 100644 index 0000000..4474e41 Binary files /dev/null and b/out/production/Astroids2D/Sounds/theme.wav differ diff --git a/out/production/Astroids2D/Sprites/Astroid.class b/out/production/Astroids2D/Sprites/Astroid.class new file mode 100644 index 0000000..7d4903d Binary files /dev/null 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 new file mode 100644 index 0000000..4ced187 Binary files /dev/null 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 new file mode 100644 index 0000000..0812556 Binary files /dev/null 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 new file mode 100644 index 0000000..e37eaf9 Binary files /dev/null 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 new file mode 100644 index 0000000..2f695d3 Binary files /dev/null and b/out/production/Astroids2D/Sprites/Sprite.class differ diff --git a/src.zip b/src.zip new file mode 100644 index 0000000..1ca5b94 Binary files /dev/null and b/src.zip differ diff --git a/src/Engine/CollisionUtil.java b/src/Engine/CollisionUtil.java new file mode 100644 index 0000000..8d63b53 --- /dev/null +++ b/src/Engine/CollisionUtil.java @@ -0,0 +1,107 @@ +package Engine; + +import Sprites.Sprite; + +import java.awt.*; +import java.awt.image.PixelGrabber; + +/** + * Created by sagi on 12/19/15. + */ +public class CollisionUtil { + + /** + * Checks if a Sprite is colliding with another Sprite. + * @param otherSprite The Sprite to check a collission with. + * @param pixelPerfect If true, it will use a pixel-perfect algorithm. If + * false, it only checks its bounding box. + * @return true if the Sprites collide, otherwise false. + */ + public static boolean collidesWith(Sprite thisSprite, Sprite otherSprite) { + boolean isColliding=false; + + Rectangle r1 = thisSprite.getBounds(); + Rectangle r2 = otherSprite.getBounds(); + + r1.intersection(r2); + + if(intersection(r1, r2)) { + isColliding = pixelPerfectCollision(thisSprite, otherSprite, r1, r2); + } + return isColliding; + } + + private static boolean intersection(Rectangle r, Rectangle d) { + int rect1x = r.x; + int rect1y = r.y; + int rect1w = r.width; + int rect1h = r.height; + + int rect2x = d.x; + int rect2y = d.y; + int rect2w = d.width; + int rect2h = d.height; + + return (rect1x + rect1w >= rect2x && + rect1y + rect1h >= rect2y && + rect1x <= rect2x + rect2w && + rect1y <= rect2y + rect2h); + } + + + /* + * pixelPerfectCollision(); first determines the area where the sprites collides + * AKA the collision-rectangle. It then grabs the pixels from both sprites + * which are inside the rectangle. It then checks every pixel from the arrays + * given by grabPixels();, and if 2 pixels at the same position are opaque, + * (alpha value over 0) it will return true. Otherwise it will return false. + */ + private static boolean pixelPerfectCollision(Sprite sprite1,Sprite sprite2, Rectangle r1, Rectangle r2) { + /* + * Get the X-values and Y-values for the two coordinates where the sprites collide + */ + + int cornerTopX = (r1.x>r2.x)?r1.x:r2.x; + int cornerBottomX = ((r1.x+r1.width) < (r2.x+r2.width))?(r1.x+r1.width):(r2.x+r2.width); + + int cornerTopY = (r1.y>r2.y)?r1.y:r2.y; + int cornerBottomY = ((r1.y+r1.height) < (r2.y+r2.height))?(r1.y+r1.height):(r2.y+r2.height); + + //Determine the width and height of the collision rectangle + int width=cornerBottomX-cornerTopX; + int height=cornerBottomY-cornerTopY; + + //Create arrays to hold the pixels + int[] pixels1 = new int[width*height]; + int[] pixels2 = new int[width*height]; + + //Create the pixelgrabber and fill the arrays + PixelGrabber pg1 = new PixelGrabber(sprite1.getbImage(), cornerTopX-(int)sprite1.getLocX(), cornerTopY-(int)sprite1.getLocY(), width, height, pixels1, 0, width); + PixelGrabber pg2 = new PixelGrabber(sprite2.getbImage(), cornerTopX-(int)sprite2.getLocX(), cornerTopY-(int)sprite2.getLocY(), width, height, pixels2, 0, width); + + //Grab the pixels + try { + pg1.grabPixels(); + pg2.grabPixels(); + } catch (InterruptedException ex) { + //Logger.getLogger(Sprite.class.getName()).log(Level.SEVERE, null, ex); + } + + //Check if pixels at the same spot from both arrays are not transparent. + for(int i=0;i>> 24) & 0xff; + int a2 = (pixels2[i] >>> 24) & 0xff; + + /* Awesome, we found two pixels in the same spot that aren't + * completely transparent! Thus the sprites are colliding! + */ + if(a > 0 && a2 > 0) return true; + + } + + return false; + } + + + +} diff --git a/src/Engine/GameEngine.java b/src/Engine/GameEngine.java new file mode 100644 index 0000000..6d2e378 --- /dev/null +++ b/src/Engine/GameEngine.java @@ -0,0 +1,319 @@ +package Engine; + +import Sprites.*; + +import javax.swing.*; +import java.applet.Applet; +import java.applet.AudioClip; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.image.BufferedImage; +import java.net.URL; +import java.util.Random; +import java.util.Vector; + +/** + * Created by sagi on 12/18/15. + */ + + + +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 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(); + } + + 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)); + } + } + + 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 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(); + + for (int i =0 ; i<4 ; i++) + createAstroid(); + } + + 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)); + } + } + + public int getScore(){ + return score; + } + + public boolean isGameOver(){ + return this.gameOver; + } + + public int getCountDown(){ + return countDown; + } + + private void createAstroid(){ + astroidsSprites.add(new Astroid((-1)*r.nextInt(),(-1)*r.nextInt(),pWidth,pHeight,ASTROID_SPEED,"astroid.png",r.nextInt(360),LARGE)); + } + + @Override + public void keyTyped(KeyEvent keyEvent) { + } + + @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; + } + + break; + case KeyEvent.VK_F2: + this.isFirstGame = false; + gameOver = false; + startNewGame(); + startTime.start(); + break; + + default: + break; + } + } + } + + @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; + + } + } + + + } + + 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(); + } + + + collisionHandler(); + + + } + + private void collisionHandler(){ + + Vector newAstroids = new Vector<>(); + Vector removeLasers = new Vector<>(); + Vector removeAstroids = new Vector<>(); + + for (int i = 0; i 0) + lives.remove(lives.size()-1); + initGame(); + System.out.println("==============GAME OVER=============="); + } + } + + + + 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; + } + + +} diff --git a/src/GamePanels/GamePanel.java b/src/GamePanels/GamePanel.java new file mode 100644 index 0000000..98fcd25 --- /dev/null +++ b/src/GamePanels/GamePanel.java @@ -0,0 +1,139 @@ +package GamePanels; +import Engine.*; + +import javax.swing.*; +import java.awt.*; +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 GameEngine engine; + private JLabel lbl_score, lbl_countDown, lbl_gameOver, lbl_bg, lbl_start; + + private ImageIcon img_bg, img_go, img_start; + private Image bg_image; + + + + + public GamePanel(int width, int height){ + this.setLayout(null); + this.width = width; + this.height = height; + this.engine = new GameEngine(width, height); + this.addKeyListener(engine); + this.setFocusable(true); + this.requestFocus(); + + this.lbl_score = new JLabel(""); + lbl_score.setBounds(15,15,width,30); + + this.lbl_countDown = new JLabel(""); + lbl_countDown.setBounds(width/2,0,width,height); + System.out.println("URL = " +bgURL); + + img_bg = new ImageIcon(bgURL); + Image tmp_BG = img_bg.getImage(); + bg_image = tmp_BG.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH); + img_bg = new ImageIcon(tmp_BG); + lbl_bg = new JLabel(img_bg); + lbl_bg.setBounds(0,0,width,height); + + lbl_countDown.setFont(new Font("Ariel", Font.BOLD, 100)); + lbl_countDown.setForeground(Color.WHITE); + this.add(lbl_countDown); + + img_start = new ImageIcon(startURL); + lbl_start = new JLabel(img_start); + lbl_start.setVisible(true); + lbl_start.setBounds(0,0,width,height); + this.add(lbl_start); + + img_go = new ImageIcon(gameOverURL); + lbl_gameOver = new JLabel(img_go); + lbl_gameOver.setVisible(false); + lbl_gameOver.setBounds(0,0,width,height); + this.add(lbl_gameOver); + + + lbl_score.setFont(new Font("Ariel", Font.BOLD, 24)); + lbl_score.setForeground(Color.GREEN); + this.add(lbl_score); + repaint(); + + } + + @Override + public void run() { + while(engine.gameOn){ + engine.update(); + + if(engine.isGameOver()){ + this.lbl_countDown.setVisible(false); + if(!engine.isFirstGame) + this.lbl_gameOver.setVisible(true); + + }else{ + this.lbl_gameOver.setVisible(false); + this.lbl_countDown.setVisible(true); + lbl_start.setVisible(false); + + } + + engine.render(this); + repaint(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + repaint(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Override + public void addNotify(){ + super.addNotify(); + (new Thread(this)).start(); + } + + + public void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2d = (Graphics2D)g; + + 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){ + lbl_countDown.setText(engine.getCountDown()+""); + }else if(engine.getCountDown() == 0){ + lbl_countDown.setText("GO!"); + }else{ + lbl_countDown.setText(""); + } + } + + +} diff --git a/src/Images/Fog.png b/src/Images/Fog.png new file mode 100644 index 0000000..b74affd Binary files /dev/null and b/src/Images/Fog.png differ diff --git a/src/Images/astroid.png b/src/Images/astroid.png new file mode 100644 index 0000000..d44526a Binary files /dev/null and b/src/Images/astroid.png differ diff --git a/src/Images/bg2.gif b/src/Images/bg2.gif new file mode 100644 index 0000000..94c093e Binary files /dev/null and b/src/Images/bg2.gif differ diff --git a/src/Images/gameOver.png b/src/Images/gameOver.png new file mode 100644 index 0000000..5683036 Binary files /dev/null and b/src/Images/gameOver.png differ diff --git a/src/Images/laser2.gif b/src/Images/laser2.gif new file mode 100644 index 0000000..795e213 Binary files /dev/null and b/src/Images/laser2.gif differ diff --git a/src/Images/ship.png b/src/Images/ship.png new file mode 100644 index 0000000..4bb54bc Binary files /dev/null and b/src/Images/ship.png differ diff --git a/src/Images/ship2.png b/src/Images/ship2.png new file mode 100644 index 0000000..742306c Binary files /dev/null and b/src/Images/ship2.png differ diff --git a/src/Images/start.png b/src/Images/start.png new file mode 100644 index 0000000..5891d9e Binary files /dev/null and b/src/Images/start.png differ diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..37197ef --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..b3b3b60 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,18 @@ +import javax.swing.*; +import GamePanels.*; + +/** + * Created by sagi on 12/18/15. + */ +public class Main { + + public static void main(String args[]){ + + JFrame frame = new JFrame("Asteroids 2D - Star Wars Edition"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setResizable(false); + frame.add(new GamePanel(800,600)); + frame.setVisible(true); + } +} diff --git a/src/Sounds/laser.wav b/src/Sounds/laser.wav new file mode 100644 index 0000000..d65446a Binary files /dev/null and b/src/Sounds/laser.wav differ diff --git a/src/Sounds/theme.wav b/src/Sounds/theme.wav new file mode 100644 index 0000000..4474e41 Binary files /dev/null and b/src/Sounds/theme.wav differ diff --git a/src/Sprites/Astroid.java b/src/Sprites/Astroid.java new file mode 100644 index 0000000..823deb2 --- /dev/null +++ b/src/Sprites/Astroid.java @@ -0,0 +1,38 @@ +package Sprites; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +/** + * Created by sagi on 12/18/15. + */ +public class Astroid extends Sprite { + + private double selfAngle, rotationSpeed; + private Random r; + + public Astroid(int x, int y, int w, int h, int acc, String imgName, double angle, int size) { + super(x, y, w, h, acc, imgName, angle, size); + selfAngle = 0; + r= new Random(); + rotationSpeed = ((r.nextDouble()*10) % 3) + 0.01; + } + + @Override + public void update() { + locX += acceleration * Math.cos(Math.toRadians(angle)); + locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle))); + outOfScreeFix(); + selfAngle+=rotationSpeed; + } + + @Override + public void drawSprite(Graphics g, JPanel p) { + Graphics2D g2d = (Graphics2D)g; + g2d.rotate(Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + g2d.drawImage(bImage, (int)locX, (int)locY, p); + g2d.rotate(-1 * Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + + } +} diff --git a/src/Sprites/Fog.java b/src/Sprites/Fog.java new file mode 100644 index 0000000..4849962 --- /dev/null +++ b/src/Sprites/Fog.java @@ -0,0 +1,44 @@ +package Sprites; + + + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + + + +public class Fog extends Sprite { + + private double selfAngle, rotationSpeed; + private float alphaChannel; + private Random r; + + public Fog(int x, int y, int w, int h, int acc, String imgName, double angle, int size) { + super(x, y, w, h, acc, imgName, angle, size); + selfAngle = 0; + r= new Random(); + rotationSpeed = 0; + alphaChannel = r.nextFloat()-0.04f; + } + + @Override + public void update() { + locX += acceleration * Math.cos(Math.toRadians(angle)); + locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle))); + outOfScreeFix(); + selfAngle+=rotationSpeed; + } + + @Override + public void drawSprite(Graphics g, JPanel p) { + Graphics2D g2d = (Graphics2D)g; + g2d.rotate(Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaChannel)); + g2d.drawImage(bImage, (int)locX, (int)locY, p); + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); + + g2d.rotate(-1 * Math.toRadians(selfAngle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + + } +} diff --git a/src/Sprites/LaserBlast.java b/src/Sprites/LaserBlast.java new file mode 100644 index 0000000..84854fb --- /dev/null +++ b/src/Sprites/LaserBlast.java @@ -0,0 +1,32 @@ +package Sprites; + +import javax.swing.*; +import java.awt.*; + +/** + * Created by sagi on 12/18/15. + */ +public class LaserBlast extends Sprite { + + + public LaserBlast(int x, int y, int w, int h, int acc, String imgName, double angle, int size) { + super(x, y, w, h, acc, imgName, angle, size); + + } + + @Override + public void update() { + locX += acceleration * Math.cos(Math.toRadians(angle)); + locY -= acceleration * (-1 * Math.sin(Math.toRadians(angle))); + outOfScreeFix(); + } + + @Override + public void drawSprite(Graphics g, JPanel p) { + Graphics2D g2d = (Graphics2D)g; + g2d.rotate(Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + g2d.drawImage(bImage, (int)locX, (int)locY, p); + g2d.rotate(-1 * Math.toRadians(angle), locX + (bImage.getWidth()/2), locY + (bImage.getHeight()/2)); + + } +} diff --git a/src/Sprites/Ship.java b/src/Sprites/Ship.java new file mode 100644 index 0000000..677b56f --- /dev/null +++ b/src/Sprites/Ship.java @@ -0,0 +1,80 @@ +package Sprites; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; +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; + + + 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(); + + } + + 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; + } + + + 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)); + + } + public void turnShip(int direction){ + turnDirection=direction; + } + +} diff --git a/src/Sprites/Sprite.java b/src/Sprites/Sprite.java new file mode 100644 index 0000000..833c53a --- /dev/null +++ b/src/Sprites/Sprite.java @@ -0,0 +1,117 @@ +package Sprites; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.net.URL; + + +public abstract class Sprite { + 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 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; + + setImageDimensions(); + } + + + public 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; + } + + public abstract void update(); + +// public Rectangle getBoundingBox() +// { +// return new Rectangle(getLocX(), getLocY(), imageWidth, imageHeight); +// } + + public double getLocX() { + return locX; + } + + 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); + + +}