如何更改1按钮2标签(2人游戏)

时间:2014-04-15 12:14:41

标签: c# wpf class button label

我有一个2人骰子游戏。每次点击播放的播放器都需要更改1个按钮,并更改播放的分数。我找到了一个按钮的解决方案,需要更换播放器但我仍然坚持得分。现在它改变了两个标签,我知道为什么,但我找不到解决方案。到目前为止这是我的代码。

骰子类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameLib
{
    public class Dobbelsteen
    {
        public int aantalOgen;
        static Random rnd = new Random();

        public Dobbelsteen()
        {
            Dobbel();
        }

        public Dobbelsteen(int aantalOgen)
        {
            this.aantalOgen = aantalOgen;
        }

        public void Dobbel()
        {
            aantalOgen = rnd.Next(1, 7);
        }

游戏类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameLib
{
    public class Vijftig
    {
        public Dobbelsteen steen1 = new Dobbelsteen();
        public Dobbelsteen steen2 = new Dobbelsteen();

        public int score;
        public int waarde;


        public int score1;
        public int score2;

        public void Speel()
        {
            steen1.Dobbel();
            steen2.Dobbel();

            if (steen1.aantalOgen == steen2.aantalOgen)
            {
                if (steen1.aantalOgen == 6)
                    score += 25;
                else
                    if (steen1.aantalOgen == 3)
                        score = 0;
                    else score += 5;
            }
        }

xaml.cs:

using GameLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfVijftig
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Vijftig spel = new Vijftig();

        public MainWindow()
        {
            InitializeComponent();

            for (spel.waarde = 1; spel.waarde < 7; spel.waarde++)
            lbWaarden.Items.Add(spel.waarde);
        }

        private void btnSpelen_Click(object sender, RoutedEventArgs e)
        {
            spel.Speel();


            int score1 = 0;
            int score2 = 0;

            imgDobbelsteen1.Source = new BitmapImage(new Uri("Resources/" + spel.steen1.aantalOgen + ".jpg", UriKind.Relative));
            imgDobbelsteen2.Source = new BitmapImage(new Uri("Resources/" + spel.steen2.aantalOgen + ".jpg", UriKind.Relative));

            if ((string)btnSpelen.Content == "Gooien speler 1")
            {
                btnSpelen.Content = "Gooien speler 2";
                lblScoreSpeler1.Content = score1 + spel.score;
            }
            else if ((string)btnSpelen.Content == "Gooien speler 2")
            {
                btnSpelen.Content = "Gooien speler 1";
                lblScoreSpeler2.Content = score2 + spel.score;
            }
            if (spel.score >= 50)
                MessageBox.Show("Je hebt gewonnen!!!");

            Debug.Print("je gooide " + spel.steen1.aantalOgen + " en " + spel.steen2.aantalOgen + ". Je score is " + spel.score);
        }

3 个答案:

答案 0 :(得分:0)

首先,您需要使用英语编码,以便您的代码更容易理解。

我想你想从听众那里检索点击的按钮,试试这样做:

int score1 = 0;
int score2 = 0;

private void btnSpelen_Click(object sender, RoutedEventArgs e)
{
    Button clickedButton = sender as Button;
    if((string)clickedButton.Content == "Gooien speler 1")
    {
            score1++;
    }
    else if((string)clickedButton.Content == "Gooien speler 2")
    {
            score2++;
    }
}

答案 1 :(得分:0)

尝试将用于存储分数的变量声明移动为类范围变量:

Vijftig spel = new Vijftig();
int score1 = 0;
int score2 = 0;
.......
private void btnSpelen_Click(object sender, RoutedEventArgs e)
{
    spel.Speel();

    //remove local declaration of score1 & score2 from here

    ......
    ......
}

现有代码会在每次点击按钮时将两个分数重置为零,因为它们是在本地声明的。这些分数会在触发的每个点击事件中被处理和重新创建。

答案 2 :(得分:0)

我找到了解决方案。不知道它是否是最好的,但它运作正常。 感谢您帮助找到解决方案!

这是游戏类中的基本代码。

public int BerekenScore(int score)
    {
        if (steen1.aantalOgen == steen2.aantalOgen)
        {
            if (steen1.aantalOgen == 6)
                score += 25;
              else
              if (steen1.aantalOgen == 3)
                  score = 0;
                else score += 5;
        }
        return score;
    }

public void Speel()
    {
        steen1.Dobbel();
        steen2.Dobbel();
    }

这是main.cs中的基本代码

public partial class MainWindow : Window
{
    Vijftig spel = new Vijftig();  

private void btnSpelen_Click(object sender, RoutedEventArgs e)
    {
        spel.Speel();
        ToonStenen();

        Button clickedButton = sender as Button;
        if ((string)clickedButton.Content == "Gooien speler 1")
        {
            clickedButton.Content = "Gooien speler 2";
            spel.score1 = spel.BerekenScore(spel.score1);
            lblScoreSpeler1.Content = spel.score1.ToString();
        }
        else if ((string)clickedButton.Content == "Gooien speler 2")
        {
            clickedButton.Content = "Gooien speler 1";
            spel.score2 = spel.BerekenScore(spel.score2);
            lblScoreSpeler2.Content = spel.score2.ToString();
        }

public void ToonStenen()
    {
        imgDobbelsteen1.Source = new BitmapImage(new Uri("Resources/" + spel.steen1.aantalOgen + ".jpg", UriKind.Relative));
        imgDobbelsteen2.Source = new BitmapImage(new Uri("Resources/" + spel.steen2.aantalOgen + ".jpg", UriKind.Relative));
    }
}