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

本文关键字:2人 游戏 标签 2个 何更改 1个 按钮 | 更新日期: 2023-09-27 17:58:29

我有一个双人骰子游戏。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);
        }

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

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

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
    ......
    ......
}

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

第一个技巧是用英语编写代码,这会使代码更容易理解。

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

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++;
    }
}

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

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

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));
    }
}