《Java 2D游戏编程入门》—— 8.5 原型Bullet类

本节书摘来异步社区《Java 2D游戏编程入门》一书中的第8章,第8.5节,作者:【美】Timothy Wright(莱特),更多章节内容可以访问云栖社区“异步社区”公众号查看。

8.5 原型Bullet类

PrototypeBullet代码位于javagames.prototype包中,这是一个最简单的原型游戏源代码。除了绘制圆以便可以调整屏幕大小外,没有其他任何值得讨论的内容。

package javagames.prototype;
import java.awt.*;
import javagames.util.*;

public class PrototypeBullet {
    private Vector2f velocity;
    private Vector2f position;
    private Color color;
    private float radius;
    public PrototypeBullet( Vector2f position, float angle ) {
     this.position = position;
      velocity = Vector2f.polar( angle, 1.0f );
      radius = 0.006f;
      color = Color.GREEN;
  }
  public Vector2f getPosition() {
    return position;
  }
  public void draw( Graphics2D g, Matrix3x3f view ) {
    g.setColor( color );
    Vector2f topLeft = new Vector2f(
      position.x - radius, position.y + radius );
    topLeft = view.mul( topLeft );
    Vector2f bottomRight = new Vector2f(
      position.x + radius, position.y - radius );
    bottomRight = view.mul( bottomRight );
    int circleX = (int)topLeft.x;
    int circleY = (int)topLeft.y;
    int circleWidth = (int)(bottomRight.x - topLeft.x);
    int circleHeight = (int)(bottomRight.y - topLeft.y);
    g.fillOval(
      circleX, circleY, circleWidth, circleHeight
    );
  }
  public void update( float time ) {
    position = position.add( velocity.mul( time ) );
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值