using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace SnakeIt { class Food { // Texture representing the food public Texture2D Texture; // Position of the food relative to the upper left side of the screen public Vector2 Position; // Get the width of the food public int Width { get { return Texture.Width; } } // Get the height of the food public int Height { get { return Texture.Height; } } // Set Food Texture and Position public void Initialize(Texture2D texture, Vector2 position) { Texture = texture; Position = position; } // Draw Food to the Screen public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(Texture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } } }
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic;
// public Texture2D Texture; // public Vector2 Position; // public List<Tile> snakeTiles; // public float speed; const float speedIncrement = 0.2f; // // 1==, 2==, 3==, 4== public int direction; // -> public bool newTile;
// public void Initialize(Texture2D texture, Vector2 position) { Position = position; Texture = texture; Reset(); } // ( ) public void Reset() { speed = 5f; direction = 1; Tile tile = new Tile(Texture, Position); snakeTiles = new List<Tile>(); snakeTiles.Add(tile); newTile = false; }
public void Update() { // Vector2 headPosition = snakeTiles[0].Position; // if (newTile) { // Tile tile = new Tile(Texture, headPosition); // SnakeTiles ( HeadPosition, ) snakeTiles.Insert(1, tile); } // , "" else if (snakeTiles.Count > 1) { Tile last = snakeTiles[snakeTiles.Count - 1]; last.Position = headPosition; snakeTiles.RemoveAt(snakeTiles.Count-1); snakeTiles.Insert(0, last); } // if (newTile) { speed += speedIncrement; newTile = false; } // "" switch (direction) { case 1: snakeTiles[0].Position.X -= speed; break; case 2: snakeTiles[0].Position.Y -= speed; break; case 3: snakeTiles[0].Position.X += speed; break; case 4: snakeTiles[0].Position.Y += speed; break; } }
public void Draw(SpriteBatch spriteBatch) { for (int i = 0; i < snakeTiles.Count; i++) { spriteBatch.Draw(snakeTiles[i].Texture, snakeTiles[i].Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } }
class Tile { public Vector2 Position; public Texture2D Texture; public Tile(Texture2D texture, Vector2 position) { Position = position; Texture = texture; } // public int Width { get { return Texture.Width; } } // public int Height { get { return Texture.Height; } } }
using System; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input.Touch;
// private GraphicsDeviceManager graphics; // private SpriteBatch spriteBatch; // private Snake snake; // private Food food; // private SpriteFont segoe20; // private Vector2 scorePosition; // private float score; private const float FOOD_POINTS = 20; // GameMode Mode; // enum GameMode {Menu, Running, Complete}
public Game1() { // graphics = new GraphicsDeviceManager(this); // Content.RootDirectory = "Content"; // 30 Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); // . InactiveSleepTime = TimeSpan.FromSeconds(1); // Tap Flick TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Flick; }
protected override void Initialize() { // Menu, Menu Draw() Mode = GameMode.Menu; // snake = new Snake(); food = new Food(); // 0 score = 0f; base.Initialize(); }
protected override void LoadContent() { // SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice); // Vector2 snakePosition = new Vector2( GraphicsDevice.Viewport.TitleSafeArea.X + GraphicsDevice.Viewport.TitleSafeArea.Width / 2, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2); snake.Initialize(Content.Load<Texture2D>("SnakeTile"), snakePosition); // Vector2 foodPosition = RandPosition(); food.Initialize(Content.Load<Texture2D>("FoodTile"), foodPosition); // segoe20 = this.Content.Load<SpriteFont>("Segoe20"); // scorePosition = new Vector2(20, 20); }
protected override void Update(GameTime gameTime) { // if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); if (Mode == GameMode.Running) { snake.Update(); CheckCollision(); } CheckGestures(); base.Update(gameTime); }
private void CheckCollision() { // int snakeHeadX = (int)snake.snakeTiles[0].Position.X; int snakeHeadY = (int)snake.snakeTiles[0].Position.Y; Rectangle rectangle1 = new Rectangle(snakeHeadX, snakeHeadY, snake.snakeTiles[0].Width, snake.snakeTiles[0].Height); Rectangle rectangle2 = new Rectangle((int)food.Position.X, (int)food.Position.Y, food.Width, food.Height); // if (rectangle1.Intersects(rectangle2)) { // score += FOOD_POINTS; // food.Position = RandPosition(); // snake.newTile = true; } // if (snakeHeadX < 0 || snakeHeadY < 0 || snakeHeadX + snake.snakeTiles[0].Width > GraphicsDevice.Viewport.Width || snakeHeadY + snake.snakeTiles[0].Height > GraphicsDevice.Viewport.Height) { Mode = GameMode.Complete; } }
private Vector2 RandPosition() { // Random random = new Random(); Vector2 position = new Vector2( GraphicsDevice.Viewport.TitleSafeArea.X + random.Next(GraphicsDevice.Viewport.TitleSafeArea.Width - 45) + 20, GraphicsDevice.Viewport.TitleSafeArea.Y + random.Next(GraphicsDevice.Viewport.TitleSafeArea.Height - 45) + 20); return position; }
private void CheckGestures() { // ? while (TouchPanel.IsGestureAvailable) { // GestureSample gesture = TouchPanel.ReadGesture(); // Flick Tap? switch (gesture.GestureType) { case GestureType.Flick: // ? Single x = gesture.Delta.X, y = gesture.Delta.Y; // ? if (Math.Abs(x) > Math.Abs(y)) { // left or right if (x < 0) { if (snake.direction == 3 && snake.snakeTiles.Count() > 1) Mode = GameMode.Complete; else snake.direction = 1; // } else { if (snake.direction == 1 && snake.snakeTiles.Count() > 1) Mode = GameMode.Complete; else snake.direction = 3; // } } else { // ? if (y < 0) { if (snake.direction == 4 && snake.snakeTiles.Count() > 1) Mode = GameMode.Complete; else snake.direction = 2; // } else { if (snake.direction == 2 && snake.snakeTiles.Count() > 1) Mode = GameMode.Complete; else snake.direction = 4; // } } break; case GestureType.Tap: // Menu Running if (Mode == GameMode.Menu) { Mode = GameMode.Running; } // Complete Running else if (Mode == GameMode.Complete) { snake.Reset(); score = 0f; Mode = GameMode.Running; } break; } } }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // spriteBatch.Begin(); if (Mode == GameMode.Menu) { string GameStart = "FreeSnake\nTap the Screen to Start"; Vector2 stringSize = segoe20.MeasureString(GameStart); spriteBatch.DrawString(segoe20, GameStart, new Vector2((GraphicsDevice.Viewport.Width - stringSize.X) / 2, (GraphicsDevice.Viewport.Height - stringSize.Y) / 2), Color.White); } else if (Mode == GameMode.Running) { // snake.Draw(spriteBatch); // food.Draw(spriteBatch); // spriteBatch.DrawString(segoe20, "Score:"+score, scorePosition, Color.White); } else if (Mode == GameMode.Complete) { snake.Draw(spriteBatch); string GameOver = "Game Over\nScore : " + score +"\nTap the Screen to Restart"; Vector2 stringSize = segoe20.MeasureString(GameOver); spriteBatch.DrawString(segoe20, GameOver, new Vector2((GraphicsDevice.Viewport.Width - stringSize.X) / 2, (GraphicsDevice.Viewport.Height - stringSize.Y) / 2), Color.White); } // spriteBatch.End(); base.Draw(gameTime); }
Source: https://habr.com/ru/post/141943/
All Articles