From 1460c4d9297d0cef8f9a4c5c51b72ba8d9dafa1e Mon Sep 17 00:00:00 2001 From: Joe O'Regan Date: Tue, 5 Nov 2019 04:36:48 +0000 Subject: [PATCH] Flappy Bird - Redo for self contained jar file --- FlappyBird/Bird.java | 8 ++++++-- FlappyBird/FlappyBird.java | 10 +++++----- FlappyBird/Pipe.java | 12 ++++++++++-- FlappyBird/SoundEffect.java | 11 ++++++++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/FlappyBird/Bird.java b/FlappyBird/Bird.java index f488c12..533b8fc 100644 --- a/FlappyBird/Bird.java +++ b/FlappyBird/Bird.java @@ -1,3 +1,4 @@ +import java.net.URL; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; @@ -6,6 +7,7 @@ import java.util.Random; public class Bird extends Rectangle{ + private static final long serialVersionUID = 1L; public static final int WIDTH=28; public static final int HEIGHT=20; Image pic; @@ -14,14 +16,16 @@ public class Bird extends Rectangle{ Random random=new Random(); - Bird(int a, int b, int w, int h, String s){ + Bird(int a, int b, int w, int h){ yMotion=0; alive=true; x=a; y=b; width=w; height=h; - pic=Toolkit.getDefaultToolkit().getImage(s); + + URL url = getClass().getResource("/flappy.png"); + pic=Toolkit.getDefaultToolkit().getImage(url); } public void move(){ diff --git a/FlappyBird/FlappyBird.java b/FlappyBird/FlappyBird.java index a78eee9..369a38b 100644 --- a/FlappyBird/FlappyBird.java +++ b/FlappyBird/FlappyBird.java @@ -8,15 +8,15 @@ import java.awt.event.KeyListener; import java.awt.Font; import javax.swing.JPanel; -import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.Timer; import java.util.ArrayList; import java.util.Random; public class FlappyBird extends JPanel implements ActionListener,MouseListener,KeyListener{ - SoundEffect flapFX = new SoundEffect("flap.wav"); - SoundEffect crashFX = new SoundEffect("sadwah.wav"); + private static final long serialVersionUID = 1L; + SoundEffect flapFX = new SoundEffect("/flap.wav"); + SoundEffect crashFX = new SoundEffect("/sadwah.wav"); public static FlappyBird flappyBird; @@ -61,7 +61,7 @@ public FlappyBird(){ jframe.setResizable(false); jframe.setVisible(true); - bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT,"flappy.png"); + bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT); pipes=new ArrayList(); addPipe(true); @@ -145,7 +145,7 @@ public void addPipe(boolean start){ public void jump(){ //Restart Game if(gameOver){ - bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT,"flappy.png"); + bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT); playCrash=true; pipes.clear(); bird.yMotion=0; diff --git a/FlappyBird/Pipe.java b/FlappyBird/Pipe.java index 0d3a65c..138fdce 100644 --- a/FlappyBird/Pipe.java +++ b/FlappyBird/Pipe.java @@ -3,14 +3,20 @@ import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; +import java.net.URL; import java.util.Random; public class Pipe extends Rectangle{ + /** + * + */ + private static final long serialVersionUID = 1L; final static int WIDTH=100, HEIGHT=500, SPEED=10; Image pic; private boolean bottomPipe; Random random=new Random(); + URL url; Pipe(int a, int b, boolean bottomPipe){ this.bottomPipe=bottomPipe; @@ -20,9 +26,11 @@ public class Pipe extends Rectangle{ height=HEIGHT; if(bottomPipe){ - pic=Toolkit.getDefaultToolkit().getImage("pipe_bottom.png"); + url = getClass().getResource("/pipe_bottom.png"); + pic=Toolkit.getDefaultToolkit().getImage(url); }else{ - pic=Toolkit.getDefaultToolkit().getImage("pipe_top.png"); + url = getClass().getResource("/pipe_top.png"); + pic=Toolkit.getDefaultToolkit().getImage(url); } } diff --git a/FlappyBird/SoundEffect.java b/FlappyBird/SoundEffect.java index 418fd92..f84f303 100644 --- a/FlappyBird/SoundEffect.java +++ b/FlappyBird/SoundEffect.java @@ -1,16 +1,21 @@ + import javax.sound.sampled.Clip; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioInputStream; + +import java.io.BufferedInputStream; import java.io.File; +import java.io.InputStream; public class SoundEffect{ Clip clip; - public SoundEffect(String filename){ try{ - File file = new File(filename); - AudioInputStream sound=AudioSystem.getAudioInputStream(file); + InputStream audioSrc = getClass().getResourceAsStream(filename); + InputStream bufferedIn = new BufferedInputStream(audioSrc); + AudioInputStream sound=AudioSystem.getAudioInputStream(bufferedIn); + clip=AudioSystem.getClip(); clip.open(sound); }catch(Exception e){