小球滚动

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.util.Random;

import javax.swing.*;

class jj extends JFrame {
}

public class jumpball extends JFrame {

 protected long sleepAmount = 10;

 public jumpball() {

  setBackground(Color.CYAN);
  Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
  Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
  int screenWidth = screenSize.width; // 获取屏幕的宽
  int screenHeight = screenSize.height; // 获取屏幕的高
  setLocation(new Point(screenWidth / 2 - getWidth() / 2, screenHeight / 2 - getHeight() / 2));
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  JButton mb = new JButton();
  mb.setText("click");
  JPanel pan = new JPanel();
  pan.setBackground(Color.CYAN);
  pan.setLayout(new FlowLayout());
  pan.add(mb);
  setSize(500, 500);
  final Mypaint mp = new Mypaint();
  // mp.setBackground(Color.BLACK);
  add(mp);

  getContentPane().add(pan, BorderLayout.NORTH);
  mb.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    Thread tt = new Thread(new Runnable() {

     @Override
     public void run() {
      // TODO Auto-generated method stub

      try {
       while (true) {
        mp.repaint();
        Thread.sleep(sleepAmount);
       }
      } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
    });
    tt.start();
   }
  });

 }

 public static void main(String[] args) {
  jumpball my = new jumpball();
  my.setVisible(true);
 }
}

class Mypaint extends JPanel {
 int x = 15, y = (int) (Math.random() * 1000 % getHeight());
 double radius = 15;
 int step = 5;
 double k = Math.random() * Math.PI * 2;

 public Mypaint() {
  setDoubleBuffered(true);
 }

 @Override
 public void paint(Graphics g) {
  // TODO Auto-generated method stub
  super.paint(g);
  
  if (x < 0 || x > getWidth() - radius * 2) {
//   if (k <= Math.PI)
    k = -k + Math.PI * 2;
   // if (k < 0)
   // k = 2 * Math.PI + k;
  } else if (y < 0 || y > getHeight() - radius * 2) {
   k = Math.PI - k;
   
  }

  x += Math.sin(k) * step;
  y += Math.cos(k) * step;
  Arc2D arc = new Arc2D.Float(1);
  arc.setFrame(x, y, radius * 2, radius * 2);
  arc.setAngleExtent(360);
  Graphics2D g2 = (Graphics2D) g;
  g2.setColor(Color.BLUE);
  g2.fill(arc);

 }

}

 

### Unity 中实现小球滚动效果 在 Unity 中创建一个小球滚动效果涉及多个方面,包括物理模拟、输入处理以及相机跟随等功能。下面提供了一个完整的解决方案。 #### 设置物理材质 为了使小球能够更加真实地滚动,在项目中可以调整其物理材质。通过改变摩擦力和其他参数可以让物体表面变得更光滑或粗糙[^1]: ```csharp using UnityEngine; public class PhysicsMaterialAdjustment : MonoBehaviour { public PhysicMaterial smoothMaterial; // 光滑物理材质 void Start() { Collider collider = GetComponent<Collider>(); collider.material = smoothMaterial; } } ``` #### 控制小球运动 为了让玩家可以通过键盘或其他设备控制小球的方向和速度,通常会编写一段脚本来读取用户的输入并据此更新 Rigidbody 组件的状态。这里给出一个简单的例子来展示如何基于用户输入驱动小球前进[^2]: ```csharp using UnityEngine; public class BallController : MonoBehaviour { private Rigidbody rb; void Awake(){ rb = GetComponent<Rigidbody>(); } void FixedUpdate(){ float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); } } ``` #### 摄像机跟随机制 当角色移动时保持摄像机能始终聚焦在其身上非常重要。对于这个特定的应用场景来说,可以使摄像机如同放风筝一般固定在一个相对位置上随着目标一起平移而不会发生旋转变化[^3]: ```csharp using System.Collections; using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; // 小球的位置 private Vector3 offset; // 初始偏移量 void Start () { offset = transform.position - target.position; } void LateUpdate () { transform.position = target.position + offset; } } ``` 以上代码片段展示了怎样利用 Unity 的内置工具和技术去构建一个基本的小球滚动画面对战游戏原型。当然实际开发过程中还需要考虑更多细节比如碰撞响应、得分计算等逻辑部分。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值