本项目基于对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();
}
}