Skip to content

Commit

Permalink
Bouncing off the edges
Browse files Browse the repository at this point in the history
  • Loading branch information
pburrows committed Oct 29, 2023
1 parent f5fcfaf commit 4622e01
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
19 changes: 18 additions & 1 deletion csharp/BrickOut.Maui/BrickOut.GameLogic/BrickOutGame.cs
Expand Up @@ -2,6 +2,23 @@

public class BrickOutGame
{
public GameBoard GameBoard { get; set; } = GameBoard.NewGame();
public GameBoard GameBoard { get; set; } = GameBoard.NewGame();

public void UpdateBallPosition()
{
var newX = GameBoard.Ball.Location.X + GameBoard.Ball.VelocityX;
var newY = GameBoard.Ball.Location.Y + GameBoard.Ball.VelocityY;

if (newX >= GameBoard.Width -5 || newX <=0)
{
GameBoard.Ball.VelocityX *= -1;
}

if (newY >= GameBoard.Height -5 || newY <= 0)
{
GameBoard.Ball.VelocityY *= -1;
}

GameBoard.Ball.SetLocation(newX, newY);
}
}
2 changes: 1 addition & 1 deletion csharp/BrickOut.Maui/BrickOut.Wpf/MainWindow.xaml
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:BrickOut.Wpf"
mc:Ignorable="d"
Loaded="MainWindow_OnLoaded"
Title="BrickOut" Height="700" Width="480">
Title="BrickOut" Height="700" Width="495">
<Canvas Name="GameCanvas">

</Canvas>
Expand Down
5 changes: 2 additions & 3 deletions csharp/BrickOut.Maui/BrickOut.Wpf/MainWindow.xaml.cs
Expand Up @@ -83,9 +83,8 @@ private void UpdateGameBoard()

private void UpdateBallPosition()
{
var newX = currentGame.GameBoard.Ball.Location.X + currentGame.GameBoard.Ball.VelocityX;
var newY = currentGame.GameBoard.Ball.Location.Y + currentGame.GameBoard.Ball.VelocityY;
currentGame.GameBoard.Ball.SetLocation(newX, newY);
currentGame.UpdateBallPosition();

Canvas.SetLeft(ballRectangle, currentGame.GameBoard.Ball.Location.X);
Canvas.SetTop(ballRectangle, currentGame.GameBoard.Ball.Location.Y);
}
Expand Down

0 comments on commit 4622e01

Please sign in to comment.