Showing posts with label Game. Show all posts
Showing posts with label Game. Show all posts

Monday, July 22, 2013

Diamond Reaction: An HTML5 Chain Reaction Game using Crafty JS

I've been away from blogging for a long time. Maybe because I've not been learning code-detailed topics. I've been more like learning the basics in topics I've never tries before (like PHP) and learning some theoretical topics.

Anyways, this time I'm trying a new approach to gain more experience. This was I can gain experience from anything I want to learn. So here is my almost-full game implementation of an HTML5 chain reaction game called: Diamond Reaction.

Play Diamond Reaction now



Let's talk a bit about it:

Idea:
I wanted to make a game for fun, with the focus on game mechanics. So I started thinking of some ideas that can be fun for a 'mario-lover' like me. Whenever I think of an idea, I search about it and see how frequent I find the same mechanics on the web. Till I arrived at this concept that did not have a big hit before: chain reaction games.

Theme:
If you look at a game like Angry Birds, you can find the game mechanics are not that hard or new. But the theme is what made it a big hit. I'm doing my game for fun and experience and I'm not a graphic designer, so I started searching for the common simple themes for the popular games these days, especially on Facebook. The most noticed theme was the cartoonish diamonds theme (plus bubbles and animals). So I picked this theme because It will not need much effort to make, plus it actually makes the game more fun to play (you can search for chain reaction games and see how boring their graphics are).

Implementation:
Since the concept of chain reaction game is not that hard to implement, and I already know how to use Crafty JS framework, the game did not take much time to get to the first demo.

Gamification:
I thought about adding a small feature to add more fun and challenge to the game: highscore and leaderoard. but I did not to keep user data on my small server. So I decided to add an optional Facebook login button to login with Facebook credentials so that I can use the Scores API the Facebook has.

Balance:
These kinds of small games can get boring if I did not find the right balance for the game. Since it was a very small game with no big details, fine-tuning was made easily by playing and -trial-and-error.

Missing features:
- No sound was added, maybe I can add it later.
- I'm not satisfied with the current balancing.
- Add more items and surprises to the game to make it more fun and engaging.

Play Diamond Reaction now




Sunday, June 30, 2013

Crafty JS Tutorial (DX Ball / Breakout)



Introduction:

This Crafty JS tutorial is part of Alkottab Studio's internship program, to take developers with no previous game development experience in the quickest way to production phase.

Prerequisites:
  • Previous development experience.
  • The very basics of HTML/CSS/Javascript.
  • Finishing the first part of the Crafty tutorial on its website. [ How Crafty worksDownload and Setup ]

Section 1: Game description:


  • A ball continuously moving and hitting walls and bricks.
  • Bricks are destroyed and points are collected when hit by the ball.
  • User controls a bat and moves it right and left to prevent the ball from hitting the ground.
  • If the ball hits the ground, a life is lost.

Section 2: File Structure:

Before starting the tutorial, it is better to explain how the files are arranged:
  • index file : links to JS files, game div, some simple CSS.
  • js directory: Crafty file and game file.
  • assets directory: images and spritesheets used.

Section 3: Coding considerations:
  • For a cleaner code, some complex game items will be separated as Crafty components to be used later in game scenes.
  • To handle game constants, variables and cross-component calls (like an object wanting to affect the score bar), I will make a simple game manager to contain values and functions connecting between components.

Section 4: Scenes:
  • Loading: A scene only for loading game assets and giving the user a progress indication.
  • Home: Main Menu screen of the game. For now, it will contain only a start button, but it can have more buttons and some graphics representing the game.
  • Game: the scene with the actual play.
  • Total Score: When a user wins or loses, this scene will show the total score.


For the complete tutorial, view the PDF file online (link) or download the PDF + code (link)

Sunday, September 16, 2012

First appearance of Alkottab at Fekrety competition, AUC

Last Thursday was a special day for me that I wanted to share with the world. It was the awards ceremony for Fekrety (translated to: my idea) competition finalists and the first official appearance of Alkottab (maybe translated to: local school) company.

Alkottab is a new Egyptian startup for game development and animations with Arabic themes and content. And I'm proudly a co-founder of this Egyptian startup which aims to attract the youth in specific to games and animations that introduce them to their history and present, and invest in them to build a brighter future.



Fekrety is a national business idea competition organized by The American University in Cairo’s (AUC) School of Business – Entrepreneurship & Innovation Program (EIP), in cooperation with Intel. Although we did not win the money, it was an opportunity to announce our idea to the world of business and startups and listen to their feedback. And we had some small prizes too, if it deserves mentioning as it was not the main goal of entering this competition.

Let me introduce you to the team, and show you the idea pitching by Alkottab co-founder Eslam. The team from right to left is: Eslam, Omar and Mahmoud (me) as co-founders and developers, and Youmna and Maha as artists.



The summary of the pitch for the non-Arabic viewers is as I mentioned before about Alkottab, plus stating that the Arab countries have a bigger customer segment in game purchases than the USA and that's why foreign companies are interested in localizing their games. And because we are very interested in our idea, we decided to quit our jobs and put our savings in this company, Alkottab, and were hoping to get more fund by winning such a competition. Our first product is about the early days of our Egyptian revolution.

This was the first appearance, and not the last (if Allah wills) because we have the idea and passion, and building the experience, and trying to avoid the mistakes we noticed in different startups we witnessed or worked with.

So, this was a small report to share the news with you. And let's hope to see you very soon somewhere in a local/international event or on the market.

Monday, July 16, 2012

Full HTML5 Game Example with Crafty JS

A couple of days ago, I participated in a game jam called "Game Zanga". It was a three-day contest for game development with some conditions: in Arabic, related to a topic announced at the start of the three days (it was "Freedom"), and browser-based.

So it was a nice chance to test my knowledge and experience in game development (which is a new thing to me) and I decided to use Crafty (or maybe some call it CraftyJS). Crafty is a JavaScript game library using HTML5. One great power in Crafty is that it can use Canvas or DOM. Plus the concept of Components and Entities that allow you to develop the game in a OO style.

So, I thought I can post my code for anyone to learn from. Although it is not perfect code and it neads more refactoring and fine-tuning, it is still a good example of how a game can look like in Crafty. I almost did not need anything other than the API documentation on Crafty website (plus the tutorial if you are to start from the very beginning). So with decollapsing the code you'll immediately have a general overview of how the game code looks like, then collapsing the code can do the job to get to the details.

One important note, this game was intended to be an Arabic game, so here are some guidelines:
- The first icon in the home screen is the "Start" button.
- Once the game starts, you have two blue cannons on the left. Click any of them to use it.
- Your abjective is to let the blue soldiers pass ("unrwa" in code) and kill the green ones ("soldier" in code).
- Life points decrease if any green soldier passes.
- Your weapon is upgraded while playing.


Finally, you'll notice some faults in collision detection and a lot of other things in the code, but remember that it was made in only two days by me (a newbie) using Crafty which lacks the variety of full examples to learn from. Anyways, you'll get the idea of a full game in less than 1000 lines and then you can make your own best seller.

Here are some screenshots, followed by a download link.





Download game code here.


Saturday, April 14, 2012

My HTML5 Canvas Profile Widget

Since I've not been innovating much outside work for a while now, I thought I could make any new "thing" to aggregate a few benefits I've been aiming for. The output was this HTML5 canvas, game-like, Egytian-themed profile widget. It was a chance to play with HTML5 canvas, and still stick to the design of the game loop. It was also a chance to change the look of my boring blogs.

You play the game. Drop the egg from the profile you want to visit. If the egg hits the nest, you earn the visit to the profile.

So without any more talking, the code is pretty simple:


  • A game loop
  • Check for user input (mouse clicks)
  • Update the values of some objects
  • Detect collisions of some objects and acting accordingly
  • Draw objects

Notes:
  • The javaScript code is hosted online and only referenced to in my HTML code. This was better to update the code once and apply it to all the places where I use it. (as a desktop/mobile developer, this is great).
  • Loading an image at runtime made a lag of maybe one second when a egg is dropped, but it looked ugly so I commented the code regarding loading the egg images in runtime and used a single preloaded image instead.
  • I've looked about three times for performance boosters. But still this does not mean this is the best to get from HTML5 canvas and Javascript. It is just about the amount of time I gave to this code.
  • If I did not put much comments, it was because I thought the code is self-explaining.


I preferred to post the code here again for better syntax highlighting
[profile_widget_script.js]
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var globalFPS               = 20;
var maxItemSpeed            = 2;
var eggSpeed                = 8;
var eggSize                 = 32;
var minItemHeight           = 200;
var iconSize                = 32;
var wingSize                = 32;
var nestX                   = 100;
var nestY                   = 330;
var nestMaxX                = 150;
var nestMinX                = 50;
var nestWidth               = 96;
var nestHeight              = 57;
var nestSpeed               = 2;
var mouseEventFlag          = false;
var mouseEventX             = 0;
var mouseEventY             = 0;
var wave1X                  = -10;
var wave1Y                  = 360;
var wave1Direction          = 1;
var wave2X                  = -25;
var wave2Y                  = 375;
var wave2Direction          = 1;
var wave3X                  = -40;
var wave3Y                  = 390;
var wave3Direction          = 1;
var waveXThreshold          = -50; // where to toggle the direction of wave
var waveSpeed               = 2;

// put the items you want to show here {icon to fly, redirecting url, egg to drop}
var profilesArray = [
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/facebook_32x32.png",
            url:    "http://www.facebook.com/MahmoudAdly",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/twitter_32x32.png",
            url:    "http://www.twitter.com/MahmoudAdly",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/gplus_32x32.png",
            url:    "https://plus.google.com/103333225222170744758",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/goodreads_32x32.png",
            url:    "http://www.goodreads.com/MahmoudAdly",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/linkedin_32x32.png",
            url:    "http://www.linkedin.com/in/MahmoudAdly",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/blogger0_32x32.png",
            url:    "http://3adly.blogspot.com/",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            },
            {
            icon:   "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/blogger1_32x32.png",
            url:    "http://free-3adly.blogspot.com/",
            egg:    "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png"
            }
            ];
            
var items = [];
var eggs = [];
var wingImage = new Image;
var nestImage = new Image();
var background = new Image();
var waveImage1 = new Image();
var waveImage2 = new Image();
var waveImage3 = new Image();
var eggImage = new Image(); // loading one egg to use it later, for performance reasons

wingImage.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/wing_32x32.png";
nestImage.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/nest.png";
background.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/background.png";
waveImage1.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/wave_1.png";
waveImage2.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/wave_2.png";
waveImage3.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/wave_3.png";
eggImage.src = "http://dl.dropbox.com/u/44163256/html5-profile-widget/assets/egg.png";

var nest = {
    image: nestImage,
    x: nestX,
    y: nestY,
    width: nestWidth,
    height: nestHeight,
    speed: nestSpeed,
    xDirection: 1,
    minX: nestMinX,
    maxX: nestMaxX
};

createItems();
setInterval(gameLoop, 1000/globalFPS);

function gameLoop(){
    updateUserInput();
    updateItems();
    updateEggs();
    updateNest();
    updateWaves();
    checkEggsCollision();
    drawBackground();
    drawWaves()
    drawItems();
    drawNest();
    drawEggs();
}

function createItems() {
    for(var i in profilesArray) {
        var profile = profilesArray[i];
        var img = new Image();
        img.src = profile.icon;
        
        items.push({
                x: 0- Math.random()*500, // so icons do not appear at once
                y: Math.random()*minItemHeight,
                speed: 1+Math.random()*maxItemSpeed,
                image: img,
                url: profile.url,
                egg: profile.egg,
                wingRotation: 0,
                wingRotationDirection: 1
        });
    }
}

function updateItems() {
    for(var i in items) {
        var item = items[i];
        item.x += item.speed;
        item.wingRotation += item.wingRotationDirection * 0.1
        if(item.wingRotation > Math.PI/4)
        {
            item.wingRotationDirection = -1;
        }
        else if(item.wingRotation < 0)
        {
            item.wingRotationDirection = 1;
        }
        
        // if item gets out of canvas scope
        if(item.x > canvas.width) {
            // recycle the item and set different values instead of removing it
            item.x = 0-Math.random()*100;
            item.y = Math.random()*minItemHeight;
            item.speed = 1+Math.random()*maxItemSpeed //between 2 and 5
            item.wingRotation = 0;
        }
    }
}

function updateEggs(){
    for(var i in eggs){
        var egg = eggs[i];
        egg.y += egg.speed;
        
        // if egg gets out of canvas scope
        if(egg.x > canvas.height) {
                // remove egg from array
                eggs.splice(i, 1);
        }
    }
}

function updateNest(){
    nest.x += nest.xDirection * nest.speed;
    
    if(nest.x > nest.maxX){
        nest.xDirection = -1;
    }
    else if(nest.x < nest.minX){
        nest.xDirection = 1;
    }
}

function updateWaves(){
    wave1X += wave1Direction*waveSpeed;
    if(wave1X > 0)
        wave1Direction = -1;
    else if(wave1X < waveXThreshold)
        wave1Direction = 1;
        
    
    wave2X += wave2Direction*waveSpeed;
    if(wave2X > 0)
        wave2Direction = -1;
    else if(wave2X < waveXThreshold)
        wave2Direction = 1;
    
    wave3X += wave3Direction*waveSpeed;
    if(wave3X > 0)
        wave3Direction = -1;
    else if(wave3X < waveXThreshold)
        wave3Direction = 1;
    
}

function checkEggsCollision(){
    for(var i in eggs){
        var egg = eggs[i];
        if(egg.x+eggSize > nest.x+nest.width/3
            && egg.x < nest.x+2*nest.width/3
            && egg.y+eggSize > nest.y+nest.height/3
            && egg.y < nest.y+2*nest.height/3)
            {
                alert("Thanks for trying my profile widget. You will now be redirected to: \n"
                        + egg.url);
                location.href = egg.url;
                // remove egg from array
                eggs.splice(i, 1);
            }
    }
}

function updateUserInput() {
    if(!mouseEventFlag)
        return;
    
    for(var i in items)
    {
        var item = items[i];
        if( item.x < mouseEventX 
            && mouseEventX < item.x + iconSize
            && item.y < mouseEventY
            && mouseEventY< item.y  + iconSize)
            {
                /* instead of loading the egg in runtime, load it once (eggImage) at startup and use it.
                    Because for a web view, it takes a second to load the image, which looks bad.*/
                //var img = new Image();
                //img.src = item.egg;
                
                eggs.push({
                    x: item.x,
                    y: item.y + iconSize,
                    speed: eggSpeed,
                    image: eggImage,
                    url: item.url
                    });
                mouseEventFlag = false;
                return;
            }
    }
}

function drawBackground() {
    context.drawImage(background, 0, 0);
}

function drawWaves(){
    context.drawImage(waveImage1, wave1X, wave1Y);
    context.drawImage(waveImage2, wave2X, wave2Y);
    context.drawImage(waveImage3, wave3X, wave3Y);
}

function drawItems() {
    for(var i in items) {
        var item = items[i];
        context.drawImage(item.image, item.x, item.y);
        context.save();
        context.translate(item.x+10, item.y+10);
        context.rotate(-item.wingRotation);
        context.drawImage(wingImage, -wingSize, -wingSize);
        context.restore();
    }
}

function drawNest(){
    context.drawImage(nest.image, nest.x, nest.y);
}

function drawEggs() {
    for(var i in eggs) {
        var egg = eggs[i];
        context.drawImage(egg.image, egg.x, egg.y);
    }
}

canvas.onmousedown = function(e) {
    var mousePos = getMousePos(canvas, e);
    mouseEventX = mousePos.x;// - currentTranslationX;
    mouseEventY = mousePos.y;// - currentTranslationY;
    mouseEventFlag = true;
};

canvas.onmouseup = function(e) {
    mouseEventFlag = false;
};

function getMousePos(canvas, evt){
    // get canvas position
    var obj = canvas;
    var top = 0;
    var left = 0;
    while (obj && obj.tagName != 'BODY') {
        top += obj.offsetTop;
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
 
    // return relative mouse position
    var mouseX = evt.clientX - left + window.pageXOffset;
    var mouseY = evt.clientY - top + window.pageYOffset;
    return {
        x: mouseX,
        y: mouseY
    };
}

Resources:
HTML5 Canvas Deep Dive
HTML5 Canvas Mouse Coordinates Tutorial

Wednesday, July 6, 2011

Developing My First Android Game

So, this entry is less about explaining and more about showing references. To come up with this game, I was studying and playing for a while with Qt/Python/Android code because I was not intending to come up with a real product. But anyways, it appeared that moving between languages is not that hard once you got used to it.

This is the very basic beginning of the game where I was learning the basics of game logic, the complete game should better go to the market that be posted as a project (sorry about that). But I guess the the coding is not much from this point. It is all about a better architecture of game objects/logic and graphics design (activity lifecycle, levels, special items, licensed graphics ... etc).

And do not worry about me posting the names and graphics, these are dummy names and graphics for learning :D

Here is a look at the game:




Download Project: here
(if you have problems with compilation, check the "target=android-10" line in "default.properties" file)

This is what I read, not all Android, but still logically helpful:
1- Tile-Based Games
2- Invent Your Own Computer Games with Python
3- Android Tutorial: How to make a basic game loop and FPS counter
4- Against the Grain: The Android Game Loop (the most help I could find)

Finally, this a great daily journal of an Android developer upgrading his game (Light Racer). I've read it a couple of months ago and had a great impact on how I think:
- The Light Racer 3D Development Journal


I hope I can find the time to complete the game and post it on the market.