2016-06-10 16 views
1

Ich versuche, ein einfaches Pong-Spiel zu machen, und ich habe zwei Probleme, die ich angetroffen habe und nicht scheinen kann, herauszufinden.Monogame - Pong Spiel - Beschränkung der Mausbereich

Für das Pong-Spiel möchte ich, dass das Paddel des Spielers innerhalb der Hälfte und unter Y bleibt, was bedeutet, dass er das Paddel innerhalb der Hälfte der "Pitch" bewegen kann.

Ich habe es geschafft, das Paddel in der Mitte stoppen, aber es erscheint nach dem Schlagen Mitte. Ist es möglich, innerhalb der Hälfte der Y-Achse zu bleiben, ohne zu verschwinden?

Und das andere Problem ist, dass das Paddel und der Bereich des Spielers in der oberen Hälfte zu sein scheinen, nicht in der unteren Hälfte.

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

Ich sehe Ihren Mauscode nicht, aber Sie müssen die Bewegung der Maus überwachen. Wenn sie es außerhalb des Bereichs verschieben, auf den Sie es beschränken möchten, erzwingen Sie es in dem Ausmaß der Grenze. Wenn Sie beispielsweise den Wert 100 und den Wert 200 festlegen möchten, beschränken Sie die Mausposition auf 100. – Frecklefoot

+0

@Frecklefoot Der Mauscode lautet wie folgt: if ((mouseState.Y <(PlayerBoundaryY/2)) && (mouseState .Y> 0)) { spriteBatch.Draw (bluePaddle, neuer Vector2 (mouseState.X, mouseState.Y), Color.White); } sonst { spriteBatch.Draw (bluePaddle, neuer Vector2 (mouseState.X, PlayerBoundaryY), Color.White); } – Brian

+0

Warum wird die PlayerBoundaryY durch 2 geteilt? Du hast erwähnt, dass das Paddel in der oberen Hälfte und nicht in der unteren Hälfte erscheint. Das hat wahrscheinlich etwas damit zu tun. – Frecklefoot

Antwort

0

Sie müssen wahrscheinlich Mouse.SetPosition Methode schieben Sie den Cursor zurück auf die gewünschte Rechteck verwenden.

Ich empfehle auch die Implementierung Ihrer eigenen "virtuellen FPS-style Maus" (jedes Häkchen erhalten Sie die relativen Mauskoordinaten und drücken Sie den realen Mauszeiger in die Mitte des Fensters) über allgemeine Mausklasse. Es ist oft eine gute Wahl, wenn Sie Bewegungseinschränkungen für den Cursor haben.

+0

Auch ich denke, dass Sie Pong als eine persönliche Übung erstellen, so ist die Implementierung Ihrer eigenen virtuellen Maus eine nette Ergänzung =) Btw Ich kam zu diesem Problem genau wie du: Ich habe Pong erstellt und musste die Mausbewegung beschränken . –