Skip to content

Instantly share code, notes, and snippets.

@adam-phillipps
Created March 16, 2016 06:06
Show Gist options
  • Save adam-phillipps/4473ce4f9c64eb994dad to your computer and use it in GitHub Desktop.
Save adam-phillipps/4473ce4f9c64eb994dad to your computer and use it in GitHub Desktop.
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;
using System.Windows.Threading;
namespace DemoMovingObject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DispatcherTimer timer = new DispatcherTimer();
private Rectangle fallingSquare;
private int startTop = 80;
private int currentTop;
private int bottom = 330;
public MainWindow()
{
InitializeComponent();
InitializeCanvas();
InitTimer();
}
private void InitTimer()
{
timer.Interval = new TimeSpan(0,0,0,0,25);
timer.Tick += DropMotion;
}
private void DropMotion(object sender, EventArgs e)
{
const int updateDistance = 10;
if (currentTop + updateDistance <= bottom)
{
currentTop += updateDistance;
Canvas.SetTop(fallingSquare, currentTop);
}
else
{
timer.Stop();
}
}
private void DrawLine(int x1, int y1, int x2, int y2)
{
Line line = new Line();
line.Stroke = Brushes.LightGray;
line.StrokeThickness = 2;
line.X1 = x1;
line.X2 = x2;
line.Y1 = y1;
line.Y2 = y2;
Canvas1.Children.Add(line);
}
private void InitializeCanvas()
{
DrawLine(5, 20, 5, 380);
DrawLine(75, 20, 75, 380);
DrawLine(155, 20, 155, 380);
DrawLine(230, 20, 230, 380);
DrawLine(5, 380, 230, 380);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
DropSquare(15, 330);
}
private void DropSquare(int slot, int stopTop)
{
fallingSquare = newSquare(slot);
currentTop = startTop;
bottom = stopTop;
timer.Start();
}
private Rectangle newSquare(int slot)
{
const int size = 50;
Rectangle square = new Rectangle();
square.Fill = Brushes.Gold;
square.Width = square.Height = size;
Canvas1.Children.Add(square);
Canvas.SetTop(square, startTop);
Canvas.SetLeft(square, slot);
return square;
}
}
}
<Window x:Class="DemoMovingObject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="650">
<Window.Resources>
<Style TargetType="Canvas">
<Setter Property="Background" Value="Navy" />
<Setter Property="Height" Value="400" />
<Setter Property="Width" Value="600" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Lime" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="10,20,10,5" />
<Setter Property="Padding" Value="20,5,20,5" />
<Setter Property="FontSize" Value="25" />
</Style>
</Window.Resources>
<Grid>
<Canvas Name="Canvas1" Height="400" Width="600" Background="Navy">
<StackPanel Orientation="Horizontal">
<Button Name="Button1" Content="1" Click="Button1_Click" />
<Button Name="Button2" Content="2" />
<Button Name="Button3" Content="3" />
</StackPanel>
</Canvas>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment