我正在尝试制作一个简单的乒乓游戏,我有两个我遇到过的问题,而且似乎无法弄清楚。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);
}
}
}
我没有看到您的鼠标代码,但您需要监视鼠标的移动。如果他们将其移动到您想要限制的范围之外,请将其强制限制在边界范围内。例如,如果您希望max为100,并且它们移动到200,则将鼠标位置限制为100. – Frecklefoot
@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
为什么它检查PlayerBoundaryY除以2?你提到,桨板出现在上半部分,而不是下半部分。这可能与它有关。 – Frecklefoot