|
DirectInput
- The Walking Bear III
(Continued from last week
)
public bool GetInputState ( ref ImagePosition
imgP )
{
KeyboardState state = null ;
Point p = new Point ( 0 ) ;
bool continu ;
try
{ state = localDevice.GetCurrentKeyboardState( ) ;
}
catch ( InputException )
{ do
{ continu = false ;
Application.DoEvents( ) ;
try
{ localDevice.Acquire( ) ;
}
catch ( InputLostException e2 )
{ continu = true ;
}
catch ( OtherApplicationHasPriorityException e3 )
{ continu = true ;
}
if ( ! owner.Created )
break ;
} while ( continu ) ;
}
if ( null == state )
return true ;
if ( state [ Key.Left ] )
{ imgP.bearImgNo++ ;
if ( imgP.bearImgNo >= 12 )
imgP.bearImgNo = 0 ;
if ( imgP.bearImgNo % 3 == 0 )
{ imgP.x++ ;
if ( imgP.x >= 800 )
{ imgP.x = 0 ;
imgP.c = ( imgP.c == 0 ? -800 : 0 ) ;
imgP.c1 = ( imgP.c1 == -800 ? 0 : -800 ) ;
}
imgP.y += 2 ;
if ( imgP.y == 1200 )
{ imgP.y = 0 ;
imgP.b = ( imgP.b == 0 ? -1200 : 0 ) ;
imgP.b1 = ( imgP.b1 == -1200 ? 0 : -1200 ) ;
}
imgP.z += 3 ;
if ( imgP.z >= 1200 )
{
imgP.z = 0 ;
imgP.f = ( imgP.f == 0 ? -1200 : 0 ) ;
imgP.f1 = ( imgP.f1 == -1200 ? 0 : -1200 ) ;
}
}
}
if ( state [ Key.Escape ] )
return false ;
return true ;
}
Note the parameter passed to GetInputState( ) method is of
ref type. This is because, we are going to change the values of ImagePosition
structure elements in this method and use them in the RenderGraphics( ) method.
After the initial error checking, we have checked whether the method is called
because the left key is pressed. If the condition is true, we have incremented
the bear image number so that the next time RenderGraphics( ) is called, the
next image of the bear would get displayed. If the image number equals 12, we
have re-initialised it to 0. After every three movements of the bear, we have
changed the position of the cloud and tree images. For this, we have incremented
the x, y and z variables of the structure.
Variables c and c1 are used for cloud2 and cloud1 and both initialised to 0
and 800 respectively. So initially, the cloud2 with coordinate 0 would
be visible. The cloud1 will be displayed from 800 and cloud2 from 0. Then, x
will vary from 0 to 800, and as a result both clouds would shift and slowly
cloud1 becomes fully visible. A similar procedure occurs for trees. The variables
b and b1 are used for treeback images. b is initialised with 0 and b1 with 1200.
Similarly, f and f1 are used for treefront images; f is initialised with 0 and
f1 with 1200.
If the user hits the Escape key, we have returned false to close the window.
The StartLoop( ) method checks in a loop whether the form is still alive using
the Created property. The if condition checks the return value of GetInputState(
) and disposes the form when false is returned.
private void StartLoop( )
{
while ( Created )
{ graphics.RenderGraphics ( destination ) ;
Application.DoEvents( ) ;
if ( !input.GetInputState ( ref graphics.imgPos
) )
Dispose( ) ;
}
}
- Change the following properties of the form.
- WindowStyle: Set it to Maximized
- MaximizeBox: Set it to False
- KeyPreview: Set it to True
- FormBorderStyle: Set it to None
Copy all the images in a folder named images
and copy the images folder in bin\debug
folder. Run the program. You should get the screen as shown below.
 |
Yashavant Kanetkar, one of
the first Express Computer columnists, is an established software
expert, speaker and author with several best-sellers to his
credit, including titles like “Let Us C” and the “Fundas” series.
Contact him at kanetkar@dcubesoft.com |
|