2016-06-10 53 views
1

我正在嘗試製作一個簡單的乒乓遊戲,我有兩個我遇到過的問題,而且似乎無法弄清楚。Monogame - 乒乓遊戲 - 限制鼠標區域

對於乒乓球遊戲,我希望玩家的槳板保持在Y的一半或更低範圍內,這意味着他可以在槳距的一半範圍內移動槳板。

我已經設法讓槳停在中間,但是它在擊中中間後出現。是否有可能保持在Y軸的一半而不消失?

而另一個問題是玩家的槳和區域似乎在上半部分,而不是下半部分。

Game1.cs

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using System; 
using System.IO; 

namespace Xong 
{ 
    /// <summary> 
    /// This is the main type for your game. 
    /// </summary> 
    public class Game1 : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Texture2D redPaddle, bluePaddle, ball, blueBall, greyBall, bg1, bg2, bg3, bg4, elementBlue, elementRed; 

     int BluePaddleX = 104; 
     int BluePaddleY = 22; 

     int RedPaddleX = 104; 
     int RedPaddleY = 22; 

     int BallX = 22; 
     int BallY = 22; 


     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 

      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(graphics.GraphicsDevice); 

      redPaddle = Content.Load<Texture2D>("paddleRed.png"); 

      bluePaddle = Content.Load<Texture2D>("paddleBlu.png"); 

      blueBall = Content.Load<Texture2D>("ballBlue.png"); 

      greyBall = Content.Load<Texture2D>("ballGrey.png"); 

      elementBlue = Content.Load<Texture2D>("element_blue_square_glossy.png"); 

      elementRed = Content.Load<Texture2D>("element_red_square_glossy.png"); 


      bg1 = Content.Load<Texture2D>("set1_background.png"); 

      bg2 = Content.Load<Texture2D>("set2_background.png"); 

      bg3 = Content.Load<Texture2D>("set3_background.png"); 

      bg4 = Content.Load<Texture2D>("set4_background.png"); 




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

      // TODO: Add your update logic here 

      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.CornflowerBlue); 
      spriteBatch.Begin(); 

      MouseState mouseState = Mouse.GetState(); 


      int ScreenX = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; 
      int ScreenY = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; 

      int PlayerBoundaryX = (ScreenX/2) - (BluePaddleX/2); 
      int PlayerBoundaryY = (ScreenY/2) - (BluePaddleY/2); 

      spriteBatch.Draw(bg1, new Rectangle(0, 0, ScreenX, ScreenY), Color.White); 
      spriteBatch.Draw(redPaddle, new Vector2(ScreenX, ScreenY), Color.White); 

      if ((mouseState.Y < (PlayerBoundaryY/2)) && (mouseState.Y > 0)) 
      { 
       spriteBatch.Draw(bluePaddle, new Vector2(mouseState.X, mouseState.Y), Color.White); 
      } 
      else 
      { 
       spriteBatch.Draw(bluePaddle, new Vector2(mouseState.X, PlayerBoundaryY), Color.White); 
      } 



      spriteBatch.Draw(greyBall, new Vector2(405, 240), Color.White); 
      spriteBatch.End(); 
      // TODO: Add your drawing code here 

      base.Draw(gameTime); 
     } 





    } 
} 

enter image description here

+0

我沒有看到您的鼠標代碼,但您需要監視鼠標的移動。如果他們將其移動到您想要限制的範圍之外,請將其強制限制在邊界範圍內。例如,如果您希望max爲100,並且它們移動到200,則將鼠標位置限制爲100. – Frecklefoot

+0

@Frecklefoot鼠標代碼爲以下代碼:if((mouseState.Y <(PlayerBoundaryY/2))&&(mouseState .Y> 0)) { spriteBatch.Draw(bluePaddle,new Vector2(mouseState.X,mouseState.Y),Color.White);其他 spriteBatch.Draw(bluePaddle,new Vector2 } – Brian

+0

爲什麼它檢查PlayerBoundaryY除以2?你提到,槳板出現在上半部分,而不是下半部分。這可能與它有關。 – Frecklefoot

回答

0

您可能需要使用Mouse.SetPosition方法推光標回所需的矩形。

另外我建議在普通鼠標類上面實現你自己的「虛擬FPS風格的鼠標」(每個勾號你可以獲得相對的鼠標座標,並將真實的鼠標光標推回到窗口的中心)。如果光標有一些移動限制,通常是一個不錯的選擇。

+0

另外我猜你是在創建Pong作爲一個個人的練習,所以實現你自己的虛擬鼠標是一個很好的補充=) 順便說一句,我遇到了這個問題,就像你做的那樣:我創建Pong並需要限制鼠標移動。 –