C# 为什么我在xna 4.0中开始游戏时球模糊?
我正在使用这个代码,但我仍然不明白为什么在我开始游戏时球是模糊的,有人能帮我吗??,我只是试图在用户点击时给我球,但我不知道为什么它是模糊的,我仍然检查代码,请帮助C# 为什么我在xna 4.0中开始游戏时球模糊?,c#,xna,xna-4.0,C#,Xna,Xna 4.0,我正在使用这个代码,但我仍然不明白为什么在我开始游戏时球是模糊的,有人能帮我吗??,我只是试图在用户点击时给我球,但我不知道为什么它是模糊的,我仍然检查代码,请帮助 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Co
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间生物
{
///
///这是游戏的主要类型
///
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2d球;
矢量2播放位置;
矢量2方向;
矢量2目的地;
浮动速度;
float-playerPosX;
浮子游戏;
穆萨州;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
this.IsMouseVisible=true;
}
///
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
///
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
graphics.PreferredBackBufferWidth=700;
graphics.PreferredBackBufferHeight=500;
graphics.IsFullScreen=false;
graphics.ApplyChanges();
Window.Title=“Darrells游戏”;
playerPosY=graphics.GraphicsDevice.Viewport.Height/10*8;
playerPosX=graphics.GraphicsDevice.Viewport.Width/2;
playerPosition=新矢量2(playerPosX,playerPosY);
目的地=播放位置;
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
球=内容物负荷(“球”);
//TODO:使用此.Content向她加载游戏内容
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
if(矢量2.距离平方(目的地,播放位置)>=速度*速度)
{
MovePlayer();
}
其他的
{
播放位置=目的地;
}
MouseState leftState=Mouse.GetState();
MouseState rightState=Mouse.GetState();
如果((leftState.LeftButton==按钮状态。按下&&rightState.RightButton==按钮状态。按下))
{
int mouseX=leftState.X;
int mouseY=leftState.Y;
目的地=新矢量2(mouseX、mouseY);
速度=8.0华氏度;
}
else if(leftState.LeftButton==ButtonState.Pressed)
{
int mouseX=leftState.X;
int mouseY=leftState.Y;
目的地=新矢量2(mouseX、mouseY);
速度=2.0f;
}
更新(游戏时间);
}
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
DrawPlayer();
spriteBatch.End();
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
私人玩家()
{
Vector2 TextureCenter=新的Vector2(ball.Width/2,ball.Height/2);
spriteBatch.Draw(球、球员位置、空值、颜色、白色、0f、纹理中心、0.8f、SpriteTeffects、无、1f);
}
私人玩家()
{
{
方向=目的地-播放位置;
方向。规范化();
播放位置+=方向*速度;
}
}
}
}
在抽签通话中:
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)
:
对此的快速修复方法是在Initialize
方法中,更改行:
protected override void Initialize()
{
...
// change this
mDestination = mPlayerPosition;
// to this
mDestination = mPlayerPosition + new Vector2(1, 1);
我变了,它还是有些模糊,我不知道该怎么办?谢谢你answering@GeorgeBecjk,我补充了更多的答案。请回答另一个问题。我想做一场点球大战。我把守门员做成了一个长方形的碰撞
protected override void Initialize()
{
...
// change this
mDestination = mPlayerPosition;
// to this
mDestination = mPlayerPosition + new Vector2(1, 1);