コード例 #1
0
    def new(self):
        # start a new game
        self.all_sprites = pg.sprite.Group()
        self.tilesize = TILESIZE
        # the offset moves the camera over the board
        self.offset = BOARD_CENTER
        self.tilemap = Tilemap(self)
        self.camera = Camera(self)
        for building in self.tilemap.players[
                self.tilemap.current_player]["buildings"]:
            self.camera.apply_camera(self, building.pos)
        # self.last_time = 0
        self.mx0, self.my0, self.mx1, self.my1 = 0, 0, 0, 0
        self.mouse_pos = []
        self.enable_ui = True
        self.show_path = False
        self.tooltips = True
        self.camera_counter = 0

        # self.offset[0] = self.tilemap.unit.pos[1] * 64 - 8*64-32
        # self.offset[1] = self.tilemap.unit.pos[0] * 64 - 6*64-32
        # self.camera = Camera(self, self.tilemap.unit.pos)
        # self.offset[0] = self.camera.camera_x
        # self.offset[1] = self.camera.camera_y
        self.clicked = False
        self.right_click = False
        self.run()
コード例 #2
0
def run_game():
    """Main entry point for Py-Climber"""

    # Startup pygame object
    pygame.init()

    random.seed()

    # Load our settings object and image resources, disk I/O that can be done in advance
    settings = Settings()
    image_res = ImageResources(settings)

    # Create the main screen to render to based on settings
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption(settings.caption)
    
    # Create a 2D tilemap - this takes a list of indices and an image list to produce a tiled surface
    tile_map = Tilemap(settings, screen, settings.map_indicies, image_res.tile_images, image_res.block_image)

    # Overwrite default indices with generated map 
    tile_map.generate_basic_map(settings.map_number_floors , settings.map_number_subfloors)

    # Create player
    player = Player(settings, screen, image_res.player_sprite_images, tile_map)

    # Create an enemies list
    enemies = []

    # Reset the game
    gf.reset_game(settings, image_res, screen, player, tile_map, enemies)

    # Use pygame's simple loop management for a fixed 30 FPS
    clock = pygame.time.Clock()
    new_enemy_counter = 0
    while True:
        # Should make sure each frame spends at least 1/30 seconds in this loop
        # downside is wasted sleep on fast hardware and slow hardware will lag
        # but slow hardware will always lag and implementing a time-delta based
        # loop for this simple game is IMHO overkill.
        clock.tick(30)

        # Process system events (key-presses, joystick, etc)
        gf.check_events(settings, screen, image_res, player, tile_map, enemies)

        # Update the game (this will update all sub-object and render them to the screen)
        gf.update_screen(settings, screen, image_res, tile_map, player, enemies)

        if player.reset_game:
            gf.reset_game(settings, image_res, screen, player, tile_map, enemies)

        new_enemy_counter += 1
        if new_enemy_counter >= settings.enemy_generation_rate:
            new_enemy_counter = 0
            gf.generate_new_random_blob(settings, screen, image_res.enemy_blob_images, tile_map, enemies)
コード例 #3
0
ファイル: world.py プロジェクト: shilrobot/ld28
 def __init__(self):
     self.engine = Engine.get()
     self.scrollX = 0
     self.scrollY = 0
     self.map = Tilemap('test')
     self.blockers = [self.map]
     self.buttonMounts = []
     self.goMgr = GameObjectManager()
     self.player = None
     for spawn in self.map.spawns:
         self.doSpawn(spawn)
     self.goMgr.onMapLoaded()
     self.services = {}
コード例 #4
0
    def __init__(self, display):
        super(PlayScene, self).__init__(display)
        display.clear()
        self.log_messages = []
        self.tilemap = Tilemap()
        self.draw_frame(display)
        self.camera = Camera(35, 15, self.tilemap.width, self.tilemap.height)

        self.draw_view(display)

        self.player = Player(self.camera.half_width, self.camera.half_height)
        self.player_id = 0
        self.entities = self.tilemap.entities
        self.entities.append(self.player)
        rod = FishingRod()
        (rod.x, rod.y) = (10, 15)
        axe = Axe()
        (axe.x, axe.y) = (28, 5)
        self.entities.insert(0, rod)
        self.entities.insert(0, axe)
        self.draw_entities(display)
        self.draw_inventory(display)
        display.flush()
コード例 #5
0
ファイル: playscene.py プロジェクト: RylandAlmanza/adventure
    def __init__(self, display):
        super(PlayScene, self).__init__(display)
        display.clear()
        self.log_messages = []
        self.tilemap = Tilemap()
        self.draw_frame(display)
        self.camera = Camera(35, 15, self.tilemap.width, self.tilemap.height)

        self.draw_view(display)

        self.player = Player(self.camera.half_width, self.camera.half_height)
        self.player_id = 0
        self.entities = self.tilemap.entities
        self.entities.append(self.player)
        rod = FishingRod()
        (rod.x, rod.y) = (10, 15)
        axe = Axe()
        (axe.x, axe.y) = (28, 5)
        self.entities.insert(0, rod)
        self.entities.insert(0, axe)
        self.draw_entities(display)
        self.draw_inventory(display)
        display.flush()
コード例 #6
0
ファイル: Room.py プロジェクト: C3RV1/DarkProject
    def __init__(self,
                 room_map,
                 tileset,
                 objects=[],
                 camera=None,
                 connections=[None, None, None, None]):
        # type: (list, str, list, Camera, list) -> None
        self.tilemap = Tilemap.Tilemap(tileset,
                                       map_size=(len(room_map),
                                                 len(room_map[0])))
        self.room_map = room_map
        self.size = [len(room_map), len(room_map[0])]

        self.objects = objects  # Objects in the room
        self.camera = camera  # Camera (we have to draw relative to it)
        self.connections = connections  # Connections (used when the player changes the screen he is in)

        self.light_map = None
        self.temp_light_map = None

        self.display_surface = None

        self.collisions = []
コード例 #7
0
ファイル: level.py プロジェクト: GitExl/Turrican2Editor
    def load(self, stream, offset=0):
        stream.seek(self._offset_level_data + offset)
        self._tilemap = Tilemap.from_stream(stream, self._tilemap_width, self._tilemap_height)

        self.read_entities(stream, self._offset_blockmap_pointers)
コード例 #8
0
ファイル: world.py プロジェクト: shilrobot/ld28
class World:

    def __init__(self):
        self.engine = Engine.get()
        self.scrollX = 0
        self.scrollY = 0
        self.map = Tilemap('test')
        self.blockers = [self.map]
        self.buttonMounts = []
        self.goMgr = GameObjectManager()
        self.player = None
        for spawn in self.map.spawns:
            self.doSpawn(spawn)
        self.goMgr.onMapLoaded()
        self.services = {}

    def getService(self, name):
        return self.services.get(name)

    def registerService(self, name, service):
        self.services[name] = service

    def doSpawn(self, spawn):
        goClass = goRegistry.get(spawn.type)
        if goClass is not None:
            go = goClass(self)
            go.spawn(spawn)
            self.goMgr.add(go)
        else:
            print 'Unknown GO class: %s' % spawn.type

    def update(self, delta):

        # if self.engine.key_down(pygame.K_s):
        #     delta *= 0.25
        # elif self.engine.key_down(pygame.K_f):
        #     delta *= 4
        self.goMgr.update(delta)

    def draw(self):
        glClearColor(128/255.0,215/255.0,255/255.0,1)
        glClear(GL_COLOR_BUFFER_BIT)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        if self.player is not None:
            self.scrollX = self.player.x - self.engine.screenWidth*0.5
            self.scrollY = self.player.y - self.engine.screenHeight*0.5

        glTranslate(-self.scrollX, -self.scrollY, 0)

        boundRect = Rect(self.scrollX,
                        self.scrollY,
                        self.engine.screenWidth,
                        self.engine.screenHeight)
        self.map.draw(boundRect,'bg')
        self.goMgr.prepareDraw()
        self.goMgr.drawBelowFG()
        self.map.draw(boundRect,'fg')
        self.goMgr.drawAboveFG()

    def rectOverlaps(self, rect):
        for blocker in self.blockers:
            if blocker.rectOverlaps(rect):
                return True
        return False

    def findButtonMount(self, rect):
        for bm in self.buttonMounts:
            if bm.getButtonMountRect().intersects(rect):
                return bm
        return None
コード例 #9
0
class Game:
    def __init__(self):
        # initialize game window, etc.
        pg.init()
        pg.mixer.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        self.load_data()
        self.running = True

    def load_data(self):
        game_dir = path.dirname(__file__)
        img_folder = path.join(game_dir, 'img')
        terrain_img_folder = path.join(img_folder, 'terrain')
        self.title_font = path.join(img_folder, 'ZOMBIE.TTF')
        self.hud_font = path.join(img_folder, 'Impacted2.0.ttf')
        # self.img_list = []
        # for filename in listdir(terrain_img_folder):
        #     x = pg.image.load(path.join(terrain_img_folder, filename)).convert()
        #     x = pg.transform.scale(x, (TILESIZE, TILESIZE))
        #     self.img_list.append(x)
        self.water_img = pg.image.load(
            path.join(terrain_img_folder, 'water.png')).convert()
        self.water_img = pg.transform.scale(self.water_img,
                                            (TILESIZE, TILESIZE))
        self.forest_img = pg.image.load(
            path.join(terrain_img_folder, 'forest.png')).convert()
        self.forest_img = pg.transform.scale(self.forest_img,
                                             (TILESIZE, TILESIZE))
        self.mountain_img = pg.image.load(
            path.join(terrain_img_folder, 'mountain.png')).convert()
        self.mountain_img = pg.transform.scale(self.mountain_img,
                                               (TILESIZE, TILESIZE))
        self.grass_img = pg.image.load(
            path.join(terrain_img_folder, 'grass.png')).convert()
        self.grass_img = pg.transform.scale(self.grass_img,
                                            (TILESIZE, TILESIZE))
        # load the main base image
        self.main_base_img = pg.image.load(
            path.join(img_folder, 'MainBase.png')).convert_alpha()
        self.main_base_img = pg.transform.scale(self.main_base_img,
                                                (TILESIZE, TILESIZE))
        self.baracks_img = pg.image.load(path.join(
            img_folder, 'Baracks.png')).convert_alpha()
        self.baracks_img = pg.transform.scale(self.baracks_img,
                                              (TILESIZE, TILESIZE))
        self.unit_img = pg.image.load(path.join(
            img_folder, 'unit_img.png')).convert_alpha()
        self.unit_img = pg.transform.scale(self.unit_img, (TILESIZE, TILESIZE))
        self.warrior_img = pg.image.load(path.join(
            img_folder, 'Warrior.png')).convert_alpha()
        self.warrior_img = pg.transform.scale(self.warrior_img,
                                              (TILESIZE, TILESIZE))
        # create a transparent surface
        self.dim_screen = pg.Surface((TILESIZE, TILESIZE)).convert_alpha()
        self.dim_screen.fill((0, 0, 0, 80))

    def new(self):
        # start a new game
        self.all_sprites = pg.sprite.Group()
        self.tilesize = TILESIZE
        # the offset moves the camera over the board
        self.offset = BOARD_CENTER
        self.tilemap = Tilemap(self)
        self.camera = Camera(self)
        for building in self.tilemap.players[
                self.tilemap.current_player]["buildings"]:
            self.camera.apply_camera(self, building.pos)
        # self.last_time = 0
        self.mx0, self.my0, self.mx1, self.my1 = 0, 0, 0, 0
        self.mouse_pos = []
        self.enable_ui = True
        self.show_path = False
        self.tooltips = True
        self.camera_counter = 0

        # self.offset[0] = self.tilemap.unit.pos[1] * 64 - 8*64-32
        # self.offset[1] = self.tilemap.unit.pos[0] * 64 - 6*64-32
        # self.camera = Camera(self, self.tilemap.unit.pos)
        # self.offset[0] = self.camera.camera_x
        # self.offset[1] = self.camera.camera_y
        self.clicked = False
        self.right_click = False
        self.run()

    def run(self):
        # Game loop
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    def update(self):
        # Game loop - update
        # now = pg.time.get_ticks()
        # if now - self.last_time > 20:
        #     mx0, my0 = pg.mouse.get_pos()
        #     self.last_time = now
        # mx1, my1 = pg.mouse.get_pos()
        self.all_sprites.update()
        # self.camera.apply_camera(self, self.tilemap.unit.pos)
        self.tilemap.update_map(self)
        self.get_key_input()

    def get_key_input(self):
        key = pg.mouse.get_pressed()
        button = pg.key.get_pressed()
        if button[pg.K_LEFT]:
            self.offset[0] += 10
        if button[pg.K_RIGHT]:
            self.offset[0] -= 10
        if button[pg.K_UP]:
            self.offset[1] += 10
        if button[pg.K_DOWN]:
            self.offset[1] -= 10

        # enable to drag the map with the mouse
        self.mouse_pos.append(pg.mouse.get_pos())
        if len(self.mouse_pos) > 2:
            self.mx0, self.my0 = self.mouse_pos.pop(0)
            self.mx1, self.my1 = self.mouse_pos.pop(0)
        self.mouse_pos = self.mouse_pos[:3]
        if key[0]:
            self.offset[0] += self.mx1 - self.mx0
            self.offset[1] += self.my1 - self.my0

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    if self.playing:
                        self.playing = False
                    self.running = False
                if event.key == pg.K_RETURN:
                    self.tilemap.next_turn()
                if event.key == pg.K_u:
                    self.enable_ui = not self.enable_ui
                if event.key == pg.K_h:
                    self.show_path = not self.show_path
                if event.key == pg.K_t:
                    self.tooltips = not self.tooltips
                if event.key == pg.K_SPACE:
                    self.show_next_unit()

            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.clicked = True

                if event.button == 3:
                    self.right_click = True

                # if event.button == 4:
                #     self.tilesize += 4
                #     if self.tilesize >= 104:
                #         self.tilesize = 104

                # if event.button == 5:
                #     self.tilesize -= 4
                #     if self.tilesize <= 36:
                #         self.tilesize = 36

    def draw(self):
        # Game loop - draw
        self.screen.fill(WHITE)
        # draw the map
        self.tilemap.draw_tilemap(self, self.screen)
        if self.enable_ui:
            self.draw_hud()
        # after drawing everything, flip the display
        pg.display.update()

    def draw_hud(self):
        pg.draw.rect(self.screen, WHITE, (0, 0, WIDTH * 4 / 8, 40))
        self.draw_building_menu()
        draw_text(self.screen, self.tilemap.current_player, self.hud_font, 25,
                  BLACK, 10, 10)
        draw_text(
            self.screen, "Money: " + str(self.tilemap.players[
                self.tilemap.current_player]['resources']['money']),
            self.hud_font, 25, BLACK, WIDTH * 1 / 8, 10)
        draw_text(
            self.screen, "Crystals: " + str(self.tilemap.players[
                self.tilemap.current_player]['resources']['crystals']),
            self.hud_font, 25, BLACK, WIDTH * 2 / 8, 10)
        pg.draw.circle(self.screen, BLACK,
                       (WIDTH * 13 // 14, HEIGHT * 12 // 13), 50, 5)
        pg.draw.circle(self.screen, LIGHTBLUE,
                       (WIDTH * 13 // 14, HEIGHT * 12 // 13), 45)
        draw_text(self.screen, "END TURN", self.hud_font, 20, BLACK,
                  WIDTH * 13 // 14 - 40, HEIGHT * 12 // 13 - 15)
        draw_button(self.screen, "QUIT", self.hud_font, 20, BLACK, YELLOW,
                    DARKYELLOW, WIDTH * 9 // 10, 10, 70, 50, quit)

    def draw_building_menu(self):
        # draw building menu
        w = WIDTH * 2 // 5
        h = HEIGHT * 1 // 6
        building_menu = pg.Surface((w, h))
        building_menu.fill(WHITE)
        for player in self.tilemap.players.keys():
            for building in self.tilemap.players[player]["buildings"]:
                if building.selected and building.type == 'main_base':
                    building_menu.blit(self.baracks_img,
                                       (w // 20, h // 15, w, h))
        pg.draw.rect(building_menu, BLUE, (0, 0, w, h), 5)
        self.screen.blit(building_menu,
                         (0, HEIGHT * 5 / 6, WIDTH * 2 / 5, HEIGHT))

    def show_start_menu(self):
        # game splash/start screen
        pass

    def show_go_screen(self):
        pass

    def show_next_unit(self):
        '''center the camera on the next unit of the current player'''
        player_units = self.tilemap.players[
            self.tilemap.current_player]['units']
        player_buildings = self.tilemap.players[
            self.tilemap.current_player]['buildings']
        if len(player_units) > 0:
            index = self.camera_counter % len(player_units)
            self.camera_counter += 1
            self.camera.apply_camera(self, player_units[index].pos)
            index = self.camera_counter % len(player_buildings)
        elif len(player_buildings) > 0:
            index = self.camera_counter % len(player_buildings)
            self.camera_counter += 1
            self.camera.apply_camera(self, player_buildings[index].pos)
        else:
            self.camera.apply_camera(self, (WIDTH // 2, HEIGHT // 2))
コード例 #10
0
ファイル: playscene.py プロジェクト: RylandAlmanza/adventure
class PlayScene(Scene):
    def draw_frame(self, display):
        width, height = display.get_size()
        fg = interface.WHITE
        bg = interface.BLACK
        for i in range(width):
            display.put_char(i, 0, curses.ACS_HLINE, fg, bg)
            display.put_char(i, 16, curses.ACS_HLINE, fg, bg)
            display.put_char(i, height - 1, curses.ACS_HLINE, fg, bg)
        for i in range(height):
            display.put_char(0, i, curses.ACS_VLINE, fg, bg)
            display.put_char(width - 1, i, curses.ACS_VLINE, fg, bg)
        for i in range(16):
            display.put_char(36, i, curses.ACS_VLINE, fg, bg)

        display.put_char(0, 0, curses.ACS_ULCORNER, fg, bg)
        display.put_char(36, 0, curses.ACS_TTEE, fg, bg)
        display.put_char(0, 16, curses.ACS_LTEE, fg, bg)
        display.put_char(36, 16, curses.ACS_BTEE, fg, bg)
        display.put_char(0, height - 1, curses.ACS_LLCORNER, fg, bg)
        display.put_char(width - 1, height - 1, curses.ACS_LRCORNER, fg, bg)
        display.put_char(width - 1, 0, curses.ACS_URCORNER, fg, bg)
        display.put_char(width, - 1, 16, curses.ACS_RTEE, fg, bg)

    def draw_view(self, display):
        for i in self.camera:
            self.draw_tile(i.x, i.y, display)

    def update_and_draw_view(self, display):
        for i in self.camera:
            self.tilemap.get(i.x, i.y).update()
            self.draw_tile(i.x, i.y, display)
        self.update_entities()
        self.draw_entities(display)

    def draw_tile(self, x, y, display):
        display_x = (x - self.camera.x) + 1
        display_y = (y - self.camera.y) + 1
        sprite = self.tilemap.get(x, y).sprite
        display.put_char(display_x,
                         display_y,
                         sprite.char,
                         sprite.fg,
                         sprite.bg,
                         sprite.bold)

    def draw_entity_at(self, entity_id, display):
        x = (self.entities[entity_id].x - self.camera.x) + 1
        y = (self.entities[entity_id].y - self.camera.y) + 1
        sprite = self.entities[entity_id].sprite
        display.put_char(x, y, sprite.char, sprite.fg, sprite.bg, sprite.bold)

    def draw_entity(self, entity, display):
        x = (entity.x - self.camera.x) + 1
        y = (entity.y - self.camera.y) + 1
        sprite = entity.sprite
        bg = entity.sprite.bg
        if bg == interface.TRANSPARENT:
            bg = self.tilemap.get(entity.x, entity.y).sprite.bg
        display.put_char(x, y, sprite.char, sprite.fg, bg, sprite.bold)

    def draw_entities(self, display):
        for i in self.entities:
            if self.camera.is_visible(i.x, i.y):
                self.draw_entity(i, display)

    def update_entities(self):
        for i in self.entities:
            if self.camera.is_visible(i.x, i.y):
                i.update()
                tile = self.tilemap.get(i.x, i.y)
                if tile.solid:
                   i.move_back() 

    def print_log(self, display):
        width, height = display.get_size()
        for y in range(17, height - 1):
            for x in range(1, width - 1):
                display.put_char(x, y, ' ', 0, 0)
        for i in range(17, height - 1):
            if (len(self.log_messages) > i - 17):
                color = interface.BLACK
                if i == 17:
                    color = interface.WHITE
                display.put_string(1,
                                   i,
                                   self.log_messages[i - 17],
                                   color,
                                   interface.BLACK,
                                   False if i == 17 else True)

    def log(self, text):
        self.log_messages.insert(0, text)

    def draw_inventory(self, display):
        width, height = display.get_size()
        for y in range(1, 16):
            for x in range(37, width - 1):
                display.put_char(x, y, ' ', 0, 0)
        for i in range(len(self.player.inventory)):
            name = self.player.inventory[i].name
            pos = i + 1
            display.put_string(39, pos, name, interface.WHITE, interface.BLACK)

    def select_item(self, display):
        fg = interface.WHITE
        bg = interface.BLACK
        selection = 0
        display.put_char(37, selection + 1, '>', fg, bg)
        display.flush()
        while True:
            key = display.get_input()
            display.put_char(37, selection + 1, ' ', 0, 0)
            if key == 'UP':
                selection -= 1
                if selection < 0:
                    selection = len(self.player.inventory) - 1
            if key == 'DOWN':
                selection += 1
                if selection >= len(self.player.inventory):
                    selection = 0
            if key == 'SELECT':
                break
            if key == 'q':
                return
            display.put_char(37, selection + 1, '>', fg, bg)
            display.flush()
        return selection

    def __init__(self, display):
        super(PlayScene, self).__init__(display)
        display.clear()
        self.log_messages = []
        self.tilemap = Tilemap()
        self.draw_frame(display)
        self.camera = Camera(35, 15, self.tilemap.width, self.tilemap.height)

        self.draw_view(display)

        self.player = Player(self.camera.half_width, self.camera.half_height)
        self.player_id = 0
        self.entities = self.tilemap.entities
        self.entities.append(self.player)
        rod = FishingRod()
        (rod.x, rod.y) = (10, 15)
        axe = Axe()
        (axe.x, axe.y) = (28, 5)
        self.entities.insert(0, rod)
        self.entities.insert(0, axe)
        self.draw_entities(display)
        self.draw_inventory(display)
        display.flush()

    def update(self, display, key):
        if key == 'RESIZE':
            display.clear()
            self.draw_frame(display)
            self.draw_view(display)
            return
            
        if key == 'DROP':
            self.log('Drop what? (Directional keys to select.)')
            self.print_log(display)
            item = self.player.drop(self.select_item(display))
            self.entities.insert(0, item)
            self.log('You drop the ' + item.name + '.')
            self.draw_inventory(display)
            self.print_log(display)
            return

        if key == 'EAT':
            self.log('Eat what? (Directional keys to select.)')
            self.print_log(display)
            item = self.select_item(display)
            if self.player.inventory[item].edible:
                self.log('You eat the ' + self.player.inventory[item].name + '.')
                self.player.drop(item)
            else:
                self.log('That doesn\'t look very tasty.')
            self.draw_inventory(display)
            self.print_log(display)
            return

        if key == 'SELECT':
            tile = self.tilemap.get(self.player.x, self.player.y)
            for i in self.entities:
                if i.x == tile.x and i.y == tile.y and i.name != 'player':
                    self.log('You pick up the ' + i.name + '.')
                    self.print_log(display)
                    self.player.pickup(self.entities.pop(self.entities.index(i)))
                    self.draw_inventory(display)
                    return
            if type(tile) == Wheat:
                wheat = tile.pick()
                if wheat != None:
                    self.log('You pick some wheat.')
                    self.print_log(display)
                    self.player.pickup(wheat)
                    self.draw_inventory(display)
                    return
            self.log('There\'s nothing here to pickup.')
            self.print_log(display)
            return

        if key == 'APPLY':
            self.log('Apply what? (Directional keys to select.)')
            self.print_log(display)
            item = self.player.inventory[self.select_item(display)]
            self.log('Apply ' + item.name + ' in which direction?')
            self.print_log(display)
            direction = directions.from_key(display.get_input())
            if direction != directions.INVALID:
                delta_x, delta_y = directions.delta(direction)
                tile_x = self.player.x + delta_x
                tile_y = self.player.y + delta_y
                for i in self.entities:
                    if i.x == tile_x and i.y == tile_y:
                        if type(i) == Log and type(item) == TinderBox:
                            self.entities.remove(i)
                            self.tilemap.set(tile_x,
                                             tile_y,
                                             Fire(tile_x, tile_y))
                            self.draw_tile(tile_x, tile_y, display)
                            self.log('You make a fire')
                            self.print_log(display)
                            break
                        if type(i) == Chicken:
                            if type(item) == Wheat:
                                i.owner = self.player
                                self.log('The chicken happily eats the wheat ' +
                                         'and decides to join you in your ' +
                                         'adventures.')
                                self.print_log(display)
                            else:
                                self.log('The chicken doesn\'t seem to want ' +
                                         'that.')
                            break
                if type(self.tilemap.get(tile_x, tile_y)) == Fire:
                    if type(item) == Fish:
                        old_name = item.name
                        if item.cook():
                            self.log('You cook the ' + old_name +
                                     ' into ' + item.name)
                            self.draw_inventory(display)
                        else:
                            self.log('Uh, don\'t cook that')
                        self.print_log(display)

            else:
                self.log('Uh, that\'s not a direction')
                self.print_log(display)
            return
            
        direction = directions.from_key(key)
        if direction != directions.INVALID:
            self.player.move(direction)
            tile = self.tilemap.get(self.player.x, self.player.y)

            if tile.solid:
                self.player.move_back()
            
            if type(tile) == Water:
                fish = None
                for i in self.player.inventory:
                    if type(i) == FishingRod:
                        fish = tile.fish()
                        break
                if fish != None:
                    self.log('You catch a fish!')
                    self.player.inventory.append(fish)
                else:
                    tile.disturb()
                    self.log('You disturb the water.')

            if type(tile) == Tree:
                if tile.chopped == False:
                    has_axe = False
                    log = None
                    for i in self.player.inventory:
                        if type(i) == Axe:
                            has_axe = True
                            break
                    if has_axe:
                        log = tile.chop()
                        self.log('You cut down the tree, and aquire some logs')
                        self.player.pickup(log)
                    else:    
                        self.log('It would be very painful to chop down a ' +
                                 'tree with your fist')

        self.camera.center_on(self.player.x, self.player.y)
        self.update_and_draw_view(display)

        self.draw_inventory(display)

        self.print_log(display)

        display.flush()
コード例 #11
0
class PlayScene(Scene):
    def draw_frame(self, display):
        width, height = display.get_size()
        fg = interface.WHITE
        bg = interface.BLACK
        for i in range(width):
            display.put_char(i, 0, curses.ACS_HLINE, fg, bg)
            display.put_char(i, 16, curses.ACS_HLINE, fg, bg)
            display.put_char(i, height - 1, curses.ACS_HLINE, fg, bg)
        for i in range(height):
            display.put_char(0, i, curses.ACS_VLINE, fg, bg)
            display.put_char(width - 1, i, curses.ACS_VLINE, fg, bg)
        for i in range(16):
            display.put_char(36, i, curses.ACS_VLINE, fg, bg)

        display.put_char(0, 0, curses.ACS_ULCORNER, fg, bg)
        display.put_char(36, 0, curses.ACS_TTEE, fg, bg)
        display.put_char(0, 16, curses.ACS_LTEE, fg, bg)
        display.put_char(36, 16, curses.ACS_BTEE, fg, bg)
        display.put_char(0, height - 1, curses.ACS_LLCORNER, fg, bg)
        display.put_char(width - 1, height - 1, curses.ACS_LRCORNER, fg, bg)
        display.put_char(width - 1, 0, curses.ACS_URCORNER, fg, bg)
        display.put_char(width - 1, 16, curses.ACS_RTEE, fg, bg)

    def draw_view(self, display):
        for i in self.camera:
            self.draw_tile(i.x, i.y, display)

    def update_and_draw_view(self, display):
        for i in self.camera:
            self.tilemap.get(i.x, i.y).update()
            self.draw_tile(i.x, i.y, display)
        self.update_entities()
        self.draw_entities(display)

    def draw_tile(self, x, y, display):
        display_x = (x - self.camera.x) + 1
        display_y = (y - self.camera.y) + 1
        sprite = self.tilemap.get(x, y).sprite
        display.put_char(display_x, display_y, sprite.char, sprite.fg,
                         sprite.bg, sprite.bold)

    def draw_entity_at(self, entity_id, display):
        x = (self.entities[entity_id].x - self.camera.x) + 1
        y = (self.entities[entity_id].y - self.camera.y) + 1
        sprite = self.entities[entity_id].sprite
        display.put_char(x, y, sprite.char, sprite.fg, sprite.bg, sprite.bold)

    def draw_entity(self, entity, display):
        x = (entity.x - self.camera.x) + 1
        y = (entity.y - self.camera.y) + 1
        sprite = entity.sprite
        bg = entity.sprite.bg
        if bg == interface.TRANSPARENT:
            bg = self.tilemap.get(entity.x, entity.y).sprite.bg
        display.put_char(x, y, sprite.char, sprite.fg, bg, sprite.bold)

    def draw_entities(self, display):
        for i in self.entities:
            if self.camera.is_visible(i.x, i.y):
                self.draw_entity(i, display)

    def update_entities(self):
        for i in self.entities:
            if self.camera.is_visible(i.x, i.y):
                i.update()
                tile = self.tilemap.get(i.x, i.y)
                if tile.solid:
                    i.move_back()

    def print_log(self, display):
        width, height = display.get_size()
        for y in range(17, height - 1):
            for x in range(1, width - 1):
                display.put_char(x, y, ' ', 0, 0)
        for i in range(17, height - 1):
            if (len(self.log_messages) > i - 17):
                color = interface.BLACK
                if i == 17:
                    color = interface.WHITE
                display.put_string(1, i, self.log_messages[i - 17], color,
                                   interface.BLACK, False if i == 17 else True)

    def log(self, text):
        self.log_messages.insert(0, text)

    def draw_inventory(self, display):
        width, height = display.get_size()
        for y in range(1, 16):
            for x in range(37, width - 1):
                display.put_char(x, y, ' ', 0, 0)
        for i in range(len(self.player.inventory)):
            name = self.player.inventory[i].name
            pos = i + 1
            display.put_string(39, pos, name, interface.WHITE, interface.BLACK)

    def select_item(self, display):
        fg = interface.WHITE
        bg = interface.BLACK
        selection = 0
        display.put_char(37, selection + 1, '>', fg, bg)
        display.flush()
        while True:
            key = display.get_input()
            display.put_char(37, selection + 1, ' ', 0, 0)
            if key == 'UP':
                selection -= 1
                if selection < 0:
                    selection = len(self.player.inventory) - 1
            if key == 'DOWN':
                selection += 1
                if selection >= len(self.player.inventory):
                    selection = 0
            if key == 'SELECT':
                break
            if key == 'q':
                return
            display.put_char(37, selection + 1, '>', fg, bg)
            display.flush()
        return selection

    def __init__(self, display):
        super(PlayScene, self).__init__(display)
        display.clear()
        self.log_messages = []
        self.tilemap = Tilemap()
        self.draw_frame(display)
        self.camera = Camera(35, 15, self.tilemap.width, self.tilemap.height)

        self.draw_view(display)

        self.player = Player(self.camera.half_width, self.camera.half_height)
        self.player_id = 0
        self.entities = self.tilemap.entities
        self.entities.append(self.player)
        rod = FishingRod()
        (rod.x, rod.y) = (10, 15)
        axe = Axe()
        (axe.x, axe.y) = (28, 5)
        self.entities.insert(0, rod)
        self.entities.insert(0, axe)
        self.draw_entities(display)
        self.draw_inventory(display)
        display.flush()

    def update(self, display, key):
        if key == 'RESIZE':
            display.clear()
            self.draw_frame(display)
            self.draw_view(display)
            return

        if key == 'DROP':
            self.log('Drop what? (Directional keys to select.)')
            self.print_log(display)
            item = self.player.drop(self.select_item(display))
            self.entities.insert(0, item)
            self.log('You drop the ' + item.name + '.')
            self.draw_inventory(display)
            self.print_log(display)
            return

        if key == 'EAT':
            self.log('Eat what? (Directional keys to select.)')
            self.print_log(display)
            item = self.select_item(display)
            if self.player.inventory[item].edible:
                self.log('You eat the ' + self.player.inventory[item].name +
                         '.')
                self.player.drop(item)
            else:
                self.log('That doesn\'t look very tasty.')
            self.draw_inventory(display)
            self.print_log(display)
            return

        if key == 'SELECT':
            tile = self.tilemap.get(self.player.x, self.player.y)
            for i in self.entities:
                if i.x == tile.x and i.y == tile.y and i.name != 'player':
                    self.log('You pick up the ' + i.name + '.')
                    self.print_log(display)
                    self.player.pickup(
                        self.entities.pop(self.entities.index(i)))
                    self.draw_inventory(display)
                    return
            if type(tile) == Wheat:
                wheat = tile.pick()
                if wheat != None:
                    self.log('You pick some wheat.')
                    self.print_log(display)
                    self.player.pickup(wheat)
                    self.draw_inventory(display)
                    return
            self.log('There\'s nothing here to pickup.')
            self.print_log(display)
            return

        if key == 'APPLY':
            self.log('Apply what? (Directional keys to select.)')
            self.print_log(display)
            item = self.player.inventory[self.select_item(display)]
            self.log('Apply ' + item.name + ' in which direction?')
            self.print_log(display)
            direction = directions.from_key(display.get_input())
            if direction != directions.INVALID:
                delta_x, delta_y = directions.delta(direction)
                tile_x = self.player.x + delta_x
                tile_y = self.player.y + delta_y
                for i in self.entities:
                    if i.x == tile_x and i.y == tile_y:
                        if type(i) == Log and type(item) == TinderBox:
                            self.entities.remove(i)
                            self.tilemap.set(tile_x, tile_y,
                                             Fire(tile_x, tile_y))
                            self.draw_tile(tile_x, tile_y, display)
                            self.log('You make a fire')
                            self.print_log(display)
                            break
                        if type(i) == Chicken:
                            if type(item) == Wheat:
                                i.owner = self.player
                                self.log(
                                    'The chicken happily eats the wheat ' +
                                    'and decides to join you in your ' +
                                    'adventures.')
                                self.print_log(display)
                            else:
                                self.log('The chicken doesn\'t seem to want ' +
                                         'that.')
                            break
                if type(self.tilemap.get(tile_x, tile_y)) == Fire:
                    if type(item) == Fish:
                        old_name = item.name
                        if item.cook():
                            self.log('You cook the ' + old_name + ' into ' +
                                     item.name)
                            self.draw_inventory(display)
                        else:
                            self.log('Uh, don\'t cook that')
                        self.print_log(display)

            else:
                self.log('Uh, that\'s not a direction')
                self.print_log(display)
            return

        direction = directions.from_key(key)
        if direction != directions.INVALID:
            self.player.move(direction)
            tile = self.tilemap.get(self.player.x, self.player.y)

            if tile.solid:
                self.player.move_back()

            if type(tile) == Water:
                fish = None
                for i in self.player.inventory:
                    if type(i) == FishingRod:
                        fish = tile.fish()
                        break
                if fish != None:
                    self.log('You catch a fish!')
                    self.player.inventory.append(fish)
                else:
                    tile.disturb()
                    self.log('You disturb the water.')

            if type(tile) == Tree:
                if tile.chopped == False:
                    has_axe = False
                    log = None
                    for i in self.player.inventory:
                        if type(i) == Axe:
                            has_axe = True
                            break
                    if has_axe:
                        log = tile.chop()
                        self.log('You cut down the tree, and aquire some logs')
                        self.player.pickup(log)
                    else:
                        self.log('It would be very painful to chop down a ' +
                                 'tree with your fist')

        self.camera.center_on(self.player.x, self.player.y)
        self.update_and_draw_view(display)

        self.draw_inventory(display)

        self.print_log(display)

        display.flush()