/// <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) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); spriteBatch.DrawString(gameFont, "Heads: " + score, new Vector2(5, 5), Color.Tomato); spriteBatch.DrawString(gameFont, "Lives: " + lives, new Vector2(Window.ClientBounds.Width - 100, 5), Color.Tomato); if (!playerSnake.isCollideSelf()) { int rectLen = playerSnake.allLength; //String someString = "" + rectLen + ""; //spriteBatch.DrawString(gameFont, someString, new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2), Color.White); } else //RELOAD starting variables { //spriteBatch.DrawString(gameFont, "COOLLLIDDEEEE", new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2), Color.White); lives--; playerSnake.clearLists(this); lastDirection = DIRECTION.RIGHT; for (int i = 0; i < 10; i++) //loads initial length of the snake playerSnake.chomp(this); } if (lives <= 0) { spriteBatch.DrawString(gameFont, "You lost and disgraced your family. \nYou must now perform seppuku \nto redeem yourself", new Vector2(0, Window.ClientBounds.Height / 2), Color.White); } if (lives > 0) { playerSnake.drawMe(spriteBatch); heads.drawCollect(spriteBatch); } spriteBatch.End(); base.Draw(gameTime); }
/// <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) { newState = Keyboard.GetState(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); if (newState.IsKeyDown(Keys.Right)&& lastDirection != DIRECTION.LEFT) //Changed isKeyUp to isKeyDown lastDirection = DIRECTION.RIGHT; //left and right is switched if (newState.IsKeyDown(Keys.Left) && lastDirection != DIRECTION.RIGHT) lastDirection = DIRECTION.LEFT; //left and right is switched if (newState.IsKeyDown(Keys.Up) && lastDirection != DIRECTION.DOWN) lastDirection = DIRECTION.UP; if (newState.IsKeyDown(Keys.Down) && lastDirection != DIRECTION.UP) lastDirection = DIRECTION.DOWN; /* Deleted "old state" checking, which made the game input slower and more unresponsive. * changed Right to isKeyDown instead of isKeyUp * * Added code to reject input if user tries to move towards the opposite direction */ if (oldState.IsKeyDown(Keys.G)) { playerSnake.chomp(this); score++; } oldState = newState; playerSnake.allLength = 0; playerSnake.clientWidth = Window.ClientBounds.Width; playerSnake.clientHeight = Window.ClientBounds.Height; playerSnake.move(lastDirection, speed); if (heads.collideRand(Window.ClientBounds.Width, Window.ClientBounds.Height, playerSnake.headBox)) { score++; playerSnake.chomp(this); } base.Update(gameTime); }
public void move(DIRECTION dir, int speed) { /* * ymultiplier and xmultiplier taken out * Console.Out.WriteLine("Segments: {0}",snakeList.Count); * taken out, console checking not needed. * */ LinkedList<Segment>.Enumerator segIterator = snakeList.GetEnumerator(); headBox = snakeList.First.Value.boundBox(); snakeBoxList.Clear(); int tempx = snakeList.First.Value.locx; int tempy = snakeList.First.Value.locy; if (snakeList.First.Value.locx > clientWidth) //code for wrap around { snakeList.First.Value.locx = 0; } else if (snakeList.First.Value.locx < 0) { snakeList.First.Value.locx = clientWidth; } if (snakeList.First.Value.locy > clientHeight) //code for wrap around { snakeList.First.Value.locy = 0; } else if (snakeList.First.Value.locy < 0) { snakeList.First.Value.locy = clientHeight; } switch (dir) { case DIRECTION.DOWN: snakeList.First.Value.locy += speed; break; case DIRECTION.UP: snakeList.First.Value.locy -= speed; break; case DIRECTION.LEFT: snakeList.First.Value.locx -= speed; break; case DIRECTION.RIGHT: snakeList.First.Value.locx += speed; break; } int loopx, loopy; segIterator.MoveNext(); while (segIterator.MoveNext()) { /* * Iterates movement and move each unit one * square foward. This is how the rest of the * Snake (besides the head) moves. */ loopx = segIterator.Current.locx; loopy = segIterator.Current.locy; segIterator.Current.Move(tempx,tempy); //loops each "current" unit to move one unit ahead tempx = loopx; tempy = loopy; //if(segIterator.Current.boundBox().Intersects(headBox)) snakeBoxList.Add(segIterator.Current.boundBox()); } foreach(Rectangle check in snakeBoxList) { allLength++; } segIterator.Dispose(); //dispose? }