Movement of ghosts in Pacman

Asked

Viewed 32 times

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.

This is the map by the way: inserir a descrição da imagem aqui

1 answer

0

Your function needs to return one Booleano. The error says that your function is not returning any value, and it really is not. To resolve this error you can do so:

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
            Return True //Bateu na parede, então retorna Verdadeiro
        End If
    Next

    return False //Se chegou até aqui é porque não bateu na parede, então retorna Falso
End Function

However, I don’t think it’s a good thing you do it this way. The book Clean Code, which cites good practice in programming, says that the first rule of the functions is to make a only one thing. It is preferable to leave the more generic function, that is, the function will only be responsible for detecting the collision, and not change the direction of the phantasm:

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

    // Está colidindo?
    If IsColliding(ghost) Then
        // Bateu na parede, muda o movimento
        movement = ghostmovement.right
    End If
    DetectCollection(ghost)
End Sub

Public Function IsColliding(ByVal ghost As PictureBox) As Boolean
    For Each wall As PictureBox In WallList
        If ghost.Bounds.IntersectsWith(wall.Bounds) Then
            Return True //Bateu na parede, então retorna Verdadeiro
        End If
    Next

    Return False //Se chegou até aqui é porque não bateu na parede, então retorna Falso
End Function

This is just an idea but you can get a light from this example

  • 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

  • Yes! I think you can make one While to keep trying to draw directions until you hit nothing and then execute the movement

Browser other questions tagged

You are not signed in. Login or sign up in order to post.