飞翔的小鸟

ps:分享学习java的第三天

今天分享的内容:飞翔的小鸟

今天终于把飞翔的小鸟做出来了,以下是我的源代码

 
package com.qiku.flay;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 游戏启动类
 * 使用extends关键字继承JPansl画板类==>
 * 于是,BirdGame就具备了画板的功能
 */
public class Birdgame extends JPanel {

    //定义游戏状态
    public static final int START = 0;//开始状态
    public static final int RUNNING = 1;//运行状态
    public static final int GAME_OVER = 2;//结束状态



    static BufferedImage bg = null;//背景图片
    static BufferedImage ground_image = null;//地面图片
    static BufferedImage bird_image = null;//小鸟图片
    static BufferedImage column_image = null;//柱子图片
    static BufferedImage start_image = null;//开始状态图片
    static BufferedImage game_over_image = null;//结束状态图片

    //静态代码块  一般用来加载静态资源(视频,音频,图片等)
    static {
        try {
            //将本地的图片加载到程序中
            bg = ImageIO.read(Birdgame.class.getResourceAsStream("bg.png"));
            bird_image = ImageIO.read(Birdgame.class.getResourceAsStream("0.png"));
            column_image = ImageIO.read(Birdgame.class.getResourceAsStream("column.png"));
            ground_image = ImageIO.read(Birdgame.class.getResourceAsStream("ground.png"));
            start_image = ImageIO.read(Birdgame.class.getResourceAsStream("start.png"));
            game_over_image = ImageIO.read(Birdgame.class.getResourceAsStream("gameover.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //当前游戏状态  默认0 - 开始状态
    int state = START;


    int score = 0;//表示玩家的得分

    /**
     * 用于在画板上绘制内容的方法,
     * 想在花瓣上显示什么,在这个方法里面写就行了
     *
     * @param g 画笔
     */
    Ground ground;//声明地面
    Bird bird;//声明小鸟
    Column column1;//柱子1
    Column column2;//柱子2

    public Birdgame() throws Exception {
        ground = new Ground();//创建地面对象
        bird = new Bird();//创建小鸟对象
        column1 = new Column();
        column2 = new Column();
        //柱子2的X坐标等于柱子1的坐标的基础上+244(保证水平间距)
        column2.x = column1.x + column1.distance;
    }

    //main方法 - 程序的入库(即:有main方法,程序才能运行)
    public static void main(String[] args) throws Exception {
        //创建画框对象(即:从java提供的资源中获取JFrame)
        JFrame frame = new JFrame();
        Birdgame game = new Birdgame();//创建画板对象
        frame.setSize(432, 644);//设置宽高
        frame.setLocationRelativeTo(null);//居中显示
        //设置关闭窗口,同时,使程序结束
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);//设置可见性
        frame.add(game);//将画板放在画框上
        game.action();
    }

    public void action() throws Exception {
        //给当前对象(画板)添加鼠标单机事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (state == START) { //如果是开始状态,单机转换为运行状态
                    state = RUNNING;
                }
                if (state == RUNNING) {//运行点击转向上飞
                    bird.up();
                }
                if (state == GAME_OVER) {//结束状态单机变开始状态
                    state = START;
                    //初始化游戏状态
                    try {
                        bird = new Bird();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    column1 = new Column();
                    column2 = new Column();
                    column2.x = column1.x + column1.distance;
                    score = 0;//分数清零
                    state = START;
                }
            }

        });

        //死循环:{}内部的代码会一直执行
        while (true) {
            if (state == START) {
                ground.step();//地面移动
                bird.fly();//小鸟移动
            } else if (state == RUNNING) {
                ground.step();//地面移动
                column1.step();//柱子移动
                column2.step();
                bird.fly();//小鸟移动
                bird.down();//小鸟下落

                if (isGameOver() == true) {//
                    state = GAME_OVER;
                }

                if (bird.x == column1.x + column1.width + 1 ||
                        bird.x == column2.x + column2.width + 1) {
                    // 得分
                    score += 5;
                }
            }
            repaint();//重画:重新执行paint方法
            Thread.sleep(10);//每隔10毫秒,让程序休眠一次
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);//画背景

        g.drawImage(bird.image, bird.x, bird.y, null);//画小鸟
        g.drawImage(column1.image, column1.x, column1.y, null);//柱子1
        g.drawImage(column2.image, column2.x, column2.y, null);//柱子2
        g.drawImage(ground.image, ground.x, ground.y, null);//画地面
        if (state == START) {//开始状态
            g.drawImage(start_image, 0, 0, null);
        }
        if (state == GAME_OVER) {//结束状态
            g.drawImage(game_over_image, 0, 0, null);
        }

        //画分数
        Font font = new Font("微软雅黑", Font.BOLD, 30);
        g.setFont(font);//设置字体
        g.setColor(Color.BLACK);//设置画笔颜色
        g.drawString("分数:" + score, 30, 50);
        g.setColor(Color.WHITE);//设置画笔颜色
        g.drawString("分数:" + score, 28, 48);


    }

    public boolean isGameOver() {
        boolean isHit = bird.hitGround(ground) ||
                bird.hitColumn(column1) ||
                bird.hitColumn(column2);
        return isHit;
    }

}

 

package com.qiku.flay;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Bird {
    int x;
    int y;//坐标
    int width;
    int heigh;//宽高
    BufferedImage image;//小鸟0的图片
    BufferedImage[] images;//小鸟所有状态图片
    //飞翔的方法
    int index = 0;
    //h = v*t + g*t*t/2;
    int g = 2;//重力加速度
    double t = 0.18;//下落时间
    double v = 0;//初始速度
    double h = 0;//下落距离

    public Bird() throws Exception {
        images = new BufferedImage[8];
        for (int i = 0; i < images.length; i++) {
            images[i] = ImageIO.read(Bird.class.getResourceAsStream(i + ".png"));
        }
        image = Birdgame.bird_image;
        x = 120;
        y = 240;
        width = image.getWidth();
        heigh = image.getHeight();
    }

    public void fly() {
        image = images[index % images.length];
        index++;

    }

    //小鸟下落一次
    public void down() {
        h = v * t + g * t * t / 2;//具体下落的距离
        v = v + g * t;//末速度= 当前速度加重力加速度
        y = y + (int) h;
    }

    //小鸟向上飞
    public void up() {
        //给一个负方向的初速度即可
        v = -15;
    }
/**
 * 小鸟撞击地面
 *
 * */

    public boolean hitGround(Ground ground) {
        boolean isHit = this.y + this.heigh >= ground.y;
            return isHit;

    }
    public boolean hitColumn(Column column) {
        boolean b1 = this.x + this.width >= column.x;
        boolean b2 = this.x <= column.x + column.width;
        boolean b3 = this.y <= column.y + column.heigh/2 - column.gap/2;
        boolean b4 = this.y + this.heigh >= column.y + column.heigh/2 + column.gap / 2;
        //满足b1  b2  证明水平方向与柱子相撞
        //b1 b2 b3 同时满足,撞的是 上面柱子
        //b1 b2 b4 同时满足,撞的是下面柱子
        return b1 && b2 && (b3 || b4);
    }

}

package com.qiku.flay;

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

public class Column {
    int x;
    int y;//坐标
    int width;
    int heigh;//宽高
    BufferedImage image;//柱子图片
    int gap;//上下竹子之间的间隙144
    int distance;//水平方向柱子之间的距离244
    int min = -(1200 / 2 - 144 / 2);
    int max = 644 - 146 - 144 / 2 - 1200 / 2;

    public Column() {
        image = Birdgame.column_image;
        width = image.getWidth();
        heigh = image.getHeight();
        x = 432;
        y = (int) (Math.random() * (max - min) + min);
        gap = 200;
        distance = 244;
    }

    //柱子走一步
    public void step() {
        x = x - 1;
        if (x <= -width) {
            x = 432;
            y = (int) (Math.random() * (max - min) + min);
        }
    }
}

package com.qiku.flay;

import java.awt.image.BufferedImage;
/**
 * 地面类
 * */
public class Ground {
    int x;
    int y;//地面坐标
    int width;
    int heigh;//地面宽高
    BufferedImage image;//地面图片
    public Ground(){
        image = Birdgame.ground_image;
        x = 0;
        y = Birdgame.bg.getHeight() - image.getHeight();
        width = image.getWidth();//800
        heigh = image.getHeight();//146
    }
    public  void step(){
        x = x - 1;
        if (x <= 432 - width){
            x = 0;//0
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值