Gå till innehållet

Hela programmet - Träffa Ormen

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

MouseState mus = Mouse.GetState();
MouseState gammalMus = Mouse.GetState();
SpriteFont arial;

Random slump = new Random();

// 0 för meny, 1 för spel
int scen = 0;

// Menyvariabler
Texture2D knappBild;
Rectangle knappRect;
string välkomstText = "Träffa ormen!";
Vector2 välkomstPosition;

// Spelvariabler
List<Rectangle> ormar = new List<Rectangle>();
Texture2D ormBild;
int startAntalOrmar = 5;
int updatesMellanNyaOrmar = 90;
int updatesTillNästaOrm = 90;

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
}

/// <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()
{
    IsMouseVisible = true;

    base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    arial = Content.Load<SpriteFont>("arial");
    ormBild = Content.Load<Texture2D>("snake");
    knappBild = Content.Load<Texture2D>("button");
    knappRect = new Rectangle(400 - knappBild.Width / 2, 360, knappBild.Width, knappBild.Height);
    välkomstPosition = new Vector2(400 - arial.MeasureString(välkomstText).X / 2, 100);
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
    // TODO: Unload any non ContentManager content here
}

/// <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)
{
    gammalMus = mus;
    mus = Mouse.GetState();

    switch (scen)
    {
        case 0:
            UppdateraMeny();
            break;
        case 1:
            UppdateraSpel();
            break;
    }

    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)
{

    switch (scen)
    {
        case 0:
            RitaMeny();
            break;
        case 1:
            RitaSpel();
            break;
    }

    base.Draw(gameTime);
}

/// <summary>
/// Undersöker om den vänstra musknappen precis blev nedtryckt
/// </summary>
/// <returns></returns>
bool VänsterMusTryckt()
{
    if (mus.LeftButton == ButtonState.Pressed && gammalMus.LeftButton == ButtonState.Released)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/// <summary>
/// Byter scen till det scen-ID som anges
/// </summary>
/// <param name="nyscen"></param>
void BytScen(int nyscen)
{
    scen = nyscen;

    if (nyscen == 1)
    {
        ormar.Clear();
        for (int i = 0; i < startAntalOrmar; i++)
        {
            LäggTillOrm();
        }
    }
    updatesTillNästaOrm = updatesMellanNyaOrmar;
}

/// <summary>
/// Lägger till en ny orm på en slumpad position
/// </summary>
void LäggTillOrm()
{
    int nyOrmX = slump.Next(0, 800 - ormBild.Width);
    int nyOrmY = slump.Next(0, 480 - ormBild.Height);
    Rectangle nyOrmRect = new Rectangle(nyOrmX, nyOrmY, ormBild.Width, ormBild.Height);
    ormar.Add(nyOrmRect);
}

/// <summary>
/// Update-kod för scenen Meny
/// </summary>
void UppdateraMeny()
{
    if (VänsterMusTryckt() && knappRect.Contains(mus.Position))
    {
        BytScen(1);
    }
}

/// <summary>
/// Update-kod för scenen spel
/// </summary>
void UppdateraSpel()
{
    updatesTillNästaOrm--;
    if (updatesTillNästaOrm <= 0)
    {
        LäggTillOrm();
        updatesTillNästaOrm = updatesMellanNyaOrmar;
    }

    if (VänsterMusTryckt())
    {
        for (int i = ormar.Count - 1; i >= 0; i--)
        {
            if (ormar[i].Contains(mus.Position))
            {
                ormar.RemoveAt(i);
                break;
            }
        }
    }

    if (ormar.Count == 0)
    {
        BytScen(0);
    }
}

/// <summary>
/// Kod för att rita scenen Meny
/// </summary>
void RitaMeny()
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    spriteBatch.DrawString(arial, välkomstText, välkomstPosition, Color.White);
    spriteBatch.Draw(knappBild, knappRect, Color.White);
    spriteBatch.End();
}

/// <summary>
/// Kod för att rita scenen Spel
/// </summary>
void RitaSpel()
{
    GraphicsDevice.Clear(Color.Tomato);

    spriteBatch.Begin();
    foreach (Rectangle ormRect in ormar)
    {
        spriteBatch.Draw(ormBild, ormRect, Color.White);
    }
    spriteBatch.End();
}

Kommentarer