using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace SnakeGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class SnakeGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
public SnakeGame()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
Texture2D snakeHeadTexture;
Rectangle snakeHeadRect;
Texture2D foodTexture;
Rectangle foodRect;
SpriteBatch spriteBatch;
int snakeHeadSize = 40;
int playerLives = 3;
int playerScore = 0;
/// <summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
snakeHeadTexture = content.Load<Texture2D>(@"content\images\SnakeHead");
foodTexture = content.Load<Texture2D>(@"content\images\Apple");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
snakeHeadRect = new Rectangle(
0, 0,
snakeHeadSize, snakeHeadSize);
foodRect = new Rectangle(
50, 50,
snakeHeadSize, snakeHeadSize);
}
// TODO: Load any ResourceManagementMode.Manual content
}
//private bool intersectsWith(Rectangle r1, Rectangle r2)
//{
// return !(r2.Left > r1.Right
// || r2.Right < r1.Left
// || r2.Top > r1.Bottom
// || r2.Bottom < r1.Top
// );
//}
// r2 has a 5px perimeter for collision detection
private bool intersectsWith(Rectangle r1, Rectangle r2)
{
return (r1.X > r2.X - 5
&& r1.X + r1.Width < r2.X + r2.Width + 5
&& r1.Y > r2.Y - 5
&& r1.Y + r1.Height < r2.Y + r2.Height + 5
);
}
static Rectangle makeFood(Rectangle r1, int X, int Y)
{
Random rand = new Random();
r1.X = rand.Next(X - r1.Width);
r1.Y = rand.Next(Y - r1.Height);
return r1;
}
/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Console.WriteLine(this.TargetElapsedTime.ToString());
if(snakeHeadRect.Y < 0
|| snakeHeadRect.X < 0
|| snakeHeadRect.X + snakeHeadSize > Window.ClientBounds.Width
|| snakeHeadRect.Y + snakeHeadSize > Window.ClientBounds.Height
)
{
playerLives -= 1;
snakeHeadRect.X = 0;
snakeHeadRect.Y = 0;
}
if (intersectsWith(snakeHeadRect, foodRect))
{
foodRect = makeFood(foodRect, Window.ClientBounds.Width, Window.ClientBounds.Height);
playerScore += 1;
}
KeyboardState keystate = Keyboard.GetState();
if (keystate.IsKeyDown(Keys.Escape))
{
this.Exit();
}
if (keystate.IsKeyDown(Keys.Up))
{
snakeHeadRect.Y--;
}
if (keystate.IsKeyDown(Keys.Down))
{
snakeHeadRect.Y++;
}
if (keystate.IsKeyDown(Keys.Left))
{
snakeHeadRect.X--;
}
if (keystate.IsKeyDown(Keys.Right))
{
snakeHeadRect.X++;
}
//if (keystate.IsKeyDown(Keys.Up))
//{
// snakeHeadRect.Y -= snakeHeadSize;
//}
//if (keystate.IsKeyDown(Keys.Down))
//{
// snakeHeadRect.Y += snakeHeadSize;
//}
//if (keystate.IsKeyDown(Keys.Left))
//{
// snakeHeadRect.X -= snakeHeadSize;
//}
//if (keystate.IsKeyDown(Keys.Right))
//{
// snakeHeadRect.X += snakeHeadSize;
//}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(foodTexture, foodRect, Color.White);
switch (playerLives)
{
case 3:
spriteBatch.Draw(snakeHeadTexture,snakeHeadRect, Color.White);
break;
case 2:
spriteBatch.Draw(snakeHeadTexture, snakeHeadRect, Color.Orange);
break;
case 1:
spriteBatch.Draw(snakeHeadTexture, snakeHeadRect, Color.Red);
break;
case 0:
this.Exit();
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}