Part 2 – Building a simple JavaScript game

The thought of creating a game is pretty easy.  Ideas for what to put into the game are easy too…  but the ideas never seem to land in order.  Some of the topics you end up thinking about would come much later and some of the prerequisite item are pushed out of your mind.  It is still a really fun exercise to think through and talk through the things, but we will need to coral the thoughts to make progress.

Early Goals of the Game

The early goal is to create a simple game that will be set in a fantasy land like Lord of the Rings.  Not a direct copy of this world of course but some of the same concepts.  We will have a hero that will fight randomly spawned enemies in an automated fashion.  Then we can see how far this hero can make it in the game based on the random interactions with this contrived world.  The hero will have to have a chance to gain health and deal extra damage as he increases the body count of his fallen enemies.  Simple enough on the surface.

Expanding on what we have built

We put together a simple world in the previous article Building a simple JavaScript game and now we need to model our hero.   We will give him a name and a few other essential items, while trying to focus on what is necessary and required to make the early engine function.  The name for my example hero will be Foff as this was the toon name of my character in WoW…  my Horde undead warlock.  He has a name, now he will need some health.

// our hero
var hero = {
	name: "Foff",
	health: 100
};

Off to a good start but there are now some more complex items that we will begin to factor into the battles of our future foes.  The criteria that make up how our hero will perform in battle and that will help drive some of the random elements of the game.  Quickly our mind begins to complicate things for us as we thing of all the games that we have experienced in the past and how it was done in those.  The thoughts of armor, weapon types, defense stats, offense stats…   ugh.  What do we need?

Initially, I think he needs strength, defense, blocking capability and the ability to perform a critical strike.  That is enough complexity out of the gate to make our character interesting and allow for some items to tune and scale as we progress our hero through battles.  As we run simulations in the future, the more elements we have to consider in the attack calculations, the more complex things will become.  One point change in one criteria could potentially have a cascading effect later on as the battles get calculated.  We will gladly keep it simple for now as we reason about this game and add complexity over time.

Our hero

// our hero
var hero = {
	name: "Foff",
	health: 100,
        crit: 5,
        strength: 10,
        block: 50
};

It is easy to see how complex this could get as we anticipate the future but we have to exercise a bit of discipline as we move through the minimum requirements.  We will have to decorate this with additional items over time for sure, to make it more interesting and a bit more balanced as it scales.

Now that the hero is created, we need to create a worthy opponent to challenge him to a battle.  We will cover those details next time.

 

Advertisements
Posted in coding, node js | Leave a comment

Building a simple JavaScript game

My friend Chris showed me a simple game that he was working on in PowerShell.  He began writing it to improve his skills in that language.  When Chris showed the game to me and Marco, I honestly did not know what to expect and then he fired up the simple game…

An ASCII art scroll greeted us and introduced us to our hero and set the stage for the battle that was before us.  This simple game had a hero battle an Orc and then move on to the next battle.  We watched as with each tick progressed forward and the back and forth battle raged on.  The Orc strikes first, then the hero, then the Orc…. a block, a miss, a hit for low damage, a hit for high damage and then the hero was victorious over this first Orc.  Then he continued on to the next one as the battle raged on.

This silly, simple game got the wheels turning of me and Marco as Chris opened up the code revealing the internals of the game logic.  The three of us rationalized and reasoned about some simple changes to add more struggle to our hero to make things even more interesting.  We modified the logic to how often the hero could deal a critical strike against his foe.  It was cool to quickly implement the change and then “watch” the hero attack and see a CRITICAL STRIKE get posted on the screen as the random numbers bounced down the code through our new logic.  Nothing but geek fun.

Chris and I ended up working on the game a bit later on in the evening and we began to refactor the code to turn some of the common elements into functions and working through what elements were needed to make the engine easier extend.  I began to talk about migrating some of the logic to JavaScript and I thought about some of the possibilities there.  I have no experience with game code so this was an interesting problem to think through.  I thought about searching Google for JavaScript game engines, theory, examples…  but then, I held short and changed my mind.  I thought it would be better to stumble through some game code on my own and make my own mistakes.  It will be better to try out my own ideas in the space to see how I can build on this simple concept while improving my JavaScript skills along the way.

So, I will start with a clean slate and begin building this simple engine for fun and to learn.  Now I will start with the most basic element of the game… which is time and randomness.

Let’s establish an object to hold our configurable items for the world we are creating.  We will establish the clock tick interval and the range of the dice roll.

var world = {
   clockInterval: 1500,
   rollMin: 0,
   rollMax: 100
};

 

Now let’s utilize these variables in the “clock” that drives the time for the world.

function worldClock() {
   setTimeout(worldClock, world.clockInterval);
}

worldClock();

 

We have a world clock but it is not very interesting because there is no evidence that it is doing anything at all.  Let’s add a tick function that the world clock will call to show us it is working.

function tickEvent() {
   console.log('World Clock Tick...');
}  

And then we will call this function from the worldClock function.

function worldClock() {
   tickEvent();
   setTimeout(worldClock, world.clockInterval);
}

Now we see a tick event occur every second and display the ‘World Clock Tick…’.  Cool, it works.  Now let’s create the dice roll really quick and call it a day.

function roll(){
   return Math.floor(Math.random() * (world.rollMax - world.rollMin + 1) + world.rollMin);
}

Done.  This will take the configuration values for the high and low values provide a random number between the values that are inclusive of the numbers.

 

Now putting it all together here is the simple engine that we have put together so far:

var world = {
   clockInterval: 1500,
   rollMin: 0,
   rollMax: 100
};

function worldClock() {
   tickEvent();
   setTimeout(worldClock, world.clockInterval);
}

function tickEvent() {
   console.log('World Clock Tick...');
   var rollDice = roll();
   console.log('You shake the dice and cast them on the table...  The roll yields the value', rollDice);
}

function roll(){
   return Math.floor(Math.random() * (world.rollMax - world.rollMin + 1) + world.rollMin);
}

worldClock();

 

That is all for now.  Next time we can define our Hero and progress on to the next steps of the game.

Posted in coding, node js, random | Tagged , , | Leave a comment

A Lack of Original Content

I find that there is a lack of original content that is published out via different social and traditional information outlets.  Not that there is a large amount of plagiarism… I am not saying that at all, there are just many similar articles on the same subject.  If you look at your different information sources you will see different trends that permeate your feed of news that are the same content.  Often these things are later considered trends when we look back at them and it is always interesting to see which things catch on to become popular.

Outside of the world of technology, one example is the importance of Vitamin D and the fact that we are all likely deficient…  many news reports come in, then the articles flood in, then the books, then your doctor is screening you for Vitamin D deficiency and on and on.  It is interesting to see what new thing catches on.  Have you eaten avocado toast lately?  That is another item that caught on and you hear about it everywhere.  I am not saying these things are bad ideas at all, it is just interesting how much you hear about certain things from multiple sources that they become trends or are viral.  But when everyone is writing about and reporting about the same things it will often drown out other important things that we should be hearing about or considering.

In technology there is also the same phenomenon where we lack original content for things and have a slew of copycat articles.  The same ideas will be restated and propagated until you keep seeing the “same” information over and over.  It is most often not an outright carbon copy of the same information, it is eloquently restated, decorated with a peppering of additional facts and a fresh new cover picture is hung with care on the top of the digital article.  Then we keep reading the same content in articles and then we will often taylor our searches to support and find out more about this thing we keep hearing about…  because it must be the “new hotness” that we should be doing in our technical jobs.

Time passes and you are now converted over to the new technology and you are now it’s biggest advocate because the buzz and trend have won you over.  If the majority of the articles from the authors you trust are all advocating it…  it must be the answer to ALL of your technical problems.  That is when the lack of original content might become a problem and begins to become noise.  If all the articles point to this new solution…  then all of your problems WILL be solved by this new technology that absolutely “everyone you know” is talking about.  Then a technical following become an almost religious following.  What about the new ideas?

It will take some time but you will begin to see some people speak out about the once very popular technology and the fact it does not solve EVERY problem.  Then the public emotional and religious debates begin in an open war that is not usually based on any fact just human bias.  The people that use last year’s technology are viewed as falling behind and “just don’t get it”.  The tribe of technology current marches on and continue to advocate its use for everything, including things it was not intended for.  Then more cracks start to show in this new technology over time and as more people gain a deeper understanding…  they become a bit disappointed with their shiny new toy that is beginning to dull.  Then the battles still rage with the “old” technology guys and the new as these flaws become points of attack.  The zealous recent converts continue to defend and battle with whomever challenges them…  but they do begin to see the problems with their technology.  What next?

Suddenly the tech world begins to buzz again…  a new competitor in the space to solve ALL the problems that the previous one did not solve.  Then starts the growing wave of the media machine to wash over us and begin to convert the “old” guys or to drown them.  Hurray to the new technology that we can run from the old problems to be saved!  Now, the even “newer hotness” followers can poke fun at the previous users of last months technology because you know…. they just don’t get it.

The point is, search out original content and find your own solutions to things.  There is always some good in social trends and technical trends but they are never the solution to EVERYTHING.  Do not alienate people that solve problems in other ways because they do not utilize the same methods or tools as you.  Every technology is terrible is some way.  Just because trusted peers solve their problems in a certain way, does not mean you have the same problems and need to do it the same way.  Cling to original thought.  Most importantly look at your own issues and cling to your own original thought to solve your own problems.  Block out the noise in the world and find the signal of original content to make your own decisions for what solutions are the best to solve your problems.

 

Posted in random | Leave a comment

Do you have five minutes?

The dreaded question “Do you have five minutes?” seems to be relatively harmless on the surface.  I mean, who doesn’t have five minutes to spare?  Then you stop and say “Sure” to not be rude or seem dismissive of the question.  Then the questioner quickly blurts out the thing that they have been thinking about for a while and they need your quick input.  Then you struggle to catch up with their context and there is the inevitable knowledge transfer to catch you up to where they are in the thought process.  Even better, when the person asking the question has an answer in mind and they ask leading questions or state leading biased facts to guide you to the outcome that they thought you would say.

Now it has been 20 minutes and you have deposited your answer in their mind and they leave content with the outcome.  Now you are back at your computer to pick up where you left off…  “where was I again?”

As you try to replay the last things you remember doing, the 5 minute question conversation seems to creep back in.  You decide you should have worded something a bit different or realize there was a detail that you felt is now strangely “missing“.  You just gave an answer to something and that is likely going to be socialized with your name attached to it to add some sort of validity to the decision or statement made…

You try to shake it off and get back to the original business at hand…  “where was I again?”

It has taken you 15 minutes to get back into your original thought and resume what you were working on.  The cost of the 5 minute question is now up to roughly 35 minutes.  This is why I will often shuffle quickly through the office with my eyes lowered to avoid “quick” questions.  Eventually, I make a mistake and there is eye contact…  “Hey, you got five minutes?” and I am sure the look on my face tells the full story of how uncomfortable I am with what is about to happen.  Don’t get me wrong…  the question is fine and helping people out is great but the context switch is terrible.  It takes everyone a different amount of time to get back into context… for me, it takes a long time because I keep going back to the conversation that I just had.  My brain can be very LIFO (Last In First Out) at times and that can be bad for productivity if I am being measured on the oldest thing in the to do list.

We have to set aside time to have informal conversations with no real agenda other than identified problems…  that is very healthy for problem solving as a group.  That way there is enough time to capture context and stay in it as a group while a discussion is had on the subject.  We have to find a place to capture those items we are working on and a place for people to capture the questions that they have for you to be addressed at an appropriate time.  Also, I like to schedule meeting times and collaboration times back to back on the same day.  That way I can stay in the mindset that my context will change rapidly and having conversations together, you can pull different facts in and cut down on “knowledge transfer” time if others have the same schedule for meeting.

If you see me in the office and I am moving quickly past you…  keep the cost of a context switch in mind, because I will rarely not stop because we all want to feel important, needed and helpful.  Find a time to set aside for groups of people to collaborate and rapidly switch context.  Then, find a place to keep track of tasks and visualize your work to make it easier to get back into context if you are interrupted.

 

Posted in random | 2 Comments

Drive Without Goals is Dangerous

Not everyone is built the same.  Some individuals are wired to do things constantly and try to achieve some goal that is built up in the mind, either consciously or subconsciously.  These individuals seems restless if they are idle and can even get frustrated by idleness.  Then the inner drive continues to weigh on them and they struggle to get it all done.  The question should be “what are you trying to do?” and you should focus on that.

Major effort is put forward working on a multitude of task but “What” is the ultimate goal?  Is it a greater sense of achievement for achievement sake?  Is it financial comfort for your family?  “What” are you driven for?  What is the goal of your drive?  If you have not set a goal, how do you know what to target and how do you know when to lighten up after you have crushed that goal?

You must clearly target what your goals are, what you want to achieve and why.  Without a target you will not clearly complete the indirect things built up in your mind and that will eventually cause mental fatigue and exhaustion.  Many things we work on do not have immediate results and may even be stretched over years of time.  So this “marathon” goal that you have in your life has to be treated in that manner.  You cannot sprint a marathon.  So make a strategic plan that can incrementally get you to your end goal.  Again, chop up your big end goal into small achievable (and measurable) goals that will pull you forward to what you want to achieve.

Also, don’t forget to reward yourself along the way when you achieve your progress goals.  Take some time off.  Go on a vacation.  Get that steak dinner.  Take the day off with the family and hike in the park.  Whatever the reward is, make sure it is worthy of the goal that you just achieved and make sure you can answer these two questions:

  1. What is my next incremental goal?
  2. What is my next reward?

 

Ensure that you do not continue down the path of blindly working without targeting a specific and measurable goal.  If you cannot declare success in your mind, it is a dangerous recipe of distraction and burn out.  Chop up your large goals into smaller ones that can be completed along the way to knocking out the big goal.  Tell others about your goals too and you may be surprised at the focus other can provide you by keeping you on task or you may be surprised by the help that you receive along the way.  Make sure to write down your goals onto a piece of paper or into a tool that you can use to track and measure your success.  If you don’t document these things, how can you be sure you are on the right track?  Finally, lift your head up along the way and reward yourself for the incremental wins that you have had.

Posted in random | Leave a comment

The need for downtime

You can feel it, you are starting to wear down.  Too many grueling weeks in a row at work.  Too many things on the “To do” list in your mind.  Just too much going on.  We shuffle around quickly and feel rushed…  then, you realize it, you are just burned out.  It impacts our mood to take us to a negative place, where we dwell on what is wrong and not how to fix things.  It impacts our speech and we join in with other who are burned out too to wallow in the mire of unhappiness.  We are not patient when we are burned out and our tempers flair over a small collection of irrelevant events that we have grouped together as some catastrophe in our mind.

Stop.

Stop rushing just for a bit and slow down.  Rest.  Turn off Email or Slack and just unwind.  Being kept wound so tight all the time…  something will break.  I think there is a true benefit to unplugging from technology and being close to loved ones.  If you must “Do” something, then go analog and play a board game or something that will free your mind from the constant noise and worry of your various tasks and responsibilities.

Start.

Start planning for that “you” time and then actually break away and enjoy it.  When you feel the burn of your job or other sources of stress, think of those things that bring you comfort and the thoughts of calm.  Put together a play list on your device of choice that is filled with songs that bring good memories and settle you down.  Listen to this when you can to keep you close to center and away from burnout.

Do.

Do get away from the work and do it for your mental and physical health.  I myself have had quite a rough 4 weeks here recently at work that have been stressful on my co-workers as well.  Inevitably, that stress and list of issues does not just stay at work…  that weight is carried home and impacts interactions with my wife and kids.  Where I am distracted and quickly frustrated even by their simple act of wanting me to pay attention to them.  This is not a healthy place to be in.  Teach your loved ones to notice when you are burned out and help them to guide you to your way of reconnecting and calming down.  Not just to calm down at that point but the more long lasting calm that will help build you back up…  to be patient, kind, thoughtful and present in the moment.

Relax.

So after the 4 weeks of burnout and stress, here is what I did to recharge and reconnect.  I did not go to work on Friday and I disconnected from some of the technical distractions.  The weather was perfect on Friday.  Perfect.  So, we (the family) stayed outside and felt the warmth of the sun on our skin and the cool breeze when you walked through the shade of the trees.  I laid on the rock wall in our yard and stared at the sky…  took a few pictures to capture and remember that calm feeling and what a perfect day it was.  I pushed my son around on a tricycle and listened to him laugh and call out order on where to turn and what to run over next.  The next day we met with my brother and my mom to take the kids to the Renaissance Festival, a local mediaeval event that is held the weekends in May in our area.  I was a bit uncomfortable for a while because I do not care for crowds and the attempt to herd the cats from one thing to the next…  then it happened.  I just let go of trying to control it and had a great time.  This was a great time to reconnect with family.

You.

Then there is “you” and you need to have your own disconnected time.  It does not have to be extended or even elaborate but find a thing that will bring you comfort and peace to heal your brain and recharge your batteries.  I went to REI this weekend and looked at a few things then something jumped out at me…  something I felt like I needed.  There was a comfortable looking hammock that could be hooked into trees or whatever.  Then just hang there and relax.  This was the “me”  time that I was looking for.  I will have to say that it worked.  I went out there and it was quiet and a laid there looking at the sky and was truly relaxed.  Then as the sun filtered through the leaves in the trees, I was overwhelmingly relaxed and I fell asleep.  Not just a little asleep but I slept hard and was energized when I woke up a few hours later.

That is why I typed this…  I thought of what a great  weekend it was and how much better I felt.  I hope this feeling can carry on into the upcoming week but if it doesn’t…  it is okay.  Because I feel great now and this was the perfect weekend I needed to recharge myself.  I feel great and had some downtime.

Go get yours too!

Posted in random | Leave a comment

Reading with Flipboard

I find myself using Flipboard daily to stay up to date with articles and topics that I am interested in.  I have tried to use the iPad client but have found that with the additional real estate, there are more articles that seem to clutter the screen and distract the UI.  I have a very distractable personality…  As I type this I am sitting on the couch and watching the show Lost on Netflix.  I just can’t seem to sit idle and feel a bit restless when I try.  But let’s get back on track.

So the way I read my Flipboard articles is on the iPhone on a very focused screen size.  This can show only one article at a time and the navigation is intuitive and almost instinctive.  I think there is a valuable UI lesson about the cost of a screen and the value that the information on the screen should carry.  Also, just because you have the space, doesn’t mean you should fill it up with low quality noise.  The focus should be to keep the value high and put less valuable items off the screen or out of the app all together.

As for the articles that I read and collect, they are all technical in nature and may be of interest to you:

DEV OPSArticles on automation
LOCK IT UPArticles about security
DATABASES NOSQLDatabase articles that are non-relational
i SEEArticles about Data Science, Visualization and Machine Learning
ALL THINGS DEVGeneral articles on development, Design and UI
PERSONALITY TYPESAbout people and leadership
JAVASCRIPT FUNArticles about Node, React, jQuery… all things JavaScript
TECH COOLTechnology that is interesting
SQL STUFFRelational SQL items, mainly MSSQL
PICKSThis is the bucket that all the rest of the articles go to

Hopefully some of these articles and topics are of interest to you and I will keep sharing things as I parse through them. At the time of writing there are around 4,847 Articles in the various categories.

Posted in random | Leave a comment