• [苦逼程序员的成长之路]1、飞扬小鸟


    跟这老师写的一个飞扬小鸟游戏,写的时候,刚刚接触后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.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;
        }
    
    }

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

    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);
            }
    
        }
    }

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

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

  • 相关阅读:
    无限极分类查找所有子孙节点的改进算法
    递归方式将数组变对象
    中文编码与解码
    MYSQL数据库
    python之进程池与线程池
    ORM介绍
    python之协程
    windows10如何将python2和python3添加到环境变量中
    互斥锁、死锁现象、递归锁、信号量
    python之GIL(Global Interpreter Lock)
  • 原文地址:https://www.cnblogs.com/djavachen/p/3633015.html
Copyright © 2020-2023  润新知