-
Nov 23rd, 2022, 11:44 AM
#1
Thread Starter
New Member
How can I dim many pictureboxes into "walls"
Tried this:
Code:
Dim walls() = {wall1, wall2, wall3, wall4, wall5, wall6, wall7, wall8, wall9, wall10, wall11, wall12, wall13, wall14, wall15, wall16, wall17, wall18, wall19, wall20, wall21, wall22, wall23, wall24, wall25, wall26, wall27, wall28, wall29, wall30, wall31}
But when I say
Code:
If player.Bounds.IntersectsWith(walls) Then
player.location = New Point (71, 76)
End If
It says
Value of type '1-dimensional array of Object cannot be converted to 'System.Drawing.Rectangle
What do I do?
-
Nov 23rd, 2022, 11:59 AM
#2
Re: How can I dim many pictureboxes into "walls"
You were already given an example of how to do this in the first reply to one of your earlier threads:
https://www.vbforums.com/showthread....=1#post5586658
-
Nov 23rd, 2022, 12:09 PM
#3
Re: How can I dim many pictureboxes into "walls"
Check out this code by Paul Ishak to generate random mazes of any size:
https://social.msdn.microsoft.com/Fo...orum=vbgeneral
I tested it out and it works great! All you have to do then is to detect the color of the walls ahead of a sprite when you click a directional key. If a pixel is black in the direction you want to move, the sprite will remain at it's current position.
Last edited by Peter Porter; Nov 23rd, 2022 at 12:15 PM.
-
Nov 23rd, 2022, 12:24 PM
#4
Re: How can I dim many pictureboxes into "walls"
Why are you using pictureboxes as walls? I didn't read the other thread, but that seems like a particularly problematic choice. Controls have a fair amount of overhead. Pictureboxes are best used for showing pictures. If all you are showing are solid colors, there are better alternatives.
My usual boring signature: Nothing
 
-
Nov 23rd, 2022, 12:49 PM
#5
Thread Starter
New Member
Re: How can I dim many pictureboxes into "walls"
So how should i make the walls?
-
Nov 23rd, 2022, 12:54 PM
#6
Re: How can I dim many pictureboxes into "walls"
 Originally Posted by Greogri
So how should i make the walls?
Paint them. Visit the link I posted above.
-
Nov 23rd, 2022, 01:08 PM
#7
Thread Starter
New Member
Re: How can I dim many pictureboxes into "walls"
-
Nov 23rd, 2022, 01:16 PM
#8
Re: How can I dim many pictureboxes into "walls"
 Originally Posted by Greogri
Doesn't work
Did you add all the controls needed for it to work?
To make it simpler for you, below is a tweaked version of Paul's code for Form1 with no textboxes.
The only controls you need is two panels and a button. Panel1 docked at top with the button, and Panel2 docked fill.
Form1 Class:
Code:
Option Strict On
Public Class Form1
Dim nudRows As Integer = 40
Dim nudCols As Integer = 60
Dim nudWidth As Integer = 20
Dim nudHeight As Integer = 20
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim maze As New Maze(nudRows, nudCols, nudWidth, nudHeight)
AddHandler maze.MazeComplete, Sub(m As Image)
Panel2.BackgroundImage = m
Panel2.BackgroundImageLayout = ImageLayout.None
Panel2.Width = m.Width
Panel2.Height = m.Height
'maze.PrintMaze()
End Sub
maze.Generate()
End Sub
End Class
Cell Class:
Code:
Option Strict On
Public Class Cell
Public NorthWall As Boolean = True
Public SouthWall As Boolean = True
Public WestWall As Boolean = True
Public EastWall As Boolean = True
Public id As String '
Public Pen As Pen = Pens.Black
Public Bounds As Rectangle '
Public Cells As Dictionary(Of String, Cell) '
Public Column As Integer '
Public Row As Integer '
Public NeighborNorthID As String '
Public NeighborSouthID As String '
Public NeighborEastID As String '
Public NeighborWestID As String '
Public Visited As Boolean = False '
Public Stack As Stack(Of Cell) '
Public Sub draw(ByVal g As Graphics)
If NorthWall Then g.DrawLine(Pen, New Point(Bounds.Left, Bounds.Top), New Point(Bounds.Right, Bounds.Top))
If SouthWall Then g.DrawLine(Pen, New Point(Bounds.Left, Bounds.Bottom), New Point(Bounds.Right, Bounds.Bottom))
If WestWall Then g.DrawLine(Pen, New Point(Bounds.Left, Bounds.Top), New Point(Bounds.Left, Bounds.Bottom))
If EastWall Then g.DrawLine(Pen, New Point(Bounds.Right, Bounds.Top), New Point(Bounds.Right, Bounds.Bottom))
End Sub
Sub New(ByVal location As Point, ByVal size As Size, ByRef cellList As Dictionary(Of String, Cell), ByVal r As Integer, ByVal c As Integer, ByVal maxR As Integer, ByVal maxC As Integer)
Me.Bounds = New Rectangle(location, size)
Me.Column = c
Me.Row = r
Me.id = "c" & c & "r" & r
Dim rowNort As Integer = r - 1
Dim rowSout As Integer = r + 1
Dim colEast As Integer = c + 1
Dim colWest As Integer = c - 1
NeighborNorthID = "c" & c & "r" & rowNort
NeighborSouthID = "c" & c & "r" & rowSout
NeighborEastID = "c" & colEast & "r" & r
NeighborWestID = "c" & colWest & "r" & r
If rowNort < 0 Then NeighborNorthID = "none"
If rowSout > maxR Then NeighborSouthID = "none"
If colEast > maxC Then NeighborEastID = "none"
If colWest < 0 Then NeighborWestID = "none"
Me.Cells = cellList
Me.Cells.Add(Me.id, Me)
End Sub
Function getNeighbor() As Cell
Dim c As New List(Of Cell)
If Not NeighborNorthID = "none" AndAlso Cells(NeighborNorthID).Visited = False Then c.Add(Cells(NeighborNorthID))
If Not NeighborSouthID = "none" AndAlso Cells(NeighborSouthID).Visited = False Then c.Add(Cells(NeighborSouthID))
If Not NeighborEastID = "none" AndAlso Cells(NeighborEastID).Visited = False Then c.Add(Cells(NeighborEastID))
If Not NeighborWestID = "none" AndAlso Cells(NeighborWestID).Visited = False Then c.Add(Cells(NeighborWestID))
Dim max As Integer = c.Count
Dim currentCell As Cell = Nothing
If c.Count > 0 Then
Randomize()
Dim index As Integer = CInt(Int(c.Count * Rnd()))
currentCell = c(index)
End If
Return currentCell
End Function
Function Dig(ByRef stack As Stack(Of Cell)) As Cell
Me.Stack = stack
Dim nextCell As Cell = getNeighbor()
If Not nextCell Is Nothing Then
stack.Push(nextCell)
If nextCell.id = Me.NeighborNorthID Then
Me.NorthWall = False
nextCell.SouthWall = False
ElseIf nextCell.id = Me.NeighborSouthID Then
Me.SouthWall = False
nextCell.NorthWall = False
ElseIf nextCell.id = Me.NeighborEastID Then
Me.EastWall = False
nextCell.WestWall = False
ElseIf nextCell.id = Me.NeighborWestID Then
Me.WestWall = False
nextCell.EastWall = False
End If
ElseIf Not stack.Count = 0 Then
nextCell = stack.Pop
End If
Return nextCell
End Function
End Class
Maze Class:
Code:
Option Strict On
Public Class Maze
Inherits Control
Dim Rows As Integer
Dim Columns As Integer
Dim cellWidth As Integer
Dim cellHeight As Integer
Dim cells As New Dictionary(Of String, Cell)
Dim stack As New Stack(Of Cell)
Public Maze As Image
Public Event MazeComplete(ByVal Maze As Image)
Private Event CallComplete(ByVal Maze As Image)
Public Shadows ReadOnly Property Bounds As Rectangle
Get
Dim rect As New Rectangle(0, 0, Width, Height)
Return rect
End Get
End Property
Dim WithEvents printDoc As New Printing.PrintDocument()
Private Sub PrintImage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage
Dim nonprinters As List(Of String) = ({"Send To OneNote 2013", "PDFCreator", "PDF Architect 4",
"Microsoft XPS Document Writer", "Microsoft Print to PDF", "Fax", "-"}).ToList
Dim printerName As String = "none"
For Each a As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
If nonprinters.IndexOf(a) > -1 Then Continue For
printerName = a
Next
If printerName = "none" Then Exit Sub
printDoc.PrinterSettings.PrinterName = printerName
Dim imageLeft As Integer = CInt(e.PageBounds.Width / 2) - CInt(Maze.Width / 2)
Dim imageTop As Integer = CInt(e.PageBounds.Height / 2) - CInt(Maze.Height / 2)
e.Graphics.DrawImage(Maze, imageLeft, imageTop)
End Sub
Public Sub PrintMaze()
printDoc.Print()
End Sub
Public Sub Generate()
Dim c As Integer = 0
Dim r As Integer = 0
For y As Integer = 0 To Height Step cellHeight
For x As Integer = 0 To Width Step cellWidth
Dim cell As New Cell(New Point(x, y), New Size(cellWidth, cellHeight), cells, r, c, (Rows - 1), (Columns - 1))
c += 1
Next
c = 0 : r += 1
Next
Dim thread As New Threading.Thread(AddressOf Dig)
thread.Start()
End Sub
Function CountVisitedCells() As Integer
Dim count As Integer = 0
For Each cell As KeyValuePair(Of String, Cell) In cells
If cell.Value.Visited Then
count += 1
End If
Next
Return count
End Function
Private Sub Dig()
Dim r As Integer = 0
Dim c As Integer = 0
Dim key As String = "c" & 5 & "r" & 5
Dim startCell As Cell = cells(key)
stack.Clear()
startCell.Visited = True
Dim visitedCells As Integer = 0
Do
startCell = startCell.Dig(stack)
startCell.Visited = True
startCell.Pen = Pens.Black
visitedCells = CountVisitedCells()
Loop Until visitedCells >= Rows * Columns
stack.Clear()
Dim Maze As New Bitmap(Width, Height)
Using g As Graphics = Graphics.FromImage(Maze)
g.Clear(Color.White)
If cells.Count > 0 Then
For r = 0 To Me.Rows - 1
For c = 0 To Me.Columns - 1
Dim cell As Cell = cells("c" & c & "r" & r)
cell.draw(g)
Next
Next
End If
End Using
Me.Maze = Maze
RaiseEvent CallComplete(Maze)
End Sub
Delegate Sub dComplete(ByVal maze As Image)
Private Sub Call_Complete(ByVal maze As Image) Handles Me.CallComplete
If Me.InvokeRequired Then
Me.Invoke(New dComplete(AddressOf Call_Complete), maze)
Else
RaiseEvent MazeComplete(maze)
End If
End Sub
Sub New(ByVal rows As Integer, ByVal columns As Integer, ByVal cellWidth As Integer, ByVal cellHeight As Integer)
Me.Rows = rows
Me.Columns = columns
Me.cellWidth = cellWidth
Me.cellHeight = cellHeight
Me.Width = (Me.Columns * Me.cellWidth) + 1
Me.Height = (Me.Rows * Me.cellHeight) + 1
Me.CreateHandle()
End Sub
End Class
Last edited by Peter Porter; Nov 23rd, 2022 at 01:48 PM.
-
Nov 23rd, 2022, 01:55 PM
#9
Re: How can I dim many pictureboxes into "walls"
To remove the slight flicker when painting a new maze with the code above, add this code to the top of the Form1 Class. It double buffers everything.
Code:
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
CreateParams = MyBase.CreateParams
CreateParams.ExStyle = CreateParams.ExStyle Or &H2000000
Return CreateParams
End Get
End Property
Why do I feel like I just helped someone with their homework?
Last edited by Peter Porter; Nov 23rd, 2022 at 02:04 PM.
-
Nov 23rd, 2022, 05:12 PM
#10
Re: How can I dim many pictureboxes into "walls"
I would use either a GraphicsPath or a Rectangle. It looks like the Cell class uses a rectangle.
Not sure what that Randomize is doing in there. Unless that's some custom method that I didn't see, it shouldn't be there. Randomize was used with Rnd, which also shouldn't be used, but even so, you wouldn't be using it more than once, ever.
My usual boring signature: Nothing
 
-
Nov 23rd, 2022, 05:59 PM
#11
Re: How can I dim many pictureboxes into "walls"
 Originally Posted by Shaggy Hiker
I would use either a GraphicsPath or a Rectangle. It looks like the Cell class uses a rectangle.
Not sure what that Randomize is doing in there. Unless that's some custom method that I didn't see, it shouldn't be there. Randomize was used with Rnd, which also shouldn't be used, but even so, you wouldn't be using it more than once, ever.
Paul's code does need cleaning-up.
That's a nice project to keep Greogri busy!
-
Nov 23rd, 2022, 08:04 PM
#12
Re: How can I dim many pictureboxes into "walls"
 Originally Posted by Greogri
Doesn't work
Then, most likely, you did something wrong. If you don't show us what you did and tell us what actually happened then can't tell wht you did wrong.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|