博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[苦逼程序员的成长之路]1、飞扬小鸟
阅读量:5092 次
发布时间:2019-06-13

本文共 6286 字,大约阅读时间需要 20 分钟。

 

跟这老师写的一个飞扬小鸟游戏,写的时候,刚刚接触后java,什么都不懂,因为以前的基础,对java格式理解起来并不困难,跟着老师,也基本上把游戏做了出来,因为刚开始学,能做出一个功能就感觉很有成就感,注释也都没加。

 

下面这个是小鸟对象的代码:包括循环播放图片帧,让小鸟舞动翅膀飞起来;小鸟的上抛运动算法;

package com;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;class Bird {    BufferedImage image;    BufferedImage[] images;    int i;    int x, y;    int width, height;    int size;    double v0, g, s, t, speed;    double alpha;    public Bird() throws Exception {        images = new BufferedImage[8];        for (i = 0; i < 8; i++) {            images[i] = ImageIO.read(getClass().getResource(i + ".png"));        }        image = images[0];        x = 132;        y = 280;        v0 = 5;        speed = v0;        g = 0.5;        t = 0.25;        width = image.getWidth();        height = image.getHeight();        size = 40;    }    public void fly() {        i++;        image = images[(i / 10) % 8];    }    public void step() {        double v0 = speed;        s = v0 * t - g * t * t / 2;        y = y - (int) s;        speed = v0 - g * t;        alpha = Math.atan(s / 12);        }    public void flappy() {        speed = v0;    }    public boolean hit(Ground ground){    boolean hit=y+size/2>ground.y;    if (hit){        y=ground.y-size/2;    }    return hit;    }    public boolean hit (Column column){        if (x>column.x-column.width/2-size/2&&x
column.y-column.gap/2+size/2){ return false; } return true; } return false; }}

下面是柱子对象的代码:实现了柱子的移动算法;

package com;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;class Column {    BufferedImage image;    int x,y;    int distance;    int width,height;    int gap;        public Column(int n) throws Exception{        image=ImageIO.read(getClass().getResource("column.png"));        Random random=new Random();        y=random.nextInt(120)+220;        distance= 245;        gap=144;        x=550+(n-1)*distance;        width=image.getWidth();        height=image.getHeight();    }    public void step(){        x--;        if(x==-width/2) x=distance*2+width/2;    }}

下面是游戏主体代码:实现了地面的运动;小鸟柱子对象的移动和碰撞实现;以及鼠标事件的实现。

package com;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 java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;public class World extends JPanel {    BufferedImage background;    BufferedImage gameOverImage;    BufferedImage startedImage;    Ground ground;    Column column1;    Column column2;    Bird bird;    boolean gameOver;    boolean started;    int score;    public World() throws Exception {        background = ImageIO.read(getClass().getResource("bg.png"));        gameOverImage = ImageIO.read(getClass().getResource("gameover.png"));        startedImage = ImageIO.read(getClass().getResource("start.png"));        ground = new Ground();        column1 = new Column(1);        column2 = new Column(2);        bird = new Bird();        gameOver = false;        started = false;        score = 0;    }    public static void main(String[] args) throws Exception {        JFrame frame = new JFrame("飞翔的小鸟——by ChenJ");        System.out.println();        World game = new World();        frame.add(game);        frame.setSize(400, 600);        frame.setLocationRelativeTo(null);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);        game.action();    }    public void paint(Graphics g) {        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 - column1.width / 2, column2.y                - column1.height / 2, null);        g.drawImage(ground.image, ground.x, ground.y, null);        Font f = new Font(Font.SANS_SERIF, Font.BOLD, 40);        g.setFont(f);        g.setColor(Color.white);        g.drawString("得分:" + score, 40, 60);        Graphics2D g2 = (Graphics2D) g;        g2.rotate(-bird.alpha, bird.x, bird.y);        g.drawImage(bird.image, bird.x -bird.width /2, bird.y - bird.height                / 2, null);        g2.rotate(bird.alpha, bird.x, bird.y);        if (gameOver)            g.drawImage(gameOverImage, 0, 0, null);        if (!started)            g.drawImage(startedImage, 0, 0, null);//        g.drawRect(bird.x-bird.size/2, bird.y-bird.size/2, bird.size, bird.size);//        g.drawRect(column1.x-column1.width/2,0, column1.width, column1.y-column1.gap/2);    }    public void action() throws Exception {        MouseListener l1 = new MouseAdapter() {            @Override            public void mousePressed(MouseEvent e) {                try {                    if (gameOver) {                        column1 = new Column(1);                        column2 = new Column(2);                        bird = new Bird();                        started = false;                        gameOver = false;                        score = 0;                                                                    } else {                        started = true;                        bird.flappy();                    }                } catch (Exception x) {                    x.printStackTrace();                }            }        };        addMouseListener(l1);        while (true) {            if (bird.hit(ground) || bird.hit(column1) || bird.hit(column2)) {                gameOver = true;            }            if (!gameOver) {                if (started) {                    column1.step();                    column2.step();                                        bird.step();                    score++;                    if (bird.x == column1.x || bird.x == column2.x) {                        score+=100;                    }                }                                ground.step();                bird.fly();            }                        repaint();            Thread.sleep(1000 / 60);        }    }}

第一次写代码,很不规范,也没有注释 。

---恢复内容结束---

转载于:https://www.cnblogs.com/djavachen/p/3633015.html

你可能感兴趣的文章
CSS3 Box-sizing
查看>>
并发编程:守护进程、互斥锁、案例、进程间通讯
查看>>
如何使带背景图片的Button按钮中的文字居中偏上显示
查看>>
memcache、redis、mongoDB 如何选择?
查看>>
PHP获取汉字拼音首字母
查看>>
正则表达式2
查看>>
JS同源策略和跨域访问
查看>>
正则 去除html标签
查看>>
FZU 1889 龟兔赛跑
查看>>
java基础-Comparator接口与Collections实现排序算法
查看>>
ddrmenu
查看>>
Linux Shell常用shell命令
查看>>
项目上的阶段小结(二)
查看>>
android同一个TextView设置不同颜色字体
查看>>
YourSQLDba将数据库置于紧急模式的原因浅析
查看>>
第三次Java作业
查看>>
ECSHOP去版权_ECSHOP2.7.2去版权方法-最新方法
查看>>
购物也能乐开花 淘宝搞笑评价集萃--2
查看>>
华为离职副总裁徐家骏:年薪千万的工作感悟
查看>>
java SE :标准输入/输出
查看>>