Solved

# How do I create a collision detection for 4 different objects?

Posted on 2011-10-23
Medium Priority
330 Views
Here is the description of what I need to do:

Game 1

Create 4 rectangles of the same color that bounce off the 4 edges of the window.  I will give you artistic license to determine the initial size.  Start their trajectory at a 45 degree angle (for example, move up and left the same amount).  If any block hits the top or bottom of the window, reverse the up and down movement.  If any block hits the left or right of the window, reverse the side-to-side movement.

Every 10 seconds, increase the speed of 2 of the blocks by some amount.  (Hint:  instead of changing the timer speed, change the amount that the blocks move each tick of the timer.)  Increase the size of the other 2 blocks by some amount. (Hint: to keep the aspect ration, length:width, multiply both sides by the same amount).

Create a 5th rectangle of a different color.  This rectangle should move with your mouse.  Do not allow the user to move the rectangle off the window.  For example, if the user drags the mouse to the right edge of the window, stop the rectangle so its right edge touches the right edge of the window.

Detect collisions between the user's rectangle and the 4 "enemy" rectangles.  If there is a collision, the game is over.  Stop the movement of the "enemies" and display a button to play again or quit.

If the play again button is pressed:

Remove the button
Reset the "enemy" speed to the initial speed
Reset the "enemy" sizes to the initial size
Start the movement again
Create an initial splash screen for your game.  Give the game any name you want.

Here is the code I have
``````Public Class Form1
Private Enemy1dx As Integer = 1
Private Enemy1dy As Integer = 1
Private Enemy2dx As Integer = 1
Private Enemy2dy As Integer = 1
Private Enemy3dx As Integer = 1
Private Enemy3dy As Integer = 1
Private Enemy4dx As Integer = 1
Private Enemy4dy As Integer = 1

Private Counter As Integer = 0

Private Sub TimerMovement_Tick(sender As System.Object, e As System.EventArgs) Handles TimerMovement.Tick
Enemy1.Left += Enemy1dx
Enemy1.Top += Enemy1dy
If Enemy1.Bounds.IntersectsWith(WallTop.Bounds) Then
ElseIf Enemy1.Bounds.IntersectsWith(WallBottom.Bounds) Then
ElseIf Enemy1.Bounds.IntersectsWith(WallRight.Bounds) Then
ElseIf Enemy1.Bounds.IntersectsWith(WallLeft.Bounds) Then
End If

If Enemy1dy > 0 Then
Enemy1dy *= -1
End If

Counter += 1
If Counter > 100 Then
Me.Text = Enemy1.Left.ToString

Counter = 0
End If

Enemy2.Left += Enemy2dx
Enemy2.Top += Enemy2dy
If Enemy2.Bounds.IntersectsWith(WallTop.Bounds) Then
ElseIf Enemy2.Bounds.IntersectsWith(WallBottom.Bounds) Then
ElseIf Enemy2.Bounds.IntersectsWith(WallRight.Bounds) Then
ElseIf Enemy2.Bounds.IntersectsWith(WallLeft.Bounds) Then
End If

If Enemy2dy > 0 Then
Enemy2dy *= -1
End If

Counter += 1
If Counter > 100 Then
Me.Text = Enemy2.Left.ToString

Counter = 0
End If

Enemy3.Left += Enemy3dx
Enemy3.Top += Enemy3dy
If Enemy3.Bounds.IntersectsWith(WallTop.Bounds) Then
ElseIf Enemy3.Bounds.IntersectsWith(WallBottom.Bounds) Then
ElseIf Enemy3.Bounds.IntersectsWith(WallRight.Bounds) Then
ElseIf Enemy3.Bounds.IntersectsWith(WallLeft.Bounds) Then
End If

If Enemy3dy > 0 Then
Enemy3dy *= -1
End If

Counter += 1
If Counter > 100 Then
Me.Text = Enemy3.Left.ToString

Counter = 0
End If

Enemy4.Left += Enemy4dx
Enemy4.Top += Enemy4dy
If Enemy4.Bounds.IntersectsWith(WallTop.Bounds) Then
ElseIf Enemy4.Bounds.IntersectsWith(WallBottom.Bounds) Then
ElseIf Enemy4.Bounds.IntersectsWith(WallRight.Bounds) Then
ElseIf Enemy4.Bounds.IntersectsWith(WallLeft.Bounds) Then
End If

If Enemy4dy > 0 Then
Enemy4dy *= -1
End If

Counter += 1
If Counter > 100 Then
Me.Text = Enemy4.Left.ToString

Counter = 0
End If
End Sub

Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Player.Location = e.Location
checkCollision()

End Sub

Private Sub Player_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Player.MouseMove
'Player.Location = e.Location
Player.Left = Player.Left + e.X - Convert.ToInt32(Player.Width / 2)
Player.Top = Player.Top + e.Y - Convert.ToInt32(Player.Height / 2)
checkCollision()

End Sub

Private Sub checkCollision()
If Player.Bounds.IntersectsWith(Enemy1.Bounds) Then
ElseIf Player.Bounds.IntersectsWith(Enemy2.Bounds) Then
ElseIf Player.Bounds.IntersectsWith(Enemy3.Bounds) Then
ElseIf Player.Bounds.IntersectsWith(Enemy4.Bounds) Then
End If

Me.Text = "Ouch"

End Sub

End Class
``````

How can I get this to work properly? My collision won't stop and my borders (walls) are not stopping anything.
0
Question by:lisa5134

LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 37016127
Add this code to a BLANK Form:
``````Public Class Form1

Private R As New Random

Private InitialSize As New Size(50, 50)
Private Enemies As New List(Of PictureBox)
Private EnemyDirections As New List(Of Point)
Private EnemyStepDistance As New List(Of Integer)
Private StartTime As DateTime
Private ChangeTime As DateTime
Private ChangeTimeInSeconds As Integer = 10

Private WithEvents Walls As New Panel
Private WithEvents Btn As New Button
Private WithEvents Player As New PictureBox
Private WithEvents TimerMovement As New Timer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Btn.Text = "New Game"
Btn.Location = New Point(Btn.Margin.Left, Btn.Margin.Right)

Walls.BorderStyle = BorderStyle.FixedSingle
Walls.Location = New Point(Btn.Left, Btn.Bottom + Btn.Margin.Bottom)
Walls.Size = New Size(Me.ClientRectangle.Width - 2 * Walls.Location.X, Me.ClientRectangle.Height - Walls.Location.Y - Walls.Margin.Bottom)
Walls.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom

TimerMovement.Interval = 50
TimerMovement.Enabled = False

Player.Size = InitialSize
Player.BorderStyle = BorderStyle.FixedSingle
Player.BackColor = Color.Black
Player.Location = New Point(Walls.Width / 2 - Player.Width / 2, Walls.Height / 2 - Player.Height / 2)

Dim PB As New PictureBox
PB.BorderStyle = BorderStyle.FixedSingle
PB.BackColor = Color.Red

PB = New PictureBox
PB.BorderStyle = BorderStyle.FixedSingle
PB.BackColor = Color.Red

PB = New PictureBox
PB.BorderStyle = BorderStyle.FixedSingle
PB.BackColor = Color.Red

PB = New PictureBox
PB.BorderStyle = BorderStyle.FixedSingle
PB.BackColor = Color.Red

Reset()
End Sub

Private Sub Reset()
EnemyDirections.Clear()
EnemyStepDistance.Clear()
For i As Integer = 0 To Enemies.Count - 1
Enemies(i).Size = InitialSize
Select Case i
Case 0
Enemies(i).Location = New Point(InitialSize.Width, InitialSize.Height)

Case 1
Enemies(i).Location = New Point(Walls.Width - 2 * InitialSize.Width, InitialSize.Height)

Case 2
Enemies(i).Location = New Point(Walls.Width - 2 * InitialSize.Width, Walls.Height - 2 * InitialSize.Height)

Case 3
Enemies(i).Location = New Point(InitialSize.Width, Walls.Height - 2 * InitialSize.Height)

End Select
EnemyDirections.Add(New Point(IIf(R.NextDouble > 0.5, 1, -1), IIf(R.NextDouble > 0.5, 1, -1)))
Next
End Sub

Private Sub Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn.Click
Btn.Enabled = False
Reset()

Cursor.Position = Walls.PointToScreen(New Point(Walls.Width / 2, Walls.Height / 2))
Dim RC As Rectangle = Walls.RectangleToScreen(Walls.ClientRectangle)
RC.Inflate(-Player.Width / 2, -Player.Height / 2)
Cursor.Clip = RC

StartTime = DateTime.Now

TimerMovement.Start()
End Sub

Private Sub TimerMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMovement.Tick
For i As Integer = 0 To Enemies.Count - 1
Enemies(i).Location = New Point(Enemies(i).Location.X + EnemyDirections(i).X * EnemyStepDistance(i), Enemies(i).Location.Y + EnemyDirections(i).Y * EnemyStepDistance(i))

If Enemies(i).Top < 0 Then
Enemies(i).Location = New Point(Enemies(i).Location.X, 0)
EnemyDirections(i) = New Point(EnemyDirections(i).X, -1 * EnemyDirections(i).Y)
End If

If Enemies(i).Left < 0 Then
Enemies(i).Location = New Point(0, Enemies(i).Location.Y)
EnemyDirections(i) = New Point(-1 * EnemyDirections(i).X, EnemyDirections(i).Y)
End If

If Enemies(i).Right > Walls.Width Then
Enemies(i).Location = New Point(Walls.Width - Enemies(i).Width, Enemies(i).Location.Y)
EnemyDirections(i) = New Point(-1 * EnemyDirections(i).X, EnemyDirections(i).Y)
End If

If Enemies(i).Bottom > Walls.Height Then
Enemies(i).Location = New Point(Enemies(i).Location.X, Walls.Height - Enemies(i).Height)
EnemyDirections(i) = New Point(EnemyDirections(i).X, -1 * EnemyDirections(i).Y)
End If
Next

Dim ts As TimeSpan = DateTime.Now.Subtract(StartTime)
Dim duration As String = ts.Minutes & ":" & ts.Seconds.ToString("00")
Me.Text = duration

' Check for a Collision
For Each Enemy As PictureBox In Enemies
If Enemy.Bounds.IntersectsWith(Player.Bounds) Then
TimerMovement.Stop()
Cursor.Clip = Nothing

MessageBox.Show("You survived for: " & duration, "Game Over!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Btn.Enabled = True
Exit For
End If
Next

If DateTime.Now > ChangeTime Then
Dim Indexes As New List(Of Integer)

Dim Index As Integer

' Change Speed for two
Index = R.Next(0, Indexes.Count)
EnemyStepDistance(Indexes(Index)) = EnemyStepDistance(Indexes(Index)) + 1
Indexes.RemoveAt(Index)

Index = R.Next(0, Indexes.Count)
EnemyStepDistance(Indexes(Index)) = EnemyStepDistance(Indexes(Index)) + 1
Indexes.RemoveAt(Index)

' Change Size for the other two
For Each Index In Indexes
Enemies(Index).Size = New Size(Enemies(Index).Size.Width * 1.1, Enemies(Index).Size.Height * 1.1)
Next

End If
End Sub

Private Sub MouseMovement(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Player.MouseMove, Walls.MouseMove
If TimerMovement.Enabled Then
Dim pt As Point = Walls.PointToClient(Cursor.Position)
Player.Location = New Point(pt.X - Player.Width / 2, pt.Y - Player.Height / 2)
If Player.Left < 0 Then
Player.Location = New Point(0, Player.Location.Y)
End If
If Player.Top < 0 Then
Player.Location = New Point(Player.Location.X, 0)
End If
If Player.Right > Walls.Width Then
Player.Location = New Point(Walls.Right - Player.Width, Player.Location.Y)
End If
If Player.Bottom > Walls.Height Then
Player.Location = New Point(Player.Location.X, Walls.Height - Player.Height)
End If
End If
End Sub

End Class
``````
Idle-Mind-516026.flv
0

LVL 53

Expert Comment

ID: 37260998
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Screencast - Getting to Know the Pipeline
###### Suggested Courses
Course of the Month8 days, 1 hour left to enroll