Skip to content

Commit

Permalink
Working game :)
Browse files Browse the repository at this point in the history
  • Loading branch information
mungojam committed Apr 15, 2019
1 parent 5ecd397 commit b82f4d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions protect the pig/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:protect_the_pig"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="Protect the pig" Height="450" Width="800">
<Grid Background="#FF1D23A2">
<Image Margin="275.973,147.254,292.549,146.94" Source="Image1.png" Stretch="Fill"/>
<Button GotMouseCapture= "Button1_OnClick" x:Name="button1" Content= "" HorizontalAlignment="Left" Margin="81.469,98.629,0,0" VerticalAlignment="Top" Width="22.483"/>
<Button GotMouseCapture= "Button1_OnClick" x:Name="button1" Content= "" HorizontalAlignment="Left" Margin="769.517,399.04,0,0" VerticalAlignment="Top" Width="22.483"/>
<Label Content="SCORE" HorizontalAlignment="Left" Margin="650.92,18.01,0,0" VerticalAlignment="Top" Width="44.285" Foreground="#FFF9EE00"/>
<Label x:Name="scorelabel" Content="0" HorizontalAlignment="Left" Margin="716.829,18.01,0,0" VerticalAlignment="Top" Background="#FFF9EE00"/>
<Label x:Name="Gameover" Content="RIP piggy" HorizontalAlignment="Left" Margin="275.973,147.254,0,0" VerticalAlignment="Top" Width="223.478" Height="124.806" FontSize="48" Background="#FFCF1616" Visibility="Hidden"/>

</Grid>
</Window>
40 changes: 36 additions & 4 deletions protect the pig/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ namespace protect_the_pig
public partial class MainWindow : Window
{
private int score;
Random random=new Random();
private int buttonleft = 1;
private int buttontop = 1;
public MainWindow()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 5);
dispatcherTimer.Start();


Expand All @@ -35,15 +38,44 @@ public MainWindow()
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
var currentmargin = button1.Margin;
currentmargin.Left += 1;
currentmargin.Top += 1;
currentmargin.Left += buttonleft;
currentmargin.Top += buttontop;
button1.Margin = currentmargin;
if (currentmargin.Left>769.5)
{
buttonleft = -1;

}
if (currentmargin.Top >399)
{
buttontop = -1;

}
if (currentmargin.Left <0)
{
buttonleft = 1;

}
if (currentmargin.Top <0)
{
buttontop = 1;

}
if (currentmargin.Left > 275.973 && currentmargin.Left < 499.451 && currentmargin.Top > 147.254 && currentmargin.Top < 272.06 )
{
Gameover.Visibility = Visibility.Visible;
button1.Visibility = Visibility.Hidden;

}



}

private void Button1_OnClick(object sender, MouseEventArgs mouseEventArgs)
{
button1.Visibility = Visibility.Hidden;
var margin = new Thickness(random.Next(0, 769), random.Next(0, 399),0,0);
button1.Margin=margin;
score+=1;
scorelabel.Content = score;
}
Expand Down

0 comments on commit b82f4d1

Please sign in to comment.