java小鸟游戏制作

java小鸟游戏制作java小鸟游戏制作

 

一.新建Project项目工程

BirdGame.JAVA

package com.app.bird;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BirdGame extends JPanel{
public static void main(String[] args) throws Exception{
    JFrame frame=new JFrame("bird game");
    BirdGame game=new BirdGame();
    frame.add(game); 
    frame.setSize(430, 670);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    game.action();
}
Bird bird;
Column column1;
Column column2;
Groud groud;
BufferedImage background;
BufferedImage gameOverImage;
BufferedImage startImage;
int state;
public static final int START=0;
public static final int RUNNING=1;
public static final int GAME_OVER=2;

int score;
public BirdGame()throws Exception{
    state=START;
    startImage=ImageIO.read(getClass().getResource("start.png"));
    gameOverImage=ImageIO.read(getClass().getResource("gameover.png"));
    bird=new Bird();
    column1=new Column(1);
    column2=new Column(2);
    groud=new Groud();
    background=ImageIO.read(getClass().getResource("bg.png"));
}

public void paint(Graphics g){
    switch(state){
    case GAME_OVER:
    g.drawImage(gameOverImage,0,0,null);
    break;
    case START:
    g.drawImage(startImage,0,0,null);
    break;
    }
    g.drawImage(background, 0, 0, null);
    g.drawImage(column1.image, column1.x-column1.width/2, column1.y-column1.height/2,null);
    g.drawImage(column2.image, column2.x-column2.width/2, column2.y-column2.height/2,null);
    g.drawImage(groud.image, groud.x, groud.y, null);
    g.drawImage(bird.image, bird.x-bird.width/2, bird.y-bird.height/2,null);
Graphics2D g2=(Graphics2D) g;
g2.rotate(-bird.alp,bird.x,bird.y);
g.drawImage(bird.image, bird.x-bird.width/2, bird.y-bird.height/2,null);
g2.rotate(bird.alp, bird.x, bird.y);

Font f=new Font(Font.SANS_SERIF,Font.BOLD,40);
g.setFont(f);
g.drawString(""+score,40,60);
g.setColor(Color.WHITE);
g.drawString(""+score, 40-3, 60-3);

}
public void action() throws Exception{
    MouseListener l=new MouseAdapter() {
        public void mousePressed(MouseEvent e){
            //bird.flappy();
            try{
                switch(state){
                case GAME_OVER:
                    column1=new Column(1);
                    column2=new Column(2);
                    bird=new Bird();
                    score=0;
                    state=START;
                    break;
                case START:
                    state =RUNNING;
                case RUNNING:
                    bird.flappy();
                }
            }catch(Exception e2){
                e2.printStackTrace();
            }
        }
    };
    addMouseListener(l);
    while(true){    
        //if(!gameOver){
//groud.step();
//column1.step();
//column2.step();
//bird.step();
//bird.fly();
//}
    //if(bird.hit(groud) || bird.hit(column1) || bird.hit(column2)){
        //gameOver=true;
    //}
//if(bird.x==column1.x || bird.x==column2.x){
    //score++;
//}
        switch(state){
        case START:
            bird.fly();
            groud.step();
            break;
        case RUNNING:
            groud.step();
            column1.step();
            column2.step();
            bird.step();
            bird.fly();
            
            if(bird.x==column1.x || bird.x==column2.x){
                score++;
            }
            if(bird.hit(groud) || bird.hit(column1) || bird.hit(column2)){
                state=GAME_OVER;
            }
            break;
        }
repaint();
Thread.sleep(1000/30);
}
}

}


Column.JAVA

package com.app.bird;

import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Column extends JPanel{
    BufferedImage image;
    int x,y;
    int width,height;
    int gap;
    int distance;
    
    Random rm=new Random();
    public Column(int n)throws Exception{
    image=ImageIO.read(getClass().getResource("column.png"));
    width=image.getWidth();
    height=image.getHeight();
    gap=144;
    distance=245;
    x=550+(n-1)*distance;
    y=rm.nextInt(218)+132;
    }
    
    public void step(){
        x--;
        if(x==-width/2){
            x=distance*2-width/2;
            y=rm.nextInt(218)+123;
        }
    }
}
Groud.JAVA

package com.app.bird;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Groud extends JPanel{
BufferedImage image;
int x,y;
int width,height;

public Groud()throws Exception{
    image=ImageIO.read(getClass().getResource("ground.png"));
    width=image.getWidth();
    height=image.getHeight();
    x=0;
    y=500;
}
public void step(){
    x--;
    if(x==-109){
        x=0;
    }
}
}


Bird.JAVA

package  com.app.bird;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Bird extends JPanel{
    BufferedImage image;
    int x,y;
    int width,height;
    int size;
    double g;
    double t;
    double v0;
    double speed;
    double s;
    double alp;
    
    BufferedImage[] images;
    int index;
    public Bird()throws Exception{
        image=ImageIO.read(getClass().getResource("0.png"));
        width=image.getWidth();
        height=image.getHeight();
        x=132;
        y=280;
        size=40;
        g=4;
        v0=20;
        t=0.25;
        speed=v0;
        s=0;
        alp=0;
        
        images=new BufferedImage[8];
        for(int i=0;i<images.length;i++){
            images[i]=ImageIO.read(getClass().getResource(i+".png"));
        }
        index=0;
    }
    
    public void step(){
double v0=speed;
s=v0*t+g*t*t/2;
y=y-(int)s;
double v=v0-g*t;
speed=v;
alp=Math.atan(s/8);
}
    public void fly(){
        index++;
        image=images[index/12%8];
    }
    public void flappy(){
        speed=v0;
    }
    
    public boolean hit(Groud groud){
        boolean hit=y+size/2>groud.y;
        if(hit){
            y=groud.y-size/2;
        }
        return hit;
        
    }
    public boolean hit(Column column){
        if(x>column.x-column.width/2-size/2 && x<column.x+column.width/2+size/2){
            if(y>column.y-column.gap/2+size/2 && y<column.y+column.gap/2-size/2){
                return false;
            }
            return true;
        }
        return false;
        
    }
}