-1

I want to display the score information I earned on the MultiplicationPage on the GameOverPage. I think I pulled the data from PlayerModal but it still doesn't show up.

GameOverPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-namespace:MauiApp2.ViewModels"
             xmlns:models="clr-namespace:MauiApp2.Models"
             x:Class="MauiApp2.Views.GameOverPage"
             x:DataType="viewModels:GameOverPageViewModel"
             Shell.BackButtonBehavior="{BackButtonBehavior IsVisible=False, IsEnabled=False}"
             Title="GameOverPage">
    <VerticalStackLayout Spacing="60">
        <Label Text="GAME OVER AMK!"
               VerticalOptions="Center" 
               HorizontalOptions="Center" />
        <Label Text="Score"             
               VerticalOptions="Center" 
               HorizontalOptions="Center"/>
        <Label Text="{Binding playerModal.Score}"            
               VerticalOptions="Center" 
               HorizontalOptions="Center"/>
        <Button Text="Try Again" Command="{Binding TryAgainCommand}"/>
        <Button Text="Main Menu" Command="{Binding MainMenuCommand}"/>
    </VerticalStackLayout>
</ContentPage>

GameOverPageViewModel.cs

using CommunityToolkit.Mvvm.Input;
using MauiApp2.Models;
using MauiApp2.Services;
using MauiApp2.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp2.ViewModels
{
    public partial class GameOverPageViewModel : BaseViewModel
    {
        public ObservableCollection<PlayerModal> PlayerDetails { get; set; } = new ObservableCollection<PlayerModal>();
        public PlayerModal playerModal { get; set; } = new PlayerModal();
        public GameOverPageViewModel(INavigationService navigationService) : base(navigationService)
        {

        }
        [RelayCommand]
        public void TryAgain()
        {
            NavigationService.NavigateToAsync(nameof(MultiplicationPage));
        }
        [RelayCommand]
        async Task MainMenu()
        {
            await Shell.Current.GoToAsync("../../..");
        }
    }
}

PlayerModal.cs

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp2.Models
{
    public partial class PlayerModal : ObservableObject
    {
        [ObservableProperty]
        int score;
        [ObservableProperty]
        int highScore;
        [ObservableProperty]
        int timeReaming;
        [ObservableProperty]
        int correctAnswer;
        [ObservableProperty]
        string answer;
        [ObservableProperty]
        string question;
    }
}

MultiplicationPageViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiApp2.Models;
using MauiApp2.Services;
using MauiApp2.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MauiApp2.ViewModels
{
    public partial class MultiplicationPageViewModel : BaseViewModel
    {
        public ObservableCollection<PlayerModal> PlayerDetails { get; set; } = new ObservableCollection<PlayerModal>();
        public PlayerModal playerModal { get; set; } = new PlayerModal();
        public ICommand SubmitAnswerCommand { get; }
        public MultiplicationPageViewModel(INavigationService navigationService) : base(navigationService)
        {
            
            playerModal.Score = 0;
            playerModal.TimeReaming= 60;
            SubmitAnswerCommand = new Command(SubmitAnswer);

            // Create a question as an example
            GenerateQuestion();
            // Start the timer
            StartTimer();
        }
        
        [RelayCommand]
        public void GenerateQuestion()
        {
            Random random = new Random();
            int number1 = random.Next(1, 11); // a random number between 1 and 10
            int number2 = random.Next(1, 11); // a random number between 1 and 10

            // playerModal.Answer = (number1 * number2).ToString(); Automatic Answer
            playerModal.Question = $"{number1} x {number2}"; 
            playerModal.CorrectAnswer = number1 * number2;
        }
        public async void StartTimer()
        {
            while (true)
            {
                await Task.Delay(1000); // wait 1 sec
                playerModal.TimeReaming--;

                if (playerModal.TimeReaming == 0)
                {
                    GameOver(); // Finish the game when time is up
                }
            }
        }

        public void SubmitAnswer()
        {
            if (int.TryParse(playerModal.Answer, out int userAnswer))
            {
                int correctAnswer = playerModal.CorrectAnswer;
                if (userAnswer == correctAnswer)
                {
                    playerModal.Score += 10;
                    playerModal.TimeReaming += 5;

                }
                else
                {
                    playerModal.Score -= 10;
                    playerModal.TimeReaming -= 55;

                    // Negative score and time control
                    if (playerModal.Score < 0)
                    {
                        playerModal.Score = 0;
                    }
                    if (playerModal.TimeReaming < 0)
                    {
                        GameOver();
                        playerModal.TimeReaming = 0;
                    }
                }

                GenerateQuestion();
            }

            playerModal.Answer = "";

        }

        public async void GameOver()
        {
            // Check earned score and update new high score
            if (playerModal.Score > playerModal.HighScore)
            {
                playerModal.HighScore = playerModal.Score;
            }
            await NavigationService.NavigateToAsync(nameof(GameOverPage));
        }
    }
}
M3rtix
  • 23
  • 6

1 Answers1

2

The problem in your project is the PlayerModal in the MultiplicationPageViewModel and GameOverPageViewModel are different instance . So you can declare it in the BaseViewModel, such as:

public class BaseViewModel
{
   public static PlayerModal playerModal { get; set; } = new();
}

And in the GameOverPageViewModel:

public partial class GameOverPageViewModel : BaseViewModel
    {
        public PlayerModal playerModal { get; set; } = BaseViewModel.playerModal;

And in the MultiplicationPageViewModel:

public partial class MultiplicationPageViewModel : BaseViewModel
    {
        public PlayerModal playerModal { get; set; } = BaseViewModel.playerModal;

In addition, you can also try pass the playerModal or the score to the other page when you navigate.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thank you so much ! Now the score efficiency appears on every screen, but every time I close and open the application, the HighScore is reset. How can I save it locally? – M3rtix Aug 17 '23 at 09:32
  • You can use the [Secure storage](https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/secure-storage?tabs=android) to save it on the device storage. @M3rtix – Liyun Zhang - MSFT Aug 17 '23 at 09:49
  • Thank you, I have one more question. When playing the game again from the retry or main menu button, the counter decreases very quickly, like x2 or x4. What would be the reason? @Liyun Zhang - MSFT – M3rtix Aug 17 '23 at 09:52
  • I can't see the code and the effect said about `the counter decreases very quickly`. You can post a new question and show the details such as the code, the effect you want and the effect you got. @M3rtix – Liyun Zhang - MSFT Aug 17 '23 at 09:55