jQuery: Simple Click Game with Interval

Mar 12, 2014
Here's another simple game using HTML5 and Javascript.

The following are the requirements of the game:

  • The game starts when the "ENTER" key is pressed.
  • A "monster" will be displayed in random location of the canvas per set milliseconds.
  • Hit - when the monster is clicked.
  • Miss - when the monster is missed - only during click
  • Monster turns yellow when hit.
  • Monster turns red when hit.
  • The game is over when there are already five (5) misses.
  • The top score is recorded - this will reset on every page load, though.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
 
 <canvas id="canvas" width="450" height="450"></canvas>
 <script src="pop.js" type="text/javascript"></script>
</body>
</html>

pop.js
$(document).ready(function()
{
 // canvas
 var canvas = document.getElementById("canvas");
 var ctx = canvas.getContext("2d");
 var w = canvas.width;
 var h = canvas.height;
 
 // monster
 var locX;
 var locY;
 var monsterSize = 50;
 var monSpaceX; // space occupied by monster in X
 var monSpaceY; // space occupied by monster in Y
 
 // colors
 var bgColor = "white";
 var strokeColor = "black";
 var monsterColor = "blue";
 var monsterHitColor;
 
 // other variables
 var text; // label on canvas
 var topScore = 0;
 var topText;
 var hitScore; // number of hits
 var isHit;
 var missLimit;
 var interval;
 var msec = 700;
 var play; // game is ongoing
 
 init();
 
 // ** ----- FUNCTIONS ----- **
 
 // initialize
 function init()
 {
  // initialize variables
  hitScore = 0;
  missScore = 0;
  missLimit = 5;
  text = "Click ENTER key to play.";
  topText = "Top Score: " + topScore;
  play = false;
  window.clearInterval(interval);
  
  // draw canvas
        paintCanvas();
 }
 
 // draw canvas
 function paintCanvas()
 {
  // format canvas by setting BG color and border
  ctx.fillStyle = bgColor;
  ctx.fillRect(0, 0, w, h);
  ctx.strokeStyle = strokeColor;
  ctx.strokeRect(0, 0, w, h); 
  
  setLabel();
 }
 
 // draw monster
 function placeMonster()
 {
  // set score label
  text = "Score: " + hitScore + "; Chance left: " + missLimit;
  
  setLocation(); // random location
  paintCanvas(); // draw canvas to cover past monsters
    
  // monster
  ctx.fillStyle = monsterColor;
  ctx.fillRect(locX, locY, monsterSize, monsterSize);
  
 }
 
 // paint monster to indicate it is hit or not
 function paintMonsterHit()
 {
  ctx.fillStyle = monsterHitColor;
        ctx.fillRect(locX, locY, monsterSize, monsterSize); 
 }
 
 // place label on canvas
 function setLabel()
 {
  // set top score label above
  ctx.fillStyle = strokeColor;
  ctx.fillText(topText, 5, 10);
  
  // set label below
  ctx.fillStyle = strokeColor;
  ctx.fillText(text, 5, h - 5);
 }
 
 // randomly generate location to where monster will be placed
 function setLocation()
 {
  locX = Math.floor((Math.random() * (w - monsterSize)) + 0);
  locY = Math.floor((Math.random() * (h -monsterSize)) + 0);
  
  getMonsterSpace();
 }
 
 // get the coordinates of the space occupied by monster
 function getMonsterSpace()
 {
  monSpaceX = locX + monsterSize;
  monSpaceY = locY + monsterSize;
 }
 
 // check if monster is hit or missed
 function isMonsterHit()
 {
  if(isHit)
   hitScore++;
  else
   missLimit--;
 }
 
 // check if game is over or not
 function checkGameOver()
 {
  if(missLimit == 0)
  {
   alert('Game Over. Score: ' + hitScore);
   if(hitScore > topScore)
   {
    alert('Congratulations! New Top Score!');
    topScore = hitScore;
   }
   init();
  }
 }
 
 // ** ----- EVENTS ----- **
 
 $(document).click(function()
 {
  if(play)
  {
   if(event.x >= locX && event.x <= monSpaceX && event.y >= locY && event.y <= monSpaceY)
   {
    monsterHitColor = "yellow";
    isHit = true;
   }
   else
   {
    monsterHitColor = "red";
    isHit = false;
   }
   
   paintMonsterHit();
   isMonsterHit();
   checkGameOver(); // check if game is over
  }
 });
 
 $(document).keydown(function(e)
 {
  if(!play)
  {
   var key = e.which;
   
   if(key == '13')
   {
    interval = window.setInterval(placeMonster, msec);
    play = true;
   }
  }
 });
 
});

Improvements in the future:
  • A miss should also be counted when user fails to do a click every time a monster appears.
  • Use image for monster.
NOTE: Only works for Google Chrome. :/

Read more ...

jQuery: Simple Colored Cell Game

Mar 7, 2014
So I decided to learn new stuffs *since I am not a web-person or anything*.

The goal would be to create a simple game that does the following:

  • At the start of the game, there is a blue-colored cell in the middle of the canvas.
  • On every left-click, the cell increases in size.
  • If an arrow key is pressed, the cell changes color and moves to that direction.
  • If the enter key is pressed, the game is reset.
Though it's just a really simple game, I find this a challenge. *I'm the Tech Noob after all. ;)

I've shared the codes below. Hopefully this helps beginners like myself. ;)

index.html
<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
 
 <canvas id="canvas" width="450" height="450"></canvas>
 <script src="cell.js" type="text/javascript"></script>
</body>
</html>

cell.js
$(document).ready(function(){
 
 // canvas stuff
 var canvas = document.getElementById("canvas");
 var ctx = canvas.getContext("2d");
 var w = canvas.width;
 var h = canvas.height;
 var locX; // location in X
 var locY; // location in Y
 var pxX; // pixel size in X
 var pxY; // pixel size in Y
 var cellColor;
 var BGColor;
 var strokeColor;
 
 function init()
 {
  // set default color
  BGColor = "white";
  cellColor = "blue";
  strokeColor = "black";
  
  // set default pixel size
  pxX = 10;
  pxY = 10;
   
  // set default location (center of canvas)
  locX = (w - pxX) / 2;
  locY = (h - pxY) / 2;
  
  paint();  
 }
 
 init();

 function paint()
 {
  // paint background
  ctx.fillStyle = BGColor;
  ctx.fillRect(0, 0, w, h);
  ctx.strokeStyle = strokeColor;
  ctx.strokeRect(0, 0, w, h);
  
  // paint cell
  ctx.fillStyle = cellColor;
  ctx.fillRect(locX, locY, pxX, pxY);
  ctx.strokeStyle = strokeColor;
  ctx.strokeRect(locX, locY, pxX, pxY);
 }
 
 $(document).click(function()
 { 
  // add one pixel for x and y on every click
  pxX++;
  pxY++;
    
  paint();
 })
 
 $(document).keydown(function(e)
 {
  var key = e.which;
  
  if(key == "37") // left
  {
   locX--;
   cellColor = "red";
  }
  else if(key == "38") // up
  {
   locY--;
   cellColor = "blue";
  }
  else if(key == "39") // right
  {
   locX++;
   cellColor = "yellow";
  }
  else if(key == "40") // down
  {
   locY++;
   cellColor = "green";
  }
  else if(key == "13") // enter
   init();
  
  paint();
 })
 
})
Read more ...

Shortcut Virus

Mar 6, 2014
I was asked to fix someone's flash disk a few years back because all of the files inside it turned into shortcut icons. You cannot view the files even if you choose to show hidden files.
I was able to find the solution on the internet.

Now another someone encountered the same problem. I was searching for the solution on the internet but it seems everything that shows up are those that require users to download something. *sucks*

So I did the best I could to look over my files and see if I have saved the solution I did back then.

And FORTUNATELY, I did.

I would just like to share this one FOR FREE without you having to download anything. (:
  1. Run Command Prompt.
  2. Type this:
    attrib F:\*.* /d /s -h -r -s
Remember to change F: with the correct target drive.

The files are now visible. Just delete all the shortcut icons.

PS:
** Make sure you have an anti-virus installed and that it is updated.
** Next time, be careful on where to insert your flash disks. (:
Read more ...

C#: Check if Input is Palindrome

Mar 5, 2014
Here is one algorithm I find a challenge back on my student days.

* Check if string value entered by user is a palindrome or not.

/// <summary>
/// Checks if entered string value is a palindrome or not.
/// </summary>
/// <param name="p_word">Value entered by user</param>
/// <returns>If palindrome, returns true; otherwise, returns false</returns>
public bool IsPalindrome(string p_word)
{
 string revWord = string.Empty;
 char[] wordChar = p_word.ToCharArray();

 for (int index = p_word.Length - 1; index >= 0; index--)
 {
  revWord += wordChar[index];
 }

 if (revWord.Equals(p_word))
  return true;
 else
  return false;
}

If you have any better solution, please don't hesitate to share them on the comments section. (:

::EDIT::

Here's another solution by .NET GENE.

using System.Linq;

public bool IsPalindromeEnum(string p_word)
{
 string revWord = new string(p_word.Reverse().ToArray());

 if (revWord.Equals(p_word))
  return true;
 return false;
}

Thanks! :D

Read more ...

Free Domain Names

Mar 5, 2014
I have tried using .TK for free domain once on my other blog and it worked just fine. (:
.TK logo
Image credits: PPD Tips

Actually this is just a site that would register your URL and give you a shortened one.
The shortened URL would just redirect your user to your site.

So instead of having to type the whole ".blogspot.com" thing, you may just let your users type "blogname.tk".
Pretty handy, right? (:

If you have any thoughts about this, please share on the comments section.


Read more ...

Recent Comments Section on Blogger

Mar 4, 2014
Adding a Recent Comments widget on your blog will help let your visitors know that your blog is active - especially if you've got new comments coming from readers.

There are many widgets out there that you may use.
The following steps below would add a Recent Comments section on your sidebar without having to use third-party functionalities.

  1. On your dashboard, go to Layout.
  2. Click on Add a Gadget on where you would want to place your Recent Comments section.
  3. Choose Feed.
  4. Add your comment feed URL Format: http://blogname.blogspot.com/feeds/comments/default
  5. Click Continue.
  6. Make final changes and click Save.
Another is to use scripts generated by third-party functionalities.
Below are the links of these scripts and the instructions on how to use them:
If you've got more tips, please don't hesitate to drop your message on the comments section. Thanks!

UPDATE: Blogger now offers the RECENT COMMENTS widget, so YAY!!
Read more ...

Transfer Videos from Computer to iPhone Device

Mar 1, 2014
I have been having trouble with video transfers over iPhone devices.
Bluetooth feature of these devices doesn't seem to support file transfers. But please correct me on the comments section if I'm wrong.

What I usually do is ask someone to send the files via Viber.
If the files are on a desktop computer, I would use Dropbox. However, I could only download image files from Dropbox. NO VIDEOS.

That's just my problem right now.

I have a copy of a video on a DVD and I want it transferred to my iPod Touch so that I could upload it on Instagram. (lol)

I placed the video on Dropbox. I can view it but I CAN'T download it.
I searched for some solutions and found this one. Though I did successfully transferred the video to my iPod device, it cannot be uploaded since it's not stored in my Gallery.

I'm starting to get frustrated.

Luckily, I just kind of remembered this "wifi transfer" thing so I decided to go to App Store and searched for it.

Icon
Image credits: iTunes
I decided to install this Simple Transfer - Wireless Photo & Video Backup, Sync and Share app and see if this would help me solve my problem.

I followed its instructions and TADAAN!!

With only less a minute, I finally have my video on my gallery! :D

But being a Tech Noob, I'm not too sure if this is the best solution.
So guys, if this one is too risky for you, or you could recommend a much better solution, please feel free to drop a message on the comment box.

Thanks!
Read more ...