2017-03-22 7 views
0

これは私の最初の大きな2Dグラフィックスゲームですが、スネークが右(または左)に当たったとき同じ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がある場合は同じことが起こりますので、最小値に設定する必要があります。

関連する問題