Esempio n. 1
0
    def help(self):
        t = 40
        a = Text(_("Try and get 1 of species in the yellow escape area."),
                 size=t)
        b = Text(_("Click a cell to send the guard there."), size=t)
        b.rect.top = a.rect.bottom + 1
        c = Text(_("Prisoners can escape iff the adjacent hiding space is"),
                 size=t)
        c.rect.top = b.rect.bottom + 1
        d = Text('    ' + _("red and empty or green and occupied."), size=t)
        d.rect.top = c.rect.bottom + 1
        e = Text(_("Hit 'Esc' to return to the menu."), size=t)
        e.rect.top = d.rect.bottom + 1
        f = Text(_("Press 'r' to reset the current game"), size=t)
        f.rect.top = e.rect.bottom + 1
        text = Group((a, b, c, d, e, f))

        helping = True
        while helping:
            self.screen.fill(white)
            text.draw(self.screen)

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    helping = False

            pygame.display.flip()
Esempio n. 2
0
 def __init__(self, game, species, cell, HorF):
     self.game = game
     self.species = species
     self.my_cell = cell
     self.HorF = HorF
     self.prisoners = Group()
     self.escArea = game.escArea
Esempio n. 3
0
    def gameloop(self):
        self.setupBoard()
        playing = True
        while playing:
            self.clock.tick(self.fps)

            text1 = Text(str(self.move_count), size=int(35), color=black)
            text1.rect.center = (600, 450)
            text2 = Text(_("(h)elp"), size=50)
            text2.rect.topleft = (10, 10)
            self.text = Group((text1, text2))

            if (len(self.escArea.prisoners.sprites()) == self.cell_count
                    and self.guard.moving == False):
                pygame.time.wait(3000)
                playing = False

            while Gtk.events_pending():
                Gtk.main_iteration()

            # Handle Input Events
            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    playing, self.running = False, False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.move_count = 0
                        playing = False
                        self.makeMenu()
                        self.cell_count = 1
                    if event.key == pygame.K_r:
                        self.resetGame()
                    elif event.key == pygame.K_h:
                        self.help()

            # update sprites
            self.guards.update()
            self.escArea.prisoners.update()
            for i in range(0, self.cell_count):
                self.cells[i].prisoners.update()
                self.cells[i].getAdjHS().prisoners.update()
                self.cells[i].text.update()

            # draw everything
            self.screen.blit(self.background, (0, 0))
            self.escArea.prisoners.draw(self.screen)
            for i in range(0, self.cell_count):
                a = self.cells[i]
                a.text.draw(self.screen)
                a.getAdjHS().text.draw(self.screen)
                a.prisoners.draw(self.screen)
                a.getAdjHS().prisoners.draw(self.screen)
            self.guards.draw(self.screen)
            self.text.draw(self.screen)

            # finally, refresh the screen
            pygame.display.flip()
Esempio n. 4
0
 def makePrisoners(self):
     self.prisoners = Group()
     self.prisoners.set_pos(self.x, self.y)
     self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y,
                               'left')
     self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y,
                               'right')
     self.prisoners.add((self.prisoner1, self.prisoner2))
Esempio n. 5
0
class EscapeArea():
    def __init__(self, game):
        self.game = game
        self.prisoners = Group()
        self.prisoners.set_pos(600, 450)

    def addPrisoner(self, prisoner):
        self.prisoners.add(prisoner)
Esempio n. 6
0
class EscapeArea():
    def __init__(self, game):
        self.game = game
        self.prisoners = Group()
        self.prisoners.set_pos(600,450)
        
    def addPrisoner(self, prisoner):
        self.prisoners.add(prisoner)
Esempio n. 7
0
    def makeMenu(self):
        self.new_game = False
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill(yellow)
        cell_text = Text(_("Cells"), size = int(160))
        cell_text.rect.center = ((600,450))
        self.text = Group((cell_text))

        prompt_text = Text(_("press any key to begin"), size = int(35))
        prompt_text.rect.center = (600,530)
        self.flashing_text = Group((prompt_text))       
Esempio n. 8
0
    def makeMenu(self):
        self.new_game = False
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill(yellow)
        cell_text = Text(_("Cells"), size=int(160))
        cell_text.rect.center = ((600, 450))
        self.text = Group((cell_text))

        prompt_text = Text(_("press any key to begin"), size=int(35))
        prompt_text.rect.center = (600, 530)
        self.flashing_text = Group((prompt_text))
Esempio n. 9
0
 def __init__(self, game, species, cell, HorF):
     self.game = game
     self.species = species
     self.my_cell = cell
     self.HorF = HorF
     self.prisoners = Group()
     self.escArea = game.escArea
Esempio n. 10
0
    def mainloop(self):
        self.load_all()
        self.makeMenu()
        self.running = True
        count = 0
        while self.running:
            self.clock.tick(self.fps)

            self.screen.blit(self.background, (0, 0))

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if self.cell_count == 2 and not self.new_game:
                        self.new_game = True

            if self.new_game == True:
                if self.cell_count == 9:
                    self.new_game = False
                self.gameloop()
                self.cell_count += 1

            if self.cell_count == 9:
                self.background.fill(black)
                text1 = Text(_("Congratulations"), color=white, size=120)
                text2 = Text(_("You finished in %s moves.") %
                             str(self.move_count),
                             color=white,
                             size=60)
                text2.rect.top = text1.rect.bottom + 10
                self.text = Group((text1, text2))

            self.text.draw(self.background)

            if self.cell_count == 2:
                count += 1
                if (count / (self.fps / 2)) % 2 == 1:
                    self.flashing_text.draw(self.screen)

            pygame.display.flip()
Esempio n. 11
0
    def setPos(self, x, y):
        self.x = x
        self.y = y

        pygame.draw.circle(self.game.background, black, (x, y), 54)
        pygame.draw.circle(self.game.background, white, (x, y), 50)

        text = Text(str(self.species + 1), size=20)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))
Esempio n. 12
0
class Hideout():
    def __init__(self, game, species, cell, HorF):
        self.game = game
        self.species = species
        self.my_cell = cell
        self.HorF = HorF
        self.prisoners = Group()
        self.escArea = game.escArea

    def seti(self, int):
        self.i = int

    def setPos(self, x, y):
        self.x = x
        self.y = y
        self.prisoners.set_pos(self.x, self.y)

        # since we're at it, let's use our crayon to color on the board
        # this is so we can tell if a space is 'hostile' or 'friendly'
        color = red
        if self.HorF == "f":
            color = green
        pygame.draw.circle(self.game.background, black, (x, y), 54)
        pygame.draw.circle(self.game.background, color, (x, y), 50)

        text = Text(str(self.species + 1), size=20, color=white)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))

    def isOccupied(self):
        if len(self.prisoners.sprites()) == 0:
            return False
        return True

    def vacate(self):
        if self.isOccupied():
            x = self.prisoners.sprites()[0]
            self.prisoners.remove(x)
            self.my_cell.prisoners.add(x)

    def canHelp(self):
        return ((self.isOccupied() and self.HorF == "f")
                or (not self.isOccupied() and self.HorF == "h"))

    def escape(self, prisoner):
        self.escArea.addPrisoner(prisoner)

    def addPrisoner(self, prisoner):
        if not self.isOccupied():
            self.prisoners.add((prisoner))
        else:
            self.escape(prisoner)
Esempio n. 13
0
class Hideout():
    def __init__(self, game, species, cell, HorF):
        self.game = game
        self.species = species
        self.my_cell = cell
        self.HorF = HorF
        self.prisoners = Group()
        self.escArea = game.escArea

    def seti(self, int):
        self.i = int

    def setPos(self, x, y):
        self.x = x
        self.y = y
        self.prisoners.set_pos(self.x, self.y)

        # since we're at it, let's use our crayon to color on the board
        # this is so we can tell if a space is 'hostile' or 'friendly'
        color = red
        if self.HorF == "f":
            color = green
        pygame.draw.circle(self.game.background, black, (x,y), 54)
        pygame.draw.circle(self.game.background, color, (x,y), 50)

        text = Text(str(self.species + 1), size = 20, color = white)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))
        
    def isOccupied(self):
        if len(self.prisoners.sprites()) == 0:
            return False
        return True
    
    def vacate(self):
        if self.isOccupied():
            x = self.prisoners.sprites()[0]
            self.prisoners.remove(x)
            self.my_cell.prisoners.add(x)
            
    def canHelp(self):
        return ((self.isOccupied() and self.HorF == "f") or
                (not self.isOccupied() and self.HorF == "h"))
            
    def escape(self, prisoner):
        self.escArea.addPrisoner(prisoner)

    def addPrisoner(self, prisoner):
        if not self.isOccupied():
            self.prisoners.add((prisoner))
        else:
            self.escape(prisoner)
Esempio n. 14
0
    def help(self):
        t = 40
        a = Text(_("Try and get 1 of species in the yellow escape area."), 
                 size = t)
        b = Text(_("Click a cell to send the guard there."), 
                 size = t)
        b.rect.top = a.rect.bottom + 1
        c = Text(_("Prisoners can escape iff the adjacent hiding space is"),
                 size = t)
        c.rect.top = b.rect.bottom + 1
        d = Text('    ' + _("red and empty or green and occupied."), 
                 size = t)
        d.rect.top = c.rect.bottom + 1
        e = Text(_("Hit 'Esc' to return to the menu."), 
                 size = t)
        e.rect.top = d.rect.bottom + 1
        f = Text(_("Press 'r' to reset the current game"), 
                 size = t)
        f.rect.top = e.rect.bottom + 1
        text = Group((a, b, c, d, e, f))

        helping = True
        while helping:
            self.screen.fill(white)   
            text.draw(self.screen)

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                        helping = False

            pygame.display.flip()
Esempio n. 15
0
    def setPos(self, x, y):
        self.x = x
        self.y = y
        self.prisoners.set_pos(self.x, self.y)

        # since we're at it, let's use our crayon to color on the board
        # this is so we can tell if a space is 'hostile' or 'friendly'
        color = red
        if self.HorF == "f":
            color = green
        pygame.draw.circle(self.game.background, black, (x, y), 54)
        pygame.draw.circle(self.game.background, color, (x, y), 50)

        text = Text(str(self.species + 1), size=20, color=white)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))
Esempio n. 16
0
    def mainloop(self):
        self.load_all()
        self.makeMenu()
        self.running = True
        count = 0
        while self.running:
            self.clock.tick(self.fps)

            self.screen.blit(self.background, (0, 0))

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if self.cell_count == 2 and not self.new_game:
                        self.new_game = True

            if self.new_game == True:
                if self.cell_count == 9:
                    self.new_game = False
                self.gameloop()
                self.cell_count += 1
                
            if self.cell_count == 9:
                self.background.fill(black)
                text1 = Text(_("Congratulations"), color = white, size = 120)
                text2 = Text(_("You finished in %s moves.") % str(self.move_count),
                             color = white, size = 60)
                text2.rect.top = text1.rect.bottom + 10
                self.text = Group((text1, text2))
            
            self.text.draw(self.background)
            
           
            if self.cell_count == 2:
                count += 1
                if (count / (self.fps / 2)) % 2 == 1:
                    self.flashing_text.draw(self.screen)
                    
            pygame.display.flip()
Esempio n. 17
0
    def setupBoard(self):
        # make board
        self.background = pygame.Surface(self.screen.get_size()).convert()
        r, g, b = randint(50, 255), 0, randint(50, 150)
        self.background.fill((r, g, b))
        self.drawBoard()

        # make pieces
        self.escArea = EscapeArea(self)

        # a list that corresponds to a hiding space's allignment
        # weather a hiding space is hostile or friendly
        hf = ["h", "f"] * (self.cell_count // 2)  # <- this gives us an int
        if self.cell_count % 2 == 1:
            hf += ["f"]
        shuffle(hf)

        # a list that keeps track of cells
        self.cells = []

        # put the cells in the list
        for i in range(0, self.cell_count):
            x = Cell(self, i)
            self.cells.append(x)

        # create a list of numbers [1 : the number of cells)
        # this is closed on the left
        nums = list(range(1, self.cell_count))
        shuffle(nums)

        n = 0
        while len(nums) != 0:
            x = nums.pop(0)
            y = Hideout(self, x, self.cells[x], hf.pop())
            self.cells[n].setAdjHS(y)
            self.cells[x].setMyHS(y)
            n = x

        y = Hideout(self, 0, self.cells[0], hf.pop())
        self.cells[n].setAdjHS(y)
        self.cells[0].setMyHS(y)

        # now shuffle the cells (and effectively the hiding spaces)
        # this game is not solvable if the number of cells mod 4
        # is 0 or 1 and the corresponding hiding space
        # for each cell is the hiding space to the right of it
        # so we shuffle extra if it happens ;)
        solvable = False
        while not solvable:
            if self.cell_count % 4 == (2 or 3):
                solvable == True
                break
            count = 0
            shuffle(self.cells)
            for i in range(0, self.cell_count):
                a = self.cells[i].species
                b = self.cells[(i + 1) % self.cell_count].adj_hs.species
                if a == b:
                    count += 1
                else:
                    solvable == True
                    break
            if count != self.cell_count:
                solvable = True

        # and let the cells and hiding spaces know where they've been put
        for j in range(0, self.cell_count):
            a = self.cells[j]
            a.seti(j)
            a.getAdjHS().seti(j)
            angle = (((360.0 / self.cell_count) * j) - 90) % 360
            x1, y1 = self.polToCart(245, angle)
            x2, y2 = self.polToCart(140, angle)
            a.setPos(x1, y1)
            a.getAdjHS().setPos(x2, y2)

        # lastly... sprites
        self.guard = Guard(self)
        self.guards = Group((self.guard))
        for i in self.cells:
            i.makePrisoners()
Esempio n. 18
0
class Game():
    def __init__(self, fps=30):
        self.fps = fps

    def load_all(self):
        pygame.init()
        self.cursor = pygame.cursors.compile(cursor.cursor_data)
        pygame.mouse.set_cursor((32, 32), (1, 1), *self.cursor)
        info = pygame.display.Info()
        self.ScreenWidth = info.current_w
        self.ScreenHeight = info.current_h
        self.screen = pygame.display.get_surface()
        if not (self.screen):
            self.screen = pygame.display.set_mode(
                (self.ScreenWidth, self.ScreenHeight), pygame.FULLSCREEN)

        # time stuff
        self.clock = pygame.time.Clock()

        # how many cells
        self.cell_count = 2

        # let's keep score
        self.move_count = 0

    def dist(self, x1, y1, x2, y2):
        return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)

    def polToCart(self, r, angle):
        # here the center is (512, 384)
        angle *= math.pi / 180.0
        x = int(r * math.cos(angle)) + 600
        y = int(r * math.sin(angle)) + 450
        return x, y

    def drawBoard(self):
        # this is the giant colorful thing you see when you start the game
        pos = (600, 450)
        pygame.draw.circle(self.background, black, pos, 354)
        pygame.draw.circle(self.background, (0, 255, 255), pos, 350)
        pygame.draw.circle(self.background, black, pos, 254)
        pygame.draw.circle(self.background, blue, pos, 250)
        pygame.draw.circle(self.background, black, pos, 89)
        pygame.draw.circle(self.background, yellow, pos, 85)

    def setupBoard(self):
        # make board
        self.background = pygame.Surface(self.screen.get_size()).convert()
        r, g, b = randint(50, 255), 0, randint(50, 150)
        self.background.fill((r, g, b))
        self.drawBoard()

        # make pieces
        self.escArea = EscapeArea(self)

        # a list that corresponds to a hiding space's allignment
        # weather a hiding space is hostile or friendly
        hf = ["h", "f"] * (self.cell_count // 2)  # <- this gives us an int
        if self.cell_count % 2 == 1:
            hf += ["f"]
        shuffle(hf)

        # a list that keeps track of cells
        self.cells = []

        # put the cells in the list
        for i in range(0, self.cell_count):
            x = Cell(self, i)
            self.cells.append(x)

        # create a list of numbers [1 : the number of cells)
        # this is closed on the left
        nums = list(range(1, self.cell_count))
        shuffle(nums)

        n = 0
        while len(nums) != 0:
            x = nums.pop(0)
            y = Hideout(self, x, self.cells[x], hf.pop())
            self.cells[n].setAdjHS(y)
            self.cells[x].setMyHS(y)
            n = x

        y = Hideout(self, 0, self.cells[0], hf.pop())
        self.cells[n].setAdjHS(y)
        self.cells[0].setMyHS(y)

        # now shuffle the cells (and effectively the hiding spaces)
        # this game is not solvable if the number of cells mod 4
        # is 0 or 1 and the corresponding hiding space
        # for each cell is the hiding space to the right of it
        # so we shuffle extra if it happens ;)
        solvable = False
        while not solvable:
            if self.cell_count % 4 == (2 or 3):
                solvable == True
                break
            count = 0
            shuffle(self.cells)
            for i in range(0, self.cell_count):
                a = self.cells[i].species
                b = self.cells[(i + 1) % self.cell_count].adj_hs.species
                if a == b:
                    count += 1
                else:
                    solvable == True
                    break
            if count != self.cell_count:
                solvable = True

        # and let the cells and hiding spaces know where they've been put
        for j in range(0, self.cell_count):
            a = self.cells[j]
            a.seti(j)
            a.getAdjHS().seti(j)
            angle = (((360.0 / self.cell_count) * j) - 90) % 360
            x1, y1 = self.polToCart(245, angle)
            x2, y2 = self.polToCart(140, angle)
            a.setPos(x1, y1)
            a.getAdjHS().setPos(x2, y2)

        # lastly... sprites
        self.guard = Guard(self)
        self.guards = Group((self.guard))
        for i in self.cells:
            i.makePrisoners()

    def resetGame(self):
        already_reset = True
        for cell in self.cells:
            if len(cell.prisoners) != 2:
                already_reset = False
        if not already_reset:
            self.move_count += 1
            for cell in self.cells:
                cell.reset()

    def gameloop(self):
        self.setupBoard()
        playing = True
        while playing:
            self.clock.tick(self.fps)

            text1 = Text(str(self.move_count), size=int(35), color=black)
            text1.rect.center = (600, 450)
            text2 = Text(_("(h)elp"), size=50)
            text2.rect.topleft = (10, 10)
            self.text = Group((text1, text2))

            if (len(self.escArea.prisoners.sprites()) == self.cell_count
                    and self.guard.moving == False):
                pygame.time.wait(3000)
                playing = False

            while Gtk.events_pending():
                Gtk.main_iteration()

            # Handle Input Events
            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    playing, self.running = False, False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.move_count = 0
                        playing = False
                        self.makeMenu()
                        self.cell_count = 1
                    if event.key == pygame.K_r:
                        self.resetGame()
                    elif event.key == pygame.K_h:
                        self.help()

            # update sprites
            self.guards.update()
            self.escArea.prisoners.update()
            for i in range(0, self.cell_count):
                self.cells[i].prisoners.update()
                self.cells[i].getAdjHS().prisoners.update()
                self.cells[i].text.update()

            # draw everything
            self.screen.blit(self.background, (0, 0))
            self.escArea.prisoners.draw(self.screen)
            for i in range(0, self.cell_count):
                a = self.cells[i]
                a.text.draw(self.screen)
                a.getAdjHS().text.draw(self.screen)
                a.prisoners.draw(self.screen)
                a.getAdjHS().prisoners.draw(self.screen)
            self.guards.draw(self.screen)
            self.text.draw(self.screen)

            # finally, refresh the screen
            pygame.display.flip()

    def makeMenu(self):
        self.new_game = False
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill(yellow)
        cell_text = Text(_("Cells"), size=int(160))
        cell_text.rect.center = ((600, 450))
        self.text = Group((cell_text))

        prompt_text = Text(_("press any key to begin"), size=int(35))
        prompt_text.rect.center = (600, 530)
        self.flashing_text = Group((prompt_text))

    def help(self):
        t = 40
        a = Text(_("Try and get 1 of species in the yellow escape area."),
                 size=t)
        b = Text(_("Click a cell to send the guard there."), size=t)
        b.rect.top = a.rect.bottom + 1
        c = Text(_("Prisoners can escape iff the adjacent hiding space is"),
                 size=t)
        c.rect.top = b.rect.bottom + 1
        d = Text('    ' + _("red and empty or green and occupied."), size=t)
        d.rect.top = c.rect.bottom + 1
        e = Text(_("Hit 'Esc' to return to the menu."), size=t)
        e.rect.top = d.rect.bottom + 1
        f = Text(_("Press 'r' to reset the current game"), size=t)
        f.rect.top = e.rect.bottom + 1
        text = Group((a, b, c, d, e, f))

        helping = True
        while helping:
            self.screen.fill(white)
            text.draw(self.screen)

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    helping = False

            pygame.display.flip()

    def mainloop(self):
        self.load_all()
        self.makeMenu()
        self.running = True
        count = 0
        while self.running:
            self.clock.tick(self.fps)

            self.screen.blit(self.background, (0, 0))

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if self.cell_count == 2 and not self.new_game:
                        self.new_game = True

            if self.new_game == True:
                if self.cell_count == 9:
                    self.new_game = False
                self.gameloop()
                self.cell_count += 1

            if self.cell_count == 9:
                self.background.fill(black)
                text1 = Text(_("Congratulations"), color=white, size=120)
                text2 = Text(_("You finished in %s moves.") %
                             str(self.move_count),
                             color=white,
                             size=60)
                text2.rect.top = text1.rect.bottom + 10
                self.text = Group((text1, text2))

            self.text.draw(self.background)

            if self.cell_count == 2:
                count += 1
                if (count / (self.fps / 2)) % 2 == 1:
                    self.flashing_text.draw(self.screen)

            pygame.display.flip()
Esempio n. 19
0
class Cell():
    def __init__(self, game, species):
        self.game = game
        self.species = species
    
    def isSelected(self):
        m = pygame.mouse.get_pos()
        d = self.game.dist(m[0], m[1], self.x, self.y)
        if d < 54:
            return True
   
    def seti(self, i):
        self.i = i

    def setPos(self, x, y):
        self.x = x
        self.y = y

        pygame.draw.circle(self.game.background, black, (x, y), 54)
        pygame.draw.circle(self.game.background, white, (x, y), 50)

        text = Text(str(self.species + 1), size = 20)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))

    def makePrisoners(self):
        self.prisoners = Group()
        self.prisoners.set_pos(self.x, self.y)
        self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y, 'left')
        self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y, 'right')
        self.prisoners.add((self.prisoner1, self.prisoner2))
    
    def reset(self):
        self.prisoner1.kill()
        self.prisoner2.kill()
        self.prisoners.add((self.prisoner1, self.prisoner2))

    def setMyHS(self, hs):
        self.my_hs = hs

    def getMyHS(self):
        return self.my_hs

    def setAdjHS(self, hs):
        self.adj_hs = hs

    def getAdjHS(self):
        return self.adj_hs
    
    def addPrisoner(self, prisoner):
        self.prisoners.add(prisoner)

    def update(self):
        if (len(self.prisoners) != 0 and
            self.getAdjHS().canHelp()):
            prisoner = self.prisoners.sprites()[0]
            self.prisoners.remove(prisoner)
            self.getMyHS().addPrisoner(prisoner)
Esempio n. 20
0
 def makePrisoners(self):
     self.prisoners = Group()
     self.prisoners.set_pos(self.x, self.y)
     self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y, 'left')
     self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y, 'right')
     self.prisoners.add((self.prisoner1, self.prisoner2))
Esempio n. 21
0
 def __init__(self, game):
     self.game = game
     self.prisoners = Group()
     self.prisoners.set_pos(600,450)
Esempio n. 22
0
    def gameloop(self):
        self.setupBoard()
        playing = True
        while playing:
            self.clock.tick(self.fps)

            text1 = Text(str(self.move_count), 
                        size = int(35), 
                        color = black)
            text1.rect.center = (600, 450)
            text2 = Text(_("(h)elp"), size = 50)
            text2.rect.topleft = (10, 10)
            self.text = Group((text1, text2))

            if (len(self.escArea.prisoners.sprites()) == self.cell_count
                and self.guard.moving == False):
                pygame.time.wait(3000)
                playing = False

            while Gtk.events_pending():
                Gtk.main_iteration()

            # Handle Input Events
            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    playing, self.running = False, False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.move_count = 0
                        playing = False
                        self.makeMenu()
                        self.cell_count = 1
                    if event.key == pygame.K_r:
                        self.resetGame()
                    elif event.key == pygame.K_h:
                        self.help()
       
            # update sprites
            self.guards.update()
            self.escArea.prisoners.update()
            for i in range(0,self.cell_count):
                self.cells[i].prisoners.update()
                self.cells[i].getAdjHS().prisoners.update()
                self.cells[i].text.update()
                
            # draw everything
            self.screen.blit(self.background, (0, 0))
            self.escArea.prisoners.draw(self.screen)
            for i in range(0,self.cell_count):
                a = self.cells[i]
                a.text.draw(self.screen)
                a.getAdjHS().text.draw(self.screen)
                a.prisoners.draw(self.screen)
                a.getAdjHS().prisoners.draw(self.screen)
            self.guards.draw(self.screen)
            self.text.draw(self.screen)

            # finally, refresh the screen
            pygame.display.flip()
Esempio n. 23
0
 def __init__(self, game):
     self.game = game
     self.prisoners = Group()
     self.prisoners.set_pos(600, 450)
Esempio n. 24
0
class Game():

    def __init__(self, fps = 30):
        self.fps = fps

    def load_all(self):
        pygame.init()
        self.cursor = pygame.cursors.compile(cursor.cursor_data)
        pygame.mouse.set_cursor((32,32), (1,1), *self.cursor)
        info = pygame.display.Info()
        self.ScreenWidth = info.current_w
        self.ScreenHeight = info.current_h
        self.screen = pygame.display.get_surface()
        if not(self.screen):
            self.screen = pygame.display.set_mode((self.ScreenWidth, 
                                        self.ScreenHeight), pygame.FULLSCREEN)

        # time stuff
        self.clock = pygame.time.Clock()
             
        # how many cells
        self.cell_count = 2

        # let's keep score
        self.move_count = 0

    def dist(self, x1, y1, x2, y2):
        return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    
    def polToCart(self, r, angle):
        # here the center is (512, 384)
        angle *= math.pi / 180.0
        x = int(r * math.cos(angle)) + 600
        y = int(r * math.sin(angle)) + 450
        return x, y
        
    def drawBoard(self):
        # this is the giant colorful thing you see when you start the game
        pos = (600, 450)
        pygame.draw.circle(self.background, black, pos, 354)
        pygame.draw.circle(self.background, (0,255,255), pos, 350)
        pygame.draw.circle(self.background, black, pos, 254)
        pygame.draw.circle(self.background, blue, pos, 250)
        pygame.draw.circle(self.background, black, pos, 89)
        pygame.draw.circle(self.background, yellow, pos, 85)

    def setupBoard(self):
        # make board
        self.background = pygame.Surface(self.screen.get_size()).convert()
        r, g, b = randint(50, 255), 0, randint(50, 150)
        self.background.fill((r,g,b))
        self.drawBoard()

        # make pieces
        self.escArea = EscapeArea(self)
        
        # a list that corresponds to a hiding space's allignment
        # weather a hiding space is hostile or friendly
        hf = ["h", "f"] * (self.cell_count / 2) # <- this gives us an int
        if self.cell_count % 2 == 1:
            hf += ["f"]
        shuffle(hf)
            
        # a list that keeps track of cells
        self.cells = []

        # put the cells in the list
        for i in range(0, self.cell_count):
            x = Cell(self, i)
            self.cells.append(x)
        
        # create a list of numbers [1 : the number of cells)
        # this is closed on the left
        nums = range(1, self.cell_count)
        shuffle(nums)

        n = 0
        while len(nums) != 0:
            x = nums.pop(0)
            y = Hideout(self, x, self.cells[x], hf.pop())
            self.cells[n].setAdjHS(y)
            self.cells[x].setMyHS(y)
            n = x

        y = Hideout(self, 0, self.cells[0], hf.pop())
        self.cells[n].setAdjHS(y)
        self.cells[0].setMyHS(y)
       
        # now shuffle the cells (and effectively the hiding spaces)
        # this game is not solvable if the number of cells mod 4
        # is 0 or 1 and the corresponding hiding space
        # for each cell is the hiding space to the right of it
        # so we shuffle extra if it happens ;)
        solvable = False
        while not solvable:
            if self.cell_count % 4 == (2 or 3):
                solvable == True
                break             
            count = 0
            shuffle(self.cells)
            for i in range(0, self.cell_count):
                a = self.cells[i].species 
                b = self.cells[(i + 1) % self.cell_count].adj_hs.species
                if a == b:
                    count += 1
                else:
                    solvable == True
                    break
            if count != self.cell_count:
                solvable = True

        # and let the cells and hiding spaces know where they've been put
        for j in range(0, self.cell_count):
            a = self.cells[j]
            a.seti(j)
            a.getAdjHS().seti(j)
            angle = (((360.0 / self.cell_count) * j) - 90) % 360
            x1, y1 = self.polToCart(245, angle)
            x2, y2 = self.polToCart(140, angle)
            a.setPos(x1, y1)
            a.getAdjHS().setPos(x2, y2)

        # lastly... sprites
        self.guard = Guard(self)
        self.guards = Group((self.guard))
        for i in self.cells:
            i.makePrisoners()

    def resetGame(self):
         already_reset = True
         for cell in self.cells:
             if len(cell.prisoners) != 2:
                 already_reset = False
         if not already_reset:
             self.move_count += 1
             for cell in self.cells:
                 cell.reset()
       
    def gameloop(self):
        self.setupBoard()
        playing = True
        while playing:
            self.clock.tick(self.fps)

            text1 = Text(str(self.move_count), 
                        size = int(35), 
                        color = black)
            text1.rect.center = (600, 450)
            text2 = Text(_("(h)elp"), size = 50)
            text2.rect.topleft = (10, 10)
            self.text = Group((text1, text2))

            if (len(self.escArea.prisoners.sprites()) == self.cell_count
                and self.guard.moving == False):
                pygame.time.wait(3000)
                playing = False

            while Gtk.events_pending():
                Gtk.main_iteration()

            # Handle Input Events
            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    playing, self.running = False, False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.move_count = 0
                        playing = False
                        self.makeMenu()
                        self.cell_count = 1
                    if event.key == pygame.K_r:
                        self.resetGame()
                    elif event.key == pygame.K_h:
                        self.help()
       
            # update sprites
            self.guards.update()
            self.escArea.prisoners.update()
            for i in range(0,self.cell_count):
                self.cells[i].prisoners.update()
                self.cells[i].getAdjHS().prisoners.update()
                self.cells[i].text.update()
                
            # draw everything
            self.screen.blit(self.background, (0, 0))
            self.escArea.prisoners.draw(self.screen)
            for i in range(0,self.cell_count):
                a = self.cells[i]
                a.text.draw(self.screen)
                a.getAdjHS().text.draw(self.screen)
                a.prisoners.draw(self.screen)
                a.getAdjHS().prisoners.draw(self.screen)
            self.guards.draw(self.screen)
            self.text.draw(self.screen)

            # finally, refresh the screen
            pygame.display.flip()
    
    def makeMenu(self):
        self.new_game = False
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill(yellow)
        cell_text = Text(_("Cells"), size = int(160))
        cell_text.rect.center = ((600,450))
        self.text = Group((cell_text))

        prompt_text = Text(_("press any key to begin"), size = int(35))
        prompt_text.rect.center = (600,530)
        self.flashing_text = Group((prompt_text))       

    def help(self):
        t = 40
        a = Text(_("Try and get 1 of species in the yellow escape area."), 
                 size = t)
        b = Text(_("Click a cell to send the guard there."), 
                 size = t)
        b.rect.top = a.rect.bottom + 1
        c = Text(_("Prisoners can escape iff the adjacent hiding space is"),
                 size = t)
        c.rect.top = b.rect.bottom + 1
        d = Text('    ' + _("red and empty or green and occupied."), 
                 size = t)
        d.rect.top = c.rect.bottom + 1
        e = Text(_("Hit 'Esc' to return to the menu."), 
                 size = t)
        e.rect.top = d.rect.bottom + 1
        f = Text(_("Press 'r' to reset the current game"), 
                 size = t)
        f.rect.top = e.rect.bottom + 1
        text = Group((a, b, c, d, e, f))

        helping = True
        while helping:
            self.screen.fill(white)   
            text.draw(self.screen)

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                        helping = False

            pygame.display.flip()
     
    def mainloop(self):
        self.load_all()
        self.makeMenu()
        self.running = True
        count = 0
        while self.running:
            self.clock.tick(self.fps)

            self.screen.blit(self.background, (0, 0))

            while Gtk.events_pending():
                Gtk.main_iteration()

            for event in pygame.event.get():
                # this one is for the box in the top right marked X
                if event.type == pygame.QUIT:
                    self.running = False
                # and this one is for the "ESC" key
                if event.type == pygame.KEYDOWN:
                    if self.cell_count == 2 and not self.new_game:
                        self.new_game = True

            if self.new_game == True:
                if self.cell_count == 9:
                    self.new_game = False
                self.gameloop()
                self.cell_count += 1
                
            if self.cell_count == 9:
                self.background.fill(black)
                text1 = Text(_("Congratulations"), color = white, size = 120)
                text2 = Text(_("You finished in %s moves.") % str(self.move_count),
                             color = white, size = 60)
                text2.rect.top = text1.rect.bottom + 10
                self.text = Group((text1, text2))
            
            self.text.draw(self.background)
            
           
            if self.cell_count == 2:
                count += 1
                if (count / (self.fps / 2)) % 2 == 1:
                    self.flashing_text.draw(self.screen)
                    
            pygame.display.flip()
Esempio n. 25
0
class Cell():
    def __init__(self, game, species):
        self.game = game
        self.species = species

    def isSelected(self):
        m = pygame.mouse.get_pos()
        d = self.game.dist(m[0], m[1], self.x, self.y)
        if d < 54:
            return True

    def seti(self, i):
        self.i = i

    def setPos(self, x, y):
        self.x = x
        self.y = y

        pygame.draw.circle(self.game.background, black, (x, y), 54)
        pygame.draw.circle(self.game.background, white, (x, y), 50)

        text = Text(str(self.species + 1), size=20)
        text.rect.center = (self.x, self.y - 20)
        self.text = Group((text))

    def makePrisoners(self):
        self.prisoners = Group()
        self.prisoners.set_pos(self.x, self.y)
        self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y,
                                  'left')
        self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y,
                                  'right')
        self.prisoners.add((self.prisoner1, self.prisoner2))

    def reset(self):
        self.prisoner1.kill()
        self.prisoner2.kill()
        self.prisoners.add((self.prisoner1, self.prisoner2))

    def setMyHS(self, hs):
        self.my_hs = hs

    def getMyHS(self):
        return self.my_hs

    def setAdjHS(self, hs):
        self.adj_hs = hs

    def getAdjHS(self):
        return self.adj_hs

    def addPrisoner(self, prisoner):
        self.prisoners.add(prisoner)

    def update(self):
        if (len(self.prisoners) != 0 and self.getAdjHS().canHelp()):
            prisoner = self.prisoners.sprites()[0]
            self.prisoners.remove(prisoner)
            self.getMyHS().addPrisoner(prisoner)
Esempio n. 26
0
    def setupBoard(self):
        # make board
        self.background = pygame.Surface(self.screen.get_size()).convert()
        r, g, b = randint(50, 255), 0, randint(50, 150)
        self.background.fill((r,g,b))
        self.drawBoard()

        # make pieces
        self.escArea = EscapeArea(self)
        
        # a list that corresponds to a hiding space's allignment
        # weather a hiding space is hostile or friendly
        hf = ["h", "f"] * (self.cell_count / 2) # <- this gives us an int
        if self.cell_count % 2 == 1:
            hf += ["f"]
        shuffle(hf)
            
        # a list that keeps track of cells
        self.cells = []

        # put the cells in the list
        for i in range(0, self.cell_count):
            x = Cell(self, i)
            self.cells.append(x)
        
        # create a list of numbers [1 : the number of cells)
        # this is closed on the left
        nums = range(1, self.cell_count)
        shuffle(nums)

        n = 0
        while len(nums) != 0:
            x = nums.pop(0)
            y = Hideout(self, x, self.cells[x], hf.pop())
            self.cells[n].setAdjHS(y)
            self.cells[x].setMyHS(y)
            n = x

        y = Hideout(self, 0, self.cells[0], hf.pop())
        self.cells[n].setAdjHS(y)
        self.cells[0].setMyHS(y)
       
        # now shuffle the cells (and effectively the hiding spaces)
        # this game is not solvable if the number of cells mod 4
        # is 0 or 1 and the corresponding hiding space
        # for each cell is the hiding space to the right of it
        # so we shuffle extra if it happens ;)
        solvable = False
        while not solvable:
            if self.cell_count % 4 == (2 or 3):
                solvable == True
                break             
            count = 0
            shuffle(self.cells)
            for i in range(0, self.cell_count):
                a = self.cells[i].species 
                b = self.cells[(i + 1) % self.cell_count].adj_hs.species
                if a == b:
                    count += 1
                else:
                    solvable == True
                    break
            if count != self.cell_count:
                solvable = True

        # and let the cells and hiding spaces know where they've been put
        for j in range(0, self.cell_count):
            a = self.cells[j]
            a.seti(j)
            a.getAdjHS().seti(j)
            angle = (((360.0 / self.cell_count) * j) - 90) % 360
            x1, y1 = self.polToCart(245, angle)
            x2, y2 = self.polToCart(140, angle)
            a.setPos(x1, y1)
            a.getAdjHS().setPos(x2, y2)

        # lastly... sprites
        self.guard = Guard(self)
        self.guards = Group((self.guard))
        for i in self.cells:
            i.makePrisoners()