0
I am developing a Vb.net project that is my attempt to create Pacman with moving pictureboxes.
I am close to finishing, but I have a great difficulty that is to define the movement of ghosts.
As I understand it, it is divided into three phases: 1. Simple random movement, in which it detects collisions with walls and sets a new random direction to move; 2. Pursuit of the Pacman when it is near; 3. Escape of the Pacman (when the Pacman consumes an "Energizer")
I still only have one attempt at code for movement one, based on what I found in research:
Dim movement As ghostmovement
Dim currentLocation As Point
Dim ghostCurrentLocation As Point
Dim random As New Random
Enum ghostmovement
up = 1
down = 2
left = 3
right = 4
End Enum
Private Sub ghosttimer_Tick(sender As Object, e As EventArgs) Handles ghosttimer.Tick
Select Case movement
Case ghostmovement.up
ghost.Top = ghost.Top - 2
Case ghostmovement.down
ghost.Top = ghost.Top + 2
Case ghostmovement.left
ghost.Left = ghost.Left - 2
Case ghostmovement.right
ghost.Left = ghost.Left + 2
End Select
DetectCollection(ghost)
End Sub
Public Function DetectCollection(ByVal ghost As PictureBox) As Boolean
For Each wall As PictureBox In WallList
If ghost.Bounds.IntersectsWith(wall.Bounds) Then
movement = ghostmovement.right
End If
Next
End Function
But even this does not work, and I quote the error: "Function 'Detectcollision' doesn’t Return a value on all code paths. Are you Missing a 'Return' Statement?"
If someone could help me with this mistake and/or give some suggestion or explanation of the other two types of movement I would be very grateful.
Thanks for the answer! I didn’t know what I had done was bad practice, thanks for the warning. But the function responsible for the collision, namely this part: If Iscolliding(ghost) Then // Hit the wall, change movement Movement = ghostmovement.right End If Detectcollection(ghost) End Sub Wouldn’t this be a problem if it collided with a wall on the right? Because it would try to move to the direct and continuously collide with the same
– Pedro Curto
Yes! I think you can make one
While
to keep trying to draw directions until you hit nothing and then execute the movement– Natan Fernandes