-
-
Save ziplokk1/8910463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package crappyBird; | |
import java.awt.Graphics; | |
import java.awt.Rectangle; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.image.BufferedImage; | |
import java.io.IOException; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import javax.swing.Timer; | |
public class BirdMan { | |
static int DIAMETER = 25; //Diameter of the bird | |
static int X = ( Game.WIDTH / 2 ) - ( DIAMETER / 2 ); //The x position of the bird. Does not change at any time. Should be exactly centered | |
static int y = Game.HEIGHT / 2; //The STARTING y position of the bird. Will change constantly | |
static int acceleration = 1; //Used in the gravity simulation below | |
static int speed = 2; //The speed at which the bird will fall (constantly increased by acceleration (1) ) | |
//Fetching bird.png from Imgur where it's hosted (not ideal, slower loading times) | |
static BufferedImage img = null;{ | |
try { | |
img = ImageIO.read(new URL("http://i.imgur.com/mw0ai3K.png")); | |
} catch (IOException e) { | |
System.out.println("WRONG BIRD"); //Prints "WRONG BIRD" if there is an error retrieving the image | |
}} | |
public BirdMan(){ | |
//just the constructor, nothing to see here | |
} | |
//This is called when the bird jumps (on mouse click). It just temporarily sets the speed to -17 (arbitrary number), then is slowly taken back down because | |
//of "gravity" | |
public void jump(){ | |
speed = - 17; | |
} | |
//all movement stuff is here | |
public static void move(){ | |
//only moves if the bird is between the top and bottom of the window | |
if ( ( y > 0 ) && ( y < Game.HEIGHT )) { | |
speed += acceleration; //Here's the gravity I was talking about the speed is just increased by 1 all the time, even after a jump | |
y += speed; //The actual movement, y location equals (where it was) + (how far it should go) | |
} | |
//or else the game resets (Bird is dead!) | |
else { | |
reset(); //rests bird's postion, actual method below | |
Game.dead = true; //bird is dead! This is used in the Main method to reset the walls after a death | |
} | |
} | |
public static void reset(){ //called after the bird dies | |
y = Game.HEIGHT / 2; //resets position, speed, etc. | |
speed = 2; | |
Game.score = 0; | |
Game.deathMessage = "you died, try again"; //also shows this lovely message | |
//This timer just makes the message dissapear after 3000 milliseconds | |
Timer deathTimer = new Timer(3000, new ActionListener(){ | |
public void actionPerformed(ActionEvent event){ | |
Game.deathMessage = ""; | |
}; | |
}); | |
deathTimer.start(); | |
} | |
public static void paint(Graphics g){ | |
g.drawImage(img, X, y, null); //paints the bird's icon | |
} | |
public static Rectangle getBounds(){ | |
return new Rectangle(X, y, DIAMETER, DIAMETER); //Gives a rectangle used to detect collisions in the Wall class | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package crappyBird; | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.image.BufferedImage; | |
import java.io.IOException; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import javax.swing.JPanel; | |
@SuppressWarnings("serial") | |
public class Game extends JPanel{ | |
static int HEIGHT = 800; //height of the window | |
static int WIDTH = 600; //width of the window | |
BirdMan birdy = new BirdMan(); //makes a new bird | |
Wall wall = new Wall(WIDTH); //makes the first wall you see | |
Wall wall2 = new Wall(WIDTH + (WIDTH / 2)); //makes the second wall you see | |
static int score = 0; //the score (how many walls you've passed) | |
int scrollX = 0; //scrolls the background | |
static boolean dead = false; //used to reset the walls | |
static String deathMessage = "" ; // "you died, try again"; | |
//grabs the background from Imgur | |
BufferedImage img = null;{ | |
try { | |
img = ImageIO.read(new URL("http://i.imgur.com/cXaR0vS.png")); | |
} catch (IOException e) { | |
System.out.println("WRONG BACKGROUND"); //prints "WRONG BACKGROUND" if there is an issue obtaining the background | |
}} | |
public Game(){ | |
//this mouseAdapter just listens for clicks, whereupon it then tells the bird to jump | |
this.addMouseListener(new MouseAdapter(){ | |
public void mousePressed(MouseEvent arg0) { | |
birdy.jump(); | |
} | |
}); | |
} | |
@SuppressWarnings("static-access") | |
public void paint(Graphics g){ | |
super.paint(g); | |
g.drawImage(img, scrollX, 0, null); //there are two backgrounds so you get that seamless transition, this is the first | |
g.drawImage(img, scrollX + 1800, 0, null); //number 2, exactly one background length away (1800 pixels) | |
wall.paint(g); //paints the first wall | |
wall2.paint(g); //the second wall | |
birdy.paint(g); //the wee little birdy | |
g.setFont(new Font("comicsans", Font.BOLD, 40)); | |
g.drawString("" + score, WIDTH / 2 - 20, 700); | |
g.drawString(deathMessage, 200, 200); //paints "" if the player has not just died, paints "you died, try again" if the user just died | |
} | |
@SuppressWarnings("static-access") | |
public void move(){ | |
wall.move(); //moves the first wall | |
wall2.move(); //moves the second wall | |
birdy.move(); //moves the wee little birdy | |
scrollX += Wall.speed; //scrolls the wee little background | |
if (scrollX == -1800) //this loops the background around after it's done | |
scrollX = 0; | |
if (dead){ //this block essentially pushes the walls back 600 pixels on birdy death | |
wall.x = 600; | |
wall2.x = 600 + (WIDTH / 2); | |
dead = false; | |
} | |
if ( (wall.x == BirdMan.X) || (wall2.x == BirdMan.X) ) //Increments the score when the player passes a wall | |
score(); | |
} | |
public static void score(){ | |
score += 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package crappyBird; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JFrame; | |
import javax.swing.Timer; | |
public class Main { | |
static JFrame frame = new JFrame(); | |
public static void main (String [] args) throws InterruptedException{ | |
frame.setSize(Game.WIDTH, Game.HEIGHT); //declares the JFrame (window) that all of this is housed in | |
frame.setVisible(true); //you have to be able to see the JFrame | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Honestly don't know what this does, but it has to be here. | |
frame.setLocationRelativeTo(null); //sets the window up in the middle of the screen | |
runnit(); // /r/running shoutout, also begins running the method below | |
} | |
public static void runnit() throws InterruptedException{ | |
final Menu menu = new Menu(); //the menu used in each instance | |
final Game game = new Game(); //the game used in each instance | |
Timer animationTimer = new Timer(20, new ActionListener(){ //animation timer for the game, repaints the bird, walls, and background every 20 milliseconds | |
public void actionPerformed(ActionEvent event){ | |
game.repaint(); | |
game.move(); | |
}; | |
}); | |
frame.add(menu); //adds the menu (Step 1) | |
menu.setVisible(true); | |
frame.revalidate(); //makes sure the menu is displayed (Step 1.5) | |
frame.repaint(); | |
while (menu.startGame == false){ //waits until the mouse is clicked in the Menu | |
Thread.sleep(10); | |
} | |
frame.remove(menu); //Removes menu when mouse is clicked(Step 2) | |
frame.add(game); //adds the game in its place (Step 3) | |
game.setVisible(true); //makes sure the game is displayed (Step 3.5) | |
frame.revalidate(); | |
animationTimer.start(); //begins animation timer, and the game begins | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package crappyBird; | |
import java.awt.Graphics; | |
import java.awt.Rectangle; | |
import java.awt.image.BufferedImage; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Random; | |
import javax.imageio.ImageIO; | |
/* Here's how this works: | |
* | |
* || || || || | |
* || || --> || || --REPEAT--> | |
* || || || || | |
* wall wall2 wall2 wall(loops back around at a different height) | |
* | |
*/ | |
public class Wall { | |
Random rnd = new Random(); //used to generate a random height for dat gap | |
int x ; //the x position of the wall, always changing (right to left) | |
int y = rnd.nextInt(Game.HEIGHT - 400) + 200; //generates the y value that is the top of the bottom wall | |
static int speed = - 6; //scrolling speed | |
int WIDTH = 45; //width of a wall, it's a constant | |
int height = Game.HEIGHT - y; //height of the wall, just the height of the window - how high the wall is | |
int GAP = 200; //gap size (also a constant) | |
//procures the Wall image from Imgur | |
static BufferedImage img = null;{ | |
try { | |
img = ImageIO.read(new URL("http://i.imgur.com/4SUsUuc.png")); | |
} catch (IOException e) { | |
System.out.println("WRONG WALL"); //prints "WRONG WALL" if there's trouble with Imgur | |
}} | |
public Wall(int i){ //allows me to differentiate the x positions of the two walls | |
this.x = i; | |
} | |
//draws the wall | |
public void paint(Graphics g){ | |
g.drawImage(img, x, y, null); //top part | |
g.drawImage(img, x, ( -Game.HEIGHT ) + ( y - GAP), null); //bottom part | |
} | |
public void move(){ | |
x += speed; //scrolls the wall | |
//These Rectanlges are used to detect collisions | |
Rectangle wallBounds = new Rectangle(x, y, WIDTH, height); | |
Rectangle wallBoundsTop = new Rectangle(x, 0, WIDTH, Game.HEIGHT - (height + GAP)); | |
//If birdman collids with a wall, he dies and the game, bird, and walls are all reset | |
if ( (wallBounds.intersects(BirdMan.getBounds()) ) || (wallBoundsTop.intersects(BirdMan.getBounds()))){ | |
BirdMan.reset(); | |
died(); | |
} | |
//pushes the wall back to just off screen on the right when it gets offscreen on the left (the loop) | |
if (x <= 0 - WIDTH){ | |
x = Game.WIDTH; | |
y = rnd.nextInt(Game.HEIGHT - 400) + 200; | |
height = Game.HEIGHT - y; | |
} | |
} | |
//this is executed on death, just sets a random y value and tells Game that the bird died :( | |
public void died(){ | |
y = rnd.nextInt(Game.HEIGHT - 400) + 200; | |
height = Game.HEIGHT - y; | |
Game.dead = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment