Prg2/pacman (applying design patterns)
- This is part of Programming 2 2563
เนื้อหา
Overview
In this assignment, you and your friend will apply design patterns to the provided Pacman code. This version of the Pacman game is a 2-player game, where each player tries to score as many points as possible. The first Pacman is controlled by WASD keys, while the other Pacman is controlled by IJKL keys.
Understanding the current code
There are three main classes:
- PacmanGame
- Pacman
- Maze
PacmanGame is the main class. It creates a Maze and 2 Pacman's. It feeds two Pacman's with user inputs. The following is its on_key_pressed method, which looks pretty ugly.
def on_key_pressed(self, event):
if event.char.upper() == 'A':
self.pacman1.set_next_direction(DIR_LEFT)
elif event.char.upper() == 'W':
self.pacman1.set_next_direction(DIR_UP)
elif event.char.upper() == 'S':
self.pacman1.set_next_direction(DIR_DOWN)
elif event.char.upper() == 'D':
self.pacman1.set_next_direction(DIR_RIGHT)
if event.char.upper() == 'J':
self.pacman2.set_next_direction(DIR_LEFT)
elif event.char.upper() == 'I':
self.pacman2.set_next_direction(DIR_UP)
elif event.char.upper() == 'K':
self.pacman2.set_next_direction(DIR_DOWN)
elif event.char.upper() == 'L':
self.pacman2.set_next_direction(DIR_RIGHT)