Finite Improbability

Just programming and math, no spontaneously jumping undergarments

Game dev in Scala II

More playing around with Scala and slick. Here’s the GeomTest.java file rewritten in scala:

/* A conversion of the GeomTest.java program from the slick tests to
 scala. Of special note is the replacement of the ternary operator
 with a value storing an anonymous fnction.  */

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Ellipse;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.RoundedRectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;

/**
 * A geomertry test
 */
object GeomTest {
  class GeomTest(s : String) extends BasicGame(s : String) {
    /** The rectangle drawn */
    val rect = new Rectangle(100,100,100,100);
    /** The rectangle drawn */
    val circle = new Circle(500,200,50);
    /** The rectangle tested */
    val rect1 = new Rectangle(150,120,50,100).transform(Transform.createTranslateTransform(50, 50));
    /** The rectangle tested */
    val rect2 = new Rectangle(310,210,50,100).transform(
      Transform.createRotateTransform(Math.toRadians(45).asInstanceOf[Float], 335, 260));
    /** The circle tested */
    val circle1 = new Circle(150,90,30);
    /** The circle tested */
    val circle2 = new Circle(310,110,70);
    /** The circle tested */
    val circle3 = new Ellipse(510, 150, 70, 70);
    /** The circle tested */
    val circle4 = new Ellipse(510, 350, 30, 30).transform(
      Transform.createTranslateTransform(-510, -350)).transform(
        Transform.createScaleTransform(2, 2)).transform(
          Transform.createTranslateTransform(510, 350));
    /** The RoundedRectangle tested */
    val roundRect = new RoundedRectangle(50, 175, 100, 100, 20);
    /** The RoundedRectangle tested - less cornders */
    val roundRect2 = new RoundedRectangle(50, 280, 50, 50, 20, 20, RoundedRectangle.TOP_LEFT | RoundedRectangle.BOTTOM_RIGHT);

    /**
     * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
     */
    override def init(container : GameContainer) { }

    /**
     * @see org.newdawn.slick.BasicGame#render(org.newdawn.slick.GameContainer, org.newdawn.slick.Graphics)
     */
    override def render(container : GameContainer, g : Graphics) {
      g.setColor(Color.white);
      g.drawString("Red indicates a collision, green indicates no collision", 50, 420);
      g.drawString("White are the targets", 50, 435);

      g.scale(10, 10);
      g.setColor(Color.red);
      g.fillRect(10,0,5,5);
      g.setColor(Color.white);
      g.drawRect(10,0,5,5);
      g.scale(1/10.0f, 1/10.0f);

      g.setColor(Color.white);
      g.draw(rect);
      g.draw(circle);


//       Since Scala doesn't have a ternary operator (that I know
//       of) we'll replace it with a custom function here. Without
//       digging into support for lazy evaluation and such I can't
//       implement an exact analog of a ternary operator.

      val pickColor = (s1 : Shape, s2 : Shape) => {
    if(s1.intersects(s2))
      g.setColor(Color.red);
    else
      g.setColor(Color.green);
      }

//       Just to clean things up a bit, we'll encapsulate the
//       pickColor->draw motion in a named function.

      def drawIntersection(s1 : Shape, s2 : Shape) {
    pickColor(s1, s2);
    g.draw(s1);
      }

      drawIntersection(rect1, rect);
      drawIntersection(rect2, rect);
      drawIntersection(roundRect, rect);
      drawIntersection(circle1, rect);
      drawIntersection(circle2, rect);
      drawIntersection(circle3, circle);
      drawIntersection(circle4, circle);

      g.draw(roundRect2);
      g.setColor(Color.blue);
      g.draw(new Circle(100,100,50));
      g.drawRect(50,50,100,100);

    }

    /**
     * @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer, int)
     */
    override def update(container : GameContainer, delta : int) { }

    /**
     * @see org.newdawn.slick.BasicGame#keyPressed(int, char)
     */
    override def keyPressed(key : int, c : char) {
      if (key == Input.KEY_ESCAPE) {
    System.exit(0);
      }
    }
  }
  /**
   * Entry point to our test
   * 
   * @param argv The arguments passed to the test
   */
  def main(argv : Array[String]) {
    try {
      val container = new AppGameContainer(new GeomTest("Geometry Test"));
      container.setDisplayMode(800,600,false);
      container.start();
    } catch {
      case e : SlickException => e.printStackTrace();
    }
  }
}
1 comment

1 Comment so far

  1. James Iry March 11th, 2008 6:35 pm

    “Since Scala doesn’t have a ternary operator (that I know of”

    It does – Scala’s “if” works like both Java’s if and ternary operator. Without an else it always returns unit and you would only call it for side effects. With an else it returns the result of the expression evaluated.

    g.setColor(if(s1.intersects(s2)) Color.red else Color.green)

Leave a reply