检测c#中的击键历史

时间:2014-04-26 00:57:45

标签: c# c#-4.0 graphics xna keyboard-events

所以目前,我是一个在c#中开始编码的新手。我开始制作一个类似于太空入侵者的太空游戏并遇到了一个问题,在绘制方法中,我设置它来绘制我的子弹,并在我击中太空时向上朝向弦。但问题是,一旦我放开太空,我的子弹就消失了。我做了一个工作,一旦子弹离开屏幕就会传送到船上。无论如何都要让游戏检查过去是否按下按钮并使子弹继续移动直到我希望它消失。

    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 Spaceteroids
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    Texture2D spriteSheet;
    Texture2D laser;
    Texture2D Enemyship;
    spriteAnimation loading;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    KeyboardState currentKeyboard;
    KeyboardState oldKeyboard;
    PlayerShip character;
    Rectangle characterRectangle;
    Bullet bullet;
    Bullet bullet2;
    bool heightCheck;

    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

        currentKeyboard = new KeyboardState();
        oldKeyboard = new KeyboardState();
        //characterRectangle = new Rectangle((int)character.Position.X,(int)character.Position.Y, spriteSheet.Width / 4, spriteSheet.Height);
        character = new PlayerShip(new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height - 40));
        bullet = new Bullet(new Vector2(character.Position.X, character.Position.Y - 20));

        base.Initialize();
    }
    void Movement()
    {
        if (IsHeld(Keys.A))
        {
            character.xSpeed = -6;
        }
        else if (IsHeld(Keys.D))
        {
            character.xSpeed = 6;
        }
        else
        {
            character.xSpeed = 0;
        }
    }
    void Collision()
    {
        if (character.Position.X < 0)
        {
            character.Position.X = 0;
        }
        else if (character.Position.X + (spriteSheet.Width/4) > graphics.GraphicsDevice.Viewport.Width)
        {
            character.Position.X = graphics.GraphicsDevice.Viewport.Width - (spriteSheet.Width/4) ;
        }
    }
    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        laser = Content.Load<Texture2D>("Laser");
        Enemyship = Content.Load<Texture2D>("EnemyShip");
        spriteSheet = Content.Load<Texture2D>("shipSpriteSheet");
        spriteBatch = new SpriteBatch(GraphicsDevice);
        loading = new spriteAnimation(Content.Load<Texture2D>("shipSpriteSheet"), 4);
        loading.Position = new Vector2(character.Position.X);
        loading.IsLooping = true;
        loading.FramesPerSecond = 30;
        // 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)
    {
        bullet.Position.X = character.Position.X;
        currentKeyboard = Keyboard.GetState();
        oldKeyboard = currentKeyboard;
        loading.Update(gameTime, character);
        bullet.ySpeed = -10;

        bullet.UpdateBullet();

        Collision();
        Movement();
        if (NotPressed(Keys.Space))
        {
            bullet.Position.Y = character.Position.Y - 15;
        }
        if (bullet.Position.Y < 0)
        {
            bullet.Position.Y = character.Position.Y - 15;
        }

        character.UpdateShip();
        // 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.Black);

        spriteBatch.Begin();


      spriteBatch.Draw(laser, new Vector2(bullet.Position.X, bullet.Position.Y), Color.White);


        loading.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }

    public bool IsHeld(Keys key)
    {
        if (currentKeyboard.IsKeyDown(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool IsReleased(Keys key)
    {
        if (currentKeyboard.IsKeyUp(key) && oldKeyboard.IsKeyDown(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool JustPressed(Keys key)
    {
        if (currentKeyboard.IsKeyDown(key) && oldKeyboard.IsKeyUp(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool NotPressed(Keys key)
    {
        if (IsHeld(key) != true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool HasBeenPressed(Keys key)
    {
        if (oldKeyboard.IsKeyDown(key))
        {
            return true;
        }
        else return false;
    }

}
}

如果您需要任何其他信息,请随时提问。

1 个答案:

答案 0 :(得分:0)

不,XNA中没有任何内容可以跟踪击键历史(或其他任何地方)。你必须自己做。

解决问题的通常方法是在代表项目符号的对象上维护状态。击键可能会消失,但物体仍然存在并使子弹保持可见并移动直至其被摧毁。您似乎在子弹对象中具有该状态,因此您可能只是在更新时遇到逻辑错误。

快速阅读问题并不是很明显,但是你对键盘状态的处理似乎过于复杂。所有你需要的是按键和键。其余的你应该自己跟踪。