using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace XNASnake
{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState oldState = Keyboard.GetState();
Snake.WhereToGo keyState;
int time;
Apple apple;
private int appleTime;
private bool drawApple;
Character Tail;
private Snake Head;
private int ileKwadratow;
SpriteFont sf;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 300;
graphics.PreferredBackBufferWidth = 300;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
ileKwadratow = 0;
Tail = new Character();
time = 0;
spriteBatch = new SpriteBatch(GraphicsDevice);
apple = new Apple();
apple.texture = Content.Load("Snake/Apple");
Head = new Snake(Content.Load("Snake/SnakeRectangle"));
Tail.wezykList.Add(Head);
sf = Content.Load("SpriteFont1");
Head.position = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
Head.velocity = new Vector2(5, 0);
}
protected override void UnloadContent()
{
}
public void SnakeUpdate()
{
Head.position += Head.velocity;
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyAdapter();
CheckApple();
time += gameTime.ElapsedGameTime.Milliseconds;
appleTime += gameTime.ElapsedGameTime.Milliseconds;
SnakeUpdate();
foreach (Snake s in Tail.wezykList)
{
s.position = Head.position;
}
if (appleTime >= 6000 || apple.isDead == true)
{
drawApple = true;
apple.X = new Random().Next(0, graphics.PreferredBackBufferWidth - apple.texture.Width);
apple.Y = 200;
appleTime -= 6000;
apple.isDead = false;
}
base.Update(gameTime);
}
public void KeyAdapter()
{
KeyboardState currentState = Keyboard.GetState();
if (currentState.IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up))
{
keyState = Snake.WhereToGo.goUP;
Head.velocity = new Vector2(0, -3);
}
if (currentState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down))
{
keyState = Snake.WhereToGo.goDOWN;
Head.velocity = new Vector2(0, 3);
}
if (currentState.IsKeyDown(Keys.Left) && oldState.IsKeyUp(Keys.Left))
{
keyState = Snake.WhereToGo.goLEFT;
Head.velocity = new Vector2(-3, 0);
}
if (currentState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right))
{
keyState = Snake.WhereToGo.goRIGHT;
Head.velocity = new Vector2(3, 0);
}
}
public void CheckApple()
{
if (apple.Rectanglee().Intersects(Tail.wezykList[0].Rectanglee()))
{
Snake dodatek = new Snake(Content.Load("Snake/SnakeRectangle"));
if (keyState == Snake.WhereToGo.goDOWN)
{
dodatek.position.X = Tail.wezykList[ileKwadratow].position.X;
dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y - 10;
}
if (keyState == Snake.WhereToGo.goUP)
{
dodatek.position.X = Tail.wezykList[ileKwadratow].position.X;
dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y + 10;
}
if (keyState == Snake.WhereToGo.goLEFT)
{
dodatek.position.X = Tail.wezykList[ileKwadratow].position.X + 10;
dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y;
}
if (keyState == Snake.WhereToGo.goRIGHT)
{
dodatek.position.X = Tail.wezykList[ileKwadratow].position.X - 10;
dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y;
}
apple.isDead = true;
ileKwadratow++;
Tail.wezykList.Add(dodatek);
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
//spriteBatch.Draw(sticzeq.wezykList[0].texture, sticzeq.wezykList[0].Rectanglee(), Color.Aqua);
// rysowanie OK
foreach (Snake s in Tail.wezykList)
{
spriteBatch.Draw(s.texture, s.Rectanglee(), Color.Aqua);
}
if (drawApple == true)
{
spriteBatch.Draw(apple.texture, apple.Rectanglee(), Color.Red);
}
spriteBatch.DrawString(sf,Convert.ToString(keyState),new Vector2(100,100),Color.Black);
spriteBatch.DrawString(sf, Convert.ToString(Tail.wezykList.Count), new Vector2(100, 130), Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
}
}