2017-03-22 51 views
0

這是我第一個比較傳統的蛇形遊戲的第一個更大的二維圖形遊戲,但是當蛇擊中右邊(或左邊)時,它應該從另一個在相同的X和Y位置。我完全被卡住,沒了主意關於如何做到這一點 這是我的代碼:Snakegame:當蛇擊中右牆我希望它從左牆進入

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); 
     } 
    } 

} 
+0

而不是傾銷所有的程序源代碼,你應該清楚地描述你有什麼問題(錯誤或意外的結果),並提供[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)它再現了你的問題 –

+1

'它應該從另一側進入相同的X和Y位置。這在數學上是不可能的。 – NotTelling

回答

2

如果你的蛇撞牆它不能「在相同的X和Y位置」輸入。

如果它碰到右邊的牆,意味着X座標處於最大值,所以您應該將其設置爲空,並保持Y座標相同。

如果碰到左側牆,則再次保留Y座標並將其X座標從最小值設置爲最大值。

同樣的情況,如果蛇擊中底部牆壁,你有最大的Y,所以你需要將它設置爲最小值。