我试图制造类似太空入侵者的游戏而且我发现了一些困难, 我的主要玩家精灵根据键盘正确移动,但不是班卓琴! 你能帮忙吗?
Game1.CS
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public float displayWidth;
float displayHeight;
float overScanPercentage = 10.0f;
float minDisplayX;
float maxDisplayX;
float minDisplayY;
float maxDisplayY;
player accordian;
baseSprite background;
PlainBanjo Banjo;
float getPercentage(float percentage, float inputValue)
{
return (inputValue * percentage) / 100;
}
private void setScreenSizes()
{
displayWidth = graphics.GraphicsDevice.Viewport.Width;
displayHeight = graphics.GraphicsDevice.Viewport.Height;
float xOverscanMargin = getPercentage(overScanPercentage, displayWidth) / 2.0f;
float yOverscanMargin = getPercentage(overScanPercentage, displayHeight) / 2.0f;
minDisplayX = xOverscanMargin;
minDisplayY = yOverscanMargin;
maxDisplayX = displayWidth - xOverscanMargin;
maxDisplayY = displayHeight - yOverscanMargin;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
public void update_accordian()
{
accordian.SpriteRectangle.X = (int)accordian.X;
accordian.SpriteRectangle.Y = (int)accordian.Y;
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.D))
{
accordian.X = accordian.X + 2;
} if (keys.IsKeyDown(Keys.A))
{
accordian.X = accordian.X - 2;
}
}
/// <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
displayWidth = graphics.GraphicsDevice.Viewport.Width;
setScreenSizes();
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);
accordian = new player(Content.Load<Texture2D>("accordian"), new Rectangle(350, 433,
Window.ClientBounds.Width / 10,
Window.ClientBounds.Height / 10));
accordian.X = 400;
accordian.Y = 400;
background = new baseSprite(Content.Load<Texture2D>("background"), new Rectangle(0, 0,
Window.ClientBounds.Width,
Window.ClientBounds.Height));
Banjo = new PlainBanjo(Content.Load<Texture2D>("PlainBanjo"), new Rectangle(0,0,50,50));
Banjo.setupSprite( 0.05f, 200.0f, 200, 100, true);
// accordian.setupSprite();
// 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();
update_accordian();
Banjo.Update(maxDisplayX, minDisplayY, minDisplayX,maxDisplayY);
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);
// TODO: Add your drawing code here
spriteBatch.Begin();
background.Draw(spriteBatch);
accordian.Draw(spriteBatch);
Banjo.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}`
baseSprite
class baseSprite : Game1
{
public Vector2 Position = new Vector2(0, 0);
public Texture2D SpriteTexture;
public Rectangle SpriteRectangle;
public float X;
public float Y;
public float XSpeed;
public float YSpeed;
public float WidthFactor;
float TicksToCrossScreen;
public bool Visible;
public void setupSprite(
float widthFactor,
float ticksToCrossScreen,
float initialX,
float initialY,
bool initialVisibility)
{
WidthFactor = widthFactor;
TicksToCrossScreen = ticksToCrossScreen;
SpriteRectangle.Width = (int)((displayWidth * widthFactor) + 25f);
float aspectRatio =
(float)SpriteTexture.Width / SpriteTexture.Height;
SpriteRectangle.Height =
(int)((SpriteRectangle.Width / aspectRatio) + 25f);
X = initialX;
Y = initialY;
XSpeed = displayWidth / ticksToCrossScreen;
YSpeed = XSpeed;
Visible = initialVisibility;
}
public void LoadTexture(Texture2D inSpriteTexture)
{
SpriteTexture = inSpriteTexture;
}
public void SetRectangle(Rectangle inSpriteRectangle)
{
SpriteRectangle = inSpriteRectangle;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(SpriteTexture, SpriteRectangle, Color.White);
}
public baseSprite(Texture2D inSpriteTexture, Rectangle inRectangle)
{
SpriteTexture = inSpriteTexture;
SpriteRectangle = inRectangle;
}
}
player.cs
class player : baseSprite
{
public player(Texture2D inSpriteTexture, Rectangle inRectangle)
: base(inSpriteTexture, inRectangle)
{
}
}
PlainBanjo.cs
class PlainBanjo : baseSprite
{
public PlainBanjo(Texture2D inSpriteTexture, Rectangle inRectangle)
: base(inSpriteTexture, inRectangle)
{
}
public void Update(float maxDisplayX, float minDisplayY, float minDisplayX,float maxDisplayY)
{
X = X + XSpeed;
SpriteRectangle.X = (int)(X + 0.5f);
SpriteRectangle.Y = (int)(Y + 0.5f);
if (X + SpriteRectangle.Width >= maxDisplayX)
{
XSpeed = Math.Abs(XSpeed) * -1;
Y += 10;
}
if (X <= minDisplayX)
{
XSpeed = Math.Abs(XSpeed);
}
if (Y + SpriteRectangle.Height >= maxDisplayY)
{
YSpeed = Math.Abs(YSpeed) * -1;
}
if (Y <= minDisplayY)
{
YSpeed = Math.Abs(YSpeed);
}
}
}
答案 0 :(得分:0)
看起来你正在计算手风琴的新值但从未真正用新的手风琴值更新accordian.SpriteRectangle.X和accordian.SpriteRectangle.Y。