我正在努力制作一个简单的乒乓球游戏,我遇到了两个问题,但似乎无法弄明白。
对于乒乓球比赛,我希望球员的球拍保持在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);
}
}
}
答案 0 :(得分:0)
您可能需要使用Mouse.SetPosition方法将光标推回到所需的矩形。
此外,我建议您实施自己的&#34;虚拟FPS风格鼠标&#34; (每个勾选你得到相对的鼠标坐标并将真正的鼠标光标推回到窗口的中心),高于一般鼠标类。如果光标有一些移动限制,它通常是一个不错的选择。