<?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>Tummai Games &#187; NES</title>
	<atom:link href="http://www.tummaigames.com/blog/category/development/nes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tummaigames.com/blog</link>
	<description>NES homebrew! Development blog for homebrew NES games</description>
	<lastBuildDate>Tue, 16 Mar 2010 13:23:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ramses Game: 3 &#8211; Monster Sequences</title>
		<link>http://www.tummaigames.com/blog/2010/02/15/ramses-game-3-monster-sequences/</link>
		<comments>http://www.tummaigames.com/blog/2010/02/15/ramses-game-3-monster-sequences/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 15:59:06 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Ramses Game]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[art]]></category>
		<category><![CDATA[attributes]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[slots]]></category>
		<category><![CDATA[sound effects]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=224</guid>
		<description><![CDATA[I wasn&#8217;t planning to post an update so soon, but this morning when I came into work I was told that my classes were cancelled for the day.  This happens occasionally at the end of the school year and generally means that I have a full day at my desk to do whatever I [...]]]></description>
			<content:encoded><![CDATA[<p>I wasn&#8217;t planning to post an update so soon, but this morning when I came into work I was told that my classes were cancelled for the day.  This happens occasionally at the end of the school year and generally means that I have a full day at my desk to do whatever I want.  I was up late last night writing my <a href="http://www.tummaigames.com/blog/2010/02/14/ramses-game-2-reading-nes-button-combinations/">last post</a>, so NES programming was on my mind when I woke up in the morning.</p>
<h2>Sequences</h2>
<p>Today&#8217;s mission was to get <strong class="red">Sequences</strong> up and running.  A Sequence is a series of monster images at the top of the screen that indicates to the player what they should whack next.  I want the Sequence engine done before I start work on monsters.  It doesn&#8217;t make sense to do the monsters first.  The Sequence engine is like the backbone of the game.</p>
<p>Here&#8217;s what I needed for Sequences:</p>
<ul>
<li>An init routine that selects a starting Sequence based on Level/Sublevel.</li>
<li>A routine to draw a sequence to the screen</li>
<li>A routine to close an individual Slot and advance the engine to the next monster in the Sequence.  This includes some helper routines to update graphics.</li>
<li>A routine to select a (random) new Sequence after all Slots in the current Sequence are closed.</li>
</ul>
<h3>Graphics</h3>
<p>I decided to start by drawing a 16&#215;16 <strong>metatile</strong> to represent a closed slot.  I had time, so I took my time and ended up redrawing a lot of the dummy graphics for the Sequence Indicator:</p>
<div id="attachment_225" class="wp-caption alignleft" style="width: 266px"><a href="http://www.tummaigames.com/blog/wp-content/uploads/2010/02/unrom-0.png"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2010/02/unrom-0.png" alt="Ramses Game - Sequence Indicator Graphics" title="Ramses Game - Sequence Indicator Graphics" width="256" height="224" class="size-full wp-image-225" /></a><p class="wp-caption-text">new graphics for the Sequence Indicator.</p></div>
<p>I went for a metal look to start because it is the easiest for me to draw.  For the final game I want to have several tilesets for different Level themes.  I&#8217;ve always sucked at art, so I was quite surprised and happy that my tiles ended up looking so good (to my eyes anyway).  I&#8217;m especially proud of my keyhole.</p>
<h3>Loading Sequences</h3>
<p>Loading a Sequence was pretty straight forward to code.  I have a table of pointers to tables of pointers to tables of pointers: Levels->Sublevels->Sequences.  Once I get down to the Sequences, I choose one randomly.  My random number generator is extremely basic: I merely increment a variable once per frame.  So far it does the job.</p>
<p>Each sequence is a series of bytes representing a monster ID.  Since Sequences can be of variable size (4, 5 or 6 monsters), I have a terminator ($FF) to signal to the engine when the Sequence ends.  Loading a sequence copies this series of bytes (including the terminator) to an array in RAM and sets an index variable to 0.</p>
<p>Drawing the sequence was pretty straight-forward too.  I cleverly designed the game board so that each Slot location corresponds to a 16&#215;16 square in the bottom-right quadrant of an attribute block.  This means that the only thing that differs between the 6 Slots as far as drawing is concerned is the low byte of the target PPU address.  Code is so much cleaner when everything lines up like this.</p>
<h3>Closing Slots</h3>
<p>Since there are no monsters yet, I have the game close a Slot every time a hole is whacked.  On the game logic level, all I have to do is increment the sequence index variable and check for the terminator.</p>
<p>Graphically it is no more complicated than drawing a monster image into a Slot.  At least it shouldn&#8217;t have been, but I got greedy and added a 3-frame animation.  It took me all day to finish as I had never done background animation before today, but it was good to get my hands dirty and figure it out.  I learned a lot.  As a final touch, I added a door-slam sound effect at the end of the animation, which incidentally forced me to code pitch envelopes for the <strong>noise channel</strong> into my sound engine.  Here&#8217;s the latest video:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/7pArurZj3Ic&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7pArurZj3Ic&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<h2>Conclusion</h2>
<p>There are still some things I want to add the the Sequence engine.  I want to animate the Slots opening when a new Sequence is displayed.  I also need to add in some mechanism to advance Levels/Sublevels after a specified number of Sequences are cleared.  What I mean is if the player is on Level 1-1, and they knock off, say 8 Sequences, it sends them to level 1-2, which would have a different set of Sequences for the engine to choose from.  These features can wait for another day though, as I&#8217;m all Nintendo&#8217;d out for today.  </p>
<p>(BTW, Google is the ultimate word-usage authority: it&#8217;s &#8220;Nintendo&#8217;d out&#8221;, not &#8220;Nintendoed out&#8221;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2010/02/15/ramses-game-3-monster-sequences/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ramses Game: 2 &#8211; Reading NES Button Combinations</title>
		<link>http://www.tummaigames.com/blog/2010/02/14/ramses-game-2-reading-nes-button-combinations/</link>
		<comments>http://www.tummaigames.com/blog/2010/02/14/ramses-game-2-reading-nes-button-combinations/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 15:42:15 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Ramses Game]]></category>
		<category><![CDATA[button combinations]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[dpad]]></category>
		<category><![CDATA[input]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=219</guid>
		<description><![CDATA[In Ramses Game, monsters pop out of holes and the player needs to whack them.  There are only 8 buttons on the NES controller, really 7 if you reserve Start for pausing the game.  This means that if I want more than 7 holes, I need to assign the holes to combinations of [...]]]></description>
			<content:encoded><![CDATA[<p>In <strong>Ramses Game</strong>, monsters pop out of holes and the player needs to whack them.  There are only 8 buttons on the <strong>NES controller</strong>, really 7 if you reserve Start for pausing the game.  This means that if I want more than 7 holes, I need to assign the holes to combinations of buttons rather than individual buttons.</p>
<p>To me the most obvious way to do this is to look for combinations like <strong>up+A</strong>, <strong>down+A</strong>, <strong>left+A</strong>, <strong>right+A</strong>.  Even if I reserve the Start button for pausing, that still leaves me with 12 combinations (4 each for A, B and Select).  12 holes is plenty for this kind of game, and actually due to screen space limitations I&#8217;ve decided to limit myself to 10 holes.</p>
<p><strong>Detecting button combinations on the NES</strong> is a little bit tricky.  I don&#8217;t want to allow the player to hold down B and spam the dpad to hit many holes very quickly.  Also, if the player presses up+left at the same time while pressing B, I don&#8217;t want two holes to be hit simulataneously.  On the other hand, it would be unreasonable to expect the player to hit two buttons at once in the exact same frame.  Nobody is that fast.  So I have to allow some leeway.  With that in mind, here is how my Button Combo routine works.</p>
<h2>Button Combo Routine</h2>
<h3>Output</h3>
<p>My button combo routine takes what the user has pressed and outputs a value 0-12.  The output possibilities are laid out like this:</p>
<ul>
<li>0: no combination was pressed.</li>
<li>1-4: B+dpad was pressed (in order: up, down, left, right)</li>
<li>5-8: A+dpad</li>
<li>9-12: Select+dpad</li>
</ul>
<p>Each of these values is mapped to a hole, and I use the outputted value to index into hole-related tables.  For example, here is a table that I use to determine if a valid hole was whacked:</p>
<pre>
<code>valid_holes:
    .byte 0             ;unused (no combo)
    .byte 1, 1, 1, 1    ;B (up, down, left, right order)
    .byte 1, 1, 1, 1    ;A
    .byte 1, 1, 0, 0    ;Select
    .byte 0, 0, 0       ;unused (select+left, select+right unused)</code>
</pre>
<p>I have tables for attribute table PPU addresses and the like as well.</p>
<h3>Input</h3>
<p>When I read the controller every frame, I store two sets of data in RAM.  One is the current button states (pressed or not pressed), which I store in a variable called <em>joypad1</em>.  The other is off-to-on transitions, ie what was unpressed last frame but is pressed this frame.  Off-to-on transitions are stored in a variable called <em>joypad1_pressed</em>.</p>
<p>Each variable is one byte, basically a bitflag with each bit corresponding to a button.  My controller reading routine stores button information in this order:</p>
<pre>
<code>joypad1 and joypad1_pressed bits:

76543210
||||||||
|||||||+- right
||||||+-- left
|||||+--- down
||||+---- up
|||+----- start
||+------ select
|+------- B
+-------- A</code>
</pre>
<h3>Logic</h3>
<p>The first thing I do in my button combo routine is to check if A, B or select is pressed.  If not, obviously we can&#8217;t have a combo, so skip to the end.</p>
<pre>
<code>;---------------------------------------------------
; check for dpad+button combos
.proc check_combos
    lda joypad1
    and #%11100000      ;isolate A, B, select
    beq @somewhere_near_the_end
    ;....snip</code>
</pre>
<p>If a button is pressed, I want to grab its combo value and store it in RAM:</p>
<pre>
<code>.proc check_combos
    lda joypad1
    and #%11100000      ;isolate A, B, select
    beq @clear_btn_val  ;if not pressed, clear btn_val
    lda joypad1_pressed
    and #%11100000      ;if off_to_on, we need to set the flag, else skip to dpad check
    beq @check_dpad
@set_btn_val:
    asl                 ;shift the A button bit to the CF
    bcc @B
    lda #$05            ;if A bit is set, store 5 in btn_flag  (5, 6, 7, 8 = A combo)
    bne @store
@B:
    asl                 ;shift the B button bit to the CF
    bcc @Select
    lda #$01            ;else if B bit is set, store 1 in btn_flag (1, 2, 3, 4 = B combo)
    bne @store
@Select:
    lda #$09            ;else Select bit must be set, so store 9 in btn_flag (9, 10, 11, 12 = Select combo)
@store:
    sta btn_val         ;add dpad value to this to get index
@check_dpad:
    ;.....snip

@clear_btn_val:
    lda #$00
    sta btn_val
@end:
    rts
.endproc
    </code>
</pre>
<p>Notice that I check if the button is newly pressed or if it was held from last frame.  This check is for a special case.  Say the user is holding A.  Then say a monster they want to smack appears in the up+B hole.  The player will press up+B, but there is a chance that there will be several frames of A+B as the player slides their thumb from A to B.  I want to catch the newest button press (B), not the older one (A).  So I check <em>joypad1_pressed</em>.  If a button is newly pressed this frame, I set btn_val.  If not, I skip straight to the dpad (btn_val having been set in a previous frame).</p>
<p>To say it another way, I only alter the value of btn_val in two cases: when a new button is pressed, or when no buttons are pressed.  This is important for another case too, which I will get to in a moment.</p>
<p>Note that if on the off-chance that B, A or Select are newly pressed simultaneous in the same frame, A takes precedence over B, which takes precedence over Select.  Not going to happen often, and when it does it&#8217;s not a big deal &#8211; the player will know they mashed both buttons.</p>
<p>Finally we check the dpad to complete the combo:</p>
<pre>
<code>@check_dpad:
    lda btn_val
    beq @end            ;if A, B, Select not pressed, no combo
    lda joypad1
    and #%00001111      ;isolate dpad
    beq @end            ;if no arrows pressed, no combo
    tay
    lda dpad_add, y     ;else, grab arrow value from table (up = 0, down=1, left=2, right=3)
    clc
    adc btn_val         ;add arrow value to button value.  Results in a number 1-12
    sta joypad1_combo
    jsr do_button_combo
@clear_btn_val:
    lda #$00
    sta btn_val
@end:
    rts</code>
</pre>
<p>First thing I check is if btn_val is 0.  But if we made it this far, shouldn&#8217;t this be non-zero?  The player is definitely holding down either A, B or Select afterall.  Yes they are, but btn_val could still be zero.  After a button combo is registered, I process the combo via do_button_combo and then I clear btn_val.  This check for btn_val == 0 covers the case that a button combination was recently processed (say last frame), but the player hasn&#8217;t released the buttons yet.  It took me some bug-hunting before I realized I needed this check.  Button Combinations are tricky, aren&#8217;t they?</p>
<p>The rest is pretty straightforward.  I check the dpad.  If no arrow is pressed, there is no combo so I return.  If an arrow is pressed, I grab its value from a table and add it to btn_val to get the final combo value.  The reason for the dpad_add table is to protect against cases of multiple arrows being pressed at the same time.  If up+left is pressed, for example, I resolve it to up.  If left+right is pressed, I resolve it to left.  If you&#8217;re curious, my table looks like this:</p>
<pre>
<code>;this table maps each possible d-pad combination to a single d-pad direction
;   0 = up, 1 = down, 2  = left, 3 = right
dpad_add:
    .byte 0     ;unused (will never be read)
    .byte 3     ;r -&gt; right
    .byte 2     ;l -&gt; left
    .byte 2     ;r+l -&gt; left
    .byte 1     ;d -&gt; down
    .byte 1     ;d+r -&gt; down
    .byte 1     ;d+l -&gt; down
    .byte 1     ;d+r+l -&gt; down
    .byte 0     ;u -&gt; up
    .byte 0     ;u+r -&gt; up
    .byte 0     ;u+l -&gt; up
    .byte 0     ;u+r+l -&gt; up
    .byte 0     ;u+d -&gt; up
    .byte 0     ;u+d+r -&gt; up
    .byte 0     ;u+d+l -&gt; up
    .byte 0     ;u+d+r+l -&gt; up</code>
</pre>
<p>Having this in table form makes it easy to modify in the future if I need to tweak some of the values (poor right arrow gets overruled a lot, doesn&#8217;t he?).  If you want to know more about this technique, I have written about <a href="http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/">protecting against odd dpad combinations using tables</a> before.</p>
<p>That&#8217;s it.  It&#8217;s a little complicated but it works.  I haven&#8217;t optimized this routine, so it&#8217;s possible that I could improve it somehow.  I&#8217;d rather move forward though, since this works fine the way it is.</p>
<p>Incidentally, the player is allowed to hold a dpad arrow and spam A/B/Select.  Doing so is of such limited use that I don&#8217;t see it being abusive.  If testing reveals otherwise though, I&#8217;ll rewrite the routine to protect against that.</p>
<h2>Full Routine</h2>
<p>Here&#8217;s the full routine in case anyone wants to copy/paste it into their game.  If you do, I will feel really happy if you mention me in the credits somewhere. :)</p>
<pre>
<code>;---------------------------------------------------
; check for dpad+button combos
.proc check_combos
    lda joypad1
    and #%11100000      ;isolate A, B, select
    beq @clear_btn_val  ;if not pressed, clear btn_val
    lda joypad1_pressed
    and #%11100000      ;if off_to_on, we need to set the flag, else skip to dpad check
    beq @check_dpad
@set_btn_val:
    asl                 ;shift the A button bit to the CF
    bcc @B
    lda #$05            ;if A bit is set, store 5 in btn_flag  (5, 6, 7, 8 = A combo)
    bne @store
@B:
    asl                 ;shift the B button bit to the CF
    bcc @Select
    lda #$01            ;else if B bit is set, store 1 in btn_flag (1, 2, 3, 4 = B combo)
    bne @store
@Select:
    lda #$09            ;else Select bit must be set, so store 9 in btn_flag (9, 10, 11, 12 = Select combo)
@store:
    sta btn_val         ;add dpad value to this to get index
@check_dpad:
    lda btn_val
    beq @end            ;if A, B, Select not pressed, no combo
    lda joypad1
    and #%00001111      ;isolate dpad
    beq @end            ;if no arrows pressed, no combo
    tay
    lda dpad_add, y     ;else, grab arrow value from table (up = 0, down=1, left=2, right=3)
    clc
    adc btn_val         ;add arrow value to button value.  Results in a number 1-12
    sta joypad1_combo
    jsr do_button_combo
@clear_btn_val:
    lda #$00
    sta btn_val
@end:
    rts
.endproc

;this table maps each possible d-pad combination to a single d-pad direction
;   0 = up, 1 = down, 2  = left, 3 = right
dpad_add:
    .byte 0     ;unused
    .byte 3     ;r -&gt; right
    .byte 2     ;l -&gt; left
    .byte 2     ;r+l -&gt;left
    .byte 1     ;d -&gt; down
    .byte 1     ;d+r -&gt; down
    .byte 1     ;d+l -&gt; down
    .byte 1     ;d+r+l -&gt; down
    .byte 0     ;u -&gt; up
    .byte 0     ;u+r -&gt; up
    .byte 0     ;u+l -&gt; up
    .byte 0     ;u+r+l -&gt; up
    .byte 0     ;u+d -&gt; up
    .byte 0     ;u+d+r -&gt; up
    .byte 0     ;u+d+l -&gt; up
    .byte 0     ;u+d+r+l -&gt; up</code>
</pre>
<h2>Conclusion</h2>
<p>So button combinations are done with, and if you watched the video embedded in the <a href="http://www.tummaigames.com/blog/2010/02/11/ramses-game-1-introduction/">last post</a> you will see that they work quite well!  This game is really coming along.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2010/02/14/ramses-game-2-reading-nes-button-combinations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ramses Game: 1 &#8211; Introduction</title>
		<link>http://www.tummaigames.com/blog/2010/02/11/ramses-game-1-introduction/</link>
		<comments>http://www.tummaigames.com/blog/2010/02/11/ramses-game-1-introduction/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 14:30:22 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Ramses Game]]></category>
		<category><![CDATA[overview]]></category>
		<category><![CDATA[progress]]></category>
		<category><![CDATA[story]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=213</guid>
		<description><![CDATA[I mentioned in my previous post that I have started a new project.  I learned a lot about code organization since the last time I worked on Explorer and I wanted to start fresh.  This project isn&#8217;t as ambitious as Explorer is, so I should be able to complete it more quickly and [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned in my <a href="http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/">previous post</a> that I have started a new project.  I learned a lot about code organization since the last time I worked on Explorer and I wanted to start fresh.  This project isn&#8217;t as ambitious as Explorer is, so I should be able to complete it more quickly and build my nesdev skills.</p>
<h2>Ramses Game</h2>
<p>The new game is tentatively titled <strong class="red">Ramses Game</strong>.  It&#8217;s a single screen action/puzzle game with a cutscene-driven storyline.  I&#8217;m using ca65 to assemble and the UNROM mapper.</p>
<h3>Story</h3>
<p>The story of the game goes like this:</p>
<blockquote><p>Ramses (the hero) was kidnapped from his home by space pirates and sold at a slave auction in the Artajian system to a cruel master named Bonehammer.  Now he finds himself on a monster-infested planet, trapped in a strange prison with a bomb strapped to his chest.  He sees a hammer in the corner of the room.  Curious, he picks up the hammer and the game for his life begins&#8230;</p></blockquote>
<h3>Gameplay</h3>
<p>I got the idea to make Ramses Game while I was playing Dragon Quest V on the DS.  There is a mini-game in DQV called <strong>Slime Smack</strong>, which is basically a touch-pen version of Whack-a-mole game with slimes:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/6PxS2zCB3Ns&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/6PxS2zCB3Ns&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p><strong>Ramses Game</strong> has a very similar concept.  The flow of gameplay looks like this:</p>
<blockquote><p>There are a series of HOLES.  CREATURES come out of the holes.  You whack them in the SEQUENCE indicated before the TIMER runs out.</p>
<p>Successfully whacking all CREATURES in a SEQUENCE will add time to the TIMER and bring up a new SEQUENCE.  Successfully completing a series of SEQUENCES (7-10?) will advance you to the next AREA.</p>
<p>If the TIMER ever reaches zero, you lose a LIFE.  If you lose all your LIVES, game over.</p></blockquote>
<p>Ramses Game will differ from Slime Smack some key areas though:</p>
<ol>
<li>You use an <strong>NES controller</strong> instead of a touch-pen.  This is why I was working on up+B, down+A button combinations.</li>
<li>A much larger variety of monsters with different abilities.  I have a design document full of monster ideas that will make the game more fun and challenging.</li>
<li>Multiple area themes, with matching monsters.</li>
<li>Possibly bosses at the end of each area.</li>
<li>Difficulty curve: at the beginning of the game there are <strong>eight holes</strong> and sequences of <strong>four monsters</strong>.  By the end there will be <strong class="red">ten holes</strong> and sequences of <strong class="red">six monsters</strong>.
<li>Cutscene-driven storyline to give the game more depth and color.</li>
</ol>
<h2>Progress</h2>
<p>I&#8217;ve made quite a bit of progress in the few days I&#8217;ve been working on it.  I have a placeholder gameboard that displays.  I have input-reading routines that successfully detect button combinations.  When you whack a hole on the gameboard, you get a visual and audio cue.  I made some dummy monster images that display up in the sequence indicator.  Everything is coming along nicely.  Here is a video of current gameplay:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/irhzagiaBbE&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/irhzagiaBbE&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>I&#8217;ll post some code snippets next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2010/02/11/ramses-game-1-introduction/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Button Combos, Sound Engines and Scopes</title>
		<link>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/</link>
		<comments>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 17:06:51 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[NES]]></category>
		<category><![CDATA[code cleanup]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[sound engine]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=208</guid>
		<description><![CDATA[It&#8217;s been a while since I posted here.  I took a break from my homebrew to write some tutorials on how to code a sound engine for the NES.  You can find them here: NES Sound Tutorials.
After a long break I finally got back to some homebrew coding.  I worked in 6502 [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I posted here.  I took a break from my homebrew to write some tutorials on how to code a <strong>sound engine</strong> for the <strong>NES</strong>.  You can find them here: <a href="http://nintendoage.com/forum/messageview.cfm?catid=22&#038;threadid=7155">NES Sound Tutorials</a>.</p>
<p>After a long break I finally got back to some homebrew coding.  I worked in <strong>6502</strong> for a few hours today.  I didn&#8217;t work on <a href="http://www.tummaigames.com/blog/category/development/nes/explorer/">Explorer</a>, but rather a different game project that is less ambitious.  I&#8217;ll introduce the game properly in a future post.</p>
<h2>What I Did Today</h2>
<h3>Button Combos</h3>
<p>One thing that I needed for the new game project was a way to detect <strong>button combinations</strong> like up+A or right+B &#8211; the kind that you use to select plays in TecmoBowl.  It was a little tricky to pull off.  For one thing, if the player presses something like up+left+A, I need to resolve that to either up+A or left+A.  I need to work that out for all possible combinations of button presses.  This includes taking care of cases where A+B are pressed at the same time.</p>
<p>Another issue is with <strong>holding buttons</strong>.  If the player holds B down and makes circles around the dpad, I only want it to catch the first combination.  To do two combos in a row, they should release the B button and press it again.  But what if the player hits up+B, and then while still holding B they press A+left?  Does the holding of B stop the 2nd combination from being picked up or not?  There are a lot of possible cases since the controller reading routine is called 60 times per second.  The player can&#8217;t control their input on a frame-by-frame scale.  Sliding from B to A might very well contain several frames (fraction of a second) where both buttons are held down before the B button is released.  Not impossible to solve, but it wasn&#8217;t as straightforward as I had expected.</p>
<h3>Sound Engine</h3>
<p>Another thing I did today was port my sound engine to <a href="http://www.cc65.org/doc/ca65.html">ca65</a>.  I wrote my <a href="http://nintendoage.com/forum/messageview.cfm?catid=22&#038;threadid=7155">NES sound tutorials</a> for the <strong>NESASM</strong> assembler.  My tutorials are a continuation of another set of tutorials which use NESASM and I wanted to stay consistent.  Plus most new nesdevers start with NESASM as it is very newbie-friendly.</p>
<p>My tutorial sound engine was an improvement over the one I had previously written in ca65, so I wanted to replace the old with the new.  So I spent some time today porting my sound engine from NESASM to ca65.  I was sure everything was going to break as soon as I got all the syntax converted.  But it didn&#8217;t!  Once I got it to assemble it worked perfectly on the first try!  That was a relief.</p>
<h3>Scopes</h3>
<p>ca65 lets you define <strong>scopes</strong>.  Scopes enable you to keep parts of your program hidden from the other parts of your program.  It&#8217;s good to separate modules, especially in large programs, so that you don&#8217;t have variable conflicts and the like.  These bugs are the hardest to trace in assembly language.</p>
<p>I didn&#8217;t use scopes before because I didn&#8217;t really know how.  But I found a great thread on <a href="http://nesdev.parodius.com/bbs/viewtopic.php?t=5481">nesdev about ca65 scopes</a> that told me how to do it, so I spent some time today separating my sound and controller reading modules from the main program.  Everything feels much &#8230; cleaner now.  Like washing your hands after working outside on a summer day.  I can physically feel how much easier things are going to be in the future for me with scopes.</p>
<h2>New NES</h2>
<p>I bought an NES!  Ordered it on Yahoo Auctions on Saturday and it arrived at my house on Sunday!  All I need now is a <a href="http://www.retrousb.com/product_info.php?cPath=24&#038;products_id=34">PowerPak</a> and I can start testing my programs on real hardware.  I can&#8217;t wait!</p>
<p>I&#8217;m too lazy to upload pictures here, but you can see the ones I posted to twitter here:</p>
<p><a href="http://twitpic.com/11ucz8">My new NES 1</a><br />
<a href="http://twitpic.com/11udov">My new NES 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2010/02/08/button-combos-sound-engines-and-scopes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Explorer: 12 &#8211; Monster Art</title>
		<link>http://www.tummaigames.com/blog/2009/08/17/explorer-12-monster-art/</link>
		<comments>http://www.tummaigames.com/blog/2009/08/17/explorer-12-monster-art/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 14:38:42 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=194</guid>
		<description><![CDATA[Took some time away from work and hobbies to spend some time with the family.  Before I left I was working on adding some enemies into the game.  I made three different monsters and added enemy-loading logic to the room-loading routines.  The enemies display onscreen fine and die when you shoot their [...]]]></description>
			<content:encoded><![CDATA[<p>Took some time away from work and hobbies to spend some time with the family.  Before I left I was working on adding some enemies into the game.  I made three different monsters and added enemy-loading logic to the room-loading routines.  The enemies display onscreen fine and die when you shoot their HP to 0.  Tomorrow I&#8217;m going to work on some movement patterns.</p>
<p>I felt I should do some kind of update though, so here are the terrifying monsters I whipped up in GIMP.  Turn on the lights before looking at them or you might die in your sleep:</p>
<ol>
<li>Purple Tongue Head<br />
<div id="attachment_195" class="wp-caption alignnone" style="width: 138px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/08/purple_tongue_head.png" alt="When you see it coming, the shit runs down your leg." title="purple_tongue_head" width="128" height="128" class="size-full wp-image-195" /><p class="wp-caption-text">When you see it coming, the shit runs down your leg.</p></div>
</li>
<li>Eyeball<br />
<div id="attachment_196" class="wp-caption alignnone" style="width: 138px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/08/eyeball.png" alt="It&#039;s looking for something to KILL!" title="eyeball" width="128" height="128" class="size-full wp-image-196" /><p class="wp-caption-text">It's looking for something to KILL!</p></div>
</li>
<li>Shuriken<br />
<div id="attachment_197" class="wp-caption alignnone" style="width: 138px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/08/shuriken.png" alt="It&#039;s pointy." title="shuriken" width="128" height="128" class="size-full wp-image-197" /><p class="wp-caption-text">It's pointy.</p></div>
</li>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/08/17/explorer-12-monster-art/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Explorer: 11 &#8211; Sobjects</title>
		<link>http://www.tummaigames.com/blog/2009/08/06/explorer-11-sobjects/</link>
		<comments>http://www.tummaigames.com/blog/2009/08/06/explorer-11-sobjects/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 14:01:11 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=185</guid>
		<description><![CDATA[I haven&#8217;t posted in a few days and that&#8217;s because I&#8217;ve been spending my time doing research.  I&#8217;ve been peeking at other games and seeing how they handle sprite objects.  By sprite object, I mean entities in a game that are represented on screen with sprites: enemies, projectiles, powerups, etc.  When I [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t posted in a few days and that&#8217;s because I&#8217;ve been spending my time doing research.  I&#8217;ve been peeking at other games and seeing how they handle <strong>sprite objects</strong>.  By sprite object, I mean entities in a game that are represented on screen with sprites: <strong>enemies</strong>, <strong>projectiles</strong>, <strong>powerups</strong>, etc.  When I was trying to code bullets, I didn&#8217;t really know how to go about it.  Should I keep them separate from other <strong>sobjects</strong> (sprite objects) or are there enough similarities to lump them together?</p>
<p>If you think about it, <strong>a bullet isn&#8217;t much different from an enemy</strong>.  It has an x and a y. It has a speed and a movement pattern.  It has a palette.  It has a hit box.  The main difference is what you check for collision against.  Enemies collide with the hero.  Hero bullets don&#8217;t.  They collide with enemies instead.</p>
<p>So some logic is different, but the data (or rather type of data stored) is the same!   As far as turning the sobject data into actual sprites, bullets can be treated just the same as enemies (the hero too).  Why do I care?  If I am able to lump them all together, I can make all the sprites in a single loop.  Here&#8217;s an example of what I mean:</p>
<h2>Sobjects to Sprites</h2>
<p>Let&#8217;s say I have a <strong>hero</strong>.  That&#8217;s one sobject.  He can shoot <strong>bullets</strong>.  Let&#8217;s say I want to allow 3 bullets maximum on the screen at a time.  That&#8217;s three more potential sobjects.  Then <strong>enemies</strong>.  Just pulling a number out of the air, lets say I want there to be 10 enemies on the screen at a time maximum.  That&#8217;s ten more potential sobjects.</p>
<p><strong>1 hero + 3 bullets + 10 enemies = 14.</strong></p>
<p>So I have 14 sobjects that could be represented on the screen at any one time.  If I treat them all the same, I can hold all of the data in arrays, like this:</p>
<pre>
<code>sobj_id: .res 14
sobj_hp: .res 14
sobj_x: .res 14
sobj_y: .res 14
sobj_hitbox_top: .res 14
sobj_hitbox_bottom: .res 14
...etc
</code>
</pre>
<p>Then whenever I need to turn them into sprites, I can just loop through them all using an index register:</p>
<pre>
<code>sobjects_to_sprites:
    ldx #$00
@loop:
    lda sobj_id, x
    <span class="codecomment">;do stuff
    ;calculate y, CHR#, attrib and x
    ;for each sprite of the sprite object
    ;and store in DMA sprite RAM</span>

    inx
    cpx #.sizeof(sobj_id) <span class="codecomment">;loop through all 14</span>
    bne @loop
    rts</code>
</pre>
<p>And that will cover all my sobjects as far as turning their data into sprites.  I don&#8217;t need to write more than one routine!</p>
<p>To differentiate between sobject <strong>types</strong> (bullet vs. enemy vs. hero), I can just have their index range set.  For example, hero is always index 0.  Bullets are always 1-3.  And enemies are always 4-13.  This way I always know what to test for collisions against: enemies will always test against index 0 (hero) and deal damage on a collision.  Enemies will also test against index 1-3 (bullets) and receive damage on a collision.</p>
<p>With defined index ranges, limit testing is easy too: If the player presses the fire button, check sobjects 1-3.  If they are full already, don&#8217;t create a new bullet.  If there is an open space there, create a new bullet.</p>
<h2>Conclusion</h2>
<p>I think I finally figured out how I&#8217;m going to arrange sprite objects.  Today I rewired all of my previous hero-moving code to fit into this new data model and I think I have it working.  Tomorrow I&#8217;m going to try to add bullets for real!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/08/06/explorer-11-sobjects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Explorer: 10 &#8211; Refocus</title>
		<link>http://www.tummaigames.com/blog/2009/08/01/explorer-10-refocus/</link>
		<comments>http://www.tummaigames.com/blog/2009/08/01/explorer-10-refocus/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 06:30:47 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=183</guid>
		<description><![CDATA[I originally started working on this game as an entry for a coding competition.  The genre was puzzle games and I thought a switch-throwing maze game would be fun to write.  I missed the deadline for the contest long ago but I kept working on the game anyway.  Now I&#8217;m thinking about [...]]]></description>
			<content:encoded><![CDATA[<p>I originally started working on this game as an entry for a coding competition.  The genre was puzzle games and I thought a switch-throwing maze game would be fun to write.  I missed the deadline for the contest long ago but I kept working on the game anyway.  Now I&#8217;m thinking about taking the game in a new direction.</p>
<p>The decision to make a maze-only game was made because there was a deadline, and because the genre for the contest was &#8220;puzzle&#8221;.  But now that I have as much time and freedom as I want, I think I&#8217;m going to expand the game beyond just finding your way through a maze.  Specifically, here are some things I want to add that weren&#8217;t in the original plan:</p>
<ol>
<li>Enemies and bosses</li>
<li>Weapons</li>
<li>A way to die (ie, health bar)</li>
<li>A way to improve (more weapons.  powerups&#8230; level ups?)</li>
</ol>
<p>In addition to these changes, I&#8217;m also going to change the setting and storyline completely.  No more treasure hunting archaeologist.  I haven&#8217;t worked out all the details but I think it&#8217;s going to be somewhat sci-fi, with the hero as a cyborg (ie, guns not swords).  I think using the Guardian Legend girl as my test sprite has influenced me in some way. :).</p>
<p>Anyway, this comes at just the right time.  I just got switches and doors working and the next thing I was going to write was a way to break the breakable blocks.  In the original maze-game concept there was going to be an item (like a hammer) that the archaeologist hero would find to allow him to break blocks.   Now I think it will be a powerup that makes your gun blast stronger.</p>
<p>Next objective: make the hero shoot a projectile when pressing A.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/08/01/explorer-10-refocus/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Explorer: 10 &#8211; Sprite Collision</title>
		<link>http://www.tummaigames.com/blog/2009/07/29/explorer-10-sprite-collision/</link>
		<comments>http://www.tummaigames.com/blog/2009/07/29/explorer-10-sprite-collision/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 13:49:59 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=166</guid>
		<description><![CDATA[In the last post, I got the hero sprite moving.  But there&#8217;s a problem: she can walk through walls.  I need to add collision detection with background tiles.  Collision detection with the background can be summed up like this:

if the player tries to move:
    check the space they want [...]]]></description>
			<content:encoded><![CDATA[<p>In the last post, I got the hero sprite moving.  But there&#8217;s a problem: she can walk through walls.  I need to add collision detection with background tiles.  Collision detection with the background can be summed up like this:</p>
<pre>
<code>if the player tries to move:
    check the space they want to move to
    if that space is solid/blocked:
        don't move
    else:
        move
</code>
</pre>
<p>The looks pretty easy, but it can actually be quite complicated.  Here are some issues:</p>
<ol>
<li>The hero sprite has x and y coordinates, which are on the pixel level.  My room tile data is stored in ram on the metatile level.  We need to make a conversion there.</li>
<li>There isn&#8217;t any one point on the hero that is a catch-all for movement checks.  If the player tries to move left, we will want to check for a collision with the left side of the hero.  If the player tries to move right, we want to check for a collision with the right side of the hero.  So we will need to make a <strong>hit box</strong>.</li>
<li>Each side of the hit box will have a length > 1, so we may have to check for a collision with more than one background tile.  For example, say the player wants to move right.  We will check using the right side of the hit box.   But it&#8217;s possible that the top half of the hit box will be adjacent to one tile, while the bottom half is adjacent to another.
<div id="attachment_167" class="wp-caption aligncenter" style="width: 409px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_hitbox_2_tiles.jpg" alt="If we only checked the top-right corner of this hit box for collision, it would show the way as clear.  But it really isn&#039;t, because the bottom half of the hit box collides with a solid tile." title="explorer_hitbox_2_tiles" width="399" height="337" class="size-full wp-image-167" /><p class="wp-caption-text">If we only checked the top-right corner of this hit box for collision, it would show the way as clear.  But it really isn't, because the bottom half of the hit box collides with a solid tile.</p></div></li>
</ol>
<h2>Step 1</h2>
<p>First thing I want to do is make a <strong>hit box</strong>.  I&#8217;m going to put it around the hero sprite&#8217;s feet.  I define where the edges are relative to the hero&#8217;s X/Y coords, then make a subroutine to calculate the box every frame:</p>
<pre>
<code><span class="codecomment">;collision box for the hero sprite (box around the feet)</span>
HERO_MOVEMENT_BOX_TOP = 26
HERO_MOVEMENT_BOX_BOTTOM = 31
HERO_MOVEMENT_BOX_LEFT = 5
HERO_MOVEMENT_BOX_RIGHT = 11
</code>
</pre>
<pre>
<code>hero_box: .res 4 <span class="codecomment">;top bottom left right</span>
</code>
</pre>
<pre>
<code>set_hero_box:
    lda hero_x
    clc
    adc #HERO_MOVEMENT_BOX_LEFT
    sta hero_box+2
    lda hero_x
    clc
    adc #HERO_MOVEMENT_BOX_RIGHT
    sta hero_box+3
    lda hero_y
    clc
    adc #HERO_MOVEMENT_BOX_TOP
    sta hero_box
    lda hero_y
    clc
    adc #HERO_MOVEMENT_BOX_BOTTOM
    sta hero_box+1
    rts
</code>
</pre>
<h2>Movement within a metatile</h2>
<p>Since every metatile is 16 pixels wide and 16 pixels tall, I can take a shortcut if I determine that the player is not at the edge of a tile.  Check out these two cases:</p>
<p><div id="attachment_172" class="wp-caption aligncenter" style="width: 120px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_hitbox_midtile.jpg" alt="We don&#039;t need to check for a collision in the left case.  The player is already on a walkable tile, and will remain on that tile if they move a pixel to the right.  " title="explorer_hitbox_midtile" width="110" height="85" class="size-full wp-image-172" /><p class="wp-caption-text">We don't need to check for a collision in the left case.  The player is already on a walkable tile, and will remain on that tile if they move a pixel to the right.  </p></div>
<p>Let&#8217;s assume the player is trying to move right.  The player is on a walkable tile in both cases.  In the first case, they are in the middle of the tile.  In the second case they are at the edge.  We only need to check for a collision in the second case.  Checking the first case would be a waste of time, because we&#8217;d be checking the tile the player is already on for walkability, but we know it must be walkable since the player is already on it.  So right away we can skip over a collision check if we determine the player to be in the middle of the tile (ie, not on the edge).</p>
<p>This is very easy to pull off with 16&#215;16 metatiles.  In all cases, the left edge of a metatile will have an x-coord of $x0, and the right edge will be $xF.  The top edge of a metatile will have a y-coord of $x0 and the bottom will be $xF.  So in our collision detection routines, we can do something like this:</p>
<pre>
<code>can_move_right:
    ldx hero_box+3 <span class="codecomment">;right edge of movement box</span>
    inx <span class="codecomment">;we want to peek at the next pixel over</span>
    txa

    and #$0F <span class="codecomment">;isolate right nibble,</span>
         <span class="codecomment">; tells us where we are WITHIN a tile</span>

    bne @move_ok <span class="codecomment">;not on the edge.  movement within current</span>
                 <span class="codecomment">;square OK (in other words, if the right</span>
                 <span class="codecomment">;nibble is 0, we are on the left edge of the</span>
                 <span class="codecomment">;NEXT tile, and therefore need to check</span>
                 <span class="codecomment">;for collision)</span>

    <span class="codecomment">;... check for collisions</span>

@move_ok:
    sec  <span class="codecomment">;return a 1 if movement is OK</span>
    rts
</code>
</pre>
<p>I use a similar check at the beginning of <strong>can_move_left</strong>, <strong>can_move_up</strong> and <strong>can_move_down</strong>.</p>
<h2>How many checks?</h2>
<p>Do I need to check one bg tile for collision or two?  It depends on the hero sprite&#8217;s position.  Imagine the player is moving right again.  If the top and bottom edges of the hit box are both within the boundaries of a single tile, we only need to check one bg tile for collision.  But check this picture (should look familiar):</p>
<div id="attachment_173" class="wp-caption aligncenter" style="width: 409px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_hitbox_2_tiles2.jpg" alt="moving right, the top and bottom edges of the hit box touch two different bg tiles.  We need to make two checks." title="explorer_hitbox_2_tiles2" width="399" height="337" class="size-full wp-image-173" /><p class="wp-caption-text">moving right, the top and bottom edges of the hit box touch two different bg tiles.  We need to make two checks.</p></div>
<p>In this case, the top and bottom edges of our hit box line up with two different bg tiles.  So we need to check both.  If either of them are unwalkable, we don&#8217;t allow movement.  </p>
<p>So, two checks or one check?  There are a couple of ways to determine if the top and bottom edges of our hit box touch different tiles.  I do it this way:</p>
<pre>
<code>    <span class="codecomment">;find how many tiles to check (1 or 2)</span>
    lda hero_box <span class="codecommen">;top edge of movement hit box</span>

    ora #$F0 <span class="codecomment">;we are going to add the box height,</span>
             <span class="codecomment">;so set us up to check for a FF-&gt;00 transition</span>

    adc #HERO_MOVEMENT_BOX_HEIGHT <span class="codecomment">;negative means our box is</span>
                                  <span class="codecomment">;h-aligned with one tile.</span>
                                  <span class="codecomment">;positive = 2 tiles</span>
    bmi @not_two

    <span class="codecomment">;... check a tile</span>

@not_two:
    <span class="codecomment">;... check a tile</span>
</code>
</pre>
<p>I do similar tests for the other directions, but for <strong>can_move_up</strong> and <strong>can_move_down</strong> I will check the left and right edges instead of the top and bottom edges.</p>
<h2>Hero pixel coords to a room coords</h2>
<p>I need to turn the hero&#8217;s pixel coordinates into room coordinates so that I can find which bg tiles to check.  Here&#8217;s my subroutine to do that:</p>
<pre>
<code><span class="codecomment">;------------------------------</span>
<span class="codecomment">; set_hero_map_coords finds the x and y room coords for the </span>
<span class="codecomment">; topleft pixel of the player's movement hit box</span>
set_hero_map_coords:
    lda hero_box+2 <span class="codecomment">;left</span>
    ldx #$00
    sec
:
    sbc #$10
    bcc :+
    inx
    jmp :-
:
    stx hero_map_x

    lda hero_box <span class="codecomment">;top</span>
    ldx #$00
    sec
:
    sbc #$10
    bcc :+
    inx
    jmp :-
:
    dex
    dex <span class="codecomment">;correct y for the status bar</span>
    stx hero_map_y
    rts
</code>
</pre>
<p>Once I have the hero&#8217;s room coordinates, I can add to or subtract from them to find the coordinates for the adjacent tiles I want to check for walkability.</p>
<h2>Walkability</h2>
<p>How do I check for walkability?  With a lookup table:</p>
<pre>
<code><span class="codecomment">;tile ids</span>
.enum
    floor
    wall
    block
    water
    block_breakable
    stairs_up
    stairs_down
    pitfall
.endenum
</code>
</pre>
<pre>
<code>.enum <span class="codecomment">;walkability</span>
    unwalkable
    walkable
.endenum
</code>
</pre>
<pre>
<code>tile_walkability:
    .byte walkable, unwalkable, unwalkable, unwalkable
    .byte unwalkable, walkable, walkable, walkable
</code>
</pre>
<p>I read a tile id from the room map in RAM and use it to index into the tile_walkability table.</p>
<p>Here&#8217;s a simplified <strong>can_move_right</strong>.  (I took out checks for room boundaries to make it more readable):</p>
<pre>
<code>can_move_right:
    ldx hero_box+3 <span class="codecomment">;right edge of movement box</span>
    inx
    txa

    and #$0F <span class="codecomment">;isolate right nibble, where are we WITHIN a tile?</span>

    bne @move_ok <span class="codecomment">;not on edge. move within current square OK</span>

    <span class="codecomment">;how many tiles to check (1 or 2)</span>
    lda hero_box <span class="codecomment">;top edge of movement box</span>
    ora #$F0
    adc #HERO_MOVEMENT_BOX_HEIGHT
    bmi @not_two

    ldy hero_map_y
    iny
    ldx hero_map_x
    inx
    jsr get_room_offset <span class="codecomment">;takes x/y room coords and returns an</span>
                        <span class="codecomment">;array index in y</span>

    lda room, y
    tay
    lda tile_walkability, y
    beq @no_move

@not_two:
    ldy hero_map_y
    ldx hero_map_x
    inx
    jsr get_room_offset

    lda room, y
    tay
    lda tile_walkability, y
    beq @no_move
@move_ok:
    sec <span class="codecomment">return 1 in the carry if we can move</span>
    rts
@no_move:
    clc <span class="codecomment">return 0 in the carry if we can't move</span>
    rts
</code>
</pre>
<p>I have seperate routines for the other 3 directions.  They are all very similar.  The last step is to update my <strong>move_hero</strong> subroutine to call these collision detection routines before moving, and then skip movement on carry clear.</p>
<h2>Conclusion</h2>
<p>Now I have a sprite that changes direction, moves, animates and bumps into walls.  Here&#8217;s the latest video demo:  </p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/a4fJnAkra9c&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/a4fJnAkra9c&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/07/29/explorer-10-sprite-collision/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Explorer: 9 &#8211; Sprite Movement</title>
		<link>http://www.tummaigames.com/blog/2009/07/26/explorer-9-sprite-movement/</link>
		<comments>http://www.tummaigames.com/blog/2009/07/26/explorer-9-sprite-movement/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 15:00:19 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=159</guid>
		<description><![CDATA[Ok, I got the hero to change directions based on user input.  Now it&#8217;s time to move her.  The first step for this is to have a hero_moving flag that the input handler will set if there is d-pad input:

hero_moving: .res 1


handle_joypad:
    lda joypad1
    and #$F0
  [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, I got the hero to <a href="http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/">change directions</a> based on user input.  Now it&#8217;s time to move her.  The first step for this is to have a hero_moving flag that the input handler will set if there is d-pad input:</p>
<pre>
<code>hero_moving: .res 1</code>
</pre>
<pre>
<code>handle_joypad:
    lda joypad1
    and #$F0
    beq @end
    lsr
    lsr
    lsr
    lsr
    tay
    lda direction_change_table, y
    sta hero_direction
    <strong>lda #$01
    sta hero_moving</strong>
    rts
@end:
    <strong>lda #$00
    sta hero_moving</strong>
    rts
</code>
</pre>
<p>With this updated input handler, the <strong>hero_moving</strong> flag will be 1 when the d-pad is pressed, and 0 when it is not.  And as before, our hero&#8217;s direction will be stored in <strong>hero_direction</strong>.</p>
<p>Next, I need to check the hero moving flag in my <strong>update_hero_sprite</strong> routine, which is called every frame:</p>
<pre>
<code>update_hero_sprite:
    lda hero_moving
    beq @hero_moving_done

    jsr move_hero
@hero_moving_done:
    <span class="codecomment">;...</span>
    <span class="codecomment">;write sprite data to RAM as before</span>
</code>
</pre>
<p>Now that I have the foundation laid, I need to write the <strong>move_hero</strong> routine.</p>
<h2>move_hero</h2>
<p>There are 8 possible directions the hero could be facing.  The current direction is stored in a variable called <strong>hero_direction</strong>.  The possible values for this variable are 0-7, as assigned by these constants:</p>
<pre>
<code><span class="codecomment">;direction indexes for tables</span>
SPRITE_UP =         $00
SPRITE_DOWN =       $01
SPRITE_LEFT =       $02
SPRITE_RIGHT =      $03
SPRITE_UP_LEFT =    $04
SPRITE_UP_RIGHT =   $05
SPRITE_DOWN_LEFT =  $06
SPRITE_DOWN_RIGHT = $07
</code>
</pre>
<p>I need to alter the hero&#8217;s coordinates based on the direction the player is moving.  If they are moving left, I need to subtract from <strong>hero_x</strong>.  If they are moving right, I need to add to <strong>hero_x</strong>.  If they are moving up, I need to subtract from <strong>hero_y</strong>.  If they are moving down, I need to add to <strong>hero_y</strong>.  If they are moving diagonally, I need to update both <strong>hero_x</strong> and <strong>hero_y</strong>.  There are many possibilities, and rather than have a million branch instructions, I&#8217;ll use table lookups again.  I will have one table for horizontal movement and one table for vertical movement.  I will index into these tables using <strong>hero_direction</strong>.  The tables look like this:</p>
<pre>
<code><span class="codecomment">;up, down, left, right, up_left, up_right, down_left, down_rt</span>
hero_movement_x:
    .byte $00, $00, $FF, $01, $FF, $01, $FF, $01

hero_movement_y:
    .byte $FF, $01, $00, $00, $FF, $FF, $01, $01
</code>
</pre>
<p>My <strong>move_hero</strong> routine will read from these tables and add to the hero coordinates.  Note that adding $FF to a number (on an 8-bit system) is the same as subtracting 1, since it will wrap around from $FF to $00.  Here is move_hero:</p>
<pre>
<code>move_hero:
    ldy hero_direction
    lda hero_movement_x, y
    clc
    adc hero_x
    sta hero_x

    ldy hero_direction
    lda hero_movement_y, y
    clc
    adc hero_y
    sta hero_y

    rts
</code>
</pre>
<p>And now she&#8217;s moving.</p>
<p><a href="http://morphcat.de/">miau</a> pointed out in a <a href="http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/comment-page-1/#comment-27">comment</a> that diagonal movement should really alter x and y by SQRT(2) rather than 1 (see <a href="http://en.wikipedia.org/wiki/Pythagorean_theorem">Pythagorean Theorum</a>).  This is something I didn&#8217;t consider before, so my hero covers ground a little more quickly when moving diagonally.  I&#8217;m not sure yet if this is undesirable behavior or not.  If I keep it a simple maze-solving game, it probably won&#8217;t be an issue.  If I get more ambitious and change it to a more action-oriented game with enemies (something I&#8217;ve been seriously thinking about), it may be an issue.  We&#8217;ll see :).</p>
<h2>Animation</h2>
<p>I want to talk very briefly about animating the sprite.  To do animation, I need a few things:</p>
<ol>
<li>Graphics for the various frames of animation</li>
<li>A counter to tell me when to change animation frames</li>
<li>A variable telling me the current animation frame</li>
</ol>
<p>I already have the graphics for my test sprite.  The other two I need to make myself.</p>
<pre>
<code>frame_counter: .res 1
hero_anim_frame: .res 1
</code>
</pre>
<p>The animation is a walking animation, so I don&#8217;t want the hero to animate unless she is moving.  It makes sense then to do the animation frame-changing logic in the <strong>move_hero</strong> routine.  The Guardian Legend hero sprite has a 4-frame walking animation, so here&#8217;s how I do it:</p>
<pre>
<code>move_hero:
    ldy hero_direction
    lda hero_movement_x, y
    clc
    adc hero_x
    sta hero_x

    ldy hero_direction
    lda hero_movement_y, y
    clc
    adc hero_y
    sta hero_y

    <strong>inc frame_counter</strong>
    <strong>lda frame_counter</strong>
    <strong>cmp #$0B</strong> <span class="codecomment">;change anim frame every 11 "moving frames"</span>
             <span class="codecomment">;found this number by trial and error</span>
    <strong>bcc @end</strong>

    <strong>lda #$00</strong>
    <strong>sta frame_counter</strong> <span class="codecomment">;reset counter</span>
    <strong>inc hero_anim_frame</strong> <span class="codecomment">;go to next anim frame</span>
    <strong>lda hero_anim_frame</strong> <span class="codecomment">;make sure we stay between 0 and 3</span>
    <strong>and #$03</strong>
    <strong>sta hero_anim_frame</strong>
<strong>@end:</strong>
    rts
</code>
</pre>
<p>Then I will modify my CHR tile lookup tables to handle graphics for all four frames of animation for all 8 directions.  Then I will update my <strong>update_hero_sprite</strong> routine to read from these tables based on the values of <strong>hero_anim_frame</strong> and <strong>hero_direction</strong>.  I&#8217;m not going to post the code, because it&#8217;s a little long and there are a lot of tables.  If anybody wants me to elaborate more, let me know in the comments :)</p>
<p>BTW, before I forget!  When adding new variables, it&#8217;s a good idea to initialize them.  Here is my updated <strong>initialize_hero_sprite</strong> routine:</p>
<pre>
<code>initialize_hero_sprite:
    lda #$50
    sta hero_x
    sta hero_y

    <strong>lda #$00
    sta hero_moving
    sta hero_anim_frame
    sta frame_counter</strong>

    lda #SPRITE_DOWN
    sta hero_direction

    jsr update_hero_sprite

    rts
</code>
</pre>
<h2>Conclusion</h2>
<p>Whew.  I covered a lot of ground today.  I probably should have made this two posts instead of one, but I want to hurry up and get to collision detection so I can put up the next demo video.  :)  </p>
<p>You may have noticed by now that I use <strong>hero_direction</strong> a lot to index into lookup tables.  This is because everything changes based on the hero&#8217;s direction, and lookup tables are a great alternative to long sections of compare/branch code.  Often when I find myself writing a lot of branching code I&#8217;ll see if I can come up with some scheme to turn the test value into a table index.</p>
<p>See you next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/07/26/explorer-9-sprite-movement/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Explorer: 8 &#8211; Sprite Direction</title>
		<link>http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/</link>
		<comments>http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 15:51:43 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Explorer]]></category>

		<guid isPermaLink="false">http://www.tummaigames.com/blog/?p=140</guid>
		<description><![CDATA[Last time I wrote code to put a hero sprite onscreen.  In my first (pre-sprite) video demo, I had room changes triggered directly by player input.  Today I&#8217;m going to recode the input handler to alter the hero instead, specifically the hero&#8217;s direction.
Joypad revisited
I posted my joypad reading code in a previous post, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tummaigames.com/blog/2009/07/23/explorer-7-sprite-time/">Last time</a> I wrote code to put a hero sprite onscreen.  In my first (pre-sprite) <a href="http://www.youtube.com/watch?v=S5s8LmuIFS4">video demo</a>, I had room changes triggered directly by player input.  Today I&#8217;m going to recode the input handler to alter the hero instead, specifically the hero&#8217;s <strong>direction</strong>.</p>
<h2>Joypad revisited</h2>
<p>I posted my joypad reading code in a <a href="http://www.tummaigames.com/blog/2009/07/21/explorer-6-navigation-and-input/">previous post</a>, but here it is again for easy reference:</p>
<pre>
<code>update_joypad_data: <span class="codecomment">;props to blargg for A-only joypad read.</span>
    lda joypad1
    sta joypad1_old
    lda #%01111111
    sta joypad1
    sta $4016
    asl a
    sta $4016
@loop:
    lda $4016
    and #$03  <span class="codecomment">;props to Disch for Famicom support</span>
    cmp #$01
    ror joypad1 <span class="codecomment">;right, left, down, up, start, select, B, A</span>
    bcs @loop

    lda joypad1_old
    eor #$FF
    and joypad1
    sta joypad1_pressed <span class="codecomment">;this tracks off-to-on transitions.</span>

    lda joypad1
    eor #$FF
    and joypad1_old
    sta joypad1_released <span class="codecomment">;this tracks on-to-off transitions</span>
    rts
</code>
</pre>
<p>Before, I was checking <strong>joypad_pressed</strong> to determine whether or not to change a room.  This was so that if the player held down a button, it would only change rooms once (instead of once per frame held).  But now that I&#8217;m going to be using input to control the sprite, it makes more sense to check <strong>joypad1</strong> and use every frame&#8217;s input.  If the player holds down the d-pad, the sprite should keep moving.</p>
<p>But I&#8217;m not quite ready to move yet!</p>
<h2>Changing Direction</h2>
<p>I want to rewrite my input handler so that it takes d-pad input and uses it to set the hero&#8217;s direction.  This sounds pretty straightforward, but there are some special cases I need to be wary of: what happens if the player presses <strong>left+right</strong>?  or <strong>up+down</strong>?  This isn&#8217;t likely to happen on a real controller, but most people play Nintendo games on emulators these days.  If you don&#8217;t protect against these strange key combinations, you open yourself up to some pretty bizarre glitches, like the <strong>Zelda 2 acceleration glitch</strong> (commentator talks about it around the 4:20 mark):</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/Vqry4oBHOsc&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Vqry4oBHOsc&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>I&#8217;m going to work around this problem by assigning a <strong>single</strong> direction to these weird combinations.  But first we need to look at our input.  In my joypad reading routine above, I store the button states in a variable called <strong>joypad1</strong>.  The left 4 bits of joypad1 will hold the d-pad states and the right 4 bits will hold the states of the other four buttons.  First I want to isolate the d-pad bits:</p>
<pre>
<code>    lda joypad1
    and #$F0
    beq @end
    lsr
    lsr
    lsr
    lsr
    <span class="codecomment">;now the d-pad button states are in the right nibble.</span>
    <span class="codecomment">;do change direction stuff here.</span>
@end:
    rts
</code>
</pre>
<p>The right nibble of the A register now holds the d-pad button states.  Next I will build a look-up table, assigning a direction to each possible combination of d-pad presses.  Let&#8217;s look at the combinations.  There are sixteen possibilities in all:</p>
<pre>
<code>0000xxxx
    ||||
    |||+--up
    ||+---down
    |+----left
    +-----right

0000 = no arrows pressed (will never happen)
0001 = up
0010 = down
0011 = <strong>up+down</strong>
0100 = left
0101 = left+up
0110 = left+down
0111 = <strong>left+up+down</strong>
1000 = right
1001 = right+up
1010 = right+down
1011 = <strong>right+up+down</strong>
1100 = <strong>right+left</strong>
1101 = <strong>right+left+up</strong>
1110 = <strong>right+left+down</strong>
1111 = <strong>right+left+down+up</strong>
</code>
</pre>
<p>The highlighted ones are the <strong>problem combinations</strong>.  I will need to assign a single, specific direction to them.  For example in the case of <strong>right+left</strong>, I will just choose one: <strong>left</strong>.  Let&#8217;s turn this into a lookup table.  Recall that I have a set of constants assigning a value to each direction (for indexing into CHR lookup tables among other things):</p>
<pre>
<code><span class="codecomment">;direction indexes for tables</span>
SPRITE_UP =         $00
SPRITE_DOWN =       $01
SPRITE_LEFT =       $02
SPRITE_RIGHT =      $03
SPRITE_UP_LEFT =    $04
SPRITE_UP_RIGHT =   $05
SPRITE_DOWN_LEFT =  $06
SPRITE_DOWN_RIGHT = $07
</code>
</pre>
<p>Now I will make a lookup table assigning these constant values to the various button combinations:</p>
<pre>
<code>direction_change_table:
    .byte $FF               ;0000 dummy value
    .byte SPRITE_UP         ;0001, up
    .byte SPRITE_DOWN       ;0010, down
    .byte <strong>SPRITE_UP</strong>         ;0011, <strong>up+down</strong>
    .byte SPRITE_LEFT       ;0100, left
    .byte SPRITE_UP_LEFT    ;0101, left+up
    .byte SPRITE_DOWN_LEFT  ;0110, left+down
    .byte <strong>SPRITE_UP_LEFT</strong>    ;0111, <strong>left+up+down</strong>
    .byte SPRITE_RIGHT      ;1000, right
    .byte SPRITE_UP_RIGHT   ;1001, right+up
    .byte SPRITE_DOWN_RIGHT ;1010, right+down
    .byte <strong>SPRITE_DOWN_RIGHT</strong> ;1011, <strong>right+up+down</strong>
    .byte <strong>SPRITE_LEFT</strong>       ;1100, <strong>left+right</strong>
    .byte <strong>SPRITE_UP_RIGHT</strong>   ;1101, <strong>left+right+up</strong>
    .byte <strong>SPRITE_DOWN_RIGHT</strong> ;1110, <strong>left+right+down</strong>
    .byte <strong>SPRITE_DOWN_LEFT</strong>  ;1111, <strong>left+right+up+down
</strong></code>
</pre>
<p>Now all button combinations have a <strong>single</strong> direction associated with each of them.  All that I have left to do is read from this table and set the hero&#8217;s direction, like so:</p>
<pre>
<code>handle_joypad:
    lda joypad1
    and #$F0
    beq @end
    lsr
    lsr
    lsr
    lsr
    tay <span class="codecomment">;right nibble has the d-pad button states. use as index</span>
    lda direction_change_table, y
    sta hero_direction
@end:
    rts
</code>
</pre>
<h2>Changing the sprite</h2>
<p>The rest of the work is already done for me.  I already have a routine <strong>update_hero_sprite</strong> (see <a href="http://www.tummaigames.com/blog/2009/07/23/explorer-7-sprite-time/">last post</a>) that takes <strong>hero_direction</strong> and uses it to index into some CHR lookup tables.  I don&#8217;t have to change anything to get the new sprites to display.  It works! (you can see the input in the bottom left of each picture):</p>
<div id="attachment_151" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_sprite_up.jpg" alt="If I press up, the sprite faces up." title="explorer_sprite_up" width="400" height="350" class="size-full wp-image-151" /><p class="wp-caption-text">If I press up, the sprite faces up.</p></div>
<div id="attachment_152" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_sprite_down_right.jpg" alt="Pressing down+right makes her face southeast" title="explorer_sprite_down_right" width="400" height="350" class="size-full wp-image-152" /><p class="wp-caption-text">Pressing down+right makes her face southeast</p></div>
<div id="attachment_153" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_sprite_left_right.jpg" alt="Left+Right gets mapped to left." title="explorer_sprite_left_right" width="400" height="350" class="size-full wp-image-153" /><p class="wp-caption-text">Left+Right gets mapped to left.</p></div>
<div id="attachment_154" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.tummaigames.com/blog/wp-content/uploads/2009/07/explorer_sprite_up_down_left_right.jpg" alt="up+down+left+right" title="explorer_sprite_up_down_left_right" width="400" height="350" class="size-full wp-image-154" /><p class="wp-caption-text">up+down+left+right</p></div>
<h2>Conclusion</h2>
<p>My hero sprite is now capable of looking in 8 directions.  I use a lookup table to assign a specific direction to each possible d-pad button combination.  The next step is to actually move her in the direction she is facing.  And when she is moving, I will want to animate her.  Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tummaigames.com/blog/2009/07/24/explorer-8-sprite-direction/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
