samedi 25 mai 2013

<> My Memory Game <> Lighting Keyboard's Locks ...

Hello everybody !
 
In this post, I will present my little game that I named "Memory Game". To illustrate the technique used in the game I will show the following steps :
- In fact, I use the keyboard's Locks because I want to make an exam for my memory if is it OK or not. Well, it's just a little introduction for fun purpose that's all...
  The following steps are :
- Using the keybd_event function in order to make Num Lock or Caps Lock or Scroll Lock lighted.
- Creating a function named "ReturnState". It's used for returning if the Keyboard's Locks are On or Off.
- A counter to counts the number of times when a keyboard's Lock is lighted.

- The rule of playing the game is presented as follow:
+ Run the Game and wait your chance, Look at the following picture :


Posted Image
 
+ Take a look at the choosen Keyboard's Lock which is Caps Lock and counting the numbers of times when Caps Lock is lighted.
 
+ When your death is arrived I mean when your time is over hhhh, this message will printed on screen :




Posted Image
 
+ If your answer is Correct, you will get "Congratulations !!!". Otherwise you will get this :
Posted Image

- The Complete Source Code :

// Memory Game By Hamza Qdider
// All Rights Reserved...
// By The Hidden Ghost

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int ReturnState( char *which_key )
{
     if( strcmp( which_key,"Num Lock" ) == 0 ) 
     
         return GetKeyState( VK_NUMLOCK ) & 1 == 1 ? 1 : 0;
     
     else if( strcmp( which_key,"Caps Lock" ) == 0 ) 
     
         return GetKeyState( VK_CAPITAL ) & 1 == 1 ? 1 : 0; 
     
     else if( strcmp( which_key,"Scroll Lock" ) == 0 ) 
     
         return GetKeyState( VK_SCROLL ) & 1 == 1 ? 1 : 0;
     
     return -1;
}

void PauseScreen( void )
{
     ReadConsole(
                 GetStdHandle( STD_INPUT_HANDLE ),
                 NULL,
                 NULL,
                 NULL,
                 NULL
                ); 
}

int main( )
{
    SYSTEMTIME systime;
    int nRand,nTimes = 0,nElement,nSec,sysSec;
    int Answer;
    char *szLock[ 3 ] = { "Num Lock","Caps Lock","Scroll Lock" };
    
    srand( time( NULL ) );
    
    nElement = rand( ) % ( 0,3 );
    
    GetSystemTime( &systime );
    
    sysSec = systime.wSecond;
    
    nSec = sysSec + ( rand( ) % ( 1,60 ) );
    
    nSec >= 59 ? nSec = 59 : nSec;
    
    printf( "sysSec = %d ; nSec = %d { you have %d seconds }\n",sysSec,
            nSec,nSec - sysSec 
           );
    printf( "Looking at {%s}. Are you Ready ?\n",szLock[ nElement ] );
    getchar( );
     
    while( sysSec != nSec ) 
    {
           sysSec++;
           
           printf( "\rsysSec = %d",sysSec );
           
           nRand =  rand( ) % ( 100,1000 );
           
           Sleep( nRand );
           
           keybd_event( VK_CAPITAL,( char )VK_CAPITAL,0,0 );
           keybd_event( VK_CAPITAL,( char )VK_CAPITAL,KEYEVENTF_KEYUP,0 );
    
           Sleep( nRand );
    
           keybd_event( VK_NUMLOCK,( char )VK_NUMLOCK,0,0 );
           keybd_event( VK_NUMLOCK,( char )VK_NUMLOCK,KEYEVENTF_KEYUP,0 );
    
           Sleep( nRand );
    
           keybd_event( VK_SCROLL,( char )VK_SCROLL,0,0 );
           keybd_event( VK_SCROLL,( char )VK_SCROLL,KEYEVENTF_KEYUP,0 );
           
           if( ReturnState( szLock[ nElement ] ) )
           {
               nTimes++;
           } 
     }
     
     if( !ReturnState( "Num Lock" ) )
     {
         keybd_event( VK_NUMLOCK,( char )VK_NUMLOCK,0,0 );
         keybd_event( VK_NUMLOCK,( char )VK_NUMLOCK,KEYEVENTF_KEYUP,0 );
     }
     
     system( "cls" );
     
     printf( "How many times this key {%s} is lighted ? => ",szLock[ nElement ] );
     scanf( "%d",&Answer );
         
     printf( 
             Answer == nTimes ?  
            "\n\n\tCongratulations !!!" :  
       "\n\n\t The Correct Answer is : %d times. Good Luck Next Time :)",nTimes 
           );
 
 if( ReturnState( "Caps Lock" ) ) 
     {
         keybd_event( VK_CAPITAL,( char )VK_CAPITAL,0,0 );
         keybd_event( VK_CAPITAL,( char )VK_CAPITAL,KEYEVENTF_KEYUP,0 );
     }
     if( ReturnState( "Scroll Lock") )
     {
         keybd_event( VK_SCROLL,( char )VK_SCROLL,0,0 );
         keybd_event( VK_SCROLL,( char )VK_SCROLL,KEYEVENTF_KEYUP,0 );
     }
     
    PauseScreen( ); 
}
 
Download The Game from this link : click here

Aucun commentaire :

Enregistrer un commentaire