harraps [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=10142 More shorthands <p>Hi,<br /> I've made two games in pico-8 until now and because pico-8 is meant to be both a fantasy game system and a development environnement, I wanted to make sure that my code could be read inside the pico-8 editor.<br /> The problem I came across is that lua is highly verbose. That means that writing a simple if else statement takes almost all of the screen width. Since pico-8 already include some shorthands such as += or -=, I though we could have some more to reduce the number of characters used in code. This could be useful for future twitter jam eventually.</p> <p>so I've read a bit of the lua doc to know which keywords and which tokens are already used:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> --used keywords and break do else elseif end false for function goto if in local nil not or repeat return then true until while --used tokens + - * / % ^ # &amp; ~ | &lt;&lt; &gt;&gt; // == ~= &lt;= &gt;= &lt; &gt; = ( ) { } [ ] :: ; : , . .. ... --token added by pico-8 += -= *= /= %= != ? </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Since they are already used, we cannot use them for an other purpose or else the lua interpreter might be confused.<br /> So then I've made a list of tokens that do not exists neither in pico-8 or regular lua:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> --not used tokens ! $ \ @ +&gt; -&gt; *&gt; /&gt; %&gt; ^&gt; #&gt; &amp;&gt; ~&gt; |&gt; =&gt; &lt;&gt; ~~ &amp;&amp; || :&gt; .&gt; </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>And from that list I've picked token that could replace current keywords:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> --example of shorthands ! : not @ : local \ : end $ : self =&gt; : function -&gt; : then &lt;&gt; : elseif *&gt; : return |&gt; : break 0? : false 1? : true </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>So here an example of code to see the result:<br /> (the lines represent the width of the pico-8 screen)</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> --current pico-8 code -------------------------------- function xor(a,b) if a and not b then return true elseif b and not a then return true else return false end end -------------------------------- function l_v_add(l1,l2) for i=0,#l1 do if l1[i] == nil then break end l1[i]:add(l2[i]) end return l1,l2 end -------------------------------- function vec:add(v) if v != nil then self.x += v.x self.y += v.y end return self end -------------------------------- --code with additional shorthands -------------------------------- =&gt; xor(a,b) if a and !b -&gt; *&gt; 1? &lt;&gt; b and !a -&gt; *&gt; 1? else *&gt; 0? \ \ -------------------------------- =&gt; l_v_add(l1,l2) for i=0,#l1 do if l1[i] == nil -&gt; |&gt; \ l1[i]:add(l2[i]) \ *&gt; l1,l2 \ -------------------------------- =&gt; vec:add(v) if v != nil -&gt; $.x += v.x $.y += v.y \ *&gt; $ \ -------------------------------- </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>One thing to note is that the second code is more cryptic.<br /> So I've made some screenshot to see how it would look like in the editor<br /> and here it seems easier to read actually.</p> <img style="margin-bottom:16px" border=0 src="https://www.lexaloffle.com/bbs/files/10142/xor_function.png" width=570 height=533 alt="" /> <img style="margin-bottom:16px" border=0 src="https://www.lexaloffle.com/bbs/files/10142/l_v_add_function.png" width=566 height=533 alt="" /> <img style="margin-bottom:16px" border=0 src="https://www.lexaloffle.com/bbs/files/10142/vec_add_function.png" width=561 height=533 alt="" /> <p>To avoid too cryptic code, maybe it would be better to define shorthand keywords instead.<br /> (instead of &quot;then&quot; or -&gt; why not &quot;do&quot;, instead of &quot;elseif&quot; or &lt;&gt; why not &quot;ef&quot;)<br /> Also we could have alternative way to write &quot;and&quot; and &quot;or&quot; like in other programming language: &amp;&amp; and || </p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> --alternative code -------------------------------- =&gt; xor(a,b) if a &amp;&amp; !b do *&gt; 1? ef b &amp;&amp; !a do *&gt; 1? else *&gt; 0? \ \ -------------------------------- =&gt; l_v_add(l1,l2) for i=0,#l1 do if l1[i] == nil do |&gt; \ l1[i]:add(l2[i]) \ *&gt; l1,l2 \ -------------------------------- =&gt; vec:add(v) if v != nil do $.x += v.x $.y += v.y \ *&gt; $ \ -------------------------------- </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>So what do you think about that ?<br /> Is it feasible ?<br /> Is the code too cryptic to be useful ?<br /> Is this whole thread pointless ?</p> https://www.lexaloffle.com/bbs/?tid=3975 https://www.lexaloffle.com/bbs/?tid=3975 Sun, 31 Jul 2016 10:34:15 UTC GO game <p> <table><tr><td> <a href="/bbs/?pid=25318#p"> <img src="/bbs/thumbs/pico26216.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25318#p"> Pico-GO 1.0.1</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25318#p"> [Click to Play]</a> </td></tr></table> </p> <p>And here is my second game.<br /> After making the chess game, I though it would be fun to make a GO game.<br /> The game is playable and implement a counting<br /> system to display the score at the end of the game.<br /> Now there is a message asking the player if he's sure he want to pass his turn.<br /> And we can switch the final screen between score and territories.</p> <p>RULES:</p> <p>While playing,<br /> Press button O to place a stone (goishi),<br /> Press button X to pass your turn,<br /> The game ends when both players have passed their turn twice in a row</p> <p>When the game ends,<br /> Press button O to declare a group of stones as dead,<br /> Press button X to display the score<br /> Once the score are displayed, the game is definitely over</p> <p>At the very end, you can switch between<br /> territory and scoreboard with button O or button X</p> <p><div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;"><br /> version 1.0.0<br /> <table><tr><td> <a href="/bbs/?pid=25318#p"> <img src="/bbs/thumbs/pico26141.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25318#p"> GO game 1.0.0</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25318#p"> [Click to Play]</a> </td></tr></table> </p> <p>Older version 0.0.1 (no counting system)<br /> <table><tr><td> <a href="/bbs/?pid=25318#p"> <img src="/bbs/thumbs/pico25317.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25318#p"> go game 0.0.1</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25318#p"> [Click to Play]</a> </td></tr></table> <br /> </div></div></div></p> https://www.lexaloffle.com/bbs/?tid=3827 https://www.lexaloffle.com/bbs/?tid=3827 Thu, 14 Jul 2016 19:39:44 UTC Chess <p> <table><tr><td> <a href="/bbs/?pid=25316#p"> <img src="/bbs/thumbs/pico26019.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25316#p"> Chess 1.0.0</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25316#p"> [Click to Play]</a> </td></tr></table> </p> <p>Here is my first pico-8 game<br /> Since pico-8 has a 128<em>128 screen and 8</em>8 sprites,<br /> I though it would be fun to make a chess game in it.<br /> <strong>The game is now fully playable</strong><br /> <strong>And I've recoded the whole thing so it runs faster</strong><br /> The game needs to run all possible moves of the next turn to define<br /> if a move is valid or not and if it should be presented to the player.</p> <p>It implements pawns double step, pawn promotion, castling and en passant.<br /> Have fun. </p> <p>Older versions:<br /> <div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;"><br /> <table><tr><td> <a href="/bbs/?pid=25316#p"> <img src="/bbs/thumbs/pico25929.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25316#p"> Chess 0.0.2</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25316#p"> [Click to Play]</a> </td></tr></table> <br /> <table><tr><td> <a href="/bbs/?pid=25316#p"> <img src="/bbs/thumbs/pico25315.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=25316#p"> Chess 0.0.1</a><br><br> by <a href="/bbs/?uid=10142"> harraps</a> <br><br><br> <a href="/bbs/?pid=25316#p"> [Click to Play]</a> </td></tr></table> <br /> </div></div></div></p> https://www.lexaloffle.com/bbs/?tid=3826 https://www.lexaloffle.com/bbs/?tid=3826 Thu, 14 Jul 2016 19:32:59 UTC Class-like Objects in pico-8 <p>With the introduction of the function type()<br /> it is now possible to know the type of the variable.</p> <p>So I tried to find a way to implement a simple class system and I think I found a way</p> <p>First we need a function to copy tables</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> function copy(o) local c if type(o) == 'table' then c = {} for k, v in pairs(o) do c[k] = copy(v) end else c = o end return c end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>then we can declare our objects the following way:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> vec = {} vec.x = 0 vec.y = 0 function vec:add(v) self.x += v.x self.y += v.y end function vec:subs(v) self.x -= v.x self.y -= v.y end function vec:mult(c) self.x *= c self.y *= c end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>to create multiple instances of vector, we just need to copy the object and edit it's attributes</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> a = copy(vec) b = copy(vec) a.x = 1 a.y = 2 b.x = 3 b.y = -1 </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>and when we call the a:add(b) we get a.x == 4, a.y == 1</p> <p>what's more we can define &quot;subclass&quot;</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> vec3 = copy(vec) vec3.z = 0 function vec3:printz() print(self.z) end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>the function printz() will run just fine on copies of vec3 but will return an error on copies of vec</p> https://www.lexaloffle.com/bbs/?tid=2951 https://www.lexaloffle.com/bbs/?tid=2951 Mon, 04 Jan 2016 11:56:49 UTC