SDK windows phone C# random.Next();

本文关键字:Next random windows phone SDK | 更新日期: 2023-09-27 18:14:10

我在做这组代码时卡住了,我试图为我的手机游戏做一个random.Next(),一切都很好,但是我面临这个逻辑错误。

我的游戏左边有两个箭头&对,游戏从一张随机图片开始(左边或右边),如果箭头左<<看来我必须按btnLeft才能得到1个点,如果右箭头>>出现,我必须按btnRight来得到点,对于我的random。next(0,2) 0是btnLeft, 1是btnRight。问题是当第一个图片是随机生成的,假设其左箭头,当我按下btn,我已经要求下一个随机的箭头,这意味着屏幕会显示左箭头键,但它已经生成下一个随机数,但屏幕上仍然显示左箭头,当我按btnLeft,我没有获得分数和箭头更改为正确的>>,意思我只是失去了一个点,我怎么解决这个问题?.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Windows.Threading;
    using System.Windows.Media.Imaging;

    namespace madAssignment
    {
        public partial class gamePlay : PhoneApplicationPage
        {
            int counter = 1000000;
            DispatcherTimer timer;
            int count = 0;
            public gamePlay()
            {
                InitializeComponent();
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(0.1);
                timer.Tick += timer_Tick;
                // Sample code to localize the ApplicationBar
                //BuildLocalizedApplicationBar();
            }
            void timer_Tick(object sender, EventArgs e)
            {
                //int num;
                counter -= 1;
                txtCount.Text = counter.ToString();
                if(Convert.ToInt32(txtCount.Text) == 0)
                {
                    timer.Stop();
                    Uri uri = new Uri("/gameEnd.xaml", UriKind.Relative);
                    this.NavigationService.Navigate(uri);
                }
            }
    int rand()
    {
        Random r = new Random();
        int p = 1;
        if(p == 0)
        {
            imgLeft.Visibility = Visibility.Visible;
            imgRight.Visibility = Visibility.Collapsed;
        }
        else
        {
            imgLeft.Visibility = Visibility.Collapsed;
            imgRight.Visibility = Visibility.Visible;
        }
        int i = r.Next(0, 2);
        return i;
    }
    private void btnTimerStart_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        imgLeft.Visibility = Visibility.Collapsed;
        imgRight.Visibility = Visibility.Collapsed;
        timer.Start();
        rand();
    }
    private void btnLeft_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        if (rand() == 0)
        {
            count += 1;
            txtScore.Text = count.ToString();
        }
        else
        {
            count -= 1;
            txtScore.Text = count.ToString();
        }
    }
    private void btnRight_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        if(rand() == 1)
        {
            count += 1;
            txtScore.Text = count.ToString();
        }
        else
        {
            count -= 1;
            txtScore.Text = count.ToString();
        }
    }
}

}

SDK windows phone C# random.Next();

您面临的问题是,您在点击按钮后生成一个随机数。

你要做的实际上是生成随机数,将其存储在一个实例属性中,处理输入,然后生成和更新按钮的可见性。

同样在rand()方法中,您显式地将p变量设置为1,因此这将始终导致不显示左侧按钮而显示右侧按钮。我猜这不是你想要的可见度。

如果你需要更多的帮助,请在这个答案下评论。

如果这解决了你的问题,请将其标记为接受的答案