Groovy Invaders


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>https://codeforfun.wordpress.com</url&gt;
    <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…

Next entry in the series

5 thoughts on “Groovy Invaders

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s