using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; using SnakeGame.Common.Objects; using SnakeGame.Common.Interfaces; namespace Snake360 { public class Game360 : Microsoft.Xna.Framework.Game, IGame { Matrix viewMatrix; Matrix projectionMatrix; BasicEffect greenEffect; BasicEffect redEffect; BasicEffect blueEffect; VertexDeclaration vertexDeclaration; VertexPositionNormalTexture[] snakeBlocks; VertexPositionNormalTexture[] appleBlocks; VertexPositionNormalTexture[] wallBlocks; VertexBuffer vertexBuffer; int gridWidth = 32; int gridHeight = 19; int old = 0; GraphicsDeviceManager graphics; ContentManager content; GenericGame genericGame; public Game360() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); genericGame = new GenericGame(gridWidth, gridHeight); genericGame.passThru(this); } protected override void Initialize() { base.Initialize(); } protected override void LoadGraphicsContent(bool loadAllContent) { InitializeTransform(); if (loadAllContent) { InitializeEffect(); //InitializePointList(); } } private void InitializeTransform() { viewMatrix = Matrix.CreateLookAt( new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up ); projectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f); } private void InitializeEffect() { vertexDeclaration = new VertexDeclaration( graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements); greenEffect = new BasicEffect(graphics.GraphicsDevice, null); greenEffect.DiffuseColor = new Vector3(0, 1.0f, 0); greenEffect.View = viewMatrix; greenEffect.Projection = projectionMatrix; redEffect = new BasicEffect(graphics.GraphicsDevice, null); redEffect.DiffuseColor = new Vector3(1.0f, 0, 0); redEffect.View = viewMatrix; redEffect.Projection = projectionMatrix; blueEffect = new BasicEffect(graphics.GraphicsDevice, null); blueEffect.DiffuseColor = new Vector3(0, 0, 1.0f); blueEffect.View = viewMatrix; blueEffect.Projection = projectionMatrix; } VertexPositionNormalTexture createBlock(float col, float row) { float cols = gridWidth; float rows = gridHeight; return new VertexPositionNormalTexture( new Vector3( 0.0f - ((cols / 2) - col) / 6.3f, 0.0f + ((rows / 2) - row) / 6.0f, 0.0f), Vector3.Forward, new Vector2()); } private void InitializePointList() { vertexDeclaration = new VertexDeclaration( graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements); snakeBlocks = new VertexPositionNormalTexture[gridWidth * gridHeight]; appleBlocks = new VertexPositionNormalTexture[gridWidth * gridHeight]; wallBlocks = new VertexPositionNormalTexture[gridWidth * gridHeight]; for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { switch(genericGame.getStatus(i, j)) { case LocationStatus.EMPTY: clearBlock(snakeBlocks,i,j); clearBlock(appleBlocks, i, j); clearBlock(wallBlocks, i, j); break; case LocationStatus.APPLE: setBlock(appleBlocks,i,j); clearBlock(snakeBlocks,i,j); clearBlock(wallBlocks,i,j); break; case LocationStatus.SNAKE: setBlock(snakeBlocks,i,j); clearBlock(appleBlocks,i,j); clearBlock(wallBlocks,i,j); break; case LocationStatus.WALL: setBlock(wallBlocks,i,j); clearBlock(appleBlocks,i,j); clearBlock(snakeBlocks,i,j); break; } } } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } } 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(); CheckInput(); int b = 0; if ((b = gameTime.ElapsedGameTime.Milliseconds) + old > 100) { old = b + old - 100; genericGame.step(); if (genericGame.gameover) { genericGame = new GenericGame(gridWidth, gridHeight); genericGame.init(); } } else old+=b; base.Update(gameTime); } private void CheckInput() { KeyboardState newState = Keyboard.GetState(); Microsoft.Xna.Framework.Input.GamePadState padstate = Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.One); if ((padstate.DPad.Up == ButtonState.Pressed)||(padstate.ThumbSticks.Left.Y>0.9f)) { genericGame.up(); } if ((padstate.DPad.Left == ButtonState.Pressed)||(padstate.ThumbSticks.Left.X<-0.9f)) { genericGame.left(); } if ((padstate.DPad.Right == ButtonState.Pressed)||(padstate.ThumbSticks.Left.X>0.9f)) { genericGame.right(); } if ((padstate.DPad.Down == ButtonState.Pressed)||(padstate.ThumbSticks.Left.Y<-0.9f)) { genericGame.down(); } /* if (newState.IsKeyDown(Keys.NumPad0)) { typeToDraw = PrimitiveType.PointList; } */ } protected override void Draw(GameTime gameTime) { InitializePointList(); graphics.GraphicsDevice.Clear(Color.Black); graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration; // effect is a compiled effect created and compiled elsewhere // in the application greenEffect.Begin(); foreach (EffectPass pass in greenEffect.CurrentTechnique.Passes) { pass.Begin(); DrawBlocks(snakeBlocks); pass.End(); } greenEffect.End(); blueEffect.Begin(); foreach (EffectPass pass in blueEffect.CurrentTechnique.Passes) { pass.Begin(); DrawBlocks(wallBlocks); pass.End(); } blueEffect.End(); redEffect.Begin(); foreach (EffectPass pass in redEffect.CurrentTechnique.Passes) { pass.Begin(); DrawBlocks(appleBlocks); pass.End(); } redEffect.End(); base.Draw(gameTime); } private void DrawBlocks(VertexPositionNormalTexture[] list) { // Initialize the vertex buffer, allocating memory for each vertex vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionNormalTexture.SizeInBytes * (list.Length), ResourceUsage.None, ResourceManagementMode.Automatic); // Set the vertex buffer data to the array of vertices vertexBuffer.SetData(list); graphics.GraphicsDevice.RenderState.PointSize = 20; graphics.GraphicsDevice.Vertices[0].SetSource( vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes); graphics.GraphicsDevice.DrawPrimitives( PrimitiveType.PointList, 0, // index of the first vertex to draw list.Length // number of primitives ); } public void add(int x, int y) { genericGame.add(x, y); //setBlock(x, y); } public void remove(int x, int y) { genericGame.remove(x, y); //clearBlock(x, y); } public LocationStatus getStatus(int x, int y) { return genericGame.getStatus(x, y); } public void left() { genericGame.left(); } public void right() { genericGame.right(); } public void up() { genericGame.up(); } public void down() { genericGame.down(); } public void step() { genericGame.step(); } public void eat() { genericGame.eat(); } public void setBlock(VertexPositionNormalTexture[] blocks, int x, int y) { if (blocks == null) return; blocks[y * gridWidth + x] = createBlock(x, y); } public void clearBlock(VertexPositionNormalTexture[] blocks, int x, int y) { if (blocks == null) return; blocks[y * gridWidth + x] = createBlock(999,999); } } }