The fruit is a fairly simple concept. It's just an object that appears at a certain time during the level. If Pacman collides with it then he gets points. If Pacman does not collide with it within a certain time period, then the fruit disappears. The fruit appears twice during each level. The fruit may be different for each successive level and worth more and more points capping out at some value. In the Ms. Pacman games, the fruit hopped around the maze along a certain path and would enter the maze via one portal and leave via the other portal. For now we'll just have the fruit stationary.
The location of the fruit depends on the maze. For this maze the fruit appears directly below the ghost home. As you can see in the picture, the fruit is the green circle. The nodes that it appears between are located at (9, 20) and (18, 20).
When Pacman eats a fruit, it appears in the lower right corner of the screen as well.
To define the fruit, we need to add these to constants to the constants.py file. We'll make our fruit green and give it a value of 8. The value doesn't matter, the last one we used was 7, so 8 is next. They just have to be unique.
We are going to create a new file called fruit.py and create this class inside that file. This is so far very simple. When we create a fruit object we just place that fruit object between two nodes. But how and when do we create this fruit object you may ask? Well read on!
Our Fruit object will inherit Entity just like Pacman and the ghosts do. The fruit isn't navigating the maze like them though. But, maybe they will.
A fruit has a lifespan of 5 seconds. That means if Pacman doesn't eat the fruit within that time limit, the fruit will disappear. So we need to keep track of how much time has passed since the fruit was created. The update method keeps track of that.
Add this so we can set any entity between 2 nodes. It is assumed that the entity already has it's node defined. You just need to supply a direction where the second node should be. Then we just find the position between those 2 nodes. We can use this for Pacman as well since he starts between 2 nodes.
We'll use that method to place Pacman between the two nodes.
Finally we make the necessary changes in the GameController class in the run.py file. We import the Fruit class, give it an initial value of None. Because our fruit object can have a value of None, we check to make sure it is not None before we try to update it.
We'll have a new method similar to the pellet and ghost methods where we place most of the fruit related events. Here we can check to see if Pacman has eaten 50 or 140 pellets. When either of those occurs, we create a Fruit object. Then we need to check to see when and if we can make the fruit object None again.