Tuesday, December 31, 2013

Reading-a-16-Color-32x32-bitmap

This is the article which I have written which show how to read binary file. I have written this for one of my friend in this article it read a 16 color 32 x 32 bitmap



I have written this article in codeproject here is the link


 http://www.codeproject.com/Tips/503490/Reading-a-16-Color-32x32-bitmap

Folder Styler : Change Folder Background


This was the first application which I had created . The application is programmed in Autoit
http://www.autoitscript.com/site/autoit/

This application can change folder background in explorer,change icon and text color.

Download:

http://www.box.com/shared/b4s6tvt6uhvifjqvrx2m

I download contain only binaries. I have lost the source code. I will upload it if I find it.
Software only works in windows xp . It will not work in window 7 or higher.


Saturday, December 28, 2013

Rok Model Viewer


This application can display 3d models created by Hexagreat 3d modeller.
download from : http://www.flat2d.com/ for hexagreat3d



I created this application while I am learning openGl .
Application contain many bugs.And lacks many function.I will implement them when i learn more.

Note:
This application can only display model which contain faces

Download
https://app.box.com/s/2zbfok8no3b8ijjilcbx

The model is not my property. I just downloaded it form internet

Friday, December 6, 2013

Snake Game in c++ : Part 2

Snake Game in CPP : Part 1
Snake game in c++ : Part 2

Now contuning 2nd part of Snake game in c++. In previous code there was a problem that screen was flickring while running game.
This can be eliminated by drawing the things that are updated in loop. Like the snake, fruit and wall is constant it not needed to draw wall every loop.

First remove system("CLS"); from main method because clearing the screen drawing it takes time.

Now alter our draw method. From the previous code remove the drawwall()  method from draw() ,so wall will not be drawn again and again.

Now the thing how to make snake look  like it moving because screen is not cleared and drawn again. So snake become larger and larger.

To remove this problem we to further alter our draw method. Insted of using system("CLS"); to clear whole screen ,we can use space ‘  ‘ to make screen clear. Space can be put on place where we want to remove things.

Now to make snake appear moving we will just replace its last tail with blank space ‘ ‘. So as snake is moving its last position is been replaced with space.

Here is the draw method code

void snake::draw()
{
     /*
cur_cord.X=10;
cur_cord.Y=5;

SetConsoleCursorPosition(console_handle,cur_cord);
cout << "hi";  
*/



tail *tmp , *last;
tmp=start;
last = current;

                     

while(tmp!=NULL)
                      {
                        cur_cord.X=tmp->x;
                        cur_cord.Y=tmp->y;
                        SetConsoleCursorPosition(console_handle,cur_cord);
                        //cout << "*" << cur_cord.X <<"-" <<  cur_cord.Y  ;
                        cout << "#";
                        tmp=tmp->next;                         
                      }
// remove tail
cur_cord.X=last->x;
cur_cord.Y=last->y;
SetConsoleCursorPosition(console_handle,cur_cord);
cout << ' ';
//draw the food
 cur_cord.X=foodx;
 cur_cord.Y=foody;
 SetConsoleCursorPosition(console_handle,cur_cord);
  cout << "?";
 
}

To draw wall just call drawWall() in main method.
Thus the game works fine now.

//------------------ hope enjoy it
The code used in this tutorial is little bit different from the code in the archive
use DEV C++ to compile the code.
Download the full code and study it

Download
https://app.box.com/s/jjfbfs94r8nukmdgynr7


Tuesday, August 27, 2013

Snake Game in C++ : Part 1

Snake Game in CPP : Part 1
Snake Game In cPP : Part 1
After creating the blog I decided to post a tutorial to create a snake game in c++ using only text mode.
This program I created as a project for my college
This tutorial is for beginners.
This is not Complete Game because it dont't contain any title screen or score




For those who don’t know this is the game where player control a snake , when snake eat food it grows in size and get points . Game ends when snake collide with wall or collide with itself.

Requirement
DEV C++ IDE http://www.bloodshed.net/or use any other you like
Basic knowledge of c++ .


Get Started
Before creating our game we have to think what we are going to make. That is the snake game in c++ using only text mode.

Now look at the structure of snake game.
There is a snake which player controls
There is obstacle wall.
There is food which make snake grow in length.

Design of game

Lets Program the game

Launch Dev C++ and create new c++ file. To keep it simple it will be a single file program.

First include the basic header file.

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;

Now create a stucture to store our snake position this is double linklist I will explain it later.

typedef struct tailpos
{
int x;   // x cordinate of the snake
int y;  // y cordinate of the snake
struct tailpos *next;   // pointer to next node
struct tailpos *prev;   // pointer to previous node 
} tail;

// d used to set the direction
int d=4; // up = 1 , down = 2 , left =3 , right = 4;

A variable ‘d’ which will store direction .you can declare this variable inside class also.

The snake class

class snake
{
public:  
      
       int foodx,foody;             // position of the food
      
       HANDLE console_handle; // handle for the console      
       COORD cur_cord;        // COORD struct to keep the coordinate
      
       /*
       tail struct ==>
                      start = pointer to first part of body
                      current = pointer to last node
                      newtail = temp node for new tail
      
       */
      
       tail *start,*current,*newtail;
       snake();  // construct to initalise tail variable to null
       void insert(int x , int y); // insert the body of snake append to next node
       void draw();    // draw the snake
       void drawWall(); // draw the wall
       void move();    // control the movement
       bool collision();  //check snake collision with itself or wall
       void drawfood(int x=0); // draw food to new position if x==1     
     
};


Looking at the code you can understand what it will do

For storing snake position you can use 2d array. But i prefer linklist . For those who don’t know let me explain it.

 By using this data structure we can easily store and retrive snake parts position
For more information ... http://en.wikipedia.org/wiki/Linked_list


Now implement snake class methods:

  The constructor
   
snake::snake()
{
start = NULL; // start node point to null
current = NULL; // same
newtail = NULL;  // same 
console_handle=GetStdHandle( STD_OUTPUT_HANDLE );  // getting console handle

foodx=12;                    // starting food positions
foody=14;   
}

  The drawwall method this will draw boundary for our snake game

    void snake::drawWall()
{
     /*
     for drawing wall you can use variables to store wall starting and ending position
     */
     // draw left column
     cur_cord.X=0;
     for(int y=0;y<=30;y++)
           {
            cur_cord.Y=y;
            SetConsoleCursorPosition(console_handle,cur_cord);
            cout << '#';
           }
    // draw top row  
       cur_cord.Y=0;
       for(int x=0;x<=30;x++)
           {
            cur_cord.X=x;
            SetConsoleCursorPosition(console_handle,cur_cord);
            cout << '#';
           }
    // draw right column
     cur_cord.X=30;
     for(int y=0;y<=30;y++)
           {
            cur_cord.Y=y;
            SetConsoleCursorPosition(console_handle,cur_cord);
            cout << '#';
           }
          
     // draw bottom row
     cur_cord.Y=30;
       for(int x=0;x<=30;x++)
           {
            cur_cord.X=x;
            SetConsoleCursorPosition(console_handle,cur_cord);
            cout << '#';
           }   
       

}  


Draw the food for our snake

void snake::drawfood(int x)
{
tail *tmp;
tmp=start->next;
if(x==1) // draw new food
        {
                         foodx=rand()%20+7;  // rand number between 2 to 27
                         foody=rand()%20+7;

                        /*check if food not created on snake body
                         so we will loop through all snake node and check
                         if snake body pos meet with food position
                         if yes the generate new position else continue
                         */

                         while(tmp->next!=NULL)
                       {
                            if(foodx == tmp->x && foody == tmp->y)
                            {
                            drawfood(1);
                            }                                                
                                              
                            tmp=tmp->next;                
                       }                    
                                         
        }
}

Increasing snake length . when snake eat food its length should increase . For this
We have insert method
void snake :: insert(int x , int y)
{
     // check if start is null
     if(start == NULL)
     {
     newtail = new tail;
     newtail->x=x;
     newtail->y=y;
     newtail->next=NULL;
     newtail->prev=NULL;
     start=newtail;
     current=newtail;
     }
     else            // insert new node to start node
     {
     newtail = new tail;
     newtail->x=x;
     newtail->y=y;
     newtail->next=NULL;
     newtail->prev=current;  
     current->next=newtail;
     current=newtail;
     }
     
}

This method just append a new tailposition structure to the tailpos structure.

Here ‘start’ varaible points to node1 where as ‘current’ variable points to node3 i.e the last node.

After appending the new node.
After appending the new node the ‘current’ variable points to newely created last node i.e node4.

Now the best part how to move snake ? So the tails follow the head.
You know in game tails follow the head every where it goes.
So first Question how to move snake head?
Ans : Simple we have to print snake head at new cordinate at console every cycle.
         We can increment the current X,Y position of console. To move snake in specified direction.

Q: How to make tail follow the head ?
Ans : The tailpos structure which keep position of every part of snake. We just have to move value of head to its tail and value of first tail should move to next tail till last and increment position of head . In simple way remove value of last tail put previous tail value in it .

This way position of snake parts is shifted

The code to move:

void snake::move()
{
tail *tmp,*cur; // cur no use

tmp =current; // point tmp to last node
//cur =start->next;

while(tmp->prev!=NULL)
                      {
                      tmp->x=tmp->prev->x;        // copy val from revious to last
                      tmp->y=tmp->prev->y;
                      tmp=tmp->prev;              // set tmp to previous node
                     
                      }
/*
check for the value of d in order to change its direction
*/
if(d==1)
start->y--;

if(d==2)
start->y++; 

if(d==3)
start->x--;

if(d==4)
start->x++;                 
                     
}

The move method is called every cycle.

Collision Detection:

To check collision we can simpily compare snake head posiotion with
1)     Its tail
2)     Wall
3)     Food

Code to collision:
bool snake::collision()
{
                       tail *tmp;
                       tmp=start->next;
                       //check collision with itself
                       while(tmp->next!=NULL)
                       {
                            if(start->x == tmp->x && start->y == tmp->y)
                            return true;
                                                    
                                              
                            tmp=tmp->next;                
                       }
                       //check collision with food
                       if(start->x == foodx && start->y == foody)
                       {
                       insert(foodx,foody);          
                       drawfood(1);  // draw food at new position
                      
                       }
                       //check collision with wall
                         //collision top
                          for(int x=0;x<=30;x++)
                           {
                                  if(start->x == x && start->y == 0)
                                  {
                                         return true;                
                                  }
                            
                           }
                           //collision left
                          for(int y=0;y<=30;y++)
                           {
                                  if(start->x == 0 && start->y == y)
                                  {
                                         return true;                
                                  }
                            
                           }
                            //collision right
                          for(int y=0;y<=30;y++)
                           {
                                  if(start->x == 30 && start->y == y)
                                  {
                                         return true;                
                                  }
                            
                           }
                           //collision bottom
                          for(int x=0;x<=30;x++)
                           {
                                  if(start->x == x && start->y == 30)
                                  {
                                         return true;                
                                  }                            
                           }                         
                                                return false;
     }

Rendering the game:
void snake::draw()
{

drawWall(); // draw wall

tail *tmp;
tmp=start;

while(tmp!=NULL)
                      {
                        cur_cord.X=tmp->x;
                        cur_cord.Y=tmp->y;
                        SetConsoleCursorPosition(console_handle,cur_cord);
                        //cout << "*" << cur_cord.X <<"-" <<  cur_cord.Y  ;
                        cout << "#"; // print any value you want
                        tmp=tmp->next;                          
                      }
//draw the food
 cur_cord.X=foodx;
 cur_cord.Y=foody;
 SetConsoleCursorPosition(console_handle,cur_cord);
  cout << "?"; // print any value you want
 
}

This method prints wall , snake , food at the console.

The game loop:
The game loop which is repeated every time to draw snake,take input and process , and detect collision
int main()
{
snake ob;            // snake object
ob.insert(10,5); // insert snake body
ob.insert(10,4); // insert snake body
ob.insert(10,3);
ob.insert(10,2);

//ob.draw();
int dir=1; // initial direction
while(1) // main loop
{
ob.draw(); // draw food snake wall
Sleep(100); // wait after draw better to use a timer
system("CLS"); // clearthe screen
 

          if(ob.collision())// check for collision
          {
            cout << " you died ";
            break;               
            }

            if(kbhit())// check if keyboard key is pressed
            {
             switch(getch())
             {
             case 'w':d=1;
             break;
             case 's':d=2;
             break; 
             case 'a':d=3;
             break;
             case 'd':d=4;
             break;
             case 'm':ob.insert(10,7);
             break;                               
              
               }
             

               }
              
 ob.move();  // move the snake
}


// to wait for user input
getch();
return 0;   
}

I have used W,A,S,D key to control .
This is just the core of the game there is no welcome screen nor end screen . and there is score .
You can implement these your self.

The main problem in the game screen flicker every time. This problem can be elminated which I will discuss in Next Part. OR implement it your self thats easy.

I am sorry because The post is not formatted nor any synatx hilighter. I I am just getting started with blog.Maybe I will update it later

Here is the complete code copy and paste it

WELCOME

Hi,
This is the first time I have created blog .