2 Replies - 779 Views - Last Post: 20 October 2011 - 05:04 AM Rate Topic: -----

#1 lisa_lingoes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 11

Collision detection

Posted 19 October 2011 - 04:56 PM

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.

How do I code for the collision to make the player stop and how do I code for the other 3 enemies?

Thank you for any help you can give me.


Public Class Form1
    Private Enemy1dx As Integer = 1
    Private Enemy1dy 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(WallBottom.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

    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
        End If
            Me.Text = "Ouch"

    End Sub

    End Class


This post has been edited by modi123_1: 19 October 2011 - 04:59 PM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Collision detection

#2 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Collision detection

Posted 19 October 2011 - 09:53 PM

Look here:
http://www.dreaminco...sion-detection/
Was This Post Helpful? 1
  • +
  • -

#3 lisa_lingoes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 11

Re: Collision detection

Posted 20 October 2011 - 05:04 AM

Thank you for the help. I will read this over and give it a try.

Lisa Lingoes

View Postlisa_lingoes, on 19 October 2011 - 04:56 PM, said:

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.

How do I code for the collision to make the player stop and how do I code for the other 3 enemies?

Thank you for any help you can give me.


Public Class Form1
    Private Enemy1dx As Integer = 1
    Private Enemy1dy 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(WallBottom.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

    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
        End If
            Me.Text = "Ouch"

    End Sub

    End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1