为什么当我在 xna 4.0 中启动游戏时我的球很模糊
本文关键字:游戏 我的 模糊 启动 xna 为什么 | 更新日期: 2023-09-27 18:31:16
我正在使用此代码,但我仍然不明白为什么我开始游戏时球模糊,有人可以帮助我吗??, 当用户点击时,我只是试图给我球,但我不知道为什么它很模糊,我仍然在检查代码,请帮忙
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Critters
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ball;
Vector2 playerPosition;
Vector2 direction;
Vector2 destination;
float speed;
float playerPosX;
float playerPosY;
MouseState oldState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
}
/// <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
graphics.PreferredBackBufferWidth = 700;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Darrells Game";
playerPosY = graphics.GraphicsDevice.Viewport.Height / 10 * 8;
playerPosX = graphics.GraphicsDevice.Viewport.Width / 2;
playerPosition = new Vector2(playerPosX, playerPosY);
destination = playerPosition;
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(GraphicsDevice);
ball = Content.Load<Texture2D>("orb");
// TODO: use this.Content to load your game content her
}
/// <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
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Vector2.DistanceSquared(destination, playerPosition) >= speed * speed)
{
MovePlayer();
}
else
{
playerPosition = destination;
}
MouseState leftState = Mouse.GetState();
MouseState rightState = Mouse.GetState();
if ((leftState.LeftButton == ButtonState.Pressed && rightState.RightButton == ButtonState.Pressed))
{
int mouseX = leftState.X;
int mouseY = leftState.Y;
destination = new Vector2(mouseX, mouseY);
speed = 8.0f;
}
else if (leftState.LeftButton == ButtonState.Pressed)
{
int mouseX = leftState.X;
int mouseY = leftState.Y;
destination = new Vector2(mouseX, mouseY);
speed = 2.0f;
}
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();
DrawPlayer();
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
private void DrawPlayer()
{
Vector2 textureCentre = new Vector2(ball.Width / 2, ball.Height / 2);
spriteBatch.Draw(ball, playerPosition, null, Color.White, 0f, textureCentre, 0.8f, SpriteEffects.None, 1f);
}
private void MovePlayer()
{
{
direction = destination - playerPosition;
direction.Normalize();
playerPosition += direction * speed;
}
}
}
}
在抽奖调用中:
spriteBatch.Draw
(
ball, // texture
playerPosition, // location
null, // source rect
Color.White, // color
0f, // rotation
textureCentre, // origin
0.8f, // scale
SpriteEffects.None, // effects
1f // layerDepth
);
您已将比例设置为 0.8f
。这会将您的纹理绘制得比实际更小,这将涉及一些"模糊"。将缩放值更改为1f
应该可以消除缩放引起的任何模糊。
编辑:如果您的意思是模糊是在单击之前发生的闪烁,那是由于目的地与当前位置完全相等引起的。然后,您的MovePlayer
方法会导致当前位置变为(NaN, NaN)
:
// before the user has clicked, destination == playerPosition
private void MovePlayer()
{
direction = destination - playerPosition; // direction = (0, 0)
direction.Normalize(); // direction = (NaN, NaN)
playerPosition += direction * speed; // playerPosition = (NaN, NaN)
}
解决此问题的快速方法是在您的Initialize
方法中,更改行:
protected override void Initialize()
{
...
// change this
mDestination = mPlayerPosition;
// to this
mDestination = mPlayerPosition + new Vector2(1, 1);