Presentation is loading. Please wait.

Presentation is loading. Please wait.

PACMAN OO Style.

Similar presentations


Presentation on theme: "PACMAN OO Style."— Presentation transcript:

1 PACMAN OO Style

2 Pacman: an object oriented view
What aspects of the Pacman game could be made into classes, and why would you choose those? What inheritance would there be, and why?

3 The player’s character moves continuously and autonomously in the last direction the player moves, except at the start of the game where the payer’s character automatically moves to the left Two integer fields are used, called X and Y to represent the character’s position. The coordinate (0,0) is the top left of the screen. On each game cycle, the player’s character moves 2 units in the current direction, unless that would result in hitting a wall, in which case it does not move A variable called dir is used to store the current direction of movement, and holds either the value “U”, “D”, “L” or “R” Game logic

4 Using the rules on the previous slide, write pseudocode for the Move() function of the player’s character. You can assume that there is a function called WillHitWall() that returns true if the player will hit the wall, and false otherwise Pseudocode

5 When the maze has gaps in its vertical edges, the player can move out of the maze to the left, and re-enter on the right, immediately opposite. This also works in reverse, where leaving the right-hand edge results in reappearing on the left. Assume the X coordinate of ‘outside maze’ is 0 on the left, and 968 on the right. The character should move from 0 to 968 or 968 to 0 as appropriate. Game logic

6 Add logic to allow for the aforementioned gaps in the maze walls
Pseudocode 2

7 Compare and contrast How does the player move around the maze? How does a ghost move around the maze? What is the same, and what is different? How could we show this using OO styles?

8 OO Design Base +Common Logic Inherit
Create a BASE class, called GameCharacter +Common Logic Program in the logic designed so far in a function called Move() Inherit Create a Player class, inheriting from GameCharacter Create a Ghost class, inheriting from GameCharacter

9 Draw the class diagram, showing the inheritance
Hierarchy

10 Ghost behavior In normal situations, the ghost is not scared of pacman. How does the ghost move? Consider especially what happens when a ghost can no longer move in its current direction Hint: knowing how many different directions you can choose from when deciding the new direction is useful, because your strategy should differ accordingly

11 Write a method called ChooseDirection() that picks the ghost’s new direction after coming up to a wall Assume there are routines called CanMoveUp(), CanMoveDown(), CanMoveLeft() and CanMoveRight() available, all of which return true if that direction is clear, and false if it isn’t The player is a global variable called player, and it has an X and a Y attribute (player.X and player.Y) Pseudocode 3

12 Ghost behavior 2 When Pacman pops a power pill, two things happen:
The ghosts immediately reverse their direction of travel Logic is different at junctions with two possible choices of direction – how?

13 Add a function called PillEaten() that sets the value of a class variable scared to true and reverses the direction of travel of the ghost. (variable is called dir from earlier) Amend your ChooseDirection() function to take account of this new behaviour Pseudocode 4

14 Ghost behavior 3 Once a ghost becomes scared, they remain in this state for a set period of time. Assume the ghost should remain scared for 500 cycles of the game loop.

15 When they become scared, a value of 500 is set in the variable scareTimer
This reduces by 1 in every game cycle. When it reaches zero, the value of scared is set to false Amend PillEaten() to account for this Amend Move() as appropriate Pseudocode 5


Download ppt "PACMAN OO Style."

Similar presentations


Ads by Google