JAVA下的桌球运动轨迹

       本项目基于对Java语言学习所做的小项目,通过不断刷新桌子的静态图,实现肉眼可见的母球运动轨迹。如果想更好实现游戏效果。

1.可以加入不同的初始速度变量来模拟不同力道下球的运动。

2.设定减速变量代表桌子的摩擦力。

3.加塞下的不同运动轨迹等需要制定相对应的轨迹函数,需要借助数学工具带入。

4.至于其他子球入洞的效果是需要判定该球到入的洞口的坐标,

   我本人感觉IJ做成该游戏具有一定复杂性,可以用unity相应的游戏平台更便捷。代码仅供参考,相应的图片和参数都需要自己调整,后续我会把完整项目放入网盘,供大家查阅。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hellojava.firstproject;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import javax.swing.JFrame;

public class ballGame extends JFrame {
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball(1)(1).png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk4(1).png");
    double x = 200.0;
    double y = 200.0;
    double degree = -1.0466666666666666;

    public ballGame() {
    }

    public void paint(Graphics g) {
        System.out.println("窗口被画了一次。");
        g.drawImage(this.desk, -20, -10, (ImageObserver)null);
        g.drawImage(this.ball, (int)this.x, (int)this.y, (ImageObserver)null);
        this.x += 10.0 * Math.cos(this.degree);
        this.y += 10.0 * Math.sin(this.degree);
        if (this.y > 405.0 || this.y < 80.0) {
            this.degree = -this.degree;
        }

        if (this.x > 720.0 || this.x < 55.0) {
            this.degree = 3.14 - this.degree;
        }

    }

    void launchFrame() {
        this.setSize(815, 480);
        this.setLocation(100, 100);
        this.setVisible(true);

        while(true) {
            this.repaint();

            try {
                Thread.sleep(40L);
            } catch (InterruptedException var2) {
                var2.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("我的小游戏开始了!");
        ballGame game = new ballGame();
        game.launchFrame();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值