This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
What's happening with timer.Tick += Timer_Tick? a method += a method?
Post Body
this is a XAML interface game from Head First C# for an animal matching game, it introduces using the dispatch timer. Looking back on it, I don't understand at all what the = operator could be doing. Timer_Tick returns void, so it doesn't look like it's adding the evaluation. Is DispatcherTimer.Tick a method? Does the operator = make another method execute whenever the first method is called?
using System;
using System.Collections.Generic;
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 MatchGame
{
using System.Windows.Threading;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer();
int tenthsOfSecondsElapsed;
int matchesFound;
public MainWindow()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(.1);
timer.Tick = Timer_Tick;
SetUpGame();
}
private void Timer_Tick(object sender, EventArgs e)
{
tenthsOfSecondsElapsed ;
timeTextBlock.Text = (tenthsOfSecondsElapsed / 10F).ToString("0.0s");
if(matchesFound == 8)
{
timer.Stop();
timeTextBlock.Text = timeTextBlock.Text " - Play Again?";
}
}
private void SetUpGame()
{
List<String> animalEmoji = new List<string>()
{
"🦊","🦊",
"🐌","🐌",
"🦓","🦓",
"🐷","🐷",
"🦒","🦒",
"🦇","🦇",
"🐧","🐧",
"🐢","🐢",
};
Random random = new Random();
foreach (TextBlock textBlock in mainGrid.Children.OfType<TextBlock>())
{
if(textBlock.Name != "timeTextBlock")
{
textBlock.Visibility = Visibility.Visible;
int index = random.Next(animalEmoji.Count);
string nextEmoji = animalEmoji[index];
textBlock.Text = nextEmoji;
animalEmoji.RemoveAt(index);
}
timer.Start();
tenthsOfSecondsElapsed = 0;
matchesFound = 0;
}
}
//track clicked box
TextBlock lastBlockClicked;
//track user state (first or second click)
bool findingMatch = false;
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
/*
* if it's the first in a pair being clicked,
* keep track of which was clicked
* make the animal disappear
* if it's the second one
* if it matches
* make it dissapear
* else
* make the first one reappear
*/
TextBlock textBlock = sender as TextBlock;
if (findingMatch == false)
{
textBlock.Visibility = Visibility.Hidden;
lastBlockClicked = textBlock;
findingMatch = true;
}
else if (textBlock.Text == lastBlockClicked.Text)
{
matchesFound ;
textBlock.Visibility = Visibility.Hidden;
findingMatch = false;
}
else
{
lastBlockClicked.Visibility = Visibility.Visible;
findingMatch = false;
}
}
private void TimeTextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
//reset the game if all 8 matches found
if (matchesFound == 8)
{
SetUpGame();
}
}
}
}
Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
639,966
Link Karma
107,616
Comment Karma
529,103
Profile updated: 3 days ago
Posts updated: 3 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learncsharp...