|
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace DcamScreenSaver
{
abstract class Worm
{
#region constants & variables
// static variables
protected static Size __szScr = Globals.ScreenSize;
protected static int __iMaxWormLen = 40;
protected static int __iWormSize = 9;
protected static int __iWormStepMin = 1, __iWormStepMax = 5;
protected static Bitmap __bmpBlack = null;
// variables for each instance
protected Bitmap _bmpElement = new Bitmap( __iWormSize, __iWormSize );
protected Point[] _pntArrElements = null;
#endregion // constants & variables
#region constructors & destructors
static Worm()
{
// the black "eaten" element of a worm
__bmpBlack = new Bitmap( __iWormSize, __iWormSize );
__bmpBlack.MakeTransparent( Color.White );
Graphics g = Graphics.FromImage( __bmpBlack );
g.FillEllipse( Brushes.Black, 0, 0, __bmpBlack.Width - 1, __bmpBlack.Height - 1 );
g.Dispose();
}
public Worm( Point pntXStart )
{
// instantiate a random number of elements and set all start position to the passed one
_pntArrElements = new Point[ Globals.Rand.Next( 5, __iMaxWormLen ) ];
for( int i = 0; i < _pntArrElements.Length; i++ )
{
_pntArrElements[ i ] = pntXStart;
}
// create the bitmap for the elements of this worm with a random color,
// but not black ("eaten" background) or white (transparent)
Color col = Color.FromArgb( Globals.Rand.Next( 1, 255 ),
Globals.Rand.Next( 1, 255 ), Globals.Rand.Next( 1, 255 ) );
_bmpElement = new Bitmap( __iWormSize, __iWormSize );
_bmpElement.MakeTransparent( Color.White );
Graphics g = Graphics.FromImage( _bmpElement );
SolidBrush br = new SolidBrush( col );
g.FillEllipse( br, 0, 0, __iWormSize - 1, __iWormSize - 1 );
g.DrawEllipse( Pens.Black, 0, 0, __iWormSize - 1, __iWormSize - 1 );
br.Dispose();
g.Dispose();
}
public virtual void Dispose()
{
if( _bmpElement != null ) _bmpElement.Dispose();
}
#endregion // constructors & destructors
#region methods
public abstract void CalcNewPosition();
public void Move()
{
// draw the last element in black
Globals.Graph.DrawImage( __bmpBlack, _pntArrElements[ _pntArrElements.Length - 1 ].X,
_pntArrElements[ _pntArrElements.Length - 1 ].Y );
// move all element positions one towards the head of the worm (beginning of the array)
for( int i = _pntArrElements.Length - 1; i > 0; i-- )
{
_pntArrElements[ i ] = _pntArrElements[ i - 1 ];
}
// calculate a new position for the head of the worm (beginning of the array) and draw it
CalcNewPosition();
Globals.Graph.DrawImage( _bmpElement, _pntArrElements[ 0 ].X, _pntArrElements[ 0 ].Y );
}
#endregion // methods
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.