C# 图像冲突

本文关键字:冲突 图像 | 更新日期: 2023-09-27 18:36:57

嗨,伙计们,基本上这是僵尸行走的代码,我现在想做的是当我点击僵尸时它们消失了,我该怎么做?或者,如果僵尸在与另一个图像相撞时行走时会消失怎么办?就像子弹击中他们一样。

public class Gaston {
        public double X { get; set; }
        public double Y { get; set; }
        public Image Image { get; set; }
        public int Colspan { get; set; }
        public int Rowspan { get; set; }
    }
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
         DispatcherTimer dispatcherTimer;
         List<Gaston> Gastons;
         int timesTicked = 0;
         int timesToTick = 10;
         Image zombieImg;
         public MainPage()
        {
            this.InitializeComponent();
            Gastons = new List<Gaston>();
                DispatcherTimerSetup();
                Zombie();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void Zombie()
        {
            Gaston newGaston = new Gaston
            {
                Colspan = -10,
                Image = new Image(),
                Rowspan = -10,
                X = -10,
                Y = -10
            };
            BitmapImage bmp;
            bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
            newGaston.Image.Source = bmp;
            Gastons.Add(newGaston);    
            Grid.Children.Add(newGaston.Image);
        }
        private void ZombieWalks()
        {
            foreach (Gaston me in Gastons)
            {
                me.Image.Margin = new Thickness(me.Image.Margin.Left - 10, me.Image.Margin.Bottom - 10, me.Image.Margin.Right + 10, me.Image.Margin.Top + 10);
           }
        }


        public void DispatcherTimerSetup()
        {
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }
        void dispatcherTimer_Tick(object sender, object e)
        {
            AddZombie();
            timesTicked++;
            ZombieWalks();
            if (timesTicked > timesToTick)
            {
            }
        }
        private void AddZombie()
        {
            if (timesTicked == 5)
            {
                    Zombie();
                DispatcherTimerSetup();
            }
        }        
    }
}

C# 图像冲突

将 Tap

事件挂接到图像上(并将 IsTapEnabled 转换为 true)。然后,在这种情况下,删除僵尸。

BitmapImage bmp;
bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
newGaston.Image.Source = bmp;
newGaston.Image.Tapped += GastonTapped;
....
private void GastonTapped(object sender, TappedRoutedEventArgs e)
{
// remove the zombie.
}

另外 - 我强烈建议您使用画布来定位您的元素。容易多了。