首页 > 解决方案 > 子元素不想移动

问题描述

我试图通过按上/下/左/右键使图像移动。我使用 wpf 和 canvas 作为面板控件。然后,我将图像命名为 pacman,并在未按下键时使用 onKeyDown,在按下键时使用 onKeyUp,在 xaml 代码中,当我尝试进行定义时,它链接到 xaml 中的正确方法。 CS。这是 xaml.cs 文件:

    namespace Pacman
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer gameTimer = new DispatcherTimer();
        Boolean goup, godown, goleft, goright, isGameOver;
        int score, playerSpeed = 8, redGhostSpeed, yellowGhostSpeed, pinkGhostX, pinkGhostY;



        public MainWindow()
        {
            InitializeComponent();
            
            gameTimer.Interval = TimeSpan.FromMilliseconds(20);
            gameTimer.Tick += Timer_Tick;

            resetGame();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            txtScore.Text = "Score : " + score;

            if (goleft && Canvas.GetLeft(pacman) > 0)
            {
                Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - playerSpeed);
            }
            if (goright && Canvas.GetLeft(pacman) + (pacman.Width * 2) < Application.Current.MainWindow.Width)
            {
                Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + playerSpeed);
            }
            if (goup && Canvas.GetTop(pacman) > 0)
            {
                Canvas.SetTop(pacman, Canvas.GetTop(pacman) - playerSpeed);
            }
            if (godown && Canvas.GetTop(pacman) + (pacman.Height * 2) < Application.Current.MainWindow.Height)
            {
                Canvas.SetTop(pacman, Canvas.GetTop(pacman) + playerSpeed);
            }
        }

        private void onKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Up)
            {
                goup = true;
            }
            if(e.Key == Key.Down)
            {
                godown = true;
            }
            if (e.Key == Key.Left)
            {
                goleft = true;
            }
            if (e.Key == Key.Right)
            {
                goright = true;
            }
        }

        private void onKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                goup = false;
            }
            if (e.Key == Key.Down)
            {
                godown = false;
            }
            if (e.Key == Key.Left)
            {
                goleft = false;
            }
            if (e.Key == Key.Right)
            {
                goright = false;
            }
        }

        private void resetGame()
        {
            txtScore.Text = "Score : 0";
            score = 0;

            redGhostSpeed = 5;
            yellowGhostSpeed = 5;
            pinkGhostX = 5;
            pinkGhostY = 5;
            playerSpeed = 8;

            isGameOver = false;

            gameTimer.Start();
        }

        private void gameOver(string message)
        {

        }
    }
}

我认为代码现在应该可以工作了,但是没有人知道我该怎么做?

编辑:这是 xaml 代码:

<Window x:Class="Pacman.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Pacman"
        mc:Ignorable="d"
        Title="Pacman Game" Height="700" Width="600" Background="Black"
        xmlns:gif ="https://github.com/XamlAnimatedGif/XamlAnimatedGif">
    <Canvas Background="Black" KeyDown="onKeyDown" KeyUp="onKeyUp" Focusable="True">
<Image x:Name="pacman" HorizontalAlignment="Left" Height="55" VerticalAlignment="Top" Width="55" gif:AnimationBehavior.SourceUri="Resources\right.gif"/>
    </Canvas>
</Window>

我已经修复了这些家伙,我忘了给画布名​​称和MyCanvas.Focus();我的 Timer_Tick()

太感谢了

标签: c#wpf

解决方案


推荐阅读