【发布时间】:2016-07-26 04:22:58
【问题描述】:
我正在尝试制作一个简单的乒乓球游戏,但我遇到了两个问题,但似乎无法解决。
对于乒乓球比赛,我希望玩家的球拍保持在 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);
}
}
}
【问题讨论】:
-
我没有看到你的鼠标代码,但是你需要监控鼠标的移动。如果他们将其移出您要限制的范围,请强制将其移至边界范围。例如,如果您希望最大值为 100 并且它们移动到 200,请将鼠标位置限制为 100。
-
@Frecklefoot 鼠标代码是这样的: if ((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); }
-
为什么要检查 PlayerBoundaryY 除以 2?你提到桨出现在上半部分,而不是下半部分。这可能与它有关。
-
逻辑是取屏幕总大小,除以二,去掉paddle高度的一半,这样正好取中间。