C# - Code referenece
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 WpfApp2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private readonly DispatcherTimer animationTimer = new DispatcherTimer();
private bool gehtNachRachts = true;
private bool gehtNachUnten = true;
public int counter;
int score;
private double vel;
public MainWindow()
{
InitializeComponent();
animationTimer.Interval = TimeSpan.FromMilliseconds(20);
animationTimer.Tick += replaceBall;
// Bouncer
Canvas.SetLeft(Bouncer, 300);
Canvas.SetTop(Bouncer,350);
var H = Playground.ActualHeight;
var W = Playground.ActualWidth;
}
private void replaceBall(object sender, EventArgs e)
{
vel = SpeedSlider.Value;
var dx = vel;
var dy = dx;
var x = Canvas.GetLeft(Ball);
Canvas.SetLeft(Ball, x + dx);
// |||| Right
// |||| Left
if (gehtNachRachts)
{
Canvas.SetLeft(Ball, x + dx);
}
else
{
Canvas.SetLeft(Ball, x - dx);
}
if (x >= Playground.ActualWidth - Ball.ActualWidth)
{
gehtNachRachts = false;
}
else if (x <= 0)
{
gehtNachRachts = true;
}
//|||| Up
//|||| Down
var y = Canvas.GetTop(Ball);
// |||| Right
// |||| Left
if (gehtNachUnten)
{
Canvas.SetTop(Ball, y + dy);
}
else
{
Canvas.SetTop(Ball, y - dy);
}
if (y >= Playground.ActualHeight - Ball.ActualHeight)
{
gehtNachUnten = false;
}
else if (y <= 0)
{
gehtNachUnten = true;
}
// check for collision
collision();
}
#region BALL FUNCTIONS
private void Button_Click(object sender, RoutedEventArgs e)
{
var mitteX = Playground.ActualWidth / 2;
var mitteY = Playground.ActualHeight / 2;
Ball.Width = 30;
Ball.Height = 30;
vel = 5;
GameLbl.Content = "";
SpeedSlider.Value = 5;
score = 0;
Canvas.SetLeft(Ball, mitteX);
Canvas.SetTop(Ball, mitteY);
Canvas.SetLeft(Bouncer, mitteX - Bouncer.Width/2);
Canvas.SetTop(Bouncer, mitteY * 1.8);
if (animationTimer.IsEnabled)
{
animationTimer.Stop();
}
else
{
animationTimer.Start();
counter = 0;
ClickLabel.Content = $"... Don't drop the snowflake !";
}
}
private void Ball_MouseUp(object sender, MouseButtonEventArgs e)
{
if (animationTimer.IsEnabled)
{
ClickLabel.Content = "";
}
}
private void Ball_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.F)
{
Ball.Fill = Brushes.Red;
}
}
private void Ball_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F)
{
Ball.Fill = Brushes.White;
}
}
#endregion
#region BOUNCER MOVING FUNCTIONS
private void Bouncer_KeyUp(object sender, KeyEventArgs e)
{
int DX = 120;
var x = Canvas.GetLeft(Bouncer);
if (e.Key == Key.J)
{
if (x <= Bouncer.Width )
{
Canvas.SetLeft(Bouncer, x - x);
}
else
{
Canvas.SetLeft(Bouncer, x - DX);
}
}
if (e.Key == Key.K)
{
var deltaCrash = Playground.ActualWidth - Bouncer.Width;
if (x >= (deltaCrash))
{
Canvas.SetLeft(Bouncer, deltaCrash);
}
else
{
Canvas.SetLeft(Bouncer, x + DX);
}
}
}
#endregion
# region COLLISON
private void collision()
{
double ball_y = Canvas.GetTop(Ball);
double ball_x = Canvas.GetLeft(Ball);
double bouncer_y = Canvas.GetTop(Bouncer);
double bouncer_x = Canvas.GetLeft(Bouncer);
Ycoord.Content = Convert.ToString(Math.Round(ball_y, 2));
Xcoord.Content = Convert.ToString(Math.Round(ball_x, 2));
ClickLabel.Content = "Catch the Snowflake !";
Info.Content = "SCORE:";
GameLbl.Content = "";
if (ball_y >= bouncer_y)
{
if (ball_x < bouncer_x || ball_x > (bouncer_x + Bouncer.Width))
{
GameLbl.Content = " melted ! ";
Ball.Fill.Opacity = 10;
if (Ball.Width > 4)
{
Ball.Width -= 0.3;
Ball.Height -= 0.3;
}
else
{ // game over ..
ClickLabel.Content = "GAME OVER ";
Info.Content = "GAME OVER";
GameLbl.Content = "Game over .. ";
animationTimer.Stop();
}
}
else if( ball_x >bouncer_x && ball_x < (bouncer_x + Bouncer.Width))
{
// collision
gehtNachUnten = false;
score += 1;
ScoreLabel.Content = $"{score}";
if (score % 5 == 0)
{
vel += 3.5;
SpeedSlider.Value = vel;
Info.Content = "GAME OVER";
}
}
}
}
#endregion
}
}