private void level1ToolStripMenuItem_Click(object sender, EventArgs e) { int speedX = 2, speedY = 2; if (this.timer1.Enabled == false) { this.timer1.Enabled = true; } obj = new Object(100, 100, speedX, speedY, dimensionX, dimensionY); racket = new Racket(200, 220, racket_dimensionX, racket_dimensionY); obstacles = new List <Obstacle>(); obstacles.Add(new Obstacle(0, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(110, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(220, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(330, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(440, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(550, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(660, this.menuStrip1.Height, obstacle_dimensionX, obstacle_dimensionY)); obstacles.Add(new Obstacle(45, this.menuStrip1.Height + 30, 120, obstacle_dimensionY)); obstacles.Add(new Obstacle(265, this.menuStrip1.Height + 30, 220, obstacle_dimensionY)); obstacles.Add(new Obstacle(595, this.menuStrip1.Height + 30, 120, obstacle_dimensionY)); obstacles.Add(new Obstacle(45, this.menuStrip1.Height + 50, 120, obstacle_dimensionY)); obstacles.Add(new Obstacle(265, this.menuStrip1.Height + 50, 220, obstacle_dimensionY)); obstacles.Add(new Obstacle(595, this.menuStrip1.Height + 50, 120, obstacle_dimensionY)); obstacles.Add(new Obstacle(265, this.menuStrip1.Height + 70, 220, obstacle_dimensionY)); }
public void CollisionObjectRacket(Object obj, Racket racket, List <Obstacle> obstacles) { bool collision = false; bool wrong_collision = false; if (obj.PositionY + obj.DimensionY >= racket.PositionY && obj.CenterX <= racket.PositionX + racket.DimensionX && obj.CenterX >= racket.PositionX) { collision = true; obj.MoveY = -obj.MoveY; } if (obj.PositionY <= 0 + this.menuStrip1.Height) { collision = true; obj.MoveY = -obj.MoveY; } if (obj.PositionY + obj.DimensionY >= this.ClientSize.Height) { wrong_collision = true; obj.MoveY = -obj.MoveY; } if (obj.PositionX <= 0) { collision = true; obj.MoveX = -obj.MoveX; } if (obj.PositionX + obj.DimensionX >= this.ClientSize.Width) { collision = true; obj.MoveX = -obj.MoveX; } foreach (var obstacle in obstacles) { if (obj.PositionY <= obstacle.PositionY + obstacle.DimensionY && obj.CenterX <= obstacle.PositionX + obstacle.DimensionX && obj.CenterX >= obstacle.PositionX) { collision = true; obstacle_detected = obstacle; counter_score++; obj.MoveY = -obj.MoveY; } } obstacles.Remove(obstacle_detected); Checker_Score(); Checker_ListObstacles(); if (collision) { return; } if (wrong_collision) { this.timer1.Enabled = false; MessageBox.Show("Game Over! Try more!"); } }