Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Friday, February 25, 2005

First Video Game Written In Ant

Presenting the world's first video game written in Ant. This is a cross-platform downhill skiing game in which you use the < and > keys to race down a dangerous ski slope. System requirements: Ant 1.6 and the Ant-Contrib library.

And yes, it's written in Ant.

How this came to be: Some co-workers and I were chatting today about some Ant features we learned about, in particular the very useful macrodef and import tags. Then it dawned on me that with the function-like behaviour that macrodef tags give us, we could do virtually anything. Heck, we could write a video game in Ant if we wanted to! My coworkers laughed at me.

Well, laugh no more, my friends. Below is a screenshot showing the first-ever Ant video game in action:

In the above screenshot, I have crashed into the trees on the right. As this is an Ant script, the bad news begins with "Build Failed".

Note: If you succeed in skiing through the entire course and arriving at the bottom of the mountain, there is a pretty end-game screen as your reward!

Simply copy the code below and fire up Ant using the command "ant -quiet". Note that you will need Ant 1.6 and the Ant-Contrib library.

<?xml version="1.0"?>
To start the game, type "ant -quiet". Requires Ant 1.6
and the Ant-Contrib Tasks [Jon Aquino 2005-02-24]
<project name="ski" default="run" basedir=".">
<taskdef resource="net/sf/antcontrib/"/>
<taskdef name="for" classname="net.sf.antcontrib.logic.For"/>
<typedef name="isgreaterthan" classname="net.sf.antcontrib.logic.condition.IsGreaterThan"/>
<typedef name="islessthan" classname="net.sf.antcontrib.logic.condition.IsLessThan"/>
<var name="skiier-position" value="5"/>
<!-- Generated using random numbers from
Make your own course up! [Jon Aquino 2005-02-25] -->
<property name="course" value="1,0,1,2,1,2,3,2,3,4,3,4,5,4,3,4,3,2,3,4,3"/>
<property name="course-width" value="13"/>
<property name="skiier-width" value="7"/>
<target name="run">
<echo message="Use the &lt; and &gt; keys to move left and right."/>
<for list="${course}" param="tree-position">
<var name="tree-position" value="@{tree-position}"/>
<macrodef name="paint">
<!-- Graphics from [Jon Aquino 2005-02-24] -->
<paint-line tree=" " skiier="+ + "/>
<paint-line tree=" /\ /\" skiier="\ O \ "/>
<paint-line tree=" / \\ /\ /\\\" skiier="\_/|\_\"/>
<paint-line tree="/ \\/ \\ / \\\" skiier=" \\ "/>
<paint-line tree="/ / \/ \\\" skiier="\/' "/>
<paint-line tree="/ / / \\\" skiier="\\_ "/>
<paint-line tree="/ / / \\\" skiier="- "/>
<macrodef name="get-input">
<var name="input" unset="true"/>
<input addproperty="input"/>
<if><equals arg1="${input}" arg2=","/>
<then><subtract a="${skiier-position}" b="1" result="skiier-position"/></then>
<elseif><equals arg1="${input}" arg2="."/>
<then><add a="${skiier-position}" b="1" result="skiier-position"/></then></elseif></if>
<macrodef name="update-clearances">
<subtract a="${skiier-position}" b="${tree-position}" result="left-clearance"/>
<add a="${tree-position}" b="${course-width}" result="right-clearance"/>
<subtract a="${right-clearance}" b="${skiier-position}" result="right-clearance"/>
<subtract a="${right-clearance}" b="${skiier-width}" result="right-clearance"/>
<macrodef name="paint-line">
<attribute name="tree"/>
<attribute name="skiier"/>
<spaces n="${tree-position}" result="left-margin"/>
<spaces n="${left-clearance}" result="left-space"/>
<spaces n="${right-clearance}" result="right-space"/>
<echo message="${left-margin}@{tree}${left-space}@{skiier}${right-space}@{tree}"/>
<macrodef name="spaces">
<attribute name="n"/>
<attribute name="result"/>
<math result="2n" operation="*" operand1="@{n}" operand2="2" datatype="int"/>
<var name="@{result}" unset="true"/>
input=" "
regexp="( {${2n}}).*"
<macrodef name="add">
<attribute name="a"/>
<attribute name="b"/>
<attribute name="result" default="result"/>
<math result="@{result}" operation="+" operand1="@{a}" operand2="@{b}" datatype="int"/>
<macrodef name="subtract">
<attribute name="a"/>
<attribute name="b"/>
<attribute name="result" default="result"/>
<add a="@{a}" b="-@{b}" result="@{result}"/>
<macrodef name="check-crash">
<if><islessthan arg1="${left-clearance}" arg2="0"/>
<if><islessthan arg1="${right-clearance}" arg2="0"/>
<macrodef name="die">
<!-- Sound effects from [Jon Aquino 2005-02-25] -->
<fail message=" *** BOINK! POINK! THUMP! You smashed into a tree and died. ***"/>
<macrodef name="success">
<!-- Graphics from [Jon Aquino 2005-02-24] -->
<echo message="Hooray! Hooray! You are the champion! Congratulations!"/>
<echo message=" *"/>
<echo message=" XX"/>
<echo message=" MMMMM"/>
<echo message=" //(00 "/>
<echo message=" .:....."/>
<echo message=" .:::::::::"/>
<echo message=" :: %%%%%% ::."/>
<echo message=" :: :::::: :::::::I)"/>
<echo message=" (% ::::: |"/>
<echo message=" / | /______ |"/>
<echo message=" / |______ )) |"/>
<echo message=" / / // |"/>
<echo message=" / / // |"/>
<echo message=" / / // |"/>
<echo message="* ZZZZ *"/>
<echo message=" _________ZZZZZZ_________//_//"/>

Here is a complete picture of a successful ski run:

And Ant gives us a nice report of the time it took to reach the bottom of the slope:

Total time: 17 seconds


Post a Comment

<< Home