/// <summary> /// Constructor defining the different parameters used in the game loop. /// </summary> /// <param name="form"></param> /// <param name="paddleModel"></param> /// <param name="paddleView"></param> /// <param name="ballModel"></param> /// <param name="ballView"></param> /// <param name="targetBlocksCollection"></param> /// <param name="messageHandler"></param> public GameplayController(GameplayForm form, PlayerPaddle paddleModel, Control paddleView, Ball ballModel, Control ballView, TargetBlocksCollection targetBlocksCollection, IMessageHandler messageHandler) { this.form = form; this.paddleModel = paddleModel; this.paddleView = paddleView; this.ballModel = ballModel; this.ballView = ballView; this.targetBlocks = targetBlocksCollection; this.targetBlocks.BallSpeedToIncrease += ballModel.OnBallSpeedIncreased; this.targetBlocks.EndGame += messageHandler.OnEndGame; this.targetBlocks.EndGame += form.OnEndGame; this.ballModel.EndGame += messageHandler.OnEndGame; this.ballModel.EndGame += form.OnEndGame; BindModelsToViews(); }
private void GameplayForm_Load(object sender, EventArgs e) { PlayerPaddle paddleModel = new PlayerPaddle(this.Width, this.Height, this.Width, this.Height); int ballX = this.Width / 2 - 15; int ballY = this.Height / 2 + 50; Ball ballModel = new Ball(ballX, ballY, this.Width, this.Height); pictureBoxPaddle.Size = new Size(paddleModel.Width, paddleModel.Height); pictureBoxBall.Size = new Size(ballModel.Width, ballModel.Height); TargetBlocksCollection targetBlocksCollection = GenerateTargetBlocks(); SetMessageLocations(); MessageHandler messageHandler = new MessageHandler(this.labelStatus, this.labelMessage, this.labelScore); this.gameplayController = new GameplayController(this, paddleModel, pictureBoxPaddle, ballModel, this.pictureBoxBall, targetBlocksCollection, messageHandler); Application.Idle += RunLoop; loopIsRunning = true; }
/// <summary> /// Changes ball position according to velocity values. /// </summary> /// <param name="targetBlocksCollection">Needed for calculating score should game end.</param> public void MoveBall(TargetBlocksCollection targetBlocksCollection) { this.X += CurrentVelocityX; if (this.X + this.Width >= this.ScreenWidth || this.X <= 0) { ChangeDirectionX(); } this.Y += CurrentVelocityY; if (this.Y <= 0) { ChangeDirectionY(); } if (this.Y + this.Width >= ScreenHeight) { EndGameArgs endGameArgs = new EndGameArgs(false, targetBlocksCollection.DestroyedBlocks); this.EndGame(this, endGameArgs); } }
private TargetBlocksCollection GenerateTargetBlocks() { IList <Control> targetBlockViews = new List <Control>(); TargetBlocksCollection targetBlocks = new TargetBlocksCollection(targetBlockViews); //Horizontal blocks -> 10 //Vertical blocks -> Half of screen height, 6 int countOfBlocksHorizontal = 10; int countOfBlocksVertical = 6; int BlockWidth = this.Width / countOfBlocksHorizontal; int BlockHeight = (this.Height / 2) / countOfBlocksVertical; int y = 0; for (int vertical = 0; vertical < countOfBlocksVertical; vertical++) { int x = 0; for (int horizontal = 0; horizontal < countOfBlocksHorizontal; horizontal++) { int width = 0; int height = 0; Button currentTargetBlockView = new Button(); currentTargetBlockView.FlatStyle = FlatStyle.Flat; currentTargetBlockView.FlatAppearance.BorderColor = Color.CornflowerBlue; currentTargetBlockView.FlatAppearance.BorderSize = 3; currentTargetBlockView.BackColor = Color.DarkGreen; currentTargetBlockView.FlatAppearance.MouseOverBackColor = Color.DarkGreen; currentTargetBlockView.FlatAppearance.MouseDownBackColor = Color.DarkGreen; currentTargetBlockView.Location = new Point(x, y); if (horizontal == countOfBlocksHorizontal - 1) { width = this.Width - x; } else { width = BlockWidth; } if (vertical == countOfBlocksVertical - 1) { height = (this.Height / 2) - y; } else { height = BlockHeight; } currentTargetBlockView.Size = new Size(width, height); TargetBlock targetBlock = new TargetBlock(x, y, width, height, this.Width, this.Height, targetBlocks); this.Controls.Add(currentTargetBlockView); targetBlockViews.Add(currentTargetBlockView); targetBlocks.Add(targetBlock); x += BlockWidth; } y += BlockHeight; } return(targetBlocks); }
public TargetBlock(int x, int y, int width, int height, int screenWidth, int screenHeight, TargetBlocksCollection targetBlocks) : base(x, y, width, height, screenWidth, screenHeight) { this.ID = targetBlocks.Count; this.Destroyed += new TargetBlockEventHandler(targetBlocks.Destroy); }