【JAVA】重力反弹,反弹高次一次比一次低

本来是想实现泡泡屏保(javascript实现漂亮的气泡碰撞效果(Chrome浏览器下更佳) 下载-脚本之家)的,还未实现 

20231221晚上整理了下,看起来简洁些,也加了点注释:

JAVA小球碰撞_哔哩哔哩_bilibili

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;

class Bubble
{
    public int x;
    public int y;
    public int vx;
    public int vy;
    public int radius;

    public Bubble(int x, int y, int vx, int vy, int radius) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.radius = radius;
    }

    @Override
    public String toString() {
        return "Bubble{" + "x=" + x + ", y=" + y + ", vx=" + vx + ", vy=" + vy + ", radius=" + radius + '}';
    }
}

public class MainFrame extends JFrame implements Runnable {
    Graphics graphics;
    private final int windowWidth; //画板的宽度
    private final int windowHeight; //画板的高度
    private final LinkedList<Bubble> bubbleLinkedList;

    MainFrame()
    {
        windowWidth = 629;
        windowHeight = 990;
        setSize(windowWidth, windowHeight);
        setVisible(true);
        graphics = getContentPane().getGraphics();
        bubbleLinkedList = new LinkedList<>();

        //初始化小球
        for (int i = 0; i < 3; i++) {
            int x =(int) ((Math.random() - 0.5) * windowWidth);
            int y = (int) ((Math.random() - 0.5) * windowHeight);
            int vx = (int) ((Math.random() - 0.5) * 16);
            int vy = (int) ((Math.random() - 0.5) * 16);
            bubbleLinkedList.add(new Bubble(x, y, vx, vy, 40));
        }
    }

    @Override
    public void run() {
        while (true)
        {
            graphics.clearRect(0, 0, windowWidth, windowHeight);
            int color = 0;
            for (Bubble b: bubbleLinkedList) {
                graphics.setColor(new Color(color,0,0));
                graphics.fillArc(b.x,b.y, b.radius, b.radius,0,360);
                move(b);
                color += 255 / bubbleLinkedList.size();
            }

            for (Bubble bubble0: bubbleLinkedList)
            {
                for (Bubble bubble1: bubbleLinkedList)
                {
                    if (!bubble0.equals(bubble1)) {
                        BulletHit(bubble0, bubble1);
                    }
                }
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void move(Bubble bubble) //球移动,并且判断是否超出屏幕的上下左右
    {
        bubble.vy += 2;
        bubble.x += bubble.vx;
        bubble.y += bubble.vy;
        System.out.println(bubble);
        if (bubble.y > windowHeight - 2 * bubble.radius)
        {
            bubble.y = windowHeight - 2 * bubble.radius;
            bubble.vy *= -0.95;
        }

        if (bubble.y <  bubble.radius )
        {
            bubble.y = bubble.radius;
            bubble.vy *= -0.95;
        }

        if (bubble.x  > windowWidth - bubble.radius) {
            bubble.x = windowWidth - bubble.radius;
            bubble.vx *= -1;
        }
        if( bubble.x < 0 )
        {
            bubble.x = 0;
            bubble.vx *= -1;
        }
    }

    public void BulletHit(Bubble bubble0, Bubble bubble1) //如果两个球撞击
    {
        int dx = bubble1.x - bubble0.x;
        int dy = bubble1.y - bubble0.y;
        int DistanceCenter = (int) Math.sqrt(dx * dx + dy * dy);
        int DistanceRadius = (bubble0.radius + bubble1.radius) / 2;
        if (DistanceCenter < DistanceRadius)
        {
            double angle = Math.atan2(dy, dx);
            double ax = (bubble0.x - bubble1.x + Math.cos(angle) * DistanceRadius) * 0.9;
            double ay = (bubble0.y - bubble1.y + Math.sin(angle) * DistanceRadius) * 0.9;

            bubble0.vx -= ax;
            bubble1.vx += ax;
            bubble0.vy -= ay;
            bubble1.vy += ay;
        }
    }

    public static void main(String[] args) {
        new Thread(new MainFrame()).start();
    }
}

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;

class Bubble
{
    public static Image image;
    public int x;
    public int y;
    public int vx;
    public int vy;
    public int displayWidth;
    public int displayHeight;
    public int gravity;

    public double bounce;

    public Bubble(String image, int x, int y, int vx, int vy, int displayWidth, int displayHeight) {
        this.image = Toolkit.getDefaultToolkit().getImage("src//" + image);
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.displayWidth = displayWidth;
        this.displayHeight = displayHeight;
        gravity = 1;

        bounce = 0.9;

    }

    public void move()
    {
    }

    @Override
    public String toString() {
        return "Bubble{" +
                "x=" + x +
                ", y=" + y +
                ", vx=" + vx +
                ", vy=" + vy +
                ", displayWidth=" + displayWidth +
                ", displayHeight=" + displayHeight +
                ", gravity=" + gravity +
                ", bounce=" + bounce +
                '}';
    }
}

public class MainFrame extends JFrame implements Runnable {
    Graphics graphics;
    Image image;

    int x;
    int y;
    private final int windowWidth;//画板的宽度
    private final int windowHeight;//画板的高度
    private LinkedList<Bubble> bubbleLinkedList;

    private double gravity= 2;  // 重力

    {
        setLayout(null);
        windowWidth = 629;
        windowHeight = 990;
        setSize(windowWidth, windowHeight);
        setLocationRelativeTo(null);
        setVisible(true);
        graphics = getContentPane().getGraphics();
    }

    MainFrame()
    {
        bubbleLinkedList = new LinkedList<>();
        for (int i = 0; i < 1; i++) {

            int x = new Random().nextInt(windowWidth / 10);
            int y = new Random().nextInt(windowHeight / 20);
            int vx = (int) (Math.random() * 16 - 8);
            int vy = (int) (Math.random() * 16 - 8);
            bubbleLinkedList.add(new Bubble("background.jpg", x, y, vx, vy, 20, 20));
        }
    }

    @Override
    public void run() {
        while (true)
        {
            for (Bubble b: bubbleLinkedList) {
                graphics.drawImage(b.image, b.x, b.y, b.displayWidth, b.displayHeight, this);
                BulletHit();
                b.move();
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void move(Bubble bubble)
    {
        //gravity += 2;
        bubble.vy += gravity;
        bubble.x += bubble.vx;
        bubble.y += bubble.vy;
        System.out.println(bubble.x + ": " + bubble.y + ": " + bubble.displayHeight + ": " + windowHeight);
        if (bubble.y + bubble.displayHeight > windowHeight)
        {
            bubble.y = windowHeight  - bubble.displayHeight;
            bubble.vy *= -0.95;
        }

        if (bubble.x + bubble.displayWidth > windowWidth) {
            bubble.x = windowWidth  - bubble.displayWidth;
            bubble.vx *= -1;
        }
        if( bubble.x <= bubble.displayWidth )
        {
            bubble.x = bubble.displayWidth;
            bubble.vx *= -1;
        }
    }

    public void BulletHit()
    {
        for (Bubble bubble0: bubbleLinkedList)
        {
            for (Bubble bubble1: bubbleLinkedList)
            {
                if (!bubble0.equals(bubble1))
                {
                    double x = Math.abs(bubble0.x - bubble1.x);
                    double x0 = bubble0.x - bubble1.x;
                    double y = Math.abs(bubble0.y - bubble1.y);
                    double y0 = bubble0.y - bubble1.y;
                    double dist = Math.sqrt(x * x + y * y);
                    if (dist <= bubble0.displayWidth)
                    {
                        double angle = Math.atan2(y0, x0);
                        double tx = bubble0.x + Math.cos(angle) * dist;
                        double ty = bubble0.y + Math.sin(angle) * dist;
                        double ax = (tx - bubble1.x) * 0.9;
                        double ay = (ty - bubble1.y) * 0.9;

                        bubble0.x -= ax;
                        bubble0.y -= ay;
                        bubble1.x -= ax;
                        bubble1.y -= ay;
                    }
                }
            }
        }

        for (Bubble bubble0: bubbleLinkedList)
        {
            move(bubble0);
        }
    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        new Thread(mainFrame).start();
    }
}

显示效果:

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;

class Bubble
{
    public static Image image;
    public int x;
    public int y;
    public int displayWidth;
    public int displayHeight;
    public int gravity;

    public double bounce;

    public Bubble(String image, int x, int y, int displayWidth, int displayHeight) {
        this.image = Toolkit.getDefaultToolkit().getImage("src//" + image);
        this.x = x;
        this.y = y;
        this.displayWidth = displayWidth;
        this.displayHeight = displayHeight;
        gravity = 1;

        bounce = 0.9;

    }

    public void move()
    {
    }

    @Override
    public String toString() {
        return "Bubble{" +
                "x=" + x +
                ", y=" + y +
                ", displayWidth=" + displayWidth +
                ", displayHeight=" + displayHeight +
                '}';
    }
}

public class MainFrame extends JFrame implements Runnable {
    Graphics graphics;
    Image image;

    int x;
    int y;
    private final int windowWidth;//画板的宽度
    private final int windowHeight;//画板的高度
    private LinkedList<Bubble> bubbleLinkedList;

    private double ballsnum= 5;   // 小球数目
    private double spring= 0.8;   // 弹力加速度
    private double bounce= -0.95; // 反弹
    private double gravity= 0.1;  // 重力

    {
        setLayout(null);
        windowWidth = 629;
        windowHeight = 990;
        setSize(windowWidth, windowHeight);
        setLocationRelativeTo(null);
        setVisible(true);
        graphics = getContentPane().getGraphics();
    }

    MainFrame()
    {
        bubbleLinkedList = new LinkedList<>();
        for (int i = 0; i < 1; i++) {

            Integer x = new Random().nextInt(windowWidth / 10);
            Integer y = new Random().nextInt(windowHeight / 20);
            bubbleLinkedList.add(new Bubble("background.jpg", x, y, 20, 20));
        }
    }

    @Override
    public void run() {
        while (true)
        {
            for (Bubble b: bubbleLinkedList) {
                graphics.drawImage(b.image, b.x, b.y, b.displayWidth, b.displayHeight, this);
                BulletHit();
                b.move();
            }

            y+=10;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void move(Bubble bubble)
    {
        gravity += 2;
        bubble.x += 2;
        bubble.y += gravity;
        System.out.println(bubble.x + ": " + bubble.y + ": " + bubble.displayHeight + ": " + windowHeight);
        if (bubble.y + bubble.displayHeight > windowHeight)
        {
            bubble.y = windowHeight  - bubble.displayHeight;
            gravity *= -0.8;
        }
    }

    public void BulletHit()
    {
        for (Bubble bubble0: bubbleLinkedList)
        {
            for (Bubble bubble1: bubbleLinkedList)
            {
                if (!bubble0.equals(bubble1))
                {
                    double x = Math.abs(bubble0.x - bubble1.x);
                    double x0 = bubble0.x - bubble1.x;
                    double y = Math.abs(bubble0.y - bubble1.y);
                    double y0 = bubble0.y - bubble1.y;
                    double dist = Math.sqrt(x * x + y * y);
                    if (dist <= bubble0.displayWidth)
                    {
                        double angle = Math.atan2(y0, x0);
                        double tx = bubble0.x + Math.cos(angle) * dist;
                        double ty = bubble0.y + Math.sin(angle) * dist;
                        double ax = (tx - bubble1.x) * spring;
                        double ay = (ty - bubble1.y) * spring;

                        bubble0.x -= ax;
                        bubble0.y -= ay;
                        bubble1.x -= ax;
                        bubble1.y -= ay;
                    }
                }
            }
        }

        for (Bubble bubble0: bubbleLinkedList)
        {
            move(bubble0);
        }
    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        new Thread(mainFrame).start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值