Snakegame:当蛇击中右墙时,我希望它从左墙进入

时间:2017-03-22 12:45:54

标签: c# monogame

这是我的第一个更大的2D图形游戏传统的蛇形游戏,但是当蛇击中右侧(或左侧)时,它应该从另一侧进入相同的X和Y位置。我完全被困在了如何去做的想法 这是我的代码:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;

namespace SnakeJos
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont spritefont;

        Texture2D SnakeHead_texture;
        Texture2D SnakeBody_texture;
        Texture2D SnakeCandy_texture;

        Vector2 SnakeHead_speed;

        //Rectangle SnakeHead_rect;
        Rectangle SnakeBody_rect;
        Rectangle SnakeCandy_rect;
        Rectangle NewHead;
        List<Rectangle> SnakeHead_rect = new List<Rectangle>();


        int life = 1;
        int points = 0;
        int highscore;
        int bodyparts = 0;
        Random CandyRandom;
        StreamWriter sw;
        StreamReader sr;
        int wWidth;
        int wHeight;
        int DirectionState;
        int time;
        int millisecondsPerFrame = 100;
        int timeSinceLastUpdate = 0;
        Random random;



        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()
        {
            // TODO: Add your initialization logic here
            SnakeHead_speed.X = 1f;
            DirectionState = 1;

            wWidth = Window.ClientBounds.Width;
            wHeight = Window.ClientBounds.Height;
            time = 0;
            sr = new StreamReader("Highscore.txt");
            highscore = int.Parse(sr.ReadLine());
            sr.Close();
            CandyRandom = new Random();

            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);
            SnakeHead_texture = Content.Load<Texture2D>("pics/SnakeHead");
            SnakeCandy_texture = Content.Load<Texture2D>("pics/SnakeCandy");
            SnakeBody_texture = Content.Load<Texture2D>("pics/SnakeBody");
            spritefont = Content.Load<SpriteFont>("Font/Font");
            SnakeCandy_rect = new Rectangle(400, 300, SnakeCandy_texture.Width, SnakeCandy_texture.Height);
            SnakeHead_rect.Add(new Rectangle(100, 100, SnakeHead_texture.Width, SnakeHead_texture.Height));
            SnakeHead_rect.Add(new Rectangle(100,80, SnakeBody_texture.Width, SnakeBody_texture.Height));
            SnakeHead_rect.Add(new Rectangle(100, 60, SnakeBody_texture.Width, SnakeBody_texture.Height));
            // TODO: use this.Content to load your game content here
        }

        /// <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)
        {
            //time++;
            //if (time == 20)
            //    time = 0;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Down) && DirectionState != 0)
                DirectionState = 1;
            else if (ks.IsKeyDown(Keys.Up) && DirectionState != 1)
                DirectionState = 0;
            if (ks.IsKeyDown(Keys.Left) && DirectionState != 3)
                DirectionState = 2;
            else if (ks.IsKeyDown(Keys.Right) && DirectionState != 2)
                DirectionState = 3;



            timeSinceLastUpdate += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            timeSinceLastUpdate++;
            if (timeSinceLastUpdate >= millisecondsPerFrame)
            {
                timeSinceLastUpdate = 0;


                int ListLength = SnakeHead_rect.Count;
                SnakeHead_rect.RemoveAt(ListLength-1);



                Rectangle NewHead = SnakeHead_rect[0];

                if (DirectionState == 1)
                    NewHead.Y += 20;
                else if (DirectionState == 0)
                    NewHead.Y -= 20;
                if (DirectionState == 2)
                    NewHead.X -= 20;
                else if (DirectionState == 3)
                    NewHead.X += 20;

                //SnakeHead_rect[0] = NewHead;

                SnakeHead_rect.Insert(0, NewHead);


                if (SnakeHead_rect[0].Intersects(SnakeCandy_rect))
                {
                    int tempWidth = CandyRandom.Next(20, wWidth - SnakeHead_texture.Width);
                    int tempHeight = CandyRandom.Next(20, wHeight - SnakeHead_texture.Height);
                    int widthRest = tempWidth % 20;
                    int HeightRest = tempHeight % 20;
                    SnakeCandy_rect.X = tempWidth - widthRest;
                    SnakeCandy_rect.Y = tempHeight - HeightRest;
                    ListLength = SnakeHead_rect.Count - 1;
                    SnakeHead_rect.Add(new Rectangle(SnakeHead_rect[ListLength].X, SnakeHead_rect[ListLength].Y, SnakeBody_texture.Width, SnakeBody_texture.Height));
                }

                for (int i = 1; i < SnakeHead_rect.Count; i++)
                {
                    if (SnakeHead_rect[0].Intersects(SnakeHead_rect[i]))
                    {
                        Exit();
                    }
                }

            }
            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)
        {
            GraphicsDevice.Clear(Color.Pink);
            spriteBatch.Begin();
            for (int i = 0; i < SnakeHead_rect.Count; i++)
            spriteBatch.Draw(SnakeHead_texture, SnakeHead_rect[i], Color.White);

            spriteBatch.Draw(SnakeCandy_texture, SnakeCandy_rect, Color.White);

            spriteBatch.DrawString(spritefont, "direction" + DirectionState, new Vector2 (10,10), Color.Black);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }

}

1 个答案:

答案 0 :(得分:2)

如果你的蛇撞到墙上,它就不能进入同一个X和Y位置&#34;

如果它碰到右墙,则表示X坐标处于最大值,因此您应将其设置为null并保持Y坐标相同。

如果它撞到左墙,你再次离开Y坐标并将其X坐标从最小值设置为最大值。

如果蛇击中底壁你会有相同的Y值,所以你需要将它设置为最小值。