Ejemplo n.º 1
0
class AboutScene(Scene):
    back_btn = None
    about_labels = []
    version_label = None

    def start(self, screen):
        if not self.already_loaded:
            self.back_btn = Button(
                pygame.rect.Rect(10, 10, 60, 40), "Back", {
                    Options.BORDER_WIDTH: 0,
                    Options.BACKGROUND: (20, 61, 89),
                    Options.FOREGROUND: (244, 180, 26),
                    Options.HOVERED_BACKGROUND: (10, 30, 45),
                    Options.FONT: pygame.font.SysFont("Comic Sans MS", 15)
                })

            text_options = {
                Options.BACKGROUND: (82, 173, 200),
                Options.FOREGROUND: (20, 61, 89),
                Options.BORDER_WIDTH: 0
            }

            about_text = [
                "This game was made by Ritobroto Mukherjee and",
                "Shivam Gupta (who has done absolutely nothing)",
                "as practice for collaboration on a group project."
            ]
            y_offset = 0
            for line in about_text:
                lbl = Label(
                    pygame.Rect(10, 300 + y_offset * 30,
                                screen.get_width() - 10, 30), line,
                    text_options)
                self.about_labels.append(lbl)
                y_offset += 1

            version_text = "PenFight v1.0"
            self.version_label = Label(
                pygame.Rect(screen.get_width() / 2 - 25,
                            screen.get_height() - 50, 50, 30), version_text,
                text_options)

            self.already_loaded = True

    def update(self, event):
        self.back_btn.update(event)

        if self.back_btn.clicked:
            Scene.pop_scene()

    def draw(self, screen):
        screen.fill((82, 173, 200))
        self.back_btn.draw(screen)
        for elt in self.about_labels + [self.version_label]:
            elt.draw(screen)
Ejemplo n.º 2
0
class AboutScene(Scene):
    back_btn = None
    about_labels = []

    def start(self, screen):
        if not self.already_loaded:
            self.back_btn = Button(
                pygame.rect.Rect(10, 10, 60, 40), "Back", {
                    Options.BORDER_WIDTH: 0,
                    Options.BACKGROUND: (20, 61, 89),
                    Options.FOREGROUND: (244, 180, 26),
                    Options.HOVERED_BACKGROUND: (10, 30, 45),
                    Options.FONT: pygame.font.SysFont("Comic Sans MS", 15)
                })

            about_text = "This game was made by Ritobroto Mukherjee and\n" \
                         "Shivam Gupta (who has done absolutely nothing yet)\n" \
                         "as practice for collaboration."
            y_offset = 0
            for line in about_text.splitlines():
                self.about_labels.append(
                    Label(
                        pygame.rect.Rect(10, 200 + y_offset * 30,
                                         screen.get_width() - 10, 30), line, {
                                             Options.BACKGROUND:
                                             (82, 173, 200),
                                             Options.FOREGROUND: (20, 61, 89),
                                             Options.BORDER_WIDTH: 0
                                         }))
                y_offset += 1

            self.already_loaded = True

    def update(self, event):
        self.back_btn.update(event)

        if self.back_btn.clicked:
            Scene.pop_scene()

    def draw(self, screen):
        screen.fill((82, 173, 200))
        self.back_btn.draw(screen)
        for elt in self.about_labels:
            elt.draw(screen)
Ejemplo n.º 3
0
class Main(Template):
    def __init__(self, size):
        Template.__init__(self, size)
        self.name = "World Editor 1.1"
        self.size = size
        self.block = 32
        self.dt = 0.
        self.empty_tile = pg.image.load("./tiles/Empty_tile_32p.png").convert_alpha()
        self.map_bg = Map("Background", self, self.size, (120,80), self.block, self.empty_tile)
        self.map_mg = Map("Middleground", self, self.size, (120,80), self.block, self.empty_tile)
        self.map_fg = Map("Foreground", self, self.size, (120,80), self.block, self.empty_tile)
        self.poi_map = Map("POI Map", self, self.size, (120,80), self.block, self.empty_tile)
        self.poi_menu = Map("POI Menu", self, self.size, (4,1), self.block, self.empty_tile)
        self.menus = [Map("Menu", self, self.size, (2,14), self.block, self.empty_tile)]
        self.pal_menu = Menu((128, 40), (0,21))
        self.pal_menu.add_buttons(2, ["New", "Pal-1"])
        self.pal_menu.set_bg_color(SUBBUTTON_COLOR)
        self.menu = self.menus[0]
        self.menu.xy[1] += 20
        self.men_list = []
        self.sprite_map = self.setup_spritemap()
        self.sprite_map.xy = [250,0]
        self.clip = pg.Rect(0,0, self.size[0],self.size[1])
        self.c_pos = self.clip.topleft
        self.show_full_map = False
        self.show_poi = False
        self.full_map = None
        self.current_tile = None
        self.current_menu = None
        self.ground_state = None
        self.right_m_pos = None
        self.right_m_button = False
        self.fill = False
        self.m_pos = None
        self.m_select_rect = pg.Rect(1,1,1,1)
        self.menu_button = Button((120, 20), (129,1), "Menu")
        self.pal_button = Button((128,20), (0,0), "Palette")
        self.drop_menu = Menu((120, 160), (129, 21))
        self.drop_menu.add_buttons(5, ["Save", "Load", "Sprites", "See Map", "Info"])
        self.drop_menu.set_bg_color(SUBBUTTON_COLOR)
        self.load_menu = self.setup_loadmenu()
        self.floating_text = FloatingText("no text", self.size)
        self.info_panel = Panel((self.size[0] - self.size[0]/3, self.size[1] - self.size[1]/3), (self.size[0]/6, self.size[1]/6))
        self.info_panel.setup_text([self.name, "Made by Aurelio Aguirre", "", "Use WASD to move around.", "Use E to toggle the Foreground.", "Use R to toggle the Middleground.", "Use Q to toggle the Point of Interest map."])
        self.setup()
        self.setup_poi()
        self.selected_map = self.map_bg
        self.alternative = ALTERNATIVE_MODE
        self.enable_alternative_mode()

    def update(self, dt):
        self.dt = dt
        self.map_bg.update(dt)
        if not self.alternative:
            self.map_fg.update(dt)
            self.map_mg.update(dt)
            self.sprite_map.update(dt)
            self.menu.update(dt)
        self.poi_map.update(dt)
        self.poi_menu.update(dt)
        self.floating_text.update(dt)
        if self.drop_menu.buttons[0].active:
            self.savemap()
            self.drop_menu.buttons[0].active = False
        if self.drop_menu.buttons[2].active:
            self.selected_map = self.sprite_map
        else:
            self.selected_map = self.map_bg
        if not self.alternative:
            if self.pal_button.active:
                if self.pal_menu.clickinfo != None:
                    if self.pal_menu.clickinfo == "New":
                        self.new_palette()
                    else:
                        self.switch_palette(text=self.pal_menu.clickinfo)
                    self.pal_menu.clickinfo = None
        if self.map_bg.clipped:
            self.map_fg.map.set_clip()
            self.map_mg.map.set_clip()
            self.poi_map.map.set_clip()
            self.map_bg.clipped = False
        if self.c_pos != self.clip.topleft:
            self.c_pos = (self.c_pos[0] - self.map_bg.xy[0], self.c_pos[1] - self.map_bg.xy[1])
            self.clip.topleft = self.c_pos
        self.menu_button.update(self.dt)
        self.mouse_select()

    def draw(self):
        self.screen.fill(BLACK)

        # MAPs
        # Background
        self.map_bg.draw(self.screen, self.clip)
        if not self.alternative:
            # Middleground
            if self.ground_state == "MG":
                self.map_mg.xy = self.map_bg.xy
                self.map_mg.draw(self.screen, self.clip)

            # Foreground
            if self.ground_state == "FG":
                self.map_fg.xy = self.map_bg.xy
                self.map_fg.draw(self.screen, self.clip)

        # POI Map
        if self.show_poi:
            self.poi_map.xy = self.map_bg.xy
            self.poi_map.draw(self.screen, self.clip)

        # GUI
        if not self.alternative:
            self.menu.draw(self.screen)
        if self.show_poi:
            self.poi_menu.draw(self.screen)
        self.menu_button.draw(self.screen)
        if not self.alternative:
            self.pal_button.draw(self.screen)
        if self.menu_button.active:
            self.drop_menu.draw(self.screen)
        if self.pal_button.active:
            self.pal_menu.draw(self.screen)
        if self.drop_menu.buttons[1].active:
            self.load_menu.draw(self.screen)
        if self.drop_menu.buttons[2].active:
            self.sprite_map.draw(self.screen)
        if self.right_m_button:
            screen_rect = self.m_select_rect.copy()
            screen_rect.x += self.map_bg.xy[0]
            screen_rect.y += self.map_bg.xy[1]
            pg.draw.rect(self.screen, WHITE, screen_rect, 1)
        if self.show_full_map:
            if self.full_map:
                self.screen.blit(self.full_map, (0,0))
        self.info_panel.draw(self.screen)
        self.floating_text.draw(self.screen)

########################## Setup methods #######################################

    def enable_alternative_mode(self):
        if self.alternative:
            bgimage = pg.image.load("./img/CI Main 128.png").convert_alpha()
            self.map_bg = Map("Static Image", self, self.size, (120,80), 128, bgimage)
            self.map_bg.setup(BG_COLOR, alt=True)
            self.alternative = True

    def setup_loadmenu(self):
        folder = Path("./save")
        filelist = [f.basename() for f in folder.files("*.sav")]
        amount = len(filelist)
        menu = Menu((240, amount*21), (250, 21))
        menu.add_buttons(amount, filelist)
        menu.set_bg_color(WHITE)
        return menu

    def setup(self):
        self.map_bg.setup(BG_COLOR)
        self.map_fg.setup(FG_COLOR, 200)
        self.map_mg.setup(MG_COLOR, 200)
        self.menu.setup(PAL_COLOR)
        self.poi_map.setup(POI_MAP, alpha=150)
        self.poi_menu.setup(POI_MENU)

    def setup_poi(self):
        wall = pg.image.load("./tiles/Wall32.png").convert_alpha()
        door = pg.image.load("./tiles/Door32.png").convert_alpha()
        poi = pg.image.load("./tiles/POI32.png").convert_alpha()
        delete = pg.image.load("./tiles/Eliminate32.png").convert_alpha()
        self.poi_dict = OrderedDict((("Poi Wall", wall), ("Poi Door", door), ("Poi Symbol", poi), ("Delete", delete)))
        p = [i for i in self.poi_dict.keys()]
        for num, tile in enumerate(self.poi_menu.group):
            tile.image = self.poi_dict[p[num]]
            tile.rect = tile.image.get_rect()
            tile.rect.topleft = ((self.block*num), 1)
            tile.filename = p[num]
            tile.dirty = 1
        xplace = self.size[0] - self.block*4
        self.poi_menu.xy = [xplace, 1]

    def setup_spritemap(self):
        sheet = SpriteSheet("img/magecity_64p.png", 64, (0,0), (8,44))
        self.men_list = sum(sheet.image_list, [])
        length = len(self.men_list)
        size = (10, int(length/10))
        menu = Map("Palette", self, self.size, size, self.block, self.empty_tile)
        menu.setup(WHITE)
        for i, tile in enumerate(menu.group):
            tile.filename = "{}".format(i)
            tile.image = self.men_list[i]
            if i+1 >= length:
                break
        return menu

#################### Key and Mouse Methods #####################################

    def key_down(self, key):
        if key == K_ESCAPE:
            print("Quitting...")
            self.game_on == False
            self.end_game()

        if key == K_e:
            if self.ground_state == None or self.ground_state == "MG":
                self.ground_state = "FG"
            else:
                self.ground_state = None

        if key == K_r:
            if self.ground_state == None or self.ground_state == "FG":
                self.ground_state = "MG"
            else:
                self.ground_state = None

        if key == K_q:
            self.show_poi = not self.show_poi
            self.current_tile = None

        if key in (K_w, K_UP):
            self.selected_map.move["up"] = True
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = True
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = True
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = True

    def key_up(self, key):
        if key in (K_w, K_UP):
            self.selected_map.move["up"] = False
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = False
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = False
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = False

    def mouse_down(self, button, pos):
        not_menu = True
        if button == 1:
            # First we check for the unique states, Info Panel and Full Map.
            if not self.info_panel.display:
                if not self.show_full_map:
                    # Now we check for menu buttons being clicked, and switch our
                    # not_menu switch if they are. From here the rest is handled by update.
                    if self.menu_button.click(pos):
                        not_menu = False
                    elif self.pal_button.click(pos):
                        not_menu = False
                    if self.menu_button.active:
                        if self.drop_menu.click(pos):
                            not_menu = False
                    if self.pal_button.active:
                        if self.pal_menu.click(pos):
                            not_menu = False
                    # Assuming we are in our main menu we check for the the buttons being clicked.
                    # This part is allowed to happen WHILE maps are also being manipulated. (not_menu is not switched)
                    if self.drop_menu.buttons[1].active:
                        if self.load_menu.click(pos):
                            self.loadmap(self.load_menu.clickinfo)
                    elif self.drop_menu.buttons[3].active:
                        self.show_map()
                        self.drop_menu.buttons[3].active = False
                    elif self.drop_menu.buttons[4].active:
                        self.info_panel.display_panel()
                        self.drop_menu.buttons[4].active = False
                    # Here we look for our Maps. Basically we are moving a tile from one map to another.
                    # We just need to pick the right one, and they should all overwrite each other.
                    # In other words, only one map is active at a time.
                    if not_menu:
                        if self.drop_menu.buttons[2].active: # Tiles are showing.
                            self.find_tile(pos, self.sprite_map, self.menu)
                        elif self.show_poi: # POI Map is up.
                            self.find_tile(pos, self.poi_menu, self.poi_map)
                        elif self.ground_state == "FG": # we are on the Foreground map.
                            self.find_tile(pos, self.menu, self.map_fg)
                        elif self.ground_state == "MG": # We are on the middleground map.
                            self.find_tile(pos, self.menu, self.map_mg)
                        else: # We default to the Background map.
                            self.find_tile(pos, self.menu, self.map_bg)
                else:
                    # While the full map is up, any left mouse click will deactivate it.
                    self.show_full_map = False
            else:
                self.info_panel.click(pos)

        if button == 3:
            if not self.show_full_map:
                self.right_m_button = True
                self.right_m_pos = pos
            else:
                self.take_image(self.full_map)

    def mouse_up(self, button, pos):
        if button == 3:
            self.right_m_button = False
            self.right_m_pos = pos
            new_rect = self._invert_rect(self.m_select_rect)

            if self.show_poi:
                self.group_select(new_rect, self.poi_map.group)
            elif self.ground_state == "FG":
                self.group_select(new_rect, self.map_fg.group)
            elif self.ground_state == "MG":
                self.group_select(new_rect, self.map_mg.group)
            else:
                self.group_select(new_rect, self.map_bg.group)

    def mouse_motion(self, button, pos, rel):
        self.m_pos = pos

    def mouse_select(self):
        if self.right_m_button:
            old_pos = (self.right_m_pos[0] - self.map_bg.xy[0], self.right_m_pos[1] - self.map_bg.xy[1])
            new_pos = (self.m_pos[0] - self.map_bg.xy[0], self.m_pos[1] - self.map_bg.xy[1])
            self.m_select_rect = pg.Rect(1,1,1,1)
            xoff, yoff = new_pos[0] - old_pos[0], new_pos[1] - old_pos[1]
            self.m_select_rect.inflate_ip(xoff, yoff)
            self.m_select_rect.topleft = old_pos

    def _invert_rect(self, rect):
            new_rect = pg.Rect(1,1,1,1)
            new_rect.size = rect.size
            if rect.width < 0:
                new_rect.width *= -1
            if rect.height < 0:
                new_rect.height *= -1
            if rect.left > rect.right:
                new_rect.left = rect.right
            else:
                new_rect.left = rect.left
            if rect.top > rect.bottom:
                new_rect.top = rect.bottom
            else:
                new_rect.top = rect.top
            return new_rect

###################### Button Activated Methods ################################

    def group_select(self, rect, group):
        for sprite in group:
            if sprite.rect.colliderect(rect):
                self.change_tile(sprite)

    def find_tile(self, pos, menu, bg_map):
        found = False
        for tile in menu.group:
            map_pos = (pos[0] - menu.xy[0], pos[1] - menu.xy[1])
            if tile.rect.collidepoint(map_pos):
                found = True
                menu.fill = True
                menu.select_rect(tile.rect)
                menu.draw_border = True
                self.current_tile = tile
                self.current_menu = menu.name
            if found:
                break
        if not found:
            for tile in bg_map.group:
                map_pos = (pos[0] - bg_map.xy[0], pos[1] - bg_map.xy[1])
                if tile.rect.collidepoint(map_pos):
                    self.change_tile(tile)
                    bg_map.clear_map()
        return found

    def change_tile(self, tile):
        if self.current_tile != None:
            if self.current_tile.filename != "Delete":
                tile.filename = self.current_tile.filename
                tile.image = self.current_tile.image
                tile.dirty = 1
            else:
                print("Removing Tile!")
                tile.filename = "Empty_tile"
                tile.image = self.empty_tile
                tile.dirty = 1

    def new_palette(self):
        self.menus.append(Map("Menu", self, self.size, (2,14), self.block, self.empty_tile))
        self.menus[-1].setup(WHITE)

        size = self.pal_menu.size
        self.pal_menu.change_size((size[0], size[1]+20))
        num = len(self.menus)
        self.pal_menu.add_buttons(1, ["Pal-{}".format(num)])
        self.switch_palette(ind=-1)

    def switch_palette(self, ind=None, text=None):
        if ind:
            self.menu = self.menus[ind]
        elif text:
            self.menu = self.menus[int(text[-1])-1]

# <-------This Needs adjustment, needs to differentiate between Layers-------->
    def show_map(self):
        full_map = pg.Surface((40*64, 40*64))
        for tile in self.map_bg.group:
            full_map.blit(tile.image, tile.rect.topleft)
        for tile in self.map_mg.group:
            if tile.filename != "Empty_tile":
                full_map.blit(tile.image, tile.rect.topleft)
        for tile in self.map_fg.group:
            if tile.filename != "Empty_tile":
                full_map.blit(tile.image, tile.rect.topleft)
        self.full_map = pg.transform.smoothscale(full_map, self.size)
        self.show_full_map = True

    def take_image(self, surf):
        folder = Path("./image")
        amount = len(folder.files("*.jpg"))
        filename = "{}{}{}{}".format(folder, "/map", amount, ".jpg")
        pg.image.save(surf, filename)

    def savemap(self):
        if not self.alternative:
            saving = SaveMap()
            saving.add_map(self.map_bg)
            saving.add_map(self.map_mg)
            saving.add_map(self.map_fg)
            saving.add_map(self.poi_map)
            saving.write_to_file()
        else:
            saving = SaveMap()
            saving.add_map(self.poi_map)
            saving.write_to_file()

    def loadmap(self, filename):
        load = LoadMap(filename)
        maplist = []
        for m in load.maps.values():
            a_map = Map(m["info"]["name"], self, m["info"]["size"], m["info"]["grid"], m["info"]["block"], self.empty_tile)
            if m["info"]["name"] == "Foreground" or m["info"]["name"] == "Middleground":
                a_map.setup(m["info"]["color"], 200)
            elif m["info"]["name"] == "POI Map":
                a_map.setup(m["info"]["color"], 150)
            else:
                a_map.setup(m["info"]["color"])
            for t in a_map.group:
                for i in m["tiles"]:
                    if list(t.rect.topleft) in m["tiles"][i]:
                        t.filename = i
                        if i[0] == "P":
                            t.image = self.poi_dict[i]
                            t.dirty = 1
                        else:
                            t.image = self.men_list[int(i)]
                            t.dirty = 1
            maplist.append(a_map)
        for j in maplist:
            if j.name == "Background":
                self.map_bg = j
            if j.name == "Middleground":
                self.map_mg = j
            if j.name == "Foreground":
                self.map_fg = j
            if j.name == "POI Map":
                self.poi_map = j
        print("Loading from file...")
Ejemplo n.º 4
0
class Main(Template):
    def __init__(self, size):
        Template.__init__(self, size)
        self.size = size
        self.block = 64
        self.dt = 0.
        self.map1 = Map(self.size, (40, 40), self.block,
                        "./tiles/Empty_tile_64p.png")
        self.map2 = Map(self.size, (40, 40), self.block,
                        "./tiles/Empty_tile_64p.png")
        self.menu = Map(self.size, (2, 14), self.block,
                        "./tiles/Empty_tile_64p.png")
        self.menu_list = []
        self.palette = self.setup_menu()
        self.palette.xy = [250, 0]
        self.clip = pg.Rect(0, 0, self.size[0], self.size[1])
        self.c_pos = self.clip.topleft
        self.current_tile = None
        self.show_foreground = -1
        self.right_m_button = False
        self.right_m_pos = None
        self.m_pos = None
        self.fill = False
        self.m_select_rect = pg.Rect(1, 1, 1, 1)
        self.button1 = Button((120, 20), (129, 1), "Menu")
        self.drop_menu = Menu((120, 160), (129, 21))
        self.drop_menu.add_buttons(4, ["Save", "Load", "Palette", "Info"])
        self.drop_menu.set_bg_color(CONCRETE)
        self.load_menu = self.setup_loadmenu()
        self.floating_text = FloatingText("no text", self.size)
        self.setup()

        self.selected_map = self.map1

    def update(self, dt):
        self.dt = dt
        self.map1.update(dt)
        self.menu.update(dt)
        self.palette.update(dt)
        self.floating_text.update(dt)
        if self.drop_menu.buttons[0].clicked:
            self.savemap()
            self.drop_menu.buttons[0].clicked = False
        if self.drop_menu.buttons[2].clicked:
            self.selected_map = self.palette
        else:
            self.selected_map = self.map1
        if self.map1.clipped:
            self.map2.map.set_clip()
            self.map1.clipped = False
        if self.c_pos != self.clip.topleft:
            self.c_pos = (self.c_pos[0] - self.map1.xy[0],
                          self.c_pos[1] - self.map1.xy[1])
            self.clip.topleft = self.c_pos
        self.button1.update(self.dt)
        self.mouse_select()

    def draw(self):
        self.screen.fill(BLACK)

        # MAP
        # Background
        self.map1.draw(self.screen, self.clip)

        # Foreground
        if self.show_foreground > 0:
            self.map2.xy = self.map1.xy
            self.map2.draw(self.screen, self.clip)

        # GUI
        self.menu.draw(self.screen, None)
        self.button1.draw(self.screen)
        if self.button1.clicked:
            self.drop_menu.draw(self.screen)
        if self.drop_menu.buttons[1].clicked:
            self.load_menu.draw(self.screen)
        if self.drop_menu.buttons[2].clicked:
            self.palette.draw(self.screen)
        if self.right_m_button:
            screen_rect = self.m_select_rect.copy()
            screen_rect.x += self.map1.xy[0]
            screen_rect.y += self.map1.xy[1]
            pg.draw.rect(self.screen, WHITE, screen_rect, 1)

        self.floating_text.draw(self.screen)

########################## Setup methods #######################################

    def setup_loadmenu(self):
        folder = Path("./save")
        filelist = [f for f in folder.files("*.sav")]
        amount = len(filelist)
        menu = Menu((240, amount * 21), (250, 21))
        menu.add_buttons(amount, filelist)
        menu.set_bg_color(CONCRETE)
        return menu

    def setup(self):
        self.map1.setup(TEAL)
        self.map2.setup(CONCRETE, 200)
        self.menu.setup(CYAN)

    def setup_menu(self):
        sheet = SpriteSheet("./img/magecity_64p.png", 64, (0, 0), (8, 44))
        self.menu_list = sum(sheet.image_list, [])
        length = len(self.menu_list)
        size = (10, length / 10)
        menu = Map(self.size, size, self.block, "./tiles/Empty_tile_64p.png")
        menu.setup(CYAN)
        i = 1
        for tile in menu.group:
            tile.filename = "maptile{}".format(i)
            tile.image = self.menu_list[i]
            if i + 1 >= len(self.menu_list):
                break
            else:
                i += 1
        return menu

#################### Key and Mouse Methods #####################################

    def key_down(self, key):
        if key == K_ESCAPE:
            print "Quitting..."
            self.game_on == False
            self.end_game()

        if key == K_e:
            self.show_foreground *= -1

        if key in (K_w, K_UP):
            self.selected_map.move["up"] = True
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = True
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = True
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = True

    def key_up(self, key):
        if key in (K_w, K_UP):
            self.selected_map.move["up"] = False
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = False
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = False
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = False

    def mouse_down(self, button, pos):
        not_menu = True
        if button == 1:
            if self.button1.click(pos):
                not_menu = False
            if self.button1.clicked:
                if self.drop_menu.click(pos):
                    not_menu = False
            if self.drop_menu.buttons[1].clicked:
                if self.load_menu.click(pos):
                    self.loadmap()
            if not_menu:
                if self.drop_menu.buttons[2].clicked:
                    self.find_tile(pos, self.palette, self.menu)
                elif self.show_foreground > 0:
                    self.find_tile(pos, self.menu, self.map2)
                else:
                    self.find_tile(pos, self.menu, self.map1)
        if button == 3:
            self.right_m_button = True
            self.right_m_pos = pos

    def mouse_up(self, button, pos):
        if button == 3:
            self.right_m_button = False
            self.right_m_pos = pos
            new_rect = self._invert_rect(self.m_select_rect)

            if self.show_foreground < 0:
                self.group_select(new_rect, self.map1.group)
            else:
                self.group_select(new_rect, self.map2.group)

    def mouse_motion(self, button, pos, rel):
        self.m_pos = pos

    def mouse_select(self):
        if self.right_m_button:
            old_pos = (self.right_m_pos[0] - self.map1.xy[0],
                       self.right_m_pos[1] - self.map1.xy[1])
            new_pos = (self.m_pos[0] - self.map1.xy[0],
                       self.m_pos[1] - self.map1.xy[1])
            self.m_select_rect = pg.Rect(1, 1, 1, 1)
            xoff, yoff = new_pos[0] - old_pos[0], new_pos[1] - old_pos[1]
            self.m_select_rect.inflate_ip(xoff, yoff)
            self.m_select_rect.topleft = old_pos

    def _invert_rect(self, rect):
        new_rect = pg.Rect(1, 1, 1, 1)
        new_rect.size = rect.size
        if rect.width < 0:
            new_rect.width *= -1
        if rect.height < 0:
            new_rect.height *= -1
        if rect.left > rect.right:
            new_rect.left = rect.right
        else:
            new_rect.left = rect.left
        if rect.top > rect.bottom:
            new_rect.top = rect.bottom
        else:
            new_rect.top = rect.top
        return new_rect


###################### Button Activated Methods ################################

    def group_select(self, rect, group):
        for sprite in group:
            if sprite.rect.colliderect(rect):
                self.change_tile(sprite)

    def find_tile(self, pos, menu, bg_map):
        found = False
        for tile in menu.group:
            map_pos = (pos[0] - menu.xy[0], pos[1] - menu.xy[1])
            if tile.rect.collidepoint(map_pos):
                found = True
                menu.fill = True
                menu.selected = tile
                self.current_tile = tile
        if not found:
            for tile in bg_map.group:
                map_pos = (pos[0] - bg_map.xy[0], pos[1] - bg_map.xy[1])
                if tile.rect.collidepoint(map_pos):
                    self.change_tile(tile)
                    bg_map.clear_map()

    def change_tile(self, tile):
        if self.current_tile != None:
            tile.filename = self.current_tile.filename
            tile.image = self.current_tile.image
            tile.dirty = 1

    def savemap(self):
        size = tuple_mult((40, 40), self.block)
        save = SaveMap("savemap", size, self.block, "./img/magecity_64p.png",
                       self.map1.group, self.map2.group)
        save.write_to_file()
        self.floating_text.set_text("Map Saved", True)

    def loadmap(self):
        select = None
        for button in self.load_menu.buttons:
            if button.clicked:
                select = button.text
        if select != None:
            load = LoadMap(select)
            self.map1.group = load.bg_group
            self.map2.group = load.fg_group
            self.floating_text.set_text("Map Loaded", True)
Ejemplo n.º 5
0
        dy = -1

    viewport.update(elapsed, dx, dy)
    create_landmass_button.update(elapsed, mouse.get_pos(),
                                  any(mouse.get_pressed()))
    finalize_button.update(elapsed, mouse.get_pos(), any(mouse.get_pressed()))
    unfinalize_button.update(elapsed, mouse.get_pos(),
                             any(mouse.get_pressed()))
    reset_land_button.update(elapsed, mouse.get_pos(),
                             any(mouse.get_pressed()))

    screen.fill((0, 0, 0))

    viewport.draw(screen)

    create_landmass_button.draw(screen)
    finalize_button.draw(screen)
    unfinalize_button.draw(screen)
    reset_land_button.draw(screen)

    if is_creating_landmass:
        draw.circle(screen, (255, 0, 0), mouse.get_pos(), 5)
    elif is_setting_landmass_distance:
        converted_origin = viewport.deconvert_mouse_pos(land_mass_origin)
        mouse_pos = mouse.get_pos()
        distance = ((mouse_pos[0] - converted_origin[0])**2 +
                    (mouse_pos[1] - converted_origin[1])**2)**0.5
        draw.circle(screen, (255, 0, 0), converted_origin, 5)
        draw.circle(screen, (255, 0, 0), converted_origin,
                    max(5, int(distance)), 2)
Ejemplo n.º 6
0
class Main(Template):
    def __init__(self, size):
        Template.__init__(self, size)
        self.size = size
        self.block = 64
        self.dt = 0.
        self.map1 = Map(self.size, (40,40), self.block, "./tiles/Empty_tile_64p.png")
        self.map2 = Map(self.size, (40,40), self.block, "./tiles/Empty_tile_64p.png")
        self.menu = Map(self.size, (2,14), self.block, "./tiles/Empty_tile_64p.png")
        self.menu_list = []
        self.palette = self.setup_menu()
        self.palette.xy = [250,0]
        self.clip = pg.Rect(0,0, self.size[0],self.size[1])
        self.c_pos = self.clip.topleft
        self.current_tile = None
        self.show_foreground = -1
        self.right_m_button = False
        self.right_m_pos = None
        self.m_pos = None
        self.fill = False
        self.m_select_rect = pg.Rect(1,1,1,1)
        self.button1 = Button((120, 20), (129,1), "Menu")
        self.drop_menu = Menu((120, 160), (129, 21))
        self.drop_menu.add_buttons(4, ["Save", "Load", "Palette", "Info"])
        self.drop_menu.set_bg_color(CONCRETE)
        self.load_menu = self.setup_loadmenu()
        self.floating_text = FloatingText("no text", self.size)
        self.setup()

        self.selected_map = self.map1

    def update(self, dt):
        self.dt = dt
        self.map1.update(dt)
        self.menu.update(dt)
        self.palette.update(dt)
        self.floating_text.update(dt)
        if self.drop_menu.buttons[0].clicked:
            self.savemap()
            self.drop_menu.buttons[0].clicked = False
        if self.drop_menu.buttons[2].clicked:
            self.selected_map = self.palette
        else:
            self.selected_map = self.map1
        if self.map1.clipped:
            self.map2.map.set_clip()
            self.map1.clipped = False
        if self.c_pos != self.clip.topleft:
            self.c_pos = (self.c_pos[0] - self.map1.xy[0], self.c_pos[1] - self.map1.xy[1])
            self.clip.topleft = self.c_pos
        self.button1.update(self.dt)
        self.mouse_select()

    def draw(self):
        self.screen.fill(BLACK)

        # MAP
        # Background
        self.map1.draw(self.screen, self.clip)

        # Foreground
        if self.show_foreground > 0:
            self.map2.xy = self.map1.xy
            self.map2.draw(self.screen, self.clip)

        # GUI
        self.menu.draw(self.screen, None)
        self.button1.draw(self.screen)
        if self.button1.clicked:
            self.drop_menu.draw(self.screen)
        if self.drop_menu.buttons[1].clicked:
            self.load_menu.draw(self.screen)
        if self.drop_menu.buttons[2].clicked:
            self.palette.draw(self.screen)
        if self.right_m_button:
            screen_rect = self.m_select_rect.copy()
            screen_rect.x += self.map1.xy[0]
            screen_rect.y += self.map1.xy[1]
            pg.draw.rect(self.screen, WHITE, screen_rect, 1)

        self.floating_text.draw(self.screen)

########################## Setup methods #######################################

    def setup_loadmenu(self):
        folder = Path("./save")
        filelist = [f for f in folder.files("*.sav")]
        amount = len(filelist)
        menu = Menu((240, amount*21), (250, 21))
        menu.add_buttons(amount, filelist)
        menu.set_bg_color(CONCRETE)
        return menu

    def setup(self):
        self.map1.setup(TEAL)
        self.map2.setup(CONCRETE, 200)
        self.menu.setup(CYAN)

    def setup_menu(self):
        sheet = SpriteSheet("./img/magecity_64p.png", 64, (0,0), (8,44))
        self.menu_list = sum(sheet.image_list, [])
        length = len(self.menu_list)
        size = (10, length/10)
        menu = Map(self.size, size, self.block, "./tiles/Empty_tile_64p.png")
        menu.setup(CYAN)
        i = 1
        for tile in menu.group:
            tile.filename = "maptile{}".format(i)
            tile.image = self.menu_list[i]
            if i+1 >= len(self.menu_list):
                break
            else:
                i += 1
        return menu

#################### Key and Mouse Methods #####################################

    def key_down(self, key):
        if key == K_ESCAPE:
            print "Quitting..."
            self.game_on == False
            self.end_game()

        if key == K_e:
            self.show_foreground *= -1

        if key in (K_w, K_UP):
            self.selected_map.move["up"] = True
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = True
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = True
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = True

    def key_up(self, key):
        if key in (K_w, K_UP):
            self.selected_map.move["up"] = False
        if key in (K_a, K_LEFT):
            self.selected_map.move["left"] = False
        if key in (K_s, K_DOWN):
            self.selected_map.move["down"] = False
        if key in (K_d, K_RIGHT):
            self.selected_map.move["right"] = False

    def mouse_down(self, button, pos):
        not_menu = True
        if button == 1:
            if self.button1.click(pos):
                not_menu = False
            if self.button1.clicked:
                if self.drop_menu.click(pos):
                    not_menu = False
            if self.drop_menu.buttons[1].clicked:
                if self.load_menu.click(pos):
                    self.loadmap()
            if not_menu:
                if self.drop_menu.buttons[2].clicked:
                    self.find_tile(pos, self.palette, self.menu)
                elif self.show_foreground > 0:
                    self.find_tile(pos, self.menu, self.map2)
                else:
                    self.find_tile(pos, self.menu, self.map1)
        if button == 3:
            self.right_m_button = True
            self.right_m_pos = pos

    def mouse_up(self, button, pos):
        if button == 3:
            self.right_m_button = False
            self.right_m_pos = pos
            new_rect = self._invert_rect(self.m_select_rect)

            if self.show_foreground < 0:
                self.group_select(new_rect, self.map1.group)
            else:
                self.group_select(new_rect, self.map2.group)

    def mouse_motion(self, button, pos, rel):
        self.m_pos = pos

    def mouse_select(self):
        if self.right_m_button:
            old_pos = (self.right_m_pos[0] - self.map1.xy[0], self.right_m_pos[1] - self.map1.xy[1])
            new_pos = (self.m_pos[0] - self.map1.xy[0], self.m_pos[1] - self.map1.xy[1])
            self.m_select_rect = pg.Rect(1,1,1,1)
            xoff, yoff = new_pos[0] - old_pos[0], new_pos[1] - old_pos[1]
            self.m_select_rect.inflate_ip(xoff, yoff)
            self.m_select_rect.topleft = old_pos

    def _invert_rect(self, rect):
            new_rect = pg.Rect(1,1,1,1)
            new_rect.size = rect.size
            if rect.width < 0:
                new_rect.width *= -1
            if rect.height < 0:
                new_rect.height *= -1
            if rect.left > rect.right:
                new_rect.left = rect.right
            else:
                new_rect.left = rect.left
            if rect.top > rect.bottom:
                new_rect.top = rect.bottom
            else:
                new_rect.top = rect.top
            return new_rect

###################### Button Activated Methods ################################

    def group_select(self, rect, group):
        for sprite in group:
            if sprite.rect.colliderect(rect):
                self.change_tile(sprite)

    def find_tile(self, pos, menu, bg_map):
        found = False
        for tile in menu.group:
            map_pos = (pos[0] - menu.xy[0], pos[1] - menu.xy[1])
            if tile.rect.collidepoint(map_pos):
                found = True
                menu.fill = True
                menu.selected = tile
                self.current_tile = tile
        if not found:
            for tile in bg_map.group:
                map_pos = (pos[0] - bg_map.xy[0], pos[1] - bg_map.xy[1])
                if tile.rect.collidepoint(map_pos):
                    self.change_tile(tile)
                    bg_map.clear_map()

    def change_tile(self, tile):
        if self.current_tile != None:
            tile.filename = self.current_tile.filename
            tile.image = self.current_tile.image
            tile.dirty = 1

    def savemap(self):
        size = tuple_mult((40,40), self.block)
        save = SaveMap("savemap", size, self.block, "./img/magecity_64p.png",
                        self.map1.group, self.map2.group)
        save.write_to_file()
        self.floating_text.set_text("Map Saved", True)

    def loadmap(self):
        select = None
        for button in self.load_menu.buttons:
            if button.clicked:
                select = button.text
        if select != None:
            load = LoadMap(select)
            self.map1.group = load.bg_group
            self.map2.group = load.fg_group
            self.floating_text.set_text("Map Loaded", True)
Ejemplo n.º 7
0
                     sprite=sprites[12])
timerUI = SimpleText((topMenuHeight, 0, topMenuHeight, topMenuHeight),
                     "%.3f" % timer.getTime(), 60)
flagCountUI = SimpleText((topMenuHeight * 6, 0, topMenuHeight, topMenuHeight),
                         "%d" % (numMines - numFlags), 60, RED)

firstClick = True  # first discovery generates the field
mouseHold = False
run = True

while run:
    screen.fill(BEIGE)
    mouseX, mouseY = mouse.get_pos()
    hoveredTile = getHovered()

    resetButton.draw()
    timer.update()
    timerUI.update("%.3f" % timer.getTime())
    timerUI.draw()
    flagCountUI.update("%d" % (numMines - numFlags))
    flagCountUI.draw()

    for x in range(gridW):
        for y in range(gridH):
            tiles[x][y].draw()

    for e in event.get():
        if e.type == MOUSEBUTTONDOWN:
            if e.button == 1:  # LMB
                mouseHold = True
            elif e.button == 3:  # RMB
Ejemplo n.º 8
0
class PenSelectScene(Scene):
    header = None
    back_btn = None

    select_btn = None
    purchase_btn = None

    name_text = None
    density_text = None
    restitution_text = None
    description_lines = []

    coins_image = None
    coins_text = None

    pen_images = []
    visible_pen_images = []
    left_btn, right_btn = None, None

    center_pos = None
    pen_index = 0

    def start(self, screen):
        if not self.already_loaded:
            PenData.load_all_pens(Resources.get("all_pens"))

            width = screen.get_width()
            height = screen.get_height()

            self.back_btn = Button(
                pygame.Rect(10, 10, 60, 40), "Back", {
                    Options.BORDER_WIDTH: 0,
                    Options.BACKGROUND: (20, 61, 89),
                    Options.FOREGROUND: (244, 180, 26),
                    Options.HOVERED_BACKGROUND: (10, 30, 45),
                    Options.FONT: pygame.font.SysFont("Comic Sans MS", 15)
                })

            label_options = {
                Options.BACKGROUND: (20, 61, 89),
                Options.FOREGROUND: (244, 180, 26),
                Options.BORDER_WIDTH: 0,
            }
            self.header = Label(pygame.Rect(width / 2 - 200, 10, 400, 30),
                                "Select your weapon!", label_options)
            self.coins_text = Label(
                pygame.Rect(width - 110, height - 55, 100, 40), "0",
                label_options)

            label_options = {
                Options.BACKGROUND: (82, 173, 200),
                Options.FOREGROUND: (20, 61, 89),
                Options.BORDER_WIDTH: 0,
                Options.FONT: pygame.font.SysFont("Comic Sans MS", 18)
            }
            self.density_text = Label(pygame.Rect(width / 5, 110, 100, 20),
                                      "Density: ", label_options)
            self.restitution_text = Label(pygame.Rect(width / 5, 130, 100, 20),
                                          "Restitution: ", label_options)

            self.name_text = Label(
                pygame.Rect(width / 2 - 45, height - 125, 90, 50), "",
                label_options)
            self.description_lines = [
                Label(pygame.Rect(width * 2 / 3, 100 + i * 25, 100, 20), "",
                      label_options) for i in range(0, 3)
            ]

            self.coins_image = Image(
                pygame.Rect(width - 175, height - 60, 50, 50),
                Resources.get("coin"), {Options.BACKGROUND: (82, 173, 200)})

            btn_options = {
                Options.BORDER_WIDTH: 0,
                Options.BACKGROUND: (20, 61, 89),
                Options.FOREGROUND: (244, 180, 26),
                Options.HOVERED_BACKGROUND: (10, 30, 45),
                Options.FONT: pygame.font.SysFont("Comic Sans MS", 25)
            }
            self.left_btn = Button(pygame.Rect(10, height / 2 - 20, 20, 30),
                                   "<", btn_options)
            self.right_btn = Button(
                pygame.Rect(width - 30, height / 2 - 20, 20, 30), ">",
                btn_options)
            self.select_btn = Button(
                pygame.Rect(width / 2 - 45, height - 75, 90, 50), "Select",
                btn_options)
            self.purchase_btn = Button(
                pygame.Rect(width / 2 - 125, height - 75, 250, 40), "",
                btn_options)

            self.center_pos = pygame.Rect(width / 2 - 50, height / 2 - 50, 100,
                                          100)
            for pen in PenData.all_pens:
                self.pen_images.append(
                    Image(self.center_pos, Resources.get(pen.image_file),
                          {Options.BACKGROUND: (82, 173, 200)}))

            self.already_loaded = True

        self.reposition_images()
        self.update_shop_data()
        self.reset_coin_text()

    def update(self, event):
        for elt in (self.back_btn, self.left_btn, self.right_btn):
            elt.update(event)

        cur_pen = PenData.all_pens[self.pen_index]
        if cur_pen.name in Account.current_account.pens:
            self.select_btn.update(event)
            if self.select_btn.clicked:
                PenData.current_pen = cur_pen
                Scene.push_scene(6)
        else:
            self.purchase_btn.set_enabled(
                Account.current_account.money >= cur_pen.cost)
            self.purchase_btn.update(event)
            if self.purchase_btn.clicked:
                Account.current_account.purchase_pen(cur_pen)
                self.reset_coin_text()

        if self.back_btn.clicked:
            Scene.pop_scene()

        if self.left_btn.clicked or self.right_btn.clicked:
            if self.left_btn.clicked:
                self.pen_index -= 1
                self.reposition_images()
            elif self.right_btn.clicked:
                self.pen_index += 1
                self.reposition_images()
            self.update_shop_data()

    def reposition_images(self):
        self.visible_pen_images.clear()

        if self.pen_index > 0:
            img = self.pen_images[self.pen_index - 1]
            self.visible_pen_images.append(img)
            img.rect = self.center_pos.copy().move(-300, 40)

        self.visible_pen_images.append(self.pen_images[self.pen_index])
        self.pen_images[self.pen_index].rect = self.center_pos

        if self.pen_index < len(self.pen_images) - 1:
            img = self.pen_images[self.pen_index + 1]
            self.visible_pen_images.append(img)
            img.rect = self.center_pos.copy().move(300, 40)

        self.left_btn.set_enabled(self.pen_index > 0)
        self.right_btn.set_enabled(self.pen_index < len(self.pen_images) - 1)

    def reset_coin_text(self):
        self.coins_text.text = str(Account.current_account.money)
        self.coins_text.recreate()

    def update_shop_data(self):
        cur_pen = PenData.all_pens[self.pen_index]

        self.name_text.text = cur_pen.name
        self.name_text.recreate()

        self.density_text.text = f"Density: {cur_pen.density}"
        self.density_text.recreate()

        self.restitution_text.text = f"Restitution: {cur_pen.restitution}"
        self.restitution_text.recreate()

        line_index = 0
        for line in cur_pen.description:
            self.description_lines[line_index].text = line
            self.description_lines[line_index].recreate()
            line_index += 1

        if cur_pen.name not in Account.current_account.pens:
            self.purchase_btn.text = f"Purchase for {cur_pen.cost}"
            self.purchase_btn.recreate()

    def draw(self, screen):
        screen.fill((82, 173, 200))

        for elt in (self.header, self.back_btn, self.left_btn, self.right_btn,
                    self.name_text, self.coins_image, self.coins_text,
                    self.density_text, self.restitution_text):
            elt.draw(screen)

        for line in self.description_lines:
            line.draw(screen)

        if PenData.all_pens[
                self.pen_index].name in Account.current_account.pens:
            self.select_btn.draw(screen)
        else:
            self.purchase_btn.draw(screen)

        for img in self.visible_pen_images:
            img.draw(screen)