// snake.cs
// csc /t:winexe snake.cs
using
System;
using
System.Drawing;
using
System.Windows.Forms;
// We have a seperate snake structure, which tells us the snakes position
// etc... this is so we could have an array of stSnake's...for multiplayer
// later on if we wanted :)
public
struct stSnake
{
public Rectangle Dir;
// Direction our snake is moving
public int
iLength; // How many squares long it is
public Rectangle[] Body;
// Array of positions of each body part
public int
BodySize; // Size of each body part
public int
iGrowLength;// How much it needs to grow by
}//
End stSnake
public
struct stGameData
{
public int
iNumFoods;
public Rectangle[] Food;
public bool[]
bFoodAlive;
public int
iFoodSize;
}//
End stGameData
class
CSnakeGame : System.Windows.Forms.Form
{
private stSnake m_Snake;
// Snake data
private stGameData m_Data;
// Game Information
private System.Windows.Forms.Timer
Ticker; // Our game timer
private void OnPaint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
....... cut out to keep down on size
// --- Draw Food --- //
int iNumFoods = m_Data.iNumFoods;
for(int
i=0; i<iNumFoods; i++)
{
if( m_Data.bFoodAlive[i] )
{
g.FillRectangle(System.Drawing.Brushes.Blue,
m_Data.Food[i]);
}// End if
}// End for loop i
}//
End of OnPaint(..)
void MoveBody() [...]
bool IntersectsWithBody(Rectangle rCheck)
{
// Check if our Rectangle overlaps any
of our body parts
bool bOnTop =
false;
int BodyLength=m_Snake.iLength;
for(int
c=0; c< BodyLength; c++)
{
if(
m_Snake.Body[c].IntersectsWith( rCheck ) )
{
bOnTop = true;
break;
}// End if
}// End for loop c
return bOnTop;
}//
End of IntersectsWithBody(..)
int m_FoodTimer = 0;
void FoodUpdate()
{
// First part, check if we've eaten
any food
int iNumFoods = m_Data.iNumFoods;
for(int
i=0; i<iNumFoods; i++)
{
if(
m_Snake.Body[0].IntersectsWith( m_Data.Food[i] ) &&
m_Data.bFoodAlive[i] )
{
// If we are here, then
we've grabbed some food! ..so
// we should do
something..increase the score...change
// speed..grow etc..
Grow(1);
m_Data.bFoodAlive[i] =
false;
}// End if
}// End for loop i
// Second part, only add food if we
have move 10 places
// This is hard coded as 10 here...but
you could put it in the
// stGameData structure later on, so
its easier to change.
m_FoodTimer++;
if( m_FoodTimer < 10 )
return;
m_FoodTimer=0;
// If our snakes head is outside the
screen, then we scroll it
// to the opposite side
Rectangle r = this.ClientRectangle;
// Initilise our Random number
variables
long Seed = DateTime.Now.Ticks;
Random TheRandom = new Random( (int)Seed);
// Note how I've added the food size,
so we don't get food which is touching
// the wall...padding :)
int randX =
TheRandom.Next(0+m_Data.iFoodSize, r.Width-m_Data.iFoodSize);
int randY =
TheRandom.Next(0+m_Data.iFoodSize, r.Height-m_Data.iFoodSize);
for(int
i=0; i<iNumFoods; i++)
{
// Generate a random position for our
food
Rectangle newFood = new
Rectangle();
newFood.X = randX;
newFood.Y = randY;
newFood.Width = newFood.Height = m_Data.iFoodSize;
// Check that where not putting
the food on top of the snake
// Check that we have space in
our array for it..as we are only allowed
// a maximum number of foods at
once on the screen
if( !IntersectsWithBody(newFood)
&& !m_Data.bFoodAlive[i] )
{
m_Data.Food[i] = newFood;
m_Data.bFoodAlive[i]= true;
break;
}// End if
}// End for loop i
}//
End of FoodUpdate()
void Grow(int
iGrowLength)
[...]
private void AKeyDown(object sender,
System.Windows.Forms.KeyEventArgs e) [...]
private void OnTimer(object sender,
System.EventArgs e)
{
MoveBody(); // Update our snakes new
position
Invalidate(); // Invalidate our screen
so it gets redrawn
FoodUpdate(); // Add food randomly
positions every 5 moves, and check for eaten
}//
End of OnTimer(..)
private void InitializeComponent()
{
.........cut out to keep size down
// How much our
snake has to grow by
m_Snake.iGrowLength = 0;
// Game Data
m_Data.iNumFoods = 3; // Maximum
Number Of Foods At Once On The Screen.
m_Data.iFoodSize = 10;
m_Data.Food = new Rectangle[10];
// Assume Max foods possible of 10
m_Data.bFoodAlive = new
bool[10];// Hence
max bFoodAlives is 10
}//
End of InitializeComponent()
public CSnakeGame() [...]
// Our program entry point.
static void
Main() [...]
}
|