100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home

Lecture notes

Object Oriented Programming Wall Collision Guide

1 review
 10 views  1 purchase

This document contains a guide to create a wall collision piece of code in preparation for the assignment.

Preview 1 out of 1  pages

  • July 10, 2022
  • 1
  • 2018/2019
  • Lecture notes
  • N/a
  • All classes
All documents for this subject (12)

1  review

review-writer-avatar

By: MitchK • 2 year ago

avatar-seller
ItsKenzie
Detecting collisions with ‘walls’ made of labels

Here’s a quick way to detect if your player is colliding
with ‘walls’ made of labels



1. Open a new project – call it- MazeWallCollisions
2. Add a panel to the form

3. Add several labels, size them, set autosize =
false, and set backcolor = black as shown
{there’s no need to change the names of these
labels!}
4. Add a picturebox for the player, backcolor red
[or put in an image], name it picMan
5. Now we’ll create our own function to test the collision and prevent the player from going
throught the wall [type in the highlighted code]
Private Function CollidesWithWall(pic As PictureBox) As Boolean
Dim ctl As Control
For Each ctl In Me.Panel1.Controls 'loop through all controls in the panel
If TypeOf ctl Is Label Then 'if they are labels then
If ctl.Bounds.IntersectsWith(pic.Bounds) Then 'test for collision
Return True 'pic has collided with walls so return TRUE
End If
End If
Next
Return False 'no collision with walls so return FALSE
End Function


6. Finally we’ll add the code to move the player using Key_Up event…[type in the highlighted
code]
Private Sub Form1_KeyUp(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Dim locB4Move As Point = picMan.Location

'move man according to key press direction
Select Case e.KeyCode
Case Keys.Up
picMan.Top -= 10
Case Keys.Down
picMan.Top += 10
Case Keys.Left
picMan.Left -= 10
Case Keys.Right
picMan.Left += 10
End Select

If CollidesWithWall(picMan) = True Then
'man has collide with wall reposition to before move
picMan.Location = locB4Move
End If
End Sub

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller ItsKenzie. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for £0.00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

52510 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy revision notes and other study material for 14 years now

Start selling
Free  1x  sold
  • (1)