Created
September 24, 2018 21:06
-
-
Save jake-walker/89cc161b6d88f0ea44a2a9b010e04e93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import stuff | |
namespace Dice_Gambling | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
private Host GameHost; | |
// Reuse the same random instance over the UI code. | |
private Random RandomGenerator = new Random(); | |
// Timer for running animations in the background without 'blocking' the UI. | |
DispatcherTimer AnimationTimer = new DispatcherTimer(); | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
GameHost = new Host(); | |
AnimationTimer.Interval = TimeSpan.FromMilliseconds(AnimationInterval); | |
AnimationTimer.Tick += AnimationTimer_Tick; | |
} | |
// This function updates the dice on the screen it starts with a small interval and slowly increases | |
// up to 1500ms where it stops. | |
private void AnimationTimer_Tick(object sender, EventArgs e) | |
{ | |
UpdateDice(1); | |
UpdateDice(2); | |
UpdateDice(3); | |
AnimationInterval = AnimationInterval * 2; | |
AnimationTimer.Interval = TimeSpan.FromMilliseconds(AnimationInterval); | |
if (AnimationInterval >= 1500) | |
{ | |
AnimationInterval = 20; | |
AnimationTimer.Stop(); | |
} | |
} | |
private int UpdateDice(int Number) | |
{ | |
int DiceNumber = RandomGenerator.Next(1, 6); | |
System.Diagnostics.Debug.WriteLine($"Generated new random number {DiceNumber}"); | |
Image DiceToChange; | |
switch (Number) { | |
case 1: | |
DiceToChange = FirstDice; | |
break; | |
case 2: | |
DiceToChange = SecondDice; | |
break; | |
default: | |
DiceToChange = ThirdDice; | |
break; | |
} | |
ImageSource NewImage; | |
switch (DiceNumber) | |
{ | |
case 1: | |
NewImage = (ImageSource)FindResource("Dice1"); | |
break; | |
case 2: | |
NewImage = (ImageSource)FindResource("Dice2"); | |
break; | |
case 3: | |
NewImage = (ImageSource)FindResource("Dice3"); | |
break; | |
case 4: | |
NewImage = (ImageSource)FindResource("Dice4"); | |
break; | |
case 5: | |
NewImage = (ImageSource)FindResource("Dice5"); | |
break; | |
default: | |
NewImage = (ImageSource)FindResource("Dice6"); | |
break; | |
} | |
DiceToChange.Source = NewImage; | |
return DiceNumber; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment