Thursday, June 30, 2016

Glitches and Tweetcarts

Today I stumbled upon a piece of code on twitter that motivated me to start implementing the glitchy aspects of the game. The code came from Trasevol Dog's tweetcart. Carts like those are part of a somewhat unofficial Pico-8 jam to make a program in 140 characters or less. There is already a great amount of effects showing what you can do in this limited amount of code. You can look them up either by checking #tweetcart on twitter, or on Pico-8 BBS. Those carts are a great resource for both inspiration and learning. Sure, the tweetcart code is not very human readable, as most of the variables use single letter naming, but figuring out what 140 characters of code does is much easier than figuring parts of a whole finished game...
This is how I stumbled upon this effect. After seeing it on twitter, I added it inside my game and to my surprise, it produced a pretty cool distortion noise effect. Now to break it down and tweak it.


The original code was taking every other pixel on the screen and made another pixel a random amount away the same color. The problem was that the code made the game slow down pretty bad. Originally I was going to use it just as background for game over screen, but then I started tweaking that script. I found that if I will check and move less pixels around, the game will run pretty smoothly. With some trial and error I managed to to that! Then I just added a variable that will change how much noise is being rendered and came up with the code below:

function glitch_noise()
 if glitch_noise_factor > 0 then
  local f = glitch_noise_factor
  for tx=0,16 * f  do
   for ty=0,16 * f do
   tk=rnd(3)-1 
   ta=tx*(8/f)+tk 
   tb=ty*(8/f)+tk
   tc=pget(ta,tb) 
   tc+=flr(rnd(9)/8) 
   pset(ta+rnd(3)-1,tb+rnd(3)-1,tc)
   end
  end
 end  
end

Now I can just change the glitch_noise_factor and render the noise. The bigger it will get, the more noise will get rendered. Even the slowdown can now be used as a part of gameplay...after all slowdown is pretty common in machines that are bugged out and glitchy. Another bug that became a feature... as Rob Ross would say: "happy little accident."

Now that I finished the first glitch effect, the floodgates have been open and I am motivated to implement more glitches... after all, they were the one of the main ideas to behind this game.


2 comments:

  1. Is your game available on Pico-8 website so I can download your cart and see all of the code?

    ReplyDelete
  2. Not yet... I was thinking of only releasing it after it is complete. Right now there is a lot of testing happening, so it is not very stable.

    But, if you think that people would benefit from seeing all my messy code, I can upload it :)

    ReplyDelete