Skip to content

Commit

Permalink
Flappy Bird - Redo for self contained jar file
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe O'Regan committed Nov 5, 2019
1 parent fb0a5d1 commit 1460c4d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
8 changes: 6 additions & 2 deletions FlappyBird/Bird.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.net.URL;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
Expand All @@ -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;
Expand All @@ -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(){
Expand Down
10 changes: 5 additions & 5 deletions FlappyBird/FlappyBird.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Pipe>();

addPipe(true);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions FlappyBird/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
11 changes: 8 additions & 3 deletions FlappyBird/SoundEffect.java
Original file line number Diff line number Diff line change
@@ -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){
Expand Down

0 comments on commit 1460c4d

Please sign in to comment.