Sunday, September 29, 2013

A snake is born

Today I went into the algorithm part of the snake game. Exactly how do we manage the snake segments as they grow in number, and change direction?

A buddy of mine at work mentioned that a linked list made most sense. I didn't probe him on it, but that did intuitively make sense because the snake does in fact look like a bunch of linked nodes. This appears to work pretty well:
  1. Each node carries its own position in the gameboard.
  2. When advancing the snake, work backwards from the last node and make its position the next node. This is what causes the snake to appear to follow its head.
  3. Update the first node to the new position.
Do that, then voila! you get this:


Super cool!

Progress - got a ball to follow gestures on the screen

This is super rudimentary, but somewhat encouraging. The little green ball which will very soon hopefully turn into a snake is now actually following swipe gestures on the iphone:



Primitive but exciting!

Friday, September 27, 2013

IOS Dependency Injection vs Singletons

Having lived with dependency injection for most of my professional Java programming career, it seemed natural for me to seek out similar solutions in IOS. This one looks quite promising.

https://github.com/jasperblues/Typhoon

It seems like a good idea to have singletons injected into the right places so various components of the game can coordinate, such as the game controller and the UI controller.

Alternatively, the singleton pattern might be more straightfoward and appropriate for my tiny snake with lazers game:

http://www.galloway.me.uk/tutorials/singleton-classes/

I will do some tire kicking and update on how that turns out.

Gesture Control - Swiping!

This is pretty cool. Lots of pre-built goodness in IOS6 that makes swipe gesture recognition a snap. This is essential for the snake game control system: driving the snake will be done by swiping.

Here's all you have to do to gather swipe input from your iphone screen:

UISwipeGestureRecognizer * leftSwipe = [[ UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[view addGestureRecognizer:leftSwipe];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

Set up several of these in your view controller initialization method, then implement the handler:

- (void) didSwipe: (UISwipeGestureRecognizer*) sender {
    UISwipeGestureRecognizerDirection direction = sender.direction;  
    switch( direction ) {
        case UISwipeGestureRecognizerDirectionLeft:            NSLog(@"left");
            break;
        case UISwipeGestureRecognizerDirectionRight:            NSLog(@"right");
            break;
    }
}

This is excerpted from this excellent youtube tutorial:

http://www.youtube.com/watch?v=5Ou8Oqhngm8

A super userful blog on getting started with GLKit

Apparently, the way to go if you're wanting to learn how to start with OpenGL without going direct to the nitty gritty is to use GLKit. The best, most easy to understand resource I've found is Ray Wenderlich's awesome blog:

http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1

In it are all kinds of awesome goodness such as how to setup your first IOS project ready to go with GLKit 2.0 and OpenGLES, plus some really good tips on how to blit sprites properly onto your iphone display.

Amending the Snake Game with Lazers design

After talking with some folks, it seems like the game design could be tweaked a little bit:

  • The lazers can be used to slice your own body. The pieces of that segment will die!
  • Doing so however will set you back in the purpose of the game, which is to win by filling up the entire board with your snake body (not sure what % of the gameboard it should be... maybe 70% of all the available area?
  • The lazer can only be used after you get at least 3 consecutive apples or something like that.

Learning Objective C

The first thing that I had to do was learn Objective C. Coming from a deep background in Java, I find the language at once familiar and at the same time deeply disconcerting. Its got automatic memory management, sort-of, through ARC.

Perhaps the absolute most wierd thing is the method invocation syntax, which for some reason involves brackets:

[myInstance sayHi: @"hi!"]

... is the equivalent of myInstance.sayHi("hi").

Its very very weird to use brackets for something other than referencing some part of an array. The other bit of wierdness is the usage of - and + symbols to indicate class (static) methods, and instance methods:

+ (int) myNumber {...}

... is the equivalent of  public static void myNumber().

Fortunately the similarities in loops and if..then.. else with Java and C have so far made the transition pretty smooth. I am eager to explore the object oriented aspects of the language, which might be the subject of my next post.

Thursday, September 26, 2013

Wheee! This is my inaugural post

So I am embarking on a super fun adventure: learn Objective-C and IOS6 gamdevelopment ... all at once. I have decided that my very first project shouldn't be too ambitious, but should result in a halfway decent, playable game.

Why am I doing this? Because ultimately I want to create a game I'd actually wish I'd played as a kid!

Here's is the design of my very first project:

A Snake Clone, but with lazers

I loved this freaking game when I was a kid, on a computer that had 640 KB and a CGA display. Now I am going to create a poor imitation of the same thing, but with these features:


  1. A soundtrack. I play in a bluegrass band (http://www.backporchrevival.com), so I might get the guys to create some original tunes.
  2. Your snake
  3. Things for the snake to eat (apples?)
  4. Things that will come out of nowhere to try to hit you
  5. If you eat enough apples you can use your lazer to break those things
... and that's it. I'll document my learning journey in this blog for posterity and future amusement.