本节书摘来异步社区《Java 2D游戏编程入门》一书中的第8章,第8.5节,作者:【美】Timothy Wright(莱特),更多章节内容可以访问云栖社区“异步社区”公众号查看。
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 ) ); } 相关资源:Java 2D游戏编程入门_PDF电子书下载 高清 带索引书签目录