Skip to content

Commit

Permalink
Handle mousemove and screen updates for the paddle
Browse files Browse the repository at this point in the history
  • Loading branch information
pburrows committed Oct 29, 2023
1 parent 992c902 commit ff3b655
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions csharp/BrickOut.Maui/BrickOut.Wpf/MainWindow.xaml.cs
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using BrickOut.GameLogic;
using Color = System.Windows.Media.Color;
using ColorConverter = System.Windows.Media.ColorConverter;
Expand All @@ -25,6 +26,9 @@ namespace BrickOut.Wpf
public partial class MainWindow : Window
{
public GameBoard CurrentGame { get; set; } = GameBoard.NewGame();
public DispatcherTimer refreshTimer;
public Rectangle paddleRectangle;
public Rectangle ballRectangle;

public MainWindow()
{
Expand All @@ -36,12 +40,22 @@ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
GameCanvas.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F5E2E0"));
GameCanvas.MouseMove += GameCanvasOnMouseMove;
DrawGameBoard();
refreshTimer = new DispatcherTimer();
refreshTimer.Interval = new TimeSpan(0, 0, 0, 0, 33);
refreshTimer.Tick += RefreshTimerOnTick;
refreshTimer.Start();
}

private void RefreshTimerOnTick(object? sender, EventArgs e)
{
UpdateGameBoard();
}


private void GameCanvasOnMouseMove(object sender, MouseEventArgs e)
{
var position = e.GetPosition(GameCanvas);
// CurrentGame.Paddle.Shape // .X = position.X;
CurrentGame.Paddle.Location = new System.Drawing.Point((int)position.X, 0);
}

private List<Color> BrickColors = new()
Expand All @@ -55,6 +69,15 @@ private void GameCanvasOnMouseMove(object sender, MouseEventArgs e)
(Color)ColorConverter.ConvertFromString("#6D2B22"),
};

private void UpdateGameBoard()
{
if (paddleRectangle == null)
{
return;
}

Canvas.SetLeft(paddleRectangle, CurrentGame.Paddle.Location.X);
}

private void DrawGameBoard()
{
Expand All @@ -70,22 +93,22 @@ private void DrawGameBoard()
brick.Shape.Height);
}

AddRectangleToCanvas(
paddleRectangle = AddRectangleToCanvas(
new SolidColorBrush(Colors.Red),
CurrentGame.Paddle.Location.X,
CurrentGame.Paddle.Location.Y,
CurrentGame.Paddle.Shape.Width,
CurrentGame.Paddle.Shape.Height);
AddRectangleToCanvas(
new SolidColorBrush(Colors.Green),
CurrentGame.Ball.Location.X,
CurrentGame.Ball.Location.Y,
CurrentGame.Ball.Shape.Width,
CurrentGame.Ball.Shape.Height);

ballRectangle = AddRectangleToCanvas(
new SolidColorBrush(Colors.Green),
CurrentGame.Ball.Location.X,
CurrentGame.Ball.Location.Y,
CurrentGame.Ball.Shape.Width,
CurrentGame.Ball.Shape.Height);
}

private void AddRectangleToCanvas(SolidColorBrush brush, int x, int y, int width, int height)
private Rectangle AddRectangleToCanvas(SolidColorBrush brush, int x, int y, int width, int height)
{
var wpfRect = new Rectangle();
wpfRect.Fill = brush;
Expand All @@ -94,9 +117,11 @@ private void AddRectangleToCanvas(SolidColorBrush brush, int x, int y, int width
GameCanvas.Children.Add(wpfRect);
Canvas.SetTop(wpfRect, y);
Canvas.SetLeft(wpfRect, x);
return wpfRect;
}

private int nextBrickColorIndex = 0;

private SolidColorBrush GetNextBrickColorBrush()
{
var brush = new SolidColorBrush(BrickColors[nextBrickColorIndex]);
Expand Down

0 comments on commit ff3b655

Please sign in to comment.