Eric's Blog

A Game About Pushing Blocks

I've been on a big Sokoban kick these past few days. Sokoban, if you didn't know, is an old puzzle game all about pushing boxes into designated spaces. It was well-received upon its release for its many well-designed puzzles and spawned many clones.

I thought it'd be fun to make my own clone of Sokoban, so I did. I call it "Blox".

A screenshot of the third level of Blox.
A screenshot of the third level of Blox.
Blox is a very simple block-pushing game inspired by Sokoban. In Blox, you play as a smiley face whose job is to push blocks into the designated areas. It features simple graphics and sounds, and a handful of levels. I made it in two days just for fun.

You can click here to play Blox or you can click here to view Blox's source code.

What are your thoughts about Blox and its puzzles? Let me know in the comments!

Easter Hack 2017

It was only a few months ago that I created my last game for a game-making event, and already I am back at it again—this time with Keebo's Quest.

Keebo's Quest was created over the course of a single week for Easter Hack 2017, a game jam where the goal was to improve upon an existing game and to give it an Easter twist. I chose to improve upon Mori, the first game I ever created for a game-making event.

A time-lapse video of the development of Keebo's Quest.
Improvements include an experimental doodle-like art style, a movement system that is not locked to individual tiles, and the ability to blow things up with bombs. Several of Mori's original features, like trading and having conversations, remain intact.

You can play Keebo's Quest in your browser right now at this link. While you are at it, feel free to leisurely peruse the development logs here.

Let me know what you think of the game in the comments! Happy Easter!

How to Leverage Server Side Caching in PHP

Back in the early days of the Web, many sites were made up of mostly text and maybe just a few images, making for a speedy browsing experience. The need to speed up sites with caching was almost nonexistent back then, but gone are those days.

These days, many sites use content management systems to dynamically generate pages on the fly, often resulting in increased load times. As a result, there is a greater need for snappy caching solutions now more than ever. I think I have one such solution.

Here is my code to quickly and easily cache Web pages dynamically using PHP:

<?php

class Cache {

  private $path; // The full path to cached files.
  private $expiration; // How long files will persist before expiring.

  public function __construct($directory, $expiration) {

    $this->expiration = $expiration;

    // The full path is the cache directory and the current hashed URI.
    $this->path = __DIR__ . "/$directory/" . md5($_SERVER["REQUEST_URI"]);

    // Scan the cache directory for cached files.
    $cached_files = glob($this->path . "*.cache");

    if ($cached_files != false && !empty($cached_files)) {

      // A cached version of this page exists, so display it.
      $this->load($cached_files[0]);

      // Stop the rest of the page so only the cached file will be shown.
      exit;
    }

    // This page hasn't been cached; store its contents to be saved later.
    ob_start([$this, "save"]);
  }

  private function load($file_name) {

    // Display the cached version of this page.
    require $file_name;

    // Get the time of when the cached file was created.
    $creation_time = str_replace([$this->path, ".cache"], "", $file_name);

    // Check to see if the cached file has expired.
    if (time() > $creation_time + $this->expiration) {

      // The cached file has expired, so delete it.
      unlink($file_name);
    }
  }

  private function save($page_contents) {

    // Save the contents of the page to a cached version.
    file_put_contentss($this->path . time() . ".cache", $page_contents);

    // Display the page like it would be normally.
    return $page_contents;
  }
}

?>
The above script functions by first checking whether or not the current page has previously been cached. If it has, it displays the cached version and then determines if the cache has expired; otherwise, it immediate caches the page for later use.

Now put the following at the top of your Web pages to cache them automatically:

<?php

// Include the Cache class.
require "cache.php";

// Instantiate the Cache class. Set the cache directory
// to "cached" and the expiration time to one hour.
$Cache = new Cache("cached", 3600);

?>

That is all there is to it to achieving convenient, automatic server-side caching. Be sure to set the cache directory's permissions to "writable", or else it may not work. I found that cached pages load an average of 24 times faster than non-cached pages in testing.

Beyond improving load times, server-side caching is great for reducing database queries and memory consumption when using a content management system.

Feel free to use my caching code in your own sites and projects, and let me know what you thought of this post in the comments!

National Puppy Day

Nearly every day of the year has a holiday attached to it. Whether it be lovers embracing one another on Valentine's Day, or mathematicians becoming giddy on Pi Day, each one boasts something unique.

Well, today is National Puppy Day—a day when dog lovers from all over can proudly show off their pups. Being a big fan of dogs myself, I couldn't pass up the opportunity to share my four dogs with you. The picture quality isn't great, but please enjoy.

Leelee wrapped in a blanket.
Leelee wrapped in a blanket.
Koda enjoying the sun and keeping guard outside.
Koda enjoying the sun and keeping guard outside.
Sadie on the trail of something in a pond.
Sadie on the trail of something in a pond.
Buddy lazily soaking up the sun while lying on the grass.
Buddy lazily soaking up the sun while lying on the grass.
Everyone having fun outside.
Everyone having fun outside.
While my dogs are all too old to be called puppies, they'll always be puppies to me.

How are you celebrating National Puppy Day? Do you have dogs? How about cats? Does your pet have a holiday? Tell me all about it in the comments!