// gameComponents = game1 -> GameComponents //use Wall constructor to incrment the layout of walls in 5x10 grid public Wall(float x, float y, SpriteBatch spriteBatch, GameComponents gameComponents) { //multi array 5 rows down 10 row across BreakableWall = new BreakableWall[5, 10]; float breakableX = x; float breakableY = y; Color color = new Color(); //5 rows of bricks. set colors for (int i = 0; i < 5; i++) { switch (i) { case 0: color = Color.MidnightBlue; break; case 1: color = Color.DarkBlue; break; case 2: color = Color.MediumBlue; break; case 3: color = Color.Blue; break; case 4: color = Color.DodgerBlue; break; case 5: color = Color.SteelBlue; break; } breakableY = y + i * (gameComponents.imgBreakableWall.Height + 1); //for loop to spawn bricks using array created + colors set. for (int j = 0; j < 10; j++) { breakableX = x + j * (gameComponents.imgBreakableWall.Width); BreakableWall Breakable = new BreakableWall(breakableX, breakableY, color, spriteBatch, gameComponents); BreakableWall[i, j] = Breakable; } } }
public bool Move(Wall wall, Platform platform) { if (Visible == false) { return(false); } X = X + XVelocity; Y = Y + YVelocity; //check for wall hits if (X < 1) { X = 1; XVelocity = XVelocity * -1; PlaySound(gameComponents.wallBounceSound); } if (X > ScreenWidth - Width + 5) { X = ScreenWidth - Width + 5; XVelocity = XVelocity * -1; PlaySound(gameComponents.wallBounceSound); } if (Y < 1) { Y = 1; YVelocity = YVelocity * -1; PlaySound(gameComponents.wallBounceSound); } if (Y + Height > ScreenHeight) { Visible = false; Y = 0; PlaySound(gameComponents.missSound); return(false); } //check for platform collision //platform = 70 pixels. divide to determine the angle of the bounce Rectangle platformRect = new Rectangle((int)platform.X, (int)platform.Y, (int)platform.Width, (int)platform.Height); Rectangle ballRect = new Rectangle((int)X, (int)Y, (int)Width, (int)Height); //X position determines direction and angle of deflection of the egg //if ball lands on left or right side it will return in the direction on the same side // closer hits to the edge will make a sharper deflection. if (HitTest(platformRect, ballRect)) { PlaySound(gameComponents.platformSound); int offset = Convert.ToInt32((platform.Width - (platform.X + platform.Width - X + Width / 2))); offset = offset / 5; if (offset < 0) { offset = 0; } switch (offset) { // left edge case 0: XVelocity = -6; break; case 1: XVelocity = -5; break; case 2: XVelocity = -4; break; case 3: XVelocity = -3; break; case 4: XVelocity = -2; break; case 5: XVelocity = -1; break; //center case 8: XVelocity = 3; break; case 9: XVelocity = 4; break; case 10: XVelocity = 5; break; default: XVelocity = 6; break; //right edge } YVelocity = YVelocity * -1; Y = platform.Y - Height + 1; return(true); } bool hitBrick = false; //collisions for (int i = 0; i < 5; i++) { if (hitBrick == false) { for (int j = 0; j < 10; j++) { BreakableWall brick = wall.BreakableWall[i, j]; if (brick.Visible) { Rectangle brickRect = new Rectangle((int)brick.X, (int)brick.Y, (int)brick.Width, (int)brick.Height); if (HitTest(ballRect, brickRect)) { PlaySound(gameComponents.breakSound); brick.Visible = false; Score = Score + 5 - i; YVelocity = YVelocity * -1; bricksCleared++; hitBrick = true; break; } } } } } return(true); }