<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQUARISM &#187; Processing</title>
	<atom:link href="http://squarism.com/category/processing/feed/" rel="self" type="application/rss+xml" />
	<link>http://squarism.com</link>
	<description>until lambs become lions</description>
	<lastBuildDate>Mon, 07 May 2012 17:52:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Simple Ruby-Processing Collision Detection</title>
		<link>http://squarism.com/2011/04/16/simple-ruby-processing-collision-detection/</link>
		<comments>http://squarism.com/2011/04/16/simple-ruby-processing-collision-detection/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 02:30:48 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Processing]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=1222</guid>
		<description><![CDATA[In an old post I talked about doing collision detection using a 2d bitmap (or grid). That example was done using Processing (Java). It was a simple example but was very relevant to all the problems I had when building Tatris which did not use a bitmap for player position, instead it used real 2d [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2011/04/ruby-processing-deadgrid.png" alt="" title="ruby-processing-deadgrid" width="336" height="358" class="alignright size-full wp-image-1225" /><br />
<a href="http://squarism.com/2009/07/09/better-tetris-collision-detection/">In an old post</a> I talked about doing collision detection using a 2d bitmap (or grid).  That example was done using Processing (Java).  It was a simple example but was very relevant to all the problems I had when building <a href="http://squarism.com/tatris/">Tatris</a> which did not use a bitmap for player position, instead it used real 2d space of x,y.</p>
<p>I didn&#8217;t talk about it in the previous post but I had added an Observer to the Java code that would highlight blocks in green when a collision was detected.  Unfortunately, the code is a bit unclean although it works.  I have a few global variables (common in Processing sketches) and some unused variables.  So I never posted it.</p>
<p>Fast-forward to today and I have been playing around with <a href="https://github.com/jashkenas/ruby-processing/wiki/getting-started">ruby-processing</a> (rp5) and porting some of my sketches.  The code is not much smaller (300ish lines of Java, 265 lines of Ruby) but I didn&#8217;t optimize either code for line numbers.  Neither code is really easier to read.  This might be because I straight-up ported the code with only minor changes.  Even if I were to do this from scratch, I&#8217;m not sure I could make it more ruby-like.  Where I use arrays for x,y, I&#8217;m sure I could do something cooler like ruby Hashes, but a lot of the code is bound to JRuby and the Processing API so it ends up reading about the same.<br />
<span id="more-1222"></span><br />
One of the changes was to move the observer notification into the key_pressed method.  When you run into a block, it does a few checks to see if you&#8217;re at the edge of the screen and if not, it notifies the BlockedHandler which will highlight the grid block at the proper location.</p>
<p>I was unable to export an applet from ruby-processing but I have <a href="http://squarism.com/files/deadgrid_events/">the java version posted here</a>.  The controls are:</p>
<blockquote><p>Arrow keys to move<br />
Space to randomize grid</p></blockquote>
<p>I also have a Mac app of the ruby version <a href="http://squarism.com/files/rp5-deadgrid.zip">here</a>.  It should work stand-alone.  I suggest running the code before looking at it so that you can see it in motion.  It won&#8217;t make a whole lot of sense just by looking at the screenshot.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'java'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'observer'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'thread'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pp'</span>
include_class <span style="color:#996600;">'java.util.Random'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># port of deadgrid_refactor</span>
<span style="color:#9966CC; font-weight:bold;">class</span> DeadGrid
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:block_size</span>, <span style="color:#ff3333; font-weight:bold;">:grid</span>, <span style="color:#ff3333; font-weight:bold;">:width</span>, <span style="color:#ff3333; font-weight:bold;">:height</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@block_size</span> = <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">block_size</span>
    <span style="color:#0066ff; font-weight:bold;">@width</span> = <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">width</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">block_size</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
    <span style="color:#0066ff; font-weight:bold;">@height</span> = <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">height</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">block_size</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@grid</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
    <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@width<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      grid<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
      <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@height<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0066ff; font-weight:bold;">@grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>    
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">&quot;&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>DeadGrid<span style="color:#000099;">\n</span>&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;-&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@width</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
    <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@width<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@height<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> grid<span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">string</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> draw
    <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">stroke</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">255</span>,<span style="color:#006666;">255</span>,<span style="color:#006666;">255</span>,<span style="color:#006666;">125</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">stroke_weight</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@width<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@height<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span>
        x = i <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>
        y = j <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@grid<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@grid<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">255</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@grid<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">120</span>,<span style="color:#006666;">200</span>,<span style="color:#006666;">50</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
          <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">rect</span><span style="color:#006600; font-weight:bold;">&#40;</span>x,y,@block_size,@block_size<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> randomize
    <span style="color:#CC0066; font-weight:bold;">rand</span> = Random.<span style="color:#9900CC;">new</span>
    <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@width<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@height<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span>
        random_int = <span style="color:#CC0066; font-weight:bold;">rand</span>.<span style="color:#9900CC;">nextInt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> = random_int
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> can_move?<span style="color:#006600; font-weight:bold;">&#40;</span>player, distance<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> player.<span style="color:#9900CC;">direction</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;">#left</span>
      check_x = player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">-</span> distance
      check_y = player.<span style="color:#9900CC;">y</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">x</span> != <span style="color:#006666;">0</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> !is_there?<span style="color:#006600; font-weight:bold;">&#40;</span>check_x, check_y<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">2</span> <span style="color:#008000; font-style:italic;">#right</span>
      check_x = player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">+</span> distance
      check_y = player.<span style="color:#9900CC;">y</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">&lt;</span> grid.<span style="color:#9900CC;">size</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> !is_there?<span style="color:#006600; font-weight:bold;">&#40;</span>check_x, check_y<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;">#up</span>
      check_x = player.<span style="color:#9900CC;">x</span>
      check_y = player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">-</span> distance
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> !is_there?<span style="color:#006600; font-weight:bold;">&#40;</span>check_x, check_y<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">3</span> <span style="color:#008000; font-style:italic;">#down</span>
      check_x = player.<span style="color:#9900CC;">x</span>
      check_y = player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">+</span> distance
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">&lt;</span>= grid.<span style="color:#9900CC;">size</span> 
        <span style="color:#0000FF; font-weight:bold;">return</span> !is_there?<span style="color:#006600; font-weight:bold;">&#40;</span>check_x, check_y<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> is_there?<span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> grid<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>y<span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Block
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:block_size</span>, <span style="color:#ff3333; font-weight:bold;">:x</span>, <span style="color:#ff3333; font-weight:bold;">:y</span>, <span style="color:#ff3333; font-weight:bold;">:width</span>, <span style="color:#ff3333; font-weight:bold;">:height</span>, <span style="color:#ff3333; font-weight:bold;">:direction</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@block_size</span> = <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">block_size</span>
    <span style="color:#0066ff; font-weight:bold;">@x</span> = x
    <span style="color:#0066ff; font-weight:bold;">@y</span> = y
    <span style="color:#0066ff; font-weight:bold;">@width</span> = <span style="color:#0066ff; font-weight:bold;">@block_size</span>
    <span style="color:#0066ff; font-weight:bold;">@height</span> = <span style="color:#0066ff; font-weight:bold;">@block_size</span>
    <span style="color:#0066ff; font-weight:bold;">@direction</span> = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> draw
    <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">55</span>,<span style="color:#006666;">0</span>,<span style="color:#006666;">255</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">rect</span><span style="color:#006600; font-weight:bold;">&#40;</span>@x <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>, <span style="color:#0066ff; font-weight:bold;">@y</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>, <span style="color:#0066ff; font-weight:bold;">@width</span>, <span style="color:#0066ff; font-weight:bold;">@height</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># figures out which block to highlight on grid when player can't move</span>
<span style="color:#9966CC; font-weight:bold;">class</span> BlockedHandler
  <span style="color:#9966CC; font-weight:bold;">def</span> update<span style="color:#006600; font-weight:bold;">&#40;</span>player, deadgrid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> player.<span style="color:#9900CC;">direction</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">1</span>
        deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">2</span>;
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">1</span>
        deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">2</span>;
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">2</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">1</span>
        deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">2</span>;
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">3</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">1</span>
        deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">2</span>;
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MySketch <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Processing::App</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Observable</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:deadgrid</span>, <span style="color:#ff3333; font-weight:bold;">:player</span>, <span style="color:#ff3333; font-weight:bold;">:block_size</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> setup
    <span style="color:#0066ff; font-weight:bold;">@block_size</span> = <span style="color:#006666;">16</span>
    <span style="color:#0066ff; font-weight:bold;">@deadgrid</span> = DeadGrid.<span style="color:#9900CC;">new</span>
    <span style="color:#0066ff; font-weight:bold;">@player</span> = Block.<span style="color:#9900CC;">new</span> <span style="color:#006666;">7</span>,<span style="color:#006666;">7</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>
    <span style="color:#0066ff; font-weight:bold;">@blocked_state</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">add_observer</span><span style="color:#006600; font-weight:bold;">&#40;</span>BlockedHandler.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> draw
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">update</span>
    background <span style="color:#006666;">0</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">draw_grid_lines</span>
    <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>.<span style="color:#9900CC;">draw</span>
    <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">draw</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> update
    <span style="color:#0066ff; font-weight:bold;">@blocked_state</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>s<span style="color:#006600; font-weight:bold;">|</span> s = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> draw_grid_lines
    x,y = <span style="color:#006666;">0</span>
    strokeWeight <span style="color:#006666;">1</span>
    stroke<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">40</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># for each block in deadgrid, draw lines</span>
    <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">width</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      x = i <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>
      <span style="color:#008000; font-style:italic;"># vertical line</span>
      line<span style="color:#006600; font-weight:bold;">&#40;</span>x, <span style="color:#006666;">0</span>, x, <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">height</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#006666;">0</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">height</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span>
        y = j <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@block_size</span>
        <span style="color:#008000; font-style:italic;"># horizontal line</span>
        line<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>, y, <span style="color:#ff6633; font-weight:bold;">$app</span>.<span style="color:#9900CC;">width</span>, y<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># 38up 39right 40down 37left</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> key_pressed
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">case</span> key_code
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">32</span> <span style="color:#008000; font-style:italic;"># space</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Randomize.'</span>
      <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>.<span style="color:#9900CC;">randomize</span>
      <span style="color:#008000; font-style:italic;"># if grid spawns under player, remove it</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>@player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>@player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>.<span style="color:#9900CC;">grid</span><span style="color:#006600; font-weight:bold;">&#91;</span>@player.<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>@player.<span style="color:#9900CC;">y</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">0</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">37</span> <span style="color:#008000; font-style:italic;"># left</span>
      <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">direction</span> = <span style="color:#006666;">0</span>
      <span style="color:#008000; font-style:italic;"># check bounds, old java code uses OutOfBoundsException trick</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">can_move</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
          <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">x</span> = <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          changed
          notify_observers<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#0066ff; font-weight:bold;">@deadgrid</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">39</span> <span style="color:#008000; font-style:italic;"># right</span>
      <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">direction</span> = <span style="color:#006666;">2</span>
      <span style="color:#008000; font-style:italic;"># check bounds</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>.<span style="color:#9900CC;">width</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">can_move</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">x</span> = <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">x</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          changed
          notify_observers<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#0066ff; font-weight:bold;">@deadgrid</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">38</span> <span style="color:#008000; font-style:italic;"># up</span>
      <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">direction</span> = <span style="color:#006666;">1</span>
      <span style="color:#008000; font-style:italic;"># check bounds</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">can_move</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">y</span> = <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          changed
          notify_observers<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#0066ff; font-weight:bold;">@deadgrid</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">40</span> <span style="color:#008000; font-style:italic;"># down</span>
      <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">direction</span> = <span style="color:#006666;">3</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> player.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#0066ff; font-weight:bold;">@deadgrid</span>.<span style="color:#9900CC;">height</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@deadgrid.<span style="color:#9900CC;">can_move</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">y</span> = <span style="color:#0066ff; font-weight:bold;">@player</span>.<span style="color:#9900CC;">y</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          changed
          notify_observers<span style="color:#006600; font-weight:bold;">&#40;</span>@player, <span style="color:#0066ff; font-weight:bold;">@deadgrid</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
MySketch.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;DeadGrid&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:width</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">256</span>, <span style="color:#ff3333; font-weight:bold;">:height</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">256</span>, <span style="color:#ff3333; font-weight:bold;">:full_screen</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2011/04/16/simple-ruby-processing-collision-detection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hollywood Security BS</title>
		<link>http://squarism.com/2010/12/03/hollywood-security-bs/</link>
		<comments>http://squarism.com/2010/12/03/hollywood-security-bs/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 03:36:51 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=791</guid>
		<description><![CDATA[I had watched Alicia Silverstone &#8216;hack&#8217; a system recently and decided to make a mostly functional mock up like they do in the movies. I&#8217;m getting pretty comfortable in Processing and even though I really can&#8217;t stand Java anymore, I wanted to get this thing done and out of my head. I was just going [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2010/12/sshot_1-580x468.png" alt="" title="HollywoodSecurity" width="580" height="468" class="aligncenter size-large wp-image-792" /></p>
<p>I had watched Alicia Silverstone &#8216;hack&#8217; a system recently and decided to make a mostly functional mock up like they do in the movies.  I&#8217;m getting pretty comfortable in <a href="http://processing.org/">Processing</a> and even though I really can&#8217;t stand <a href="http://www.oracle.com/technetwork/java/index.html">Java</a> anymore, I wanted to get this thing done and out of my head.  I was just going to skip over this idea but inspiration just started flowing and I was idly solving it while getting coffee etc and I just had to get it on paper/code.</p>
<p>So here are the things it does:</p>
<ul>
<li>Switches between two GameState objects login and success</li>
<li>Separate the view and the state (more on this)</li>
<li>Has a super cool blinking cursor</li>
<li>Handles shift, ctrl and meta (windows key) gracefully</li>
<li>Handles backspace</li>
<li>When you type muffin, it takes you to the super secret database &#8230; or whatever</li>
<li>Goes fullscreen (which you can&#8217;t see in this shot)</li>
</ul>
<p>This could get out of hand real quick.  First, I reused a ton of code from Tatris.  But in Tatris, the game state was tied to a view.  I had pictured this project having different views.  Like instead of a password prompt, maybe there&#8217;d be a keypad with a combo.  Same state object (authorized vs unauthorized) so I didn&#8217;t need to recreate the wheel.  The problem is, I don&#8217;t really have a controller so I&#8217;m kinda breaking MVC.  But I didn&#8217;t want this thing to bloat up so I just went with it.</p>
<p>Next, UI layer.  Not great.  I&#8217;m drawing text boxes and blinking cursors.  It&#8217;s really pretty messy.  I&#8217;d be better off picking some UI components or something and using those.  It&#8217;s tough.  Even in games, there&#8217;s so many UI differences.  Different scrollbars, different OK type buttons.  What a usability nightmare.  But I&#8217;m trying to create an old school terminal so all the weirdness is actually good.</p>
<p>Anyway, it was a fun little diversion.  There&#8217;s more on <a href="https://github.com/squarism/HollywoodSecurity">the github page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/12/03/hollywood-security-bs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Better tetris collision detection</title>
		<link>http://squarism.com/2009/07/09/better-tetris-collision-detection/</link>
		<comments>http://squarism.com/2009/07/09/better-tetris-collision-detection/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 02:44:26 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=244</guid>
		<description><![CDATA[As I said in the TODO part of the Making Tetris post, a better way to do collision detection is to have the blocks on the field be bits. This is typically what I saw in academic assignments and student presentations. This is probably the right way to do it in other words. It&#8217;s more [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/07/deadgrid.png" alt="deadgrid" title="deadgrid" width="336" height="358" class="aligncenter size-full wp-image-247" /></p>
<p>As I said in the TODO part of the <a href="http://squarism.com/2009/06/22/making-tetris/4/">Making Tetris post</a>, a better way to do collision detection is to have the blocks on the field be bits.  This is typically what I saw in academic assignments and student presentations.  This is probably the right way to do it in other words.  It&#8217;s more efficient and it&#8217;s more simple (<a href="http://en.wikipedia.org/wiki/KISS_principle">KISS</a>).</p>
<p>Even though this isn&#8217;t how I did it in the game, I still wanted to play around with the concept so I made a little prototype that demonstrates the basic gist.  Instead of a piece, it&#8217;s a single block.  Instead of a tetris grid of finished blocks, it&#8217;s random blocks.  It&#8217;s really the same thing, it just looks and plays with different shapes.</p>
<p>So here it is.  Space randomizes the grid and the arrow keys move.  <a href="http://squarism.com/files/deadgrid">Play It!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/07/09/better-tetris-collision-detection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making Tatris</title>
		<link>http://squarism.com/2009/06/22/making-tatris/</link>
		<comments>http://squarism.com/2009/06/22/making-tatris/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 21:01:23 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Noteworthy]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=153</guid>
		<description><![CDATA[Backstory In my last job, I was a hourly contractor who had finished a project and was transitioning off to other things. By a chance of fortune, I had an opportunity to take some time off and do whatever. I had one week on, one week off for a period of over a month. I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/04/tatris_03.png" alt="tatris_03" title="tatris_03" width="400" height="598" class="aligncenter size-full wp-image-235" /></p>
<h2>Backstory</h2>
<p>In my last job, I was a hourly contractor who had finished a project and was transitioning off to other things.  By a chance of fortune, I had an opportunity to take some time off and do whatever.  I had one week on, one week off for a period of over a month.  I was extremely excited because I have a lot of hobbies that are bound by time and this was exactly what I wanted.  Time to do whatever.  I was curious to see whether I&#8217;d waste it or actually produce something.  I&#8217;m happy to say that I did not waste the time I had and I produced the most intense gamedev learning experience I&#8217;ve ever had.  By no means, am I claiming to be an expert.  I&#8217;m just documenting a very large personal effort.</p>
<p>Ok, enough of all that personal crap.  In reading game development community sites (like gamedev.net and idevgames.com), something that was a near cosmological constant is the post &#8220;OMG I wnt to make mmo, pls halp!&#8221;.  It&#8217;s like a nuclear clock.  Someone does some subscription revenue math, gets excited with dreams of being rich, tries to start something ridiculously complicated, gets stuck and runs to a forum looking for members or advice.  On a forum like idevgames.com, there are exceptional members (who should be praised for their patience and humanity) that take the time to respond to this never-ending line of questioning.  The most effective response is, &#8220;have you made tetris yet?&#8221;.  Usually the person has not made a simple game and I doubt that they end up doing so.  However, eventually this idea sunk in and I realized: <em>I do not want to make an MMO but I should make Tetris</em>.  Because someday I might want to make something more complicated (not an MMO).  So let&#8217;s do this.</p>
<h2>The Plan</h2>
<p>Firstly, I had been doing Java at my job for a while now and am fairly comfortable with it.  Outside of this, I had been messing around with a project called <a href="http://processing.org">Processing</a> which makes graphics and generative art really easy.  I knew this was going to be complicated so instead of diving into code, I made a plan first.  I started breaking down what Tetris is and mapped out classes and responsibility.  This planning bit I&#8217;ve always been bad at and I spent maybe a day thinking and writing down like &#8220;what a tetris piece is&#8221; and what minimal features there should be.  The gameplay and design is already done and this fact is a big step compared to coming up with something by yourself.</p>
<p>For sure, the lesson I learned is: &#8220;it&#8217;s just a plan&#8221;.  You can change it as you go and eventually it&#8217;s best to throw it away after things are sufficiently started.  As the code grew, the plan was put away; which is good because my plan wasn&#8217;t really all that special or well organized.  I had some ideas about pieces to be written and what the hard parts were but honestly the best lesson I learned was &#8220;it&#8217;s just a plan&#8221;.  You&#8217;re not going to pre-write and pre-solve all the problems.</p>
<p>Next was research and learning.  I studied other processing games (like <a href="http://lux.vu/blog/2005/01/06/monkey-patrol/">MonkeyPatrol</a> by Joshua Minor) and white papers from university CS classes.  I played Quinn (a mac OSX clone) a bit.  I knew a few things to start with.  For example, game objects should draw themselves.  There are 7 piece types (which look similar to the letters: I L O J S Z T) and many things are similar between them so I planned for a base Piece class and named the pieces after the letters they looked like (IPiece, LPiece, etc).  I collected some screenshots of existing games to use as inspiration.</p>
<h2>Drawing a Piece</h2>
<p>Ok, I had my plan and similar stuff done.  Ok, where to start?  I like to start from the top down.  IE: from the interface backwards.  So I start with a graphical mockup and then make the mockup actually function.  So started out with drawing.  First, I created a Block class.  This is a single square with an x,y,height,width,color etc.  It&#8217;s a component of a Piece.  Before going any further.  I have to explain that I intentionally did not do Tetris the easy way.  The easy way is having a bitmap style grid of blocks and simply moving the bits down and around.  Then you just represent the bitmap with graphics.  I did not do it this way because I wanted an excuse to do sorta a &#8220;2d model&#8221; where the piece is constructed from a central point, rotated etc more like what someone would do with a 3d model in a modern game.  This single decision made things extremely complicated for me but it also made it a more useful learning experience for when I want to do something like a platformer or a shooter because these game types use collision detection in a 2d/3d space similar to how I did it.  So a Piece consists of Blocks with a model describing the shape of the Piece.  For example, an LPiece looks like this:</p>
<p><code>1<br />
2<br />
34</code></p>
<p>And the IPiece looks like this:<br />
<code>1<br />
2<br />
3<br />
4</code></p>
<p>And the OPiece looks like this:<br />
<code>12<br />
34</code></p>
<p>So I created all the Pieces and eventually had a test app that looked like this:<br />
<img src="http://squarism.com/wp-content/uploads/2009/04/tatris_block_test-189x300.png" alt="tatris_block_test" title="tatris_block_test" width="189" height="300" class="aligncenter size-medium wp-image-233" /></p>
<p>Next, let&#8217;s move on to piece movement.</p>
<p><span id="more-153"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/06/22/making-tatris/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elevator Sim update</title>
		<link>http://squarism.com/2009/06/21/elevator-sim-update/</link>
		<comments>http://squarism.com/2009/06/21/elevator-sim-update/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 16:16:47 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=221</guid>
		<description><![CDATA[Quick update about the elevator sim post. Currently the cars are animating fine. IE: you press 3 and car #1 goes to floor three, stops, opens the door and waits. If you press 1, the car closes the door, moves to floor 1 and opens the door. The building controller is aware of the care [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/06/elevator_sim_2.png" alt="elevator_sim_2" title="elevator_sim_2" width="314" height="227" class="aligncenter size-full wp-image-222" /><br />
Quick update about <a href="http://squarism.com/2009/04/26/elevator-sim-wip/">the elevator sim post</a>.  Currently the cars are animating fine.  IE: you press 3 and car #1 goes to floor three, stops, opens the door and waits.  If you press 1, the car closes the door, moves to floor 1 and opens the door.  The building controller is aware of the care state as evidenced by the little labels you see.</p>
<p>The person sprite (on that line there) is currently a placeholder.  He paths over to wait for the elevator but I don&#8217;t have the &#8220;AI&#8221; done for him to wait.</p>
<p>This has lost a bit of traction since I&#8217;ve been cranking on the iPhone class.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/06/21/elevator-sim-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallax Clouds</title>
		<link>http://squarism.com/2009/05/06/parallax-clouds/</link>
		<comments>http://squarism.com/2009/05/06/parallax-clouds/#comments</comments>
		<pubDate>Thu, 07 May 2009 02:51:56 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=206</guid>
		<description><![CDATA[Updated this a bit. Added sound, scanlines, mouse following, bird sprite thing. Made a few performance tweaks. It still runs a bit slow, it&#8217;s better under OpenGL but you can&#8217;t embed it in a web page. The code is from a while back and I just polished it a bit so the source is fugly, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/05/paraclouds.png" alt="paraclouds" title="paraclouds" width="470" height="303" class="aligncenter size-full wp-image-217" /></p>
<p>Updated this a bit.  Added sound, scanlines, mouse following, bird sprite thing.  Made a few performance tweaks.  It still runs a bit slow, it&#8217;s better under OpenGL but you can&#8217;t embed it in a web page.  The code is from a while back and I just polished it a bit so the source is fugly, fugly, fugly.  I like the tune though.  The ableton live demo has turned into a musical idea notebook and a short idea is all I seem to grind on.</p>
<p>The music is original (no samples).</p>
<p>View it <a href="http://squarism.com/files/parallaxmusic/">here</a>.</p>
<p><a href="http://squarism.com/files/ParallaxClouds-mac-fullscreen.zip">Download here &#8211; Mac</a>, if you want to run it fullscreen.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/05/06/parallax-clouds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elevator Sim [wip]</title>
		<link>http://squarism.com/2009/04/26/elevator-sim-wip/</link>
		<comments>http://squarism.com/2009/04/26/elevator-sim-wip/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 02:25:29 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=183</guid>
		<description><![CDATA[Car idle: Car called, doors open. Some shots from a work in progress. Elevator simulator. People are going to get on and off and ride the cars down. It&#8217;s an exercise in 3d, queuing, event detection (without using events) and a boat load of other stuff. It&#8217;s coming along nicely. The car motion is really [...]]]></description>
			<content:encoded><![CDATA[<p>Car idle:<br />
<img src="http://squarism.com/wp-content/uploads/2009/04/elevator_sim_wip_1.png" alt="elevator_sim_wip_1" title="elevator_sim_wip_1" width="480" height="302" class="aligncenter size-full wp-image-184" /></p>
<p>Car called, doors open.<br />
<img src="http://squarism.com/wp-content/uploads/2009/04/elevator_sim_wip_2.png" alt="elevator_sim_wip_2" title="elevator_sim_wip_2" width="480" height="302" class="aligncenter size-full wp-image-185" /></p>
<p>Some shots from a work in progress.  Elevator simulator.  People are going to get on and off and ride the cars down.  It&#8217;s an exercise in 3d, queuing, event detection (without using events) and a boat load of other stuff.  It&#8217;s coming along nicely.  The car motion is really convincing.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/04/26/elevator-sim-wip/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reading style tween test</title>
		<link>http://squarism.com/2009/04/18/reading-style-tween-test/</link>
		<comments>http://squarism.com/2009/04/18/reading-style-tween-test/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 03:04:50 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=149</guid>
		<description><![CDATA[Using the tween library from megamu, I think I finally get how to use it. Going to use it for an elevator sim. Yes, I&#8217;m building an elevator. It just struck me while riding one. &#8220;I could make this.&#8221; Anyway, check out the reading thing here.]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/04/tweentest.png" alt="tweentest" title="tweentest" width="280" height="302" class="aligncenter size-full wp-image-151" /><br />
Using the <a href="http://www.megamu.com/processing/shapetween/">tween library from megamu</a>, I think I finally get how to use it.  Going to use it for an elevator sim.  Yes, I&#8217;m building an elevator.  It just struck me while riding one.  &#8220;I could make this.&#8221;</p>
<p>Anyway, check out the reading thing <a href="http://squarism.com/files/TweenTest/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/04/18/reading-style-tween-test/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Triangle Building</title>
		<link>http://squarism.com/2009/04/03/triangle-building/</link>
		<comments>http://squarism.com/2009/04/03/triangle-building/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 03:44:24 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=129</guid>
		<description><![CDATA[I got distracted while working on an actual game and started messing around with image recognition. I was greatly inspired by the LevelHead tech demo and thoroughly depressed by Julian Oliver&#8217;s talent. I started messing around with OpenCV and wanted to track rotation of objects. I figured out that a triangle would be the best [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/04/triangle_building_1-580x435.jpg" alt="triangle_building_1" title="triangle_building_1" width="580" height="435" class="aligncenter size-large wp-image-130" /><br />
I got distracted while working on an actual game and started messing around with image recognition.  I was greatly inspired by <a href="http://vimeo.com/1320756">the LevelHead tech demo</a> and thoroughly depressed by Julian Oliver&#8217;s talent.  I started messing around with OpenCV and wanted to track rotation of objects.  I figured out that a triangle would be the best way to track rotation because a right-triangle is unique when rotated 90º four times.</p>
<p>So the magic triangle again.  It seems it has endless uses.  I started trying to draw one programmatically. But my trig skills are lacking and I needed to create a test program.  My math didn&#8217;t work out that great so I asked yahoo answers.  Someone named Mathmom28 answered my question perfectly and the above sheet of paper shows the end result of her answer.  It&#8217;s a bit disheartening to be relying on a homework forum for answers from &#8220;Mathmom28&#8243; but I don&#8217;t think I&#8217;m going to pass judgement on my superiors.  Suddenly I feel like I&#8217;m in high school again.</p>
<p>The magic to this madness was a formula she posted which is something I&#8217;ve since long-forgotten: the point-slope form of a line.  A line 90º perpendicular to another one is it&#8217;s negative reciprocal.  In other words, y = 3x/5y at 90º is y=-5y/3x.  Once I got that, it was cake to finish something that&#8217;s pretty polished.</p>
<p>Try out <a href="http://squarism.com/files/triangle_building/">Triangle Building</a>.  The instructions are at the bottom of the screen.</p>
<p><img src="http://squarism.com/wp-content/uploads/2009/04/triangle_building.png" alt="triangle_building" title="triangle_building" width="380" height="402" class="aligncenter size-full wp-image-136" /></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/04/03/triangle-building/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Square Tracing</title>
		<link>http://squarism.com/2009/03/11/square-tracing/</link>
		<comments>http://squarism.com/2009/03/11/square-tracing/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 04:03:10 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=119</guid>
		<description><![CDATA[At lunch I was thinking about how I&#8217;d trace a square. Suddenly, it seemed rather difficult. I had to break out trig (again) and draw it out a few times. The hard part is when it flips. My math is a bit off and the distance calc isn&#8217;t right quite yet. Check it out here: [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://squarism.com/wp-content/uploads/2009/03/square_trace.png" alt="square_trace" title="square_trace" width="280" height="302" class="aligncenter size-full wp-image-120" /></p>
<p>At lunch I was thinking about how I&#8217;d trace a square.  Suddenly, it seemed rather difficult.  I had to break out trig (again) and draw it out a few times.  The hard part is when it flips.  My math is a bit off and the distance calc isn&#8217;t right quite yet.</p>
<p>Check it out here: <a href="http://squarism.com/files/square_trace/">square trace</a></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2009/03/11/square-tracing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

