Exemple #1
0
 def nextlevel(self):
     self.allfoods = AllFood(self, type='dot')
     self.pacman.restart()
     self.portals = []
     self.pacman.laser = None
     self.haunt = Haunt(self)
     for ghost in self.haunt.ghosts.sprites():
         ghost.SPEED = 1.5
Exemple #2
0
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((Game.WIDTH, Game.HEIGHT))
        pg.display.set_caption('Pacman')
        self.back_music = pg.mixer.Sound("sounds/pacman_beginning.wav")
        self.menu = Display(self)

        self.nodes = []  # nodes for game (node/wall, x, y)
        self.walls = []
        self.food_list = []
        self.power_list = []
        self.ghost_start = []
        self.pacman_start = (0, 0)
        self.start_node = ()
        self.bridge = []
        self.fruit = None
        self.create_walls()

        self.bg_color = (40, 40, 40)  # dark grey
        self.finished: bool = False
        self.pacmans_left = 3
        self.pacman = Pacman(self)
        self.reverse_game = False  # used when pacman eats power, ghosts flash

        self.allfoods = AllFood(self, type='dot')
        self.score = 0
        self.haunt = Haunt(self)
        self.portals = []

        self.font = pg.font.Font('freesansbold.ttf', 32)
        self.score_text = self.font.render('Score: ', True, (255, 255, 255),
                                           (40, 40, 40))
        self.textRect1 = self.score_text.get_rect()
        self.textRect1.midleft = (10, 20)
        self.score_text2 = self.font.render('0', True, (255, 255, 255),
                                            (40, 40, 40))
        self.textRect2 = self.score_text2.get_rect()
        self.textRect2.midleft = (150, 20)
        self.lives_text = self.font.render('Lives: ', True, (255, 255, 255),
                                           (40, 40, 40))
        self.textRect3 = self.score_text.get_rect()
        self.textRect3.midleft = (400, 20)
        self.lives_text2 = self.font.render('3', True, (255, 255, 255),
                                            (40, 40, 40))
        self.textRect4 = self.score_text2.get_rect()
        self.textRect4.midleft = (540, 20)
        self.font2 = pg.font.Font('freesansbold.ttf', 75)
        self.gameover_text = self.font2.render('GAME OVER', True,
                                               (255, 255, 255), (40, 40, 40))
        self.gameoverRect = self.gameover_text.get_rect()
        self.gameoverRect.midleft = (75, 400)

        self.start_time = pg.time.get_ticks()
Exemple #3
0
    def restart(self):

        self.pacmans_left -= 1
        # 2 lives left, 1 life left
        # print('{} li{} left'.format(self.pacmans_left, "ves" if self.pacmans_left > 1 else "fe"))
        if self.pacmans_left == 0:
            self.game_over()

        # self.pacman.center()
        # self.pacman.alive = True
        self.pacman.restart()
        self.portals = []
        self.pacman.laser = None

        self.haunt = Haunt(self)
Exemple #4
0
class Game:
    SHIP_SPEED = 5
    PACMAN_SPEED = 1
    WIDTH = 598
    HEIGHT = 705
    SHIPS = 3
    GHOST_DROP = 10

    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((Game.WIDTH, Game.HEIGHT))
        pg.display.set_caption('Pacman')
        self.back_music = pg.mixer.Sound("sounds/pacman_beginning.wav")
        self.menu = Display(self)

        self.nodes = []  # nodes for game (node/wall, x, y)
        self.walls = []
        self.food_list = []
        self.power_list = []
        self.ghost_start = []
        self.pacman_start = (0, 0)
        self.start_node = ()
        self.bridge = []
        self.fruit = None
        self.create_walls()

        self.bg_color = (40, 40, 40)  # dark grey
        self.finished: bool = False
        self.pacmans_left = 3
        self.pacman = Pacman(self)
        self.reverse_game = False  # used when pacman eats power, ghosts flash

        self.allfoods = AllFood(self, type='dot')
        self.score = 0
        self.haunt = Haunt(self)
        self.portals = []

        self.font = pg.font.Font('freesansbold.ttf', 32)
        self.score_text = self.font.render('Score: ', True, (255, 255, 255),
                                           (40, 40, 40))
        self.textRect1 = self.score_text.get_rect()
        self.textRect1.midleft = (10, 20)
        self.score_text2 = self.font.render('0', True, (255, 255, 255),
                                            (40, 40, 40))
        self.textRect2 = self.score_text2.get_rect()
        self.textRect2.midleft = (150, 20)
        self.lives_text = self.font.render('Lives: ', True, (255, 255, 255),
                                           (40, 40, 40))
        self.textRect3 = self.score_text.get_rect()
        self.textRect3.midleft = (400, 20)
        self.lives_text2 = self.font.render('3', True, (255, 255, 255),
                                            (40, 40, 40))
        self.textRect4 = self.score_text2.get_rect()
        self.textRect4.midleft = (540, 20)
        self.font2 = pg.font.Font('freesansbold.ttf', 75)
        self.gameover_text = self.font2.render('GAME OVER', True,
                                               (255, 255, 255), (40, 40, 40))
        self.gameoverRect = self.gameover_text.get_rect()
        self.gameoverRect.midleft = (75, 400)

        self.start_time = pg.time.get_ticks()

    def limit_on_screen(self, rect):
        rect.left = max(0, rect.left)
        rect.right = min(rect.right, self.WIDTH)
        rect.top = max(0, rect.top)
        rect.bottom = min(rect.bottom, self.HEIGHT)

    def process_events(self):
        key_up_down = [pg.KEYDOWN, pg.KEYUP]
        movement = {
            K_RIGHT: Vector(1, 0),
            K_LEFT: Vector(-1, 0),
            K_UP: Vector(0, -1),
            K_DOWN: Vector(0, 1)
        }
        translate = {K_d: K_RIGHT, K_a: K_LEFT, K_w: K_UP, K_s: K_DOWN}
        for event in pg.event.get():
            e_type = event.type
            if e_type in key_up_down:
                k = event.key
                if k in translate.keys() or k in translate.values(
                ):  # movement
                    if k in translate.keys():
                        k = translate[k]
                    # self.ship.velocity = Game.SHIP_SPEED * movement[k]
                    self.pacman.next = Game.PACMAN_SPEED * movement[k]
                elif k == pg.K_SPACE and e_type == pg.KEYDOWN:  # shoot laser
                    # self.pacman.portal()
                    self.pacman.fire()
                    # for p in self.portals:
                    # p.open(self) if p.isclosed else p.close()
                    # return
            elif e_type == QUIT:  # quit
                self.finished = True

    def process_intro_events(self):
        key_up_down = [pg.KEYDOWN, pg.KEYUP]
        for event in pg.event.get():
            e_type = event.type
            if e_type in key_up_down:
                if event.key == pg.mouse.get_pressed():
                    # if e_type in MOUSEBUTTONDOWN:
                    self.menu.button_clicks(self)
            elif e_type == QUIT:  # quit
                self.finished = True

    def create_walls(self):
        size = 13
        # self.walls = []  # List to hold the walls
        # Holds the level layout in a list of strings.
        level = [
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            "X                     XX                     X",
            "X N N N N N N N N N N XX N N N N N N N N N N X",
            "X                     XX                     X",
            "X N XXXXX N XXXXXXX N XX N XXXXXXX N XXXXX N X",
            "X   XXXXX   XXXXXXX   XX   XXXXXXX   XXXXX   X",
            "X P XXXXX N XXXXXXX N XX N XXXXXXX N XXXXX P X",
            "X                     XX                     X",
            "X N N N N N N N N N N XX N N N N N N N N N N X",
            "X                     XX                     X",
            "X N XXXXX N XX N XXXXXXXXXXXX N XX N XXXXX N X",
            "X   XXXXX   XX   XXXXXXXXXXXX   XX   XXXXX   X",
            "X N XXXXX N XX N XXXXXXXXXXXX N XX N XXXXX N X",
            "X           XX        XX        XX           X",
            "X N N N N N XX N N N  XX  N N N XX N N N N N X",
            "X           XX        XX        XX           X",
            "XXXXXXXXX N XXXXXXX   XX   XXXXXXX N XXXXXXXXX",
            "        X   XXXXXXX   XX   XXXXXXX   X        ",
            "        X   XX                  XX   X        ",
            "        X N XX n n n n  n n n n XX N X        ",
            "        X   XX                  XX   X        ",
            "        X N XX n XXXX G  XXXX n XX N X        ",
            "XXXXXXXXX   XX   X          X   XX   XXXXXXXXX",
            "          N    n X          X n    N          ",
            "B      F         X G  G  G  X                B",
            "          N    n X          X n    N          ",
            "XXXXXXXXX   XX   X          X   XX   XXXXXXXXX",
            "        X N XX n XXXXXXXXXXXX n XX N X        ",
            "        X   XX                  XX   X        ",
            "        X N XX n n n n Sn n n n XX N X        ",
            "        X   XX                  XX   X        ",
            "        X N XX n XXXXXXXXXXXX n XX N X        ",
            "XXXXXXXXX   XX   XXXXXXXXXXXX   XX   XXXXXXXXX",
            "X                     XX                     X",
            "X N N N N N N N N N N XX N N N N N N N N N N X",
            "X                     XX                     X",
            "X N XXXXX N XXXXXXX N XX N XXXXXXX N XXXXX N X",
            "X   XXXXX   XXXXXXX   XX   XXXXXXX   XXXXX   X",
            "X      XX                            XX      X",
            "X P N  XX N N N N N N N  N N N N N N XX  N P X",
            "X      XX                            XX      X",
            "XXXX N XX N XX N XXXXXXXXXXXX N XX N XX N XXXX",
            "XXXX   XX   XX   XXXXXXXXXXXX   XX   XX   XXXX",
            "X           XX        XX        XX           X",
            "X N N N N N XX N N N  XX  N N N XX N N N N N X",
            "X           XX        XX        XX           X",
            "X N XXXXXXXXXXXXXXX N XX N XXXXXXXXXXXXXXX N X",
            "X                                            X",
            "X N N N N N N N N N N N  N N N N N N N N N N X",
            "X                                            X",
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        ]

        # Parse the level string above. X = wall, space = exit
        nodesrow = {}
        r, c = 0, 0
        x, y = 0, 40
        for row in level:
            for col in row:
                if col == "X":
                    # Wall(x, y)
                    temp = Wall((x, y))
                    self.walls.append(temp)
                    # nodesrow[c] = Node(r, c, x, y)
                elif col == "N":  # node/food
                    self.food_list.append((x, y))
                    # nodesrow[c] = Node(r, c, x, y)
                    # c += 1
                    self.nodes.append(Node(x, y))
                    # end_rect = pg.Rect(x, y, 16, 16)
                elif col == "G":  # ghost start
                    self.ghost_start.append((x, y))
                    # nodesrow[c] = Node(r, c, x, y)
                    self.nodes.append(Node(x, y))
                    # c += 1
                elif col == "P":  # node/power
                    # self.food_list.append((x, y))
                    self.power_list.append((x, y))
                    # nodesrow[c] = Node(r, c, x, y)
                    # c += 1
                elif col == "S":  # pacman start spot
                    self.pacman_start = (x, y)
                    self.start_node = Node(x, y)
                    self.nodes.append(Node(x, y))
                    # self.start_node = Node(r, c, x, y)
                    # nodesrow[c] = Node(r, c, x, y)
                    # c += 1
                elif col == "n":  # node
                    self.nodes.append(Node(x, y))
                    # nodesrow[c] = Node(r, c, x, y)
                    # c += 1
                elif col == "B":  # bridge to other side
                    self.bridge.append(Wall((x, y)))
                elif col == "F":  # fruit
                    self.fruit = Fruit(self, x, y)
                    # self.fruit = ImageRect(self.screen, 'fruit', height=size, width=size)
                    # self.fruit.x = x
                    # self.fruit.y = y
                x += size
                # c += 1
            y += size
            x = 0
            if nodesrow != {}:
                self.nodes[r] = nodesrow
                r += 1
            nodesrow = {}
            c = 0

    def restart(self):

        self.pacmans_left -= 1
        # 2 lives left, 1 life left
        # print('{} li{} left'.format(self.pacmans_left, "ves" if self.pacmans_left > 1 else "fe"))
        if self.pacmans_left == 0:
            self.game_over()

        # self.pacman.center()
        # self.pacman.alive = True
        self.pacman.restart()
        self.portals = []
        self.pacman.laser = None

        self.haunt = Haunt(self)
        # NEED TO RESTART GHOSTS

    def reverse_game_play(self):
        self.reverse_game = True
        self.haunt.reverse()

    def game_over(self):
        # print("GAME OVER.")
        # quit()
        self.finished = True
        self.screen.blit(self.gameover_text, self.gameoverRect)

    def nextlevel(self):
        self.allfoods = AllFood(self, type='dot')
        self.pacman.restart()
        self.portals = []
        self.pacman.laser = None
        self.haunt = Haunt(self)
        for ghost in self.haunt.ghosts.sprites():
            ghost.SPEED = 1.5

    def update(self):
        self.screen.fill(self.bg_color)

        for wall in self.walls:
            pg.draw.rect(self.screen, (0, 0, 255), wall.rect)

        self.pacman.update()
        self.allfoods.update()
        self.haunt.update()
        self.update_values()

        # if pg.time.get_ticks() - self.start_time > 10:
        # self.fruit.update()

    def update_values(self):
        self.screen.blit(self.score_text, self.textRect1)
        self.score_text2 = self.font.render(str(self.score), True,
                                            (255, 255, 255))
        self.screen.blit(self.score_text2, self.textRect2)
        self.screen.blit(self.lives_text, self.textRect3)
        self.lives_text2 = self.font.render(str(self.pacmans_left), True,
                                            (255, 255, 255))
        self.screen.blit(self.lives_text2, self.textRect4)

    def play(self):
        pg.mixer.Sound.play(self.back_music)
        while self.menu.viewMenu:
            self.menu.draw()
            self.process_intro_events()
            pg.display.update()
        while not self.finished:
            self.process_events()
            self.update()
            pg.display.update()