I am a human on a quest to discover self. Developer by trade, Designer and a Photographer by passion. I am also an amateur Traveller.
October 16th, 2012 | In JavaScript, Projects | 1 Comment »
Everyone has played classic snake, be it on mobile, online, gameboy, whatever. If you have not, drop everything, I mean everything, including your heartbeat if possible and get on and play it.
Invertes snakes is a Javascript/Canvas implementation of Classic Snake with a twist – There are 2 snakes Red and Blue – You only control one snake, the 2nd one just mirrors your movement. The food for snakes alternate between Red & Blue, each snake only eats its colored food.
Rules are simple
Note: Some of Chrome’s plugins may cause <space> to not star the game. Try playing the game in another browser or disable your plugins.
Tags : game, JavaScript, snake
February 14th, 2012 | In Games, JavaScript, Programming | No Comments »
I love tetris. I have never written tetris before and I have always wanted to. Attempted it once but couldn’t figure out heads and tails of it and then left at it. I wanted to get over it.
So i wrote it, in less than 4 hours (bragging rights) in JavaScript using canvas. Feel free to browse the source or Play it here
http://kalyanchakravarthy.net/projects/fun/jstris/
During the process I learnt how to use the javascript “with()”. Despite all the cliches around it, I feel its a pretty awesome and useful language feature. It logically made sense to use it to group statements together, especially the drawing functions.
with( stage.ctx ) {
fillStyle = "#fff";
fillRect( theX+1, theY+1, stage.cellSize-2, stage.cellSize-2 );
fill();
}
Ain’t that neater than this?
stage.ctx.fillStyle = "#fff";
stage.ctx.fillRect( theX+1, theY+1, stage.cellSize-2, stage.cellSize-2 );
stage.ctx.fill();
Imagine doing it for 20-30 statements.
Tags : game, games, JavaScript, tetris
February 14th, 2012 | In JavaScript, Programming | No Comments »
We all know the usual way to create anonymous functions is to write something like this
(function() {
alert('hello');
})();
I recently learnt this works too (#1)
!function() {
alert('hello');
}();
But interestingly enough that same code, without the !, throws a syntax error. (#2)
function() {
alert('hello');
}();
This had me stoked for a while until I realized why and it was obvious all along.
In the first case the “!” makes the function be treated as an anonymous function object and then negating it to result false after it has executed. But in the #2, the absence of an expression and starting of the statement with ‘function’ keyword makes the interpreter look for a named function, which it wouldn’t qualify for due to the absence of a name, thereby resulting in syntax error.
The same piece of code in the context of an expression, works just fine
> x = function() { return 10; }();
> x
10
So, will naming work? Yes.
//Naming works too
> x = function xyz() { return 15; }();
> x
15
Can we call it outside an expression without grouping? No. That would throw an error, as after consuming a fully formed function, the interpreter tries to consider the empty anonymous parenthesis as a different statement.
> function xyz() { return 15; }(); //error
> (); //error
Make the empty parenthesis a valid statement, and it works
> function xyz() { return 15; }(1);
How do we know its an independent statement?
> function xyz() { return 15; }(console.log(20));
> 20
But then how do we call it ? By Grouping.
> (function xyz() { return 15; })(console.log(20));
> 20 //console log
<- 15 //return
Why does it work? The function instead of being a simple statement, now is an object in an expression, which means can be evaluated and can take arguments.
But arguments need not be an expression and can be empty. That brings us back to the standard statement.
> (function xyz() { return 15; })();
<- 15 //return
Interesting language JavaScript is.
Tags : fun, JavaScript, language play, quirks
November 6th, 2011 | In Processing, Programming | No Comments »
Most games or applications which make use of arrow keys, sometimes require the use of multiple combinations like UP+RIGHT, LEFT+DOWN, etc also. I was trying to do the same for a 3d game I was trying to write in Processing. It might seem like a simple problem, which it is, surprisingly i couldn’t find anything on it. So after playing around with a little bit i figured it out.
Each key when pressed and released individually triggers a separate event, including for combinations of keys. Hence the way to test if both Up+Right keys are pressed, is to store the pressed status of individual keys, and clear their status individually when each key is released.
Here is the processing code. The logic should be applicable for any language and platform.
class KeyStateReader {
//binary sequence for easy state storage
static final int K_UP = 1;
static final int K_RIGHT = 2;
static final int K_DOWN = 4;
static final int K_LEFT = 8;
int keyState;
//combine the key stats into single variable by logical |
public void onKeyPress() {
int kType = getK();
keyState |= kType;
}
//on release clear individual bits
public void onKeyRelease() {
int kType = getK();
keyState = keyState ^ kType;
}
// pass a key combo using logical or '|' ( UP | RIGHT ) to see if it exists.
public boolean isKey(int k) {
return (k & keyState) != 0 ? true : false;
}
// do we have any at all ?
public boolean hasAnyKey() {
return keyState > 0;
}
public int getK() {
switch(keyCode) {
case UP: return K_UP;
case DOWN: return K_DOWN;
case LEFT: return K_LEFT;
case RIGHT: return K_RIGHT;
}
return -1;
}
}
KeyStateReader keyState = new KeyStateReader();
void keyPressed() {
keyState.onKeyPress();
}
void keyReleased() {
keyState.onKeyRelease();
}
To check for key combos you can now do this
void checkKeyCombo() {
// for UP+RIGHT
if( keyState.isKey( KeyStateReader.K_UP | KeyStateReader.K_RIGHT ) ) {
do_something_up_right();
}
// You can even check them individually
if( keyState.isKey( KeyStateReader.K_UP ) ) {
do_something_up();
}
if( keyState.isKey( KeyStateReader.K_RIGHT ) ) {
do_something_right();
}
}
There are definitely other ways to do it too, like maintain an array with all the keyCodes and check if the key being tested is available in the array. That way you will be able to track multiple keys and you wouldn’t have to re-define key codes for each of them.
Tags : code, games, processing, snippet
October 25th, 2011 | In Photography | 1 Comment »
Am a big fan of Google Reader and have subscribed to hundreds of RSS feeds. I love browsing through Flickr. I am addicted to it, especially to their explore section, where daily they come up with few hundred most interesting photos from flickr for that day.
So I wrote a PHP Script which generates an RSS feed of the 100 most interesting photos on Flickr’s explore, every single day via a cron job which uses Flickr API and a bit of cUrl magic. A 100 photos are published to the RSS Feed, every-single-day. The explore positions vary through out the day, but only those that appear to the script when it runs are considered.
Every single day I go through those photos, I am left inspired and mesmerized. There are so many amazing photographers out there, that simply watching their work is a delight. It just makes my day. Here is the Feed URL for you to subscribe for your photography delight.
Feed URL - http://feeds.feedburner.com/Flickr-Daily-100-Interesting
You can use any Feed Reader you want. If you are not sure I would recommend Google Reader.
Tags : explore, feeds, flickr, reader, rss, script, subscrive
April 9th, 2011 | In Ramblings and Rantings, Thoughts & Thoughts | No Comments »
(Posted for 2nd of April, My Birthday)
Life is like a loop, it goes on, and on, iterating through every passing year. This post, commemorates my 22nd one, which as a customary, I have done in the past too. It helps keep record, of what I wanted to do, what I have done, what I am going to do or atleast that which I thought I should do, leaving behind a trail of how I have changed. So a decade later, when I have the need to slap myself, I can go against my ego and do so, with proof.
Every year It feels I have come to fill the gap, a gap at which I seemed to have sucked at, an year back. I feel good, or almost, because before that happens, an introspective realization hits that its just another improvised gap, like suckiness in recursion, for the coming year. This observation has been quite consistent over the past 5 years. Or so I remember.
Each year that goes past, I consider myself to have become an year younger, a tradition which helps keep the spirit going, for I would have left some baggage behind, to move on, to look forward for things ahead in life. This time its different, I am 1 year older, truly, for the things I have been through, what I have picked up, can never be left behind, can never be forgotten, never be healed. And yet I seem to be looking forward and ahead in life, for the times that are yet to come. The only true way of healing grief is by accepting it and letting it become an integral part of what and who we are. That probably makes me an entirely different person, different to me atleast, different from what I was an year back. I think I have started to accept and embrace so drastic a change, for its the only way to survive life and live it. Its a significant milestone in life, one which I dearly wish I didn’t have to arrive at.
Photography has stuck on with me, for over 2 years, to become a serious hobby, my best friend, helping me, when in need, to come out, cope up, move on & cheer up. It has helped me make memories few of which I would forever cherish, helped make new friends, talk to and interact with other people, with whom I might not have otherwise. Have also picked up something which I thought I lost 10 years back, only to get much better at it – Sketching. That makes it 3 things, check & done.
One of the things worthy of mentioning, that which has gone to change my way of thinking, my opinions, given a chance to make few new ones, to make some very strong & to break a quite a few too, is a book, “Fountainhead by Ayn Rand”. Those who read the book, need no more explanation, those who haven’t, should read without asking for an explanation. It is the one book which made me truly believe that books have power to influence, change and redirect a person by 180 degrees, a change worth skipping all your meals for, in case you thought books aren’t worthy enough. Reading wise, read more than 20 books, which considering that I never took to reading before, is another check.
India won the Cricket World Cup 2011, co-incidentally on the B-day, thats one more thing, no two things, double check.
2 years back I earned myself a DSLR for my B’day, out of the freelancing gigs I did, way back at college. It changed my life. Last year I earned myself my very first job as a backend/web developer. It changed my life. This time, as a life experiment, despite knowing that I love the web, I put myself (or life made me to be exact) in a position which I have no idea how I would possibly handle, and the only way of knowing how I would, is when I am in it and have no choice – a shiny new job, as a C++ dev.
This has given me a new sense of feeling towards life, me the ego, myself as a person and the world around in general, which I shall want to keep to myself. With it, I seem to have gotten a better idea of what I would want to do and importantly what I don’t want to do, which if I have to, I atleast know how I should want to.
The usual ritual is to make huge list of resolutions, as the self-observed pattern says that 60% of them get completed in most cases. This time there are only few, most of which are the ones that overflowed from last year’s list, and few other newer personal ones, which to summarize are – Run, Draw, Shoot, Travel, Read, Meet, Speak, Express.
(Posted for 2nd of April, My Birthday)
January 25th, 2011 | In Thoughts & Thoughts | No Comments »
Knowledge weighs more than anything
And what matters is we strive to push our bounds for more of it…
the first sketch was originally drawn for this post