XNA 精灵旋转
本文关键字:旋转 精灵 XNA | 更新日期: 2023-09-27 17:56:05
我正在尝试在这里旋转代码中的"golf_ball"精灵,但我遇到了很多困难。我已经查看了教程,但没有运气。我的比赛基本上应该尽可能长时间地使用球拍将球保持在空中。任何帮助将不胜感激!
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Ball sprite
Texture2D ballSprite;
// Ball location
Vector2 ballPosition = Vector2.Zero;
// Store some information about the sprite's motion.
Vector2 ballSpeed = new Vector2(150, 150);
// Paddle sprite
Texture2D paddleSprite;
// Paddle location
Vector2 paddlePosition;
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()
{
base.Initialize();
// Set the initial paddle location
paddlePosition = new Vector2(
graphics.GraphicsDevice.Viewport.Width / 2 - paddleSprite.Width / 2,
graphics.GraphicsDevice.Viewport.Height - paddleSprite.Height);
}
/// <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(GraphicsDevice);
ballSprite = Content.Load<Texture2D>("golf_ball");
paddleSprite = Content.Load<Texture2D>("Paddle");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// Move the sprite by speed, scaled by elapsed time
ballPosition += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int maxX = GraphicsDevice.Viewport.Width - ballSprite.Width;
int maxY = GraphicsDevice.Viewport.Height - ballSprite.Height;
// Check for bounce
if (ballPosition.X > maxX || ballPosition.X < 0)
ballSpeed.X *= -1;
if (ballPosition.Y < 0)
ballSpeed.Y *= -1;
else if (ballPosition.Y > maxY)
{
// Ball hit the bottom of the screen, so reset ball
ballPosition.Y = 0;
ballSpeed.X = 150;
ballSpeed.Y = 150;
}
// Ball and paddle collide? Check rectangle intersection between objects
Rectangle ballRect =
new Rectangle((int)ballPosition.X, (int)ballPosition.Y,
ballSprite.Width, ballSprite.Height);
Rectangle handRect =
new Rectangle((int)paddlePosition.X, (int)paddlePosition.Y,
paddleSprite.Width, paddleSprite.Height);
if (ballRect.Intersects(handRect))
{
// Increase ball speed
ballSpeed.Y += 50;
if (ballSpeed.X < 0)
ballSpeed.X -= 50;
else
ballSpeed.X += 50;
// Send ball back up the screen
ballSpeed.Y *= -1;
}
base.Update(gameTime);
// Update the paddle's position
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Right))
paddlePosition.X += 5;
else if (keyState.IsKeyDown(Keys.Left))
paddlePosition.X -= 5;
}
/// <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);
// Draw the sprite
spriteBatch.Begin();
spriteBatch.Draw(ballSprite, ballPosition, Color.White);
spriteBatch.Draw(paddleSprite, paddlePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
欢迎来到堆栈溢出!
正如@user3449857已经指出的那样,您的代码实际上并没有旋转精灵。
首先,请尝试以下操作:
spriteBatch.Draw(ballSprite, ballPosition, null, Color.White, 1.5f, Vector2.Zero, SpriteEffects.None, 1.0f);
你可以看到第 5 个参数是用于旋转的,它应该旋转你的球。这需要浮点数。
有了这些知识,请尝试在谷歌上搜索其他有关如何增加旋转的教程。