Pandadeath
  • Home
  • Projects
    • Rio Demo
    • Physics Otome
    • Penda >
      • Part 1
      • Part 2
      • Page 3
      • Part 4
      • Part 5
    • Anne's Book >
      • Prologue
      • Chapter 1
      • Chapter 2
      • Chapter 3
      • Chapter 4
      • Chapter 5
      • Chapter 6
      • Chapter 7
      • Chapter 8
      • Chapter 9
      • Chapter 10
      • Chapter 11
      • Chapter 12
    • Computer Theory >
      • Counting Systems
      • Logic
      • Stacks
    • Nintendo DS Lite
    • physics calculators >
      • Snell's Law
    • Programming Pacman in Python >
      • Level 0
      • Level 1
      • Level 2
      • Level 3
      • Level 4
      • Level 5
      • Level6
      • Level7
      • Level8
      • Level9
      • Level10
      • Level11
      • Level12
      • Level13
      • Level14
      • Level15
    • Laser Tag >
      • Simple laser receiver
      • More Complicated Laser Receiver
  • Math Stuff
    • Vectors
  • Comics
  • Blogs
    • Food Douche
    • Her Blog
    • Bubble Bobble Babble
  • Videos
    • Let's Play >
      • Arcade >
        • Twinkle Star Sprites
        • Aliens Gameplay
        • Gururin
        • Truxton score attempt 1
        • Waku Waku 7
  • About
  • Contact

 Level 4 - Moving From Node To Node Part 2

So we have PacMan jumping from node to node, but we want to actually see him moving from node to node.  So in this section we'll update PacMan's move method so that he does exactly this.  Let's draw out what it is we want to do to make programming it easier.
As before we start on a particular node.  We can press the UP, DOWN, LEFT, or RIGHT keys.  Also, just like before we can only move in a direction that has a connecting node.  So in the picture on the right, the only valid directions are LEFT and DOWN.
Picture
We press the LEFT key and our Pacman entity moves in the left direction towards it's target node.  Pacman continues in that direction until he reaches the target node.  While he is moving between the nodes any key presses will take no effect.  The Left key does not need to be held down either.
Picture
Eventually Pacman will overshoot the target node.  This is how we know that he has reached the target node.  When that happens we set the target node to be our current node.  
Picture
We then re-position Pacman to be on top of the new node.  We then wait for another valid key press that will send Pacman towards another node.
Picture
This isn't the entire PacMan class, just the relevant parts.
​​In the class initialization we add a target that will hold the node PacMan is moving towards.  The direction is a vector that holds unit vectors for UP, DOWN, LEFT, RIGHT,  and the zero vector for no movement.  The speed variable tells PacMan how fast to move between nodes.  You can adjust this if you want him moving faster or slower.

When calling PacMan's move method we need to input dt which is the time passed since we last called PacMan's move method.  This is used so PacMan knows how far he should move in a particular amount of time.  Remember that we can use the basic equation to determine how far one should move:
distance = velocity * time
Here, time is dt.  Velocity is equal to direction * speed.  Direction is a unit vector which just points in the direction we wish to move.  Don't forget to update the move call in the run.py file

In the move class if PacMan is resting on a node (not moving), and if a key press corresponds the node's neighbor in that direction then we'll set that neighbor node as PacMan's target node.  We then set the direction to the appropriate unit vector.

After detecting any key presses we update PacMan's position using the direction vector, speed, and dt as previously mentioned.  This will physically move PacMan around on the screen.

We then need to check to see if we overshot the target node.  To do that we create a method called overshotTarget() which will return True if we overshot the target and False otherwise.  We check to see if we overshot the target by comparing the distances from the node we're moving away from to Pacman and the distance between the two nodes.  When Pacman is between the two nodes then the distance between the two nodes will always be greater than the distance from the node Pacman is moving away from to Pacman.  When Pacman overshoots the target node, then that's when the distance between the node and Pacman is greater than the distance between the node and the target node.  Basically he's moved too far.  So when this happens we set the target node as our current node and adjust Pacman's position to be on top of the new node and set his direction to be the zero vector which means we don't want him to move until we say so.
 def __init__(self, node, tWidth, tHeight):
        self.node = node
        self.target = node
        self.direction = Vector2D()
        self.speed = 100

        self.setPosition()
        self.keyDown = False

​def move(self, dt):
        key_pressed = pygame.key.get_pressed()
        if key_pressed[K_UP] and not self.keyDown:
            if self.node.neighbors["UP"] is not None:
                self.target = self.node.neighbors["UP"]
                self.direction = Vector2D(0, -1)
                self.keyDown = True
        elif key_pressed[K_DOWN] and not self.keyDown:
            if self.node.neighbors["DOWN"] is not None:
                self.target = self.node.neighbors["DOWN"]
                self.direction = Vector2D(0, 1)

                self.keyDown = True
        elif key_pressed[K_LEFT] and not self.keyDown:
            if self.node.neighbors["LEFT"] is not None:
                self.target = self.node.neighbors["LEFT"]
                self.direction = Vector2D(-1, 0)

                self.keyDown = True
        elif key_pressed[K_RIGHT] and not self.keyDown:
            if self.node.neighbors["RIGHT"] is not None:
                self.target = self.node.neighbors["RIGHT"]
                self.direction = Vector2D(1, 0)

                self.keyDown = True

        self.position += self.direction*self.speed*dt
        overshot = self.overshotTarget()
        if overshot:
            self.node = self.target
            self.setPosition()
            self.direction = Vector2D()
            self.keyDown = False


def overshotTarget(self):
        '''Returns True if moved passed target, False otherwise'''
        vec1 = self.target.position - self.node.position
        vec2 = self.position - self.node.position
        nodeToTarget = vec1.magnitudeSquared()
        nodeToSelf = vec2.magnitudeSquared()
        return nodeToSelf > nodeToTarget

This movement type is so common that we'll most definitely use it at some point, if not in this game then in some other game.  You can use this type of movement if your game has a world map like in Super Mario 3.  Each node can be a level and the yellow circle can be Mario and you move him around to choose which level you want to play.  You can also use this type of movement for menu selections.  
 ​This is closer to how we want Pacman to move around.  But we don't want him to stop at each and every node.  Next we'll get PacMan to move around exactly the way we want him to move.  But it should be easier to understand if you understand these two movements.

✕