Skip to content

Commit

Permalink
The ball is moving!
Browse files Browse the repository at this point in the history
  • Loading branch information
pburrows committed Oct 29, 2023
1 parent ff3b655 commit e674da4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
23 changes: 18 additions & 5 deletions csharp/BrickOut.Maui/BrickOut.GameLogic/BreakoutModel.cs
Expand Up @@ -15,9 +15,9 @@ public static GameBoard NewGame()
{
var board = new GameBoard();

board.Paddle = new Paddle(0,635);
board.Paddle = new Paddle(0, 635);
board.Ball = new Ball(20, 630);

// create bricks
for (var row = 0; row < 6; row++)
{
Expand All @@ -34,7 +34,6 @@ public static GameBoard NewGame()
}
}


public interface DisplayItem
{
public Size Shape { get; set; }
Expand All @@ -51,19 +50,26 @@ public Brick(int x, int y)
Shape = new Size(20, 10);
Location = new Point(x, y);
}

}

public class Ball : DisplayItem
{
public Size Shape { get; set; }
public Point Location { get; set; }

public int VelocityX { get; set; } = 8;
public int VelocityY { get; set; } = -8;

public Ball(int x, int y)
{
Shape = new Size(5, 5);
Location = new Point(x, y);
}

public void SetLocation(int x, int y)
{
Location = new Point(x, y);
}
}

public class Paddle : DisplayItem
Expand All @@ -76,6 +82,13 @@ public Paddle(int x, int y)
Shape = new Size(40, 5);
Location = new Point(x, y);
}

public void SetLocation(int x, int y)
{
Location = new Point(x, y);
}
}

public class BrickOutGame {}
public class BrickOutGame
{
}
15 changes: 13 additions & 2 deletions csharp/BrickOut.Maui/BrickOut.Wpf/MainWindow.xaml.cs
Expand Up @@ -55,7 +55,7 @@ private void RefreshTimerOnTick(object? sender, EventArgs e)
private void GameCanvasOnMouseMove(object sender, MouseEventArgs e)
{
var position = e.GetPosition(GameCanvas);
CurrentGame.Paddle.Location = new System.Drawing.Point((int)position.X, 0);
CurrentGame.Paddle.SetLocation((int)position.X, 0);
}

private List<Color> BrickColors = new()
Expand All @@ -75,8 +75,19 @@ private void UpdateGameBoard()
{
return;
}

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

UpdateBallPosition();
}

private void UpdateBallPosition()
{
var newX = CurrentGame.Ball.Location.X + CurrentGame.Ball.VelocityX;
var newY = CurrentGame.Ball.Location.Y + CurrentGame.Ball.VelocityY;
CurrentGame.Ball.SetLocation(newX, newY);
Canvas.SetLeft(ballRectangle, CurrentGame.Ball.Location.X);
Canvas.SetTop(ballRectangle, CurrentGame.Ball.Location.Y);
}

private void DrawGameBoard()
Expand Down

0 comments on commit e674da4

Please sign in to comment.