SDK windows phone C#random.Next();

时间:2014-07-22 10:12:19

标签: c# xaml random sdk

我在做这套代码时陷入困境,我试图做一个随机的.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();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

您要做的是实际生成回合的随机数,将其存储在实例属性中,处理输入,然后生成并更新按钮的可见性。

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

如果您需要更多帮助,请对此答案发表评论。

如果这样可以解决您的问题,请将其标记为已接受的答案。