Update!! I’m adding more info about setting up the project at the tail end of this post. Please read after the Maven2 pom.xml to see the updated information.
What’s up party people??!! I’m off and running with a series devoted to creating the classic SpaceInvaders game using Groovy as my programming language of choice. Those of you who have been following me posts will recall that I’ve been wanting to post something on Open GL and Groovy. Here’s a short history on that idea. I found a simple JOGL Java tutorial that I re-coded with Groovy and tried to run. This is what was supposed to happen afterwards. I would run the code which would result in a screen with a really exciting triangle drawn on it. I would then post the code here and tell you all how cool I am because I can draw a triangle and you can’t. I would then spend another day or two reading and recoding and have Doom 3 implemented completely in Groovy while bragging some more on this here blog wagging my finger at nay sayers that don’t believe Groovy is worth a dime. Here’s what actually happened. I finished writing the code and hit run. I cussed because I got an error explaining something about a native library couldn’t be locted. I cussed some more after fixing that minor problem and a couple of other dumb issues. I finally got all jars and native libraries installed for JOGL, ran the code and found out that the example must have been using an earlier implementation of JOGL than what I was using. I browsed the net some looking for more up to date examples and cussed some more. I then resolved to do the one thing that would benefit neither me nor my employer in any way, write a video game using Java 2D. (Learn to walk before you run!) And now, here we are.
Groovy Invaders
I’ll begin my series with the link to the finished product (created by someone else in Java for the purpose of this tutorial) demonstrating what my hard work should look like at the end. I’ll also include a snippet of my Maven2 project descriptor because, well, I’m using Maven2 for everything nowadays.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.craig.groovyinvaders</groupId>
<artifactId>xsl-runner</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Groovy Space Invaders</name>
<url>http://codeforfun.wordpress.com</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>groovy</groupId>
<artifactId>groovy-all-1.0-jsr</artifactId>
<version>05</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<tasks>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.compile.classpath"/>
</taskdef>
<groovyc destdir="${project.build.outputDirectory}"
srcdir="${basedir}/src/main/groovy/" listfiles="true">
<classpath refid="maven.compile.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<configuration>
<tasks>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.compile.classpath"/>
</taskdef>
<mkdir dir="${project.build.testOutputDirectory}"/>
<groovyc destdir="${project.build.testOutputDirectory}"
srcdir="${basedir}/src/test/groovy/" listfiles="true">
<classpath refid="maven.test.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
That code is tucked in a little file named “pom.xml” that I nested in a folder called “groovy-invaders”. If you want to follow along I suggest you right click somewhere in Windows Explorer or Konqueror or what ever you use to browse your file system and create those same two artifacts. I’ve tested the Maven2 pom against my groovy invaders project and verified that it can compile all Groovy source and run all Groovy test cases. That is an important item to note because a few people have asked how to get Groovy source and Groovy tests integrated with Maven.
Updated setup information
To setup for development you may use Maven, Maven2, Ant, Eclipse, Idea or whatever you’re comfortable with. I will be following Maven2 conventions for the layout of the project and that should work just fine in most build tools and IDEs. I will assume you are following a similar structure which would indicate the Groovy source will be compiled before running it. It is not necessary to compile Groovy I will be compiling throughout the tutorial. The structure by convention will include everything under a project folder labeled groovy-invaders. In the project we will use “src/main/groovy” as the home for all of our Groovy source code. The package name I use is com/craig/groovyinvaders so most of our Groovy source will live unders “src/main/groovy/com/craig/groovyinvaders”. We will also use “src/resources” as the home for anything else that needs to be on the classpath for our application. (Our graphics will live in a folder called sprites in this directory.) In the root of the project folder we will keep our Maven2 project descriptor (the pom.xml file).
That’s all for today. I’ll start filling in the blanks with code later. Until then, holla at me…

[...] Clifton Craig has started an interesting series over on his blog called Groovy Invaders where he is attempting to write a space invaders game in Groovy using JOGL for the graphics routines. [...]
By: Blog Yellek » Blog Archive » Groovy Invaders on August 7, 2006
at 11:37 pm
[...] Yesterday I began a trail on my development of the classic Space Invaders using Groovy. So far I only shared my Maven “pom.xml” which I will use to build my final project. I’d eventually like to execute the project with Maven if possible/practical but since I’m still a newbie I’ll stay content with a basic Maven build. Let’s move onto the code for the game. I explained yesterday that if you wanted to follow along you should create a folder named groovy-invaders somewhere on your hard drive to hold all of our hard work. It doesn’t need to be called groovy-invaders at all; you can call it whatever you wish. (Name the folder “crap-that-occupies-my-time-when-i-should-be-working” if your operating system supports really long folder names.) The code in my little series is all based on code found in a tutorial here. [...]
By: Can’t see nothing but the source code » Groovy Invaders II on August 8, 2006
at 8:44 am
[...] You can copy these class definitions into corresponding files. Extra points goes to the one who can modify the write method in my script to make it write the source code to the file system in the appropriate place. Initializing our game will require that all of our entities be created and stored in instance variables so they can be managed in other methods in our Game object. We use our entities list declared at the top of Game.groovy to hold all of our entities. We want to treat the ShipEntity special as it also gets its own individual instance variable. We’ll want to provide user control over the ship so we don’t want to lose it in the list of other entities. Put a call to initEntities() method at the end of the Game constructor, define a ship instance variable and an alien counter before filling out the initEntities() method definition. In initEntities() we instantiate a ShipEntity and instantiate 60 AlienEntity objects laying the ship at the bottom center of the screen and arranging the aliens in 5 rows at the top of the screen. private void initEntities() { // create the player ship and place it roughly in the center of the screen ship = new ShipEntity(this,”sprites/ship.gif”,370,550) entities.add(ship) def game = this // create a block of aliens (5 rows, by 12 aliens, spaced evenly) 5.times { row -> 12.times { col -> Entity alien = new AlienEntity(game, ”sprites/alien.gif”, 100+(col*50), (50)+row*30) entities.add(alien) alienCount++ } } } Now here comes the hard part. I have references to gif files and they need to be available at runtime. You’ll need put these gif files into your project first. Then you’ll need to figure out how to make them available during runtime. How you make it work will depend on your development environment. For the purpose of the tutorial (and for the purpose of showing readers how I can mix a bunch of technologies in one tutorial and explain them all at once) I’m using Maven2 to build groovy-invaders. Because Groovy is a pseudo-interpreted language (it interprets and compiles source as it runs) you don’t need a build step. If you are using an IDE and you’re savvy enough with the Groovy plugins you may be able to run the project without compiling. (If so please tell me how to.) In general, you want to make the graphics available in the classpath under a folder called sprites so that the above code will work. I’ve updated part I in my series so you can referr there to see how my folders and files are laid out. If you follow suit then you should be able to create a sprites folder under the resources folder and drop the graphics in there. After copying the graphics into your project you should be able to run “mvn test” one the command line and then you’ll have an output folder full of everything you need to see our first screen. Run the Game class with the “java” command including the groovy-all-jsr-xx.jar in the classpath and you should see a window with the ship at the bottom and 60 aliens in formation at the top. (I lean on my IDE here and create a run configuration that includes the project classpath and the Game class as the main entry point.) That’ll do it for today. I gotta get some real work done. Take a moment to peruse the code and improve if you will. Until next time you can use the little box below to drop it like it’s hot… [...]
By: Can’t see nothing but the source code » Groovy Invaders IV on August 17, 2006
at 1:19 pm
Thank You
By: Mark on April 18, 2007
at 3:41 am
You’re welcome, though the topic is still incomplete. I’ll get back to this I promise…
By: Cliff on April 18, 2007
at 7:46 am