天天看点

《Java 2D游戏编程入门》—— 8.4 用原型小行星工厂生产小行星

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

位于javagames.prototype包中的prototypeasteroidfactory,包含了3个大的、3个中型的和3个小的小行星,它们都是使用编辑器并粘贴到代码中设计的。给定一个位置,这3个方法生成一个随机数来选择所返回的小行星:

// prototypeasteroidfactory.java

private vector2f[] mirror( vector2f[] polygon ) {

  vector2f[] mirror = new vector2f[ polygon.length ];

  float x = rand.nextboolean() ? 1.0f : -1.0f;

  float y = rand.nextboolean() ? 1.0f : -1.0f;

    matrix3x3f mat = matrix3x3f.scale( x, y );

    for( int i = 0; i < polygon.length; ++i ) {

    mirror[i] = mat.mul( polygon[i] );

  }

  return mirror;

}<code>`</code>

就像在执行视口矩阵乘法的时候使用矩阵来翻转y轴一样,通过乘以-1来翻转x或y轴的值,以缩放模型。

// randomasteroidexample.java

protected void render( graphics g ) {

  super.render( g );

  g.drawstring( "press esc to respawn", 20, 35 );

  matrix3x3f view = getviewporttransform();

  for( prototypeasteroid asteroid : asteroids ) {

    asteroid.draw( (graphics2d)g, view );

}

randomasteroidexample代码如下所示:

package javagames.prototype;

import java.awt.*;

import java.awt.event.keyevent;

import java.util.*;

import javagames.prototype.prototypeasteroid.size;

import javagames.util.*;

public class randomasteroidexample extends simpleframework {

  private prototypeasteroidfactory factory;

  private arraylist asteroids;

  private random rand;

  public randomasteroidexample() {

    appborderscale = 0.9f;

    appwidth = 640;

    appheight = 640;

    appmaintainratio = true;

    appsleep = 1l;

    apptitle = "random asteroids";

    appbackground = color.white;

    appfpscolor = color.black;

  @override

  protected void initialize() {

    super.initialize();

    rand = new random();

    asteroids = new arraylist();

    polygonwrapper wrapper =

      new polygonwrapper( appworldwidth, appworldheight );

    factory = new prototypeasteroidfactory( wrapper );

    createasteroids();

  private void createasteroids() {

    asteroids.clear();

    for( int i = 0; i &lt; 42; ++i ) {

      asteroids.add( getrandomasteroid() );

    }

  private prototypeasteroid getrandomasteroid() {

    float x = rand.nextfloat() * 2.0f - 1.0f;

    float y = rand.nextfloat() * 2.0f - 1.0f;

    vector2f position = new vector2f( x, y );

    size[] sizes = size.values();

    size randomsize = sizes[ rand.nextint( sizes.length ) ];

    switch( randomsize ) {

      case small: return factory.createsmallasteroid( position );

      case medium: return factory.createmediumasteroid( position );

      case large:

      default: return factory.createlargeasteroid( position );

  protected void processinput( float delta ) {

    super.processinput( delta );

    if( keyboard.keydownonce( keyevent.vk_escape ) ) {

      createasteroids();

  protected void updateobjects( float delta ) {

    super.updateobjects( delta );

    for( prototypeasteroid asteroid : asteroids ) {

      asteroid.update( delta );

  protected void render( graphics g ) {

    super.render( g );

    g.drawstring( "press esc to respawn", 20, 35 );

    matrix3x3f view = getviewporttransform();

      asteroid.draw( (graphics2d)g, view );

  public static void main( string[] args ) {

    launchapp( new randomasteroidexample() );