Express with modified FlappyBird mini game
introduce
The first article, limited germination ability, mainly records, ask big guys to teach more.
https://blog.csdn.net/codehxy/article/details/25268295
The main body of the code is from the link guys above. Flappy Brid was chosen because someone used to like the game and because it was simpler = =
content
The blog of the big boy is very clear, let me mainly talk about what I do.
Item class
This class describes the props on the screen, the pixel pictures I find on the Internet myself, and then clicks them down: the letters U,R,T,H,E, [Apple picture], OF,MY, [Eye picture], which together is "U R THE APPLE OF MY EYE", which is a hint.Explosive pictures switching to pixel effect are displayed when a prop is hit.
public class Item { BufferedImage[] images; BufferedImage image; BufferedImage image2; int x; int y; int index = 0; int width; int height; boolean flag = true; public Item() throws IOException { this.x = 850; this.y = 250; images = new BufferedImage[9]; for(int i=0; i<9; i++){ images[i] = ImageIO.read(getClass().getResource("a"+i+".png")); } image = images[0]; image2 = ImageIO.read(getClass().getResource("bmob.png")); width = image.getWidth(); height = image.getHeight(); } public void step(){ x-=6;//Move 6 per step like left if(x<= -100 ){ x=630; index++; if(index>=9) index=0; image = images[index]; } } public void paint(Graphics g) { if (flag) g.drawImage(image, x, y, null); else { g.drawImage(image2, x, y, null); flag = true; } } }
Attack class
This kind of description is an attack on the screen. I flip the picture of the bird horizontally and turn it to light yellow, then appear from the left side of the screen. If I hit the player's bird, it will deduct blood and points.
public class Attack { BufferedImage image; BufferedImage[] images; int x; int y; int width; int height; Random r = new Random(); int distance = 400; int index = 0; public Attack(int n) throws IOException { images = new BufferedImage[8]; for(int i=0; i<8; i++){ images[i] = ImageIO.read(getClass().getResource("b"+i+".png")); } image = images[0]; width = image.getWidth(); height = image.getHeight(); this.x = distance * n + 80 + 432; this.y = r.nextInt(120) + 220; } public void step(){ x=x-8; if(x<= -(111)){ x = 1200; this.y = r.nextInt(120) + 220; } } public void fly(){ //Change the picture of the bird's animation frame, of which four are to adjust the frequency of animation updates index++; image = images[(index/8)%images.length]; } public void paint(Graphics g) { g.drawImage(image, x, y, null); } }
hit method
After creating the above two classes, write the appropriate membership methods in the Bird class to determine if the enemy bird and props collided with the player bird.The code is as follows, x and y are the coordinates of the center of the picture, so attention should be paid to the width and height of the picture in order to judge the collision, and I have narrowed the scope of the collision attack.
/** Check if the current bird touches items */ public boolean hit(Item item){ if( x >= item.x-item.width/2-size/2 && x <=item.x+item.width/2+size/2){ if(y >= item.y-item.height/2-size/2 && y <= item.y+item.height/2+size/2) { return true; } return false; } return false; } /** Check if the current bird is attacked */ public boolean hit(Attack attack){ if( x >= attack.x-attack.width/2-size/2+25 && x <= attack.x+attack.width/2+size/2-25){ if(y >= attack.y-attack.height/2-size/2+25 && y <= attack.y+attack.height/2+size/2-25) { return true; } return false; } return false; }
Add Audio
Background music I picked up the piano version of Rock Canon, and then the score sound and impact sound were freely available online.Player is a class from another jar.
new Thread(new Runnable() { @Override public void run() { try { BufferedInputStream buffer = new BufferedInputStream( new FileInputStream("music\\12.mp3")); new Player(buffer).play(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (JavaLayerException e) { e.printStackTrace(); } } }).start();
style
Changed the dark background, dimmed the colours of the columns, shaded them, and felt more sensible than the blue sky and white clouds?
Speed adjustment
Speed up, that is, in step() method: x-=n; increase n so that the columns move left quickly, and then the enemy birds and props do the same
Other adjustments
Maybe it's better to add a score to show a different picture of the settlement...Here you can add something you want to say or what to say...
Demonstration effect
Add a demo effect
About packaging
If the other party does not have JRE installed, it may not be good and will not run.
The jar I checked needed to package the JRE and the project files in a folder, like this:
Run.batFill in:
start jre\bin\javaw -jar myjava.jar
This can work in any environment without music, that is, the file is a bit large, but the music file does not know how to add to jar, has limited ability and is learning.
Source code
https://download.csdn.net/download/qq_23841127/12431757