Log In  
Follow
harraps
:: Unfold ::

Hi,
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.
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.

so I've read a bit of the lua doc to know which keywords and which tokens are already used:

--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
+     -     *     /     %     ^     #
&     ~     |     <<    >>    //
==    ~=    <=    >=    <     >     =
(     )     {     }     [     ]     ::
;     :     ,     .     ..    ...

--token added by pico-8
+=    -=    *=    /=    %=    !=     ?

Since they are already used, we cannot use them for an other purpose or else the lua interpreter might be confused.
So then I've made a list of tokens that do not exists neither in pico-8 or regular lua:

--not used tokens
!     $     \     @
+>    ->    *>    />    %>    ^>    #>
&>    ~>    |>    =>    <>    ~~
&&    ||    :>    .>

And from that list I've picked token that could replace current keywords:

--example of shorthands
!  : not
@  : local
\  : end
$  : self
=> : function
-> : then
<> : elseif
*> : return
|> : break
0? : false
1? : true

So here an example of code to see the result:
(the lines represent the width of the pico-8 screen)

--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
--------------------------------
=> xor(a,b)
  if a and !b -> *> 1?
  <> b and !a -> *> 1?
  else           *> 0?
  \
\
--------------------------------
=> l_v_add(l1,l2)
  for i=0,#l1 do
   if l1[i] == nil ->
    |>
   \
   l1[i]:add(l2[i])
  \
  *> l1,l2
\
--------------------------------
=> vec:add(v)
  if v != nil ->
   $.x += v.x
   $.y += v.y
  \
  *> $
\
--------------------------------

One thing to note is that the second code is more cryptic.
So I've made some screenshot to see how it would look like in the editor
and here it seems easier to read actually.

To avoid too cryptic code, maybe it would be better to define shorthand keywords instead.
(instead of "then" or -> why not "do", instead of "elseif" or <> why not "ef")
Also we could have alternative way to write "and" and "or" like in other programming language: && and ||

--alternative code
--------------------------------
=> xor(a,b)
 if a && !b do *> 1?
 ef b && !a do *> 1?
 else          *> 0?
 \
\
--------------------------------
=> l_v_add(l1,l2)
 for i=0,#l1 do
  if l1[i] == nil do
   |>
  \
  l1[i]:add(l2[i])
 \
 *> l1,l2
\
--------------------------------
=> vec:add(v)
 if v != nil do
  $.x += v.x
  $.y += v.y
 \
 *> $
\
--------------------------------

So what do you think about that ?
Is it feasible ?
Is the code too cryptic to be useful ?
Is this whole thread pointless ?

P#26214 2016-07-31 10:34 ( Edited 2016-08-01 21:42)

:: Unfold ::

Cart #26216 | 2016-07-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
5

And here is my second game.
After making the chess game, I though it would be fun to make a GO game.
The game is playable and implement a counting
system to display the score at the end of the game.
Now there is a message asking the player if he's sure he want to pass his turn.
And we can switch the final screen between score and territories.

RULES:

While playing,
Press button O to place a stone (goishi),
Press button X to pass your turn,
The game ends when both players have passed their turn twice in a row

When the game ends,
Press button O to declare a group of stones as dead,
Press button X to display the score
Once the score are displayed, the game is definitely over

At the very end, you can switch between
territory and scoreboard with button O or button X


version 1.0.0
Cart #26141 | 2016-07-30 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
5

Older version 0.0.1 (no counting system)

Cart #25317 | 2016-07-14 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
5

P#25318 2016-07-14 19:39 ( Edited 2016-08-12 17:57)

:: Unfold ::

Cart #26019 | 2016-07-28 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
4

Here is my first pico-8 game
Since pico-8 has a 128128 screen and 88 sprites,
I though it would be fun to make a chess game in it.
The game is now fully playable
And I've recoded the whole thing so it runs faster
The game needs to run all possible moves of the next turn to define
if a move is valid or not and if it should be presented to the player.

It implements pawns double step, pawn promotion, castling and en passant.
Have fun.

Older versions:


Cart #25929 | 2016-07-26 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
4

Cart #25315 | 2016-07-14 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
4

P#25316 2016-07-14 19:32 ( Edited 2016-07-28 14:59)

:: Unfold ::

With the introduction of the function type()
it is now possible to know the type of the variable.

So I tried to find a way to implement a simple class system and I think I found a way

First we need a function to copy tables

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

then we can declare our objects the following way:

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

to create multiple instances of vector, we just need to copy the object and edit it's attributes

a = copy(vec)
b = copy(vec)
a.x = 1
a.y = 2
b.x = 3
b.y = -1

and when we call the a:add(b) we get a.x == 4, a.y == 1

what's more we can define "subclass"

vec3 = copy(vec)
vec3.z = 0
function vec3:printz()
  print(self.z)
end

the function printz() will run just fine on copies of vec3 but will return an error on copies of vec

P#18145 2016-01-04 11:56 ( Edited 2016-02-07 18:13)

Follow Lexaloffle:          
Generated 2023-01-30 20:40:44 | 0.070s | Q:21