使用WPF显示可以使用C#中的箭头键移动的图像

时间:2015-04-20 03:54:11

标签: c# wpf xaml imagesource

我正在为一个类编写游戏Galaga,并且正在使用WPF来完成它并且从未使用过WPF。我已经能够上传玩家船的图像,并找到了如何使用Canvas.SetLeft和Canvas.SetTop命令设置其位置,但我还没有找到如何使用箭头键输入来更改这些值。我把一个do while循环放在那里查找输入,但是它无法启动。下面是我的XAML代码,下面是我的XAML.cs。

<Window x:Class="GalagaGame.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="1000">
<Canvas Background="Black">
    <Image Name="HumanShipGraphic" Source="GalagaPlayerShip.png" HorizontalAlignment="Left" Height="100" Canvas.Top="350" Canvas.Left="450" VerticalAlignment="Top" Width="100"/>
    <Image Name="BlueAlienShipGraphic" Source="BlueAlienShip.png" Height="75" Width="75" Canvas.Left="100" Canvas.Top="100" />
    <TextBlock Foreground="Red" FontFamily="Arial" FontSize="20" FontWeight="Bold"  Canvas.Top="400" Canvas.Right="900" TextWrapping="Wrap" Text="Score"/>
    <TextBlock Name="ScoreText" Foreground="White" FontFamily="Arial" FontSize="15" FontWeight="Bold"  Canvas.Top="420" Canvas.Right="900" TextWrapping="Wrap" Text="300" />
    <Rectangle Name="LaserGraphic" Fill="#FFF4F4F5" Height="15" Width="5" Canvas.Top="118" Canvas.Left="532" Stroke="White"  />
    <Image Name="BlowUpImage" Height="100" Canvas.Left="408" Canvas.Top="308" Width="100">
        <Image.Triggers>
            <EventTrigger>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Name="BlowUpAnimation" Storyboard.TargetName="BlowUpImage" 
                                         Storyboard.TargetProperty="Width" From=" 100" To=" 100" 
                                         Duration="0:0:0.1" Completed="DoubleAnimation_Completed"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Image.Triggers>
    </Image>

</Canvas>

    namespace GalagaGameTestProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
        InitializeComponent();
        do
        {
            ProcessInput();
            MoveShip(x, y);
        } while (quit == 0);

    }
    int x = 100;
    int y = 100;
    int quit = 0;
    public void Start()
    {
        Game newGame = new Game();
        newGame.Run();
    }
    public void MoveShip(int x, int y)
    {
        Canvas.SetTop(HumanShipGraphic, x);
        Canvas.SetLeft(HumanShipGraphic, y);
    }
    private void ProcessInput()
    {
        ConsoleKeyInfo keyInfo;
        if (Console.KeyAvailable)
        {
            keyInfo = Console.ReadKey();
            switch (keyInfo.Key)
            {

                case ConsoleKey.LeftArrow:
                    x -= 5;
                    break;
                case ConsoleKey.RightArrow:
                    x -= 5;

                    break;
                case ConsoleKey.Spacebar:
                    quit = 1;
                    break;

            }
        }
    }


    }
}

1 个答案:

答案 0 :(得分:1)

你的循环在你的Windows构造函数中,它会保持你的窗体显示。请将键盘输入移至OnPreviewKeyDown事件。

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
   base.OnPreviewKeyDown(e);
   switch (e.Key )
   {
       case Key.Left: 
           break;
       case Key.Right:
           break; 
       case Key.Space:
           break;
   }

}