Esempio n. 1
0
	def render(self):
		if splash:
			t.layer(0)
			set_colour(colours.white, 100)
			t.put(0, 0, 0x5E)
			#show the game's title, and some credits!

			
			title = 'Death and Axes'
			center = (SCREEN_WIDTH - len(title)) // 2

			t.layer(UI_LAYER)
			set_colour(colours.dark_azure, 150)
			t.puts(center - 1, SCREEN_HEIGHT//2 - 5, BLOCK_CHAR * (len(title) + 2) * 3, len(title) + 2, 3)

			t.layer(UI_TEXT_LAYER)
			set_colour(colours.light_yellow)
			t.puts(center, SCREEN_HEIGHT//2-4, title)

			title = 'By Cerepol'
			center = (SCREEN_WIDTH - len(title)) // 2

			t.layer(UI_LAYER)
			set_colour(colours.dark_azure, 150)
			t.puts(center - 1, SCREEN_HEIGHT - 3, BLOCK_CHAR * (len(title) + 2) * 3, len(title) + 2, 3)

			t.layer(UI_TEXT_LAYER)
			set_colour(colours.light_yellow)
			t.puts(center, SCREEN_HEIGHT-2, title)

			t.refresh()
			t.delay(2000)
			t.layer(0)

			splash = False
Esempio n. 2
0
    def play(self, game_master):
        while game_master.game_state == 'playing':
            # Check game state
            for player in self.players:
                if not player.avatar.alive:
                    game_master.game_state = 'dead'

            current_player = self.players[0]

            render(current_player, game_master)

            terminal.layer(2)
            for o in self.current_map.objects:
                o.clear(current_player.camera)

            for obj in self.current_map.objects:
                if obj.active:
                    obj.update()

            for effect in self.current_map.effects:
                if effect.active:
                    effect.update()

            self.current_map.update()

            player.action = current_player.input(self)
            if player.action == 'quit':
                game_master.save_game()
                game_master.main_menu()

            terminal.delay(1000 // game_master.fps)
Esempio n. 3
0
def game_loop(game):
    while True:
        if blt.has_input():
            code = blt.read()
            if code in codes_close:
                break
            ui.update(game, code)
            draw.update(game)
        blt.delay(1)
Esempio n. 4
0
def game_loop():
    field = read_map("map.txt")
    speed = 100
    initialize_render()
    while True:
        if blt.has_input():
            key = blt.read()
            if key == blt.TK_A:
                speed = 1000
            if key == blt.TK_S:
                speed = 500
            if key == blt.TK_D:
                speed = 100
            if key == blt.TK_F:
                speed = 10

            if key == blt.TK_CLOSE or key == blt.TK_Q:
                break
        update(field)
        render()
        blt.delay(speed)
    terminate_render()
Esempio n. 5
0
def main():
    terminal.open()
    terminal.set('input.filter=[keyboard, close]')

    wl = Wall(FIELD_WIDTH, FIELD_HEIGHT, '+')
    wl.draw()

    snk = Snake(size=4)
    snk.draw()

    food = food_creator(snk)
    food.draw()

    terminal.refresh()

    while terminal.peek() != terminal.TK_CLOSE:
        if terminal.has_input():
            snk.change_direction(terminal.read())

        if snk.collision(wl) or snk.collision(snk):
            break

        if snk.collision(food):
            snk.eat(food)

            food = food_creator(snk)
            food.draw()
        else:
            snk.move()

        terminal.refresh()
        terminal.delay(100)

    if not terminal.has_input():
        game_over()
        terminal.read()

    terminal.close()
def test_extended_smooth_scroll():
    random.seed()

    blt.set("window.title='Omni: extended output / smooth scroll'")
    blt.set("input.filter={keyboard+}")
    blt.composition(True)

    # Load resources
    blt.set("U+E000: ../Media/Tiles.png, size=32x32, alignment=top-left")

    screen_width = blt.state(blt.TK_WIDTH) * blt.state(blt.TK_CELL_WIDTH)
    screen_height = blt.state(blt.TK_HEIGHT) * blt.state(blt.TK_CELL_HEIGHT)
    hspeed = vspeed = 0
    hoffset = voffset = 0

    map_ = [[0] * map_size for _ in range(map_size)]

    for _ in range(map_size * map_size // 10):
        x = random.randrange(map_size)
        y = random.randrange(map_size)
        map_[y][x] = random.randrange(8)

    while True:
        hoffset -= hspeed
        voffset -= vspeed

        blt.clear()

        tx = hoffset % tile_size
        ty = voffset % tile_size
        ix = (hoffset - tx) // tile_size
        iy = (voffset - ty) // tile_size
        jx = (-ix) % map_size if ix < 0 else map_size - (ix % map_size)
        jy = (-iy) % map_size if iy < 0 else map_size - (iy % map_size)
        hc = (screen_width + 2 * tile_size - tx - 1) // tile_size
        vc = (screen_height + 2 * tile_size - ty - 1) // tile_size

        blt.puts(2, 1, "speed: %d, %d" % (hspeed, vspeed))
        blt.puts(2, 2, "offset: %d/%d, %d/%d" % (ix, jx, iy, jy))

        for y in range(vc + 1):
            my = (jy + y) % map_size
            for x in range(hc + 1):
                mx = (jx + x) % map_size
                c = map_[my][mx]
                blt.put_ext(0, 0, (x - 1) * tile_size + tx,
                            (y - 1) * tile_size + ty, 0xE000 + c)

        blt.refresh()

        if blt.has_input():
            key = blt.read()
            if key in (blt.TK_CLOSE, blt.TK_ESCAPE):
                break

        if blt.state(blt.TK_LEFT):
            if hspeed > -speed_cap: hspeed -= 1
        elif blt.state(blt.TK_RIGHT):
            if hspeed < speed_cap: hspeed += 1
        else:
            hspeed -= sgn(hspeed)

        if blt.state(blt.TK_UP):
            if vspeed > -speed_cap: vspeed -= 1
        elif blt.state(blt.TK_DOWN):
            if vspeed < speed_cap: vspeed += 1
        else:
            vspeed -= sgn(vspeed)

        blt.delay(1000 // fps)

    blt.set("U+E000: none")
    blt.set("input.filter={keyboard}")
    blt.composition(False)
Esempio n. 7
0
def input_listener(self):
    render_rate = 0
    anim = True
    while True:
        start_time = time.time()
        key = None
        if terminal.has_input() or self.pause == True:
            key = terminal.read()
        ###################
        ## Mouse catcher ## need to catch all mouse input or pause doesn't work
        ###################
        if (key == terminal.TK_MOUSE_SCROLL):
            print('mouse')
            self.tile_size -= int(terminal.state(terminal.TK_MOUSE_WHEEL))
            terminal.set(f"font: MegaFont.ttf, size={self.tile_size}")
            key = None
            continue
        if (key == terminal.TK_MOUSE_RIGHT):
            key = None
            continue

        if self.input_state == 'main':
            self.pause = True
            if (key == terminal.TK_MOUSE_LEFT):
                self.main_menu(0, terminal.state(terminal.TK_MOUSE_X),
                               terminal.state(terminal.TK_MOUSE_Y), True,
                               False)
            if (key == terminal.TK_MOUSE_MOVE):
                self.main_menu(0, terminal.state(terminal.TK_MOUSE_X),
                               terminal.state(terminal.TK_MOUSE_Y), False,
                               False)
                key = None
            if (key == terminal.TK_UP or key == terminal.TK_KP_8):
                self.main_menu(-1, 0, 0, False, False)
            if (key == terminal.TK_DOWN or key == terminal.TK_KP_2):
                self.main_menu(1, 0, 0, False, False)
            if (key == terminal.TK_ENTER):
                self.main_menu(0, 0, 0, False, True)
            if (key == terminal.TK_ESCAPE) and self.coords != []:
                self.input_state = 'sector'
                key = None
        if self.input_state == 'intro':
            Intro.play_intro(self, 1)
            if (key == terminal.TK_ENTER) or (key == terminal.TK_ESCAPE) or (
                    key == terminal.TK_MOUSE_LEFT):
                self.main_menu(0, 0, 0, False, False)
        if self.input_state == 'sector loaded':
            print('loaded')
            if (key == terminal.TK_ENTER) or (key == terminal.TK_ESCAPE) or (
                    key == terminal.TK_MOUSE_LEFT):
                self.input_state = 'sector'
                terminal.clear()
                key = None
        if self.input_state == 'sector':
            if (key == terminal.TK_LEFT or key == terminal.TK_KP_4
                    or key == terminal.TK_KP_7 or key == terminal.TK_KP_1):
                self.move_character(0, -1, 0)
            if (key == terminal.TK_RIGHT or key == terminal.TK_KP_6
                    or key == terminal.TK_KP_3 or key == terminal.TK_KP_9):
                self.move_character(0, 1, 0)
            if (key == terminal.TK_UP or key == terminal.TK_KP_8
                    or key == terminal.TK_KP_9 or key == terminal.TK_KP_7):
                self.move_character(0, 0, -1)
            if (key == terminal.TK_DOWN or key == terminal.TK_KP_2
                    or key == terminal.TK_KP_1 or key == terminal.TK_KP_3):
                self.move_character(0, 0, 1)
            if (key == terminal.TK_W or key == terminal.TK_KP_MINUS):
                self.move_character(-1, 0, 0)
            if (key == terminal.TK_S or key == terminal.TK_KP_PLUS):
                self.move_character(1, 0, 0)
            if (key == terminal.TK_SPACE):
                self.pause = not self.pause
            if self.pause:
                self.dev_console("[color=yellow]PAUSED  ", 0)
            else:
                self.dev_console("[color=dark red]realtime", 0)
            ########################
            ## animation modifier ##
            ########################
            render_rate += 1
            if render_rate >= 15:
                render_rate = 0
                anim = True
            render_starchart(self, anim)
            if (key == terminal.TK_ESCAPE):
                self.main_menu(0, 0, 0, False, False)
        # always active
        if (key == terminal.TK_CLOSE):
            break
        if (key == terminal.TK_RESIZED):
            print('hello')
        anim = False

        ##########################
        # mainloop frame counter #
        ##########################
        delta = int(time.time() * 1000 - start_time * 1000)
        target_framerate = 15
        if delta < target_framerate:
            terminal.delay(target_framerate - delta)
        if self.input_state != 'intro' and self.input_state != 'main' and self.dev_mode == True:
            # main loop time in milliseconds
            self.dev_console("Loop: " + str(delta), 2)
            # current frames per second (not averaged)
            self.dev_console(
                "FPS: " + str(int(1.0 / (time.time() - start_time))), 3)
Esempio n. 8
0
def main():
    terminal.open()

    terminal.set("output.vsync=true")
    terminal.set(
        "window: title='Plasma Rain', resizeable=true, minimum-size=16x12")
    terminal.set("window: size={}x{}; font: ".format(INITIAL_SCREEN_WIDTH,
                                                     INITIAL_SCREEN_LENGTH) +
                 os.path.join(DIR_PATH, '../data/media/lucida.ttf') +
                 ", size={}x{}".format(TILE_WIDTH, TILE_LENGTH))
    terminal.set("input.filter= [keyboard+, mouse_move]"
                 )  # Only key release and mouse move trigger state updates
    terminal.composition(terminal.TK_ON)
    terminal.bkcolor(terminal.color_from_name("gray"))
    # grey (or gray), red, flame, orange, amber, yellow, lime,
    # chartreuse, green, sea, turquoise, cyan, sky, azure, blue,
    # han, violet, purple, fuchsia, magenta, pink, crimson, transparent
    terminal.color(terminal.color_from_name("black"))

    x_speed, y_speed, text_offset = (0, ) * 3

    # initialize blank tile_map
    tile_map: [[[Tile]]] = [[[EMPTY_TILE for _ in range(MAP_WIDTH)]
                             for _ in range(MAP_LENGTH)]
                            for _ in range(MAP_DEPTH)]
    unit_map: [[[Optional[Unit]]]] = [[[None for _ in range(MAP_WIDTH)]
                                       for _ in range(MAP_LENGTH)]
                                      for _ in range(MAP_DEPTH)]
    blueprints = build_blueprints(
        load_json(os.path.join(mods_dir, "vanilla/blueprints/"), pickle=False))
    unit_map[0][0][0] = Unit(blueprint=blueprints['unit']['Human'],
                             armor=blueprints['armor']['Suit'],
                             current_stats=[0, 0, 0, 0, 0],
                             overlay_stats=[0, 0, 0, 0, 0])
    zone = load_zone(os.path.join(DIR_PATH, '../data/placeholder/map2.json'),
                     blueprints)
    paste_zone(zone, tile_map, z=0, y=0)

    prev_frame_time = time.perf_counter()
    iterations = 0

    # tile_map will only render to screen inside the display, generally set to not overlap with UI
    # tile_map will only be scrollable between the offset bounds
    # coordinates are down-right = positive, "opposite" for the offset
    # set offset_leniency to 1 and you will only be allowed to scroll 1 more tile than enough to see every tile
    # in general the coordinate system is z, y, x: depth, length, width. No height because it's ambiguous.
    screen_width, screen_length, display_min_y, display_max_y, display_min_x, display_max_x, \
    min_y_offset, max_y_offset, min_x_offset, max_x_offset, x_scroll_leniency, y_scroll_leniency = (0,) * 12
    """
    Adjusts the bounds defined above, to be called when opening and resizing
    """
    def reset_bounds():
        # nonlocal allows modification of outer function's variables
        nonlocal screen_width, screen_length, display_min_y, display_max_y, display_min_x, display_max_x, \
            min_y_offset, max_y_offset, min_x_offset, max_x_offset, y_scroll_leniency, x_scroll_leniency

        screen_length = terminal.state(terminal.TK_HEIGHT) * TILE_LENGTH
        screen_width = terminal.state(terminal.TK_WIDTH) * TILE_WIDTH

        y_scroll_leniency = int(screen_length * SCROLL_LENIENCY)
        x_scroll_leniency = int(screen_length * SCROLL_LENIENCY)

        display_min_y = 0 + MARGIN_TOP * TILE_LENGTH
        display_max_y = screen_length - MARGIN_BOTTOM * TILE_LENGTH
        display_min_x = 0 + MARGIN_LEFT * TILE_WIDTH
        display_max_x = screen_width - MARGIN_RIGHT * TILE_WIDTH

        # TODO convert offsets into actual tile offsets, exclude the complex GUI stuff
        min_y_offset = min(0, -y_scroll_leniency)
        max_y_offset = max(
            0, -screen_length + MAP_WIDTH * TILE_LENGTH + y_scroll_leniency)
        min_x_offset = min(0, -x_scroll_leniency)
        max_x_offset = max(
            0, -screen_width + MAP_WIDTH * TILE_WIDTH + x_scroll_leniency)

    def get_highest_object_if_exists(start_z: int, tile_y: int, tile_x: int,
                                     include_tiles: bool = True, include_units: bool = False) -> \
            Union[None, Tile, Unit]:
        if tile_y not in range(0, MAP_WIDTH) or tile_x not in range(
                0, MAP_LENGTH):
            return None
        for zi in range(start_z, -1, -1):
            if include_units and unit_map[zi][tile_y][tile_x] is not None:
                return unit_map[zi][tile_y][tile_x]
            elif include_tiles and tile_map[zi][tile_y][tile_x] != EMPTY_TILE:
                return tile_map[zi][tile_y][tile_x]
        return None

    reset_bounds()

    y_offset, x_offset = max(display_min_y, 0), max(0, display_min_x)

    camera_height = 0

    proceed = True
    while proceed:
        # t = partial-tile offset in pixels
        # i = full-tile offset in tiles
        # c = number of tiles to render
        ty = y_offset % TILE_LENGTH
        tx = x_offset % TILE_WIDTH
        iy = y_offset // TILE_LENGTH
        ix = x_offset // TILE_WIDTH
        # vc = screen_length // TILE_SIZE + 1
        # hc = screen_width // TILE_SIZE + 1
        mouse_x = terminal.state(
            terminal.TK_MOUSE_X) - x_offset - display_min_x // TILE_WIDTH
        mouse_y = terminal.state(
            terminal.TK_MOUSE_Y) - y_offset - display_min_y // TILE_LENGTH

        x_offset = clamp(x_offset + x_speed, min_x_offset, max_x_offset)
        y_offset = clamp(y_offset + y_speed, min_y_offset, max_y_offset)

        terminal.clear()

        mouse_over_tile = get_highest_object_if_exists(camera_height, mouse_y,
                                                       mouse_x)
        mouse_over_tile = mouse_over_tile.blueprint.name if mouse_over_tile is not None else "Empty"
        mouse_over_unit = get_highest_object_if_exists(camera_height,
                                                       mouse_y,
                                                       mouse_x,
                                                       include_units=True,
                                                       include_tiles=False)
        mouse_over_unit = mouse_over_unit.blueprint.name if mouse_over_unit else "Empty"

        terminal.print(2, 0, "speed: {}, {}".format(x_speed, y_speed))
        terminal.print(
            2, 1, "offset: {}, {}, height : {}".format(ix, iy, camera_height))
        terminal.print(
            2, 2, "tile at ({}, {}): {}".format(mouse_x, mouse_y,
                                                mouse_over_tile))
        terminal.print(2, 3, "unit: {}".format(mouse_over_unit))

        higher_tile_already_rendered: [[bool]
                                       ] = [[False for _ in range(MAP_WIDTH)]
                                            for _ in range(MAP_LENGTH)]

        # print scrollable map
        for z in range(camera_height, -1,
                       -1):  # top has higher render priority
            for y in range(0, MAP_LENGTH):
                for x in range(0, MAP_WIDTH):
                    # s = final coords in pixels
                    sx = (x + ix) * TILE_WIDTH + tx + display_min_x
                    sy = (y + iy) * TILE_LENGTH + ty + display_min_y
                    # render only on-screen tiles
                    if (display_min_y <= sy <= display_max_y
                            and display_min_x <= sx <= display_max_x
                            and not higher_tile_already_rendered[y][x]
                            and (tile_map[z][y][x] != EMPTY_TILE
                                 or unit_map[z][y][x] is not None)):
                        if unit_map[z][y][x] is not None:
                            terminal.put_ext(0, 0, sx, sy,
                                             unit_map[z][y][x].blueprint.icon)
                            higher_tile_already_rendered[y][x] = True
                            # TODO why is it printing two chars?
                        else:
                            if z < camera_height and tile_map[z][y][
                                    x] != EMPTY_TILE:
                                terminal.put_ext(
                                    0, 0, sx, sy, 0x2588,
                                    (terminal.color_from_name('yellow'),
                                     terminal.color_from_name('red')) * 4)
                            terminal.put_ext(0, 0, sx, sy,
                                             tile_map[z][y][x].blueprint.icon)
                            higher_tile_already_rendered[y][x] = True

        terminal.refresh()

        while proceed and terminal.has_input():
            key = terminal.read()
            if key == terminal.TK_CLOSE or key == terminal.TK_ESCAPE:
                proceed = False
            elif key == terminal.TK_RESIZED:
                reset_bounds()
            elif key == terminal.TK_KP_PLUS:
                camera_height = clamp(camera_height + 1, 0, MAP_DEPTH - 1)
            elif key == terminal.TK_KP_MINUS:
                camera_height = clamp(camera_height - 1, 0, MAP_DEPTH - 1)
        if terminal.state(terminal.TK_LEFT):
            if x_speed < SPEED_CAP:
                x_speed += SPEED_ACCELERATION
            else:
                x_speed = SPEED_CAP
        elif terminal.state(terminal.TK_RIGHT):
            if x_speed > -SPEED_CAP:
                x_speed -= SPEED_ACCELERATION
            else:
                x_speed = -SPEED_CAP
        else:
            x_speed -= sgn(x_speed)
        if terminal.state(terminal.TK_UP):
            if y_speed < SPEED_CAP:
                y_speed += SPEED_ACCELERATION
            else:
                y_speed = SPEED_CAP
        elif terminal.state(terminal.TK_DOWN):
            if y_speed > -SPEED_CAP:
                y_speed -= SPEED_ACCELERATION
            else:
                y_speed = -SPEED_CAP
        else:
            y_speed -= sgn(y_speed)

        current_time = time.perf_counter()
        # If twice as slow as desirable warn us, check once every 10 seconds
        if iterations % (
                10 * FPS
        ) == 0 and iterations > 0 and current_time - prev_frame_time > 2 / FPS:
            print(
                "Lag detected. Desired frame_time: {}; actual frame_time: {}".
                format(1 / FPS, current_time - prev_frame_time))
        prev_frame_time = current_time
        iterations += 1

        terminal.delay(1000 // FPS)
    terminal.close()
Esempio n. 9
0
def test_extended_basics():
    # Setup
    blt.set("window.title='Omni: extended output / basics'")
    blt.set("0xE000: ../Media/Tiles.png, size=32x32, align=top-left")
    blt.composition(True)

    cx, cy = 10, 5
    n_symbols = 10
    radius = 5
    angle = 0.0
    fps = 25
    transparent, opaque = 0x00FFFFFF, 0xFFFFFFFF

    m00 = [0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFF00]
    m01 = [opaque, opaque, transparent, transparent]

    m11 = [transparent, transparent, opaque, transparent]
    m12 = [transparent, opaque, transparent, transparent]
    m21 = [transparent, transparent, transparent, opaque]
    m22 = [opaque, transparent, transparent, transparent]

    while True:
        blt.clear()
        blt.color("white")

        blt.puts(
            2, 1,
            "[color=orange]1.[/color] put_ext(x, y, [color=orange]dx[/color], [color=orange]dy[/color], code)"
        )
        for i in range(n_symbols):
            angle_delta = 2 * pi / n_symbols
            dx = cos(angle + i * angle_delta) * radius * blt.state(
                blt.TK_CELL_WIDTH)
            dy = sin(angle + i * angle_delta) * radius * blt.state(
                blt.TK_CELL_WIDTH) - 4
            blt.color("white" if i > 0 else "orange")
            blt.put_ext(cx, cy, int(dx), int(dy), ord('a') + i)

        angle += 2 * pi / (2 * fps)

        blt.puts(
            2, 9,
            "[color=orange]2.[/color] put_ext(x, y, dx, dy, code, [color=orange]corners[/color])"
        )
        blt.put_ext(5, 11, 0, 0, 0xE000 + 19, m00)
        blt.put_ext(10, 11, 0, 0, 0xE000 + 19, m01)

        blt.puts(2, 14, "[color=orange]3.[/color] put_ext + composition")
        x1 = 5
        y1 = 16
        blt.put(x1 + 0, y1 + 0, 0xE000 + 19)
        blt.put(x1 + 0, y1 + 2, 0xE000 + 8)
        blt.put(x1 + 5, y1 + 0, 0xE000 + 19)
        blt.put(x1 + 9, y1 + 0, 0xE000 + 19)
        blt.put(x1 + 5, y1 + 2, 0xE000 + 19)
        blt.put(x1 + 9, y1 + 2, 0xE000 + 19)
        blt.put_ext(x1 + 5, y1 + 0, 0, 0, 0xE000 + 8, m11)
        blt.put_ext(x1 + 9, y1 + 0, 0, 0, 0xE000 + 8, m12)
        blt.put_ext(x1 + 5, y1 + 2, 0, 0, 0xE000 + 8, m21)
        blt.put_ext(x1 + 9, y1 + 2, 0, 0, 0xE000 + 8, m22)

        blt.refresh()

        if blt.has_input():
            key = blt.read()
            if key in (blt.TK_CLOSE, blt.TK_ESCAPE):
                break

        blt.delay(1000 // fps)

    # Clean up
    blt.composition(False)
    blt.set("0xE000: none")
Esempio n. 10
0
def main_menu():
    """Main main for this game"""

    t.layer(0)
    set_colour(colours.white, 100)
    t.put(0, 0, 0x5E)
    #show the game's title, and some credits!

    title = 'Death and Axes'
    center = (SCREEN_WIDTH - len(title)) // 2

    t.layer(UI_LAYER)
    set_colour(colours.dark_azure, 150)
    t.puts(center - 1, SCREEN_HEIGHT // 2 - 5,
           BLOCK_CHAR * (len(title) + 2) * 3,
           len(title) + 2, 3)

    t.layer(UI_TEXT_LAYER)
    set_colour(colours.light_yellow)
    t.puts(center, SCREEN_HEIGHT // 2 - 4, title)

    title = 'By Cerepol'
    center = (SCREEN_WIDTH - len(title)) // 2

    t.layer(UI_LAYER)
    set_colour(colours.dark_azure, 150)
    t.puts(center - 1, SCREEN_HEIGHT - 3,
           BLOCK_CHAR * (len(title) + 2) * 3,
           len(title) + 2, 3)

    t.layer(UI_TEXT_LAYER)
    set_colour(colours.light_yellow)
    t.puts(center, SCREEN_HEIGHT - 2, title)

    t.refresh()
    t.delay(2000)
    t.layer(0)

    while True:
        t.clear()
        set_colour(colours.white, 100)
        t.put(0, 0, 0x5E)
        t.refresh()

        choice = menu(
            '', ['Start a new Adventure', 'Continue Previous game', 'Quit'],
            22)

        if choice == 0:
            new_game()
            play_game()

        elif choice == 1:
            try:
                load_game()
            except:
                msgbox('\n No saved game to load.\n', 24)
                continue
            play_game()

        elif choice == 2:
            break

    t.close()
Esempio n. 11
0
def cast_fireball():
    """Player targeted aoe spell"""
    (x, y) = target_tile(6)
    if x is None:
        message("Cancelled")
        return 'cancelled'
    message('The fireball explodes!', colours.orange)

    t.layer(EFFECT_LAYER)

    effected_points = [
        (x + dx, y + dy)
        for (dx, dy) in ((-1, -1), (-1, 1), (-1, 0), (0, -1), (0, 0), (0, +1),
                         (+1, 0), (+1, -1), (+1, +1))
    ]

    set_colour(colours.red)
    for (x, y) in effected_points[4:-4]:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(50)

    set_colour(colours.orange)
    for (x, y) in effected_points[2:-2]:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(50)

    set_colour(colours.red)
    for (x, y) in effected_points:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(50)

    set_colour(colours.orange)
    for (x, y) in effected_points:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(50)

    set_colour(colours.red)
    for (x, y) in effected_points:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(100)

    t.clear_area(effected_points[0][0], effected_points[0][1],
                 effected_points[-1][0], effected_points[-1][1])
    set_colour(colours.orange)
    for (x, y) in effected_points[2:-2]:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(100)

    t.clear_area(effected_points[0][0], effected_points[0][1],
                 effected_points[-1][0], effected_points[-1][1])
    set_colour(colours.dark_red)
    for (x, y) in effected_points[4:-4]:
        t.put(x, y, chr(177))
    t.refresh()
    t.delay(150)

    t.clear_area(effected_points[0][0], effected_points[0][1],
                 effected_points[-1][0], effected_points[-1][1])
    t.refresh()

    for obj in objects:
        if (obj.x, obj.y) in effected_points and obj.fighter:
            message('The ' + obj.name + ' takes 15 damage.', colours.orange)
            obj.fighter.take_damage(15)
Esempio n. 12
0
def play_game(game_map, player, game_state, camera, message_log, map_type):

    log_frame = FrameWithScrollbar(message_log, 'dark orange')

    previous_game_state = game_state

    render_all(game_map, player, camera, game_state, log_frame)
    print('1')
    fps = 25
    blt.refresh()
    while True:

        blt.clear()

        key = blt.read()
        action = handle_keys(game_state, key)
        move = action.get('move')
        fullscreen = action.get('fullscreen')
        pickup = action.get('pickup')
        show_inventory = action.get('show_inventory')
        drop_inventory = action.get('drop_inventory')
        inventory_index = action.get('inventory_index')
        left_click = action.get('left_click')
        right_click = action.get('right_click')
        exit = action.get('exit')

        start_loop = time()
        player_turn_results = list()

        if move and game_state == GameStates.PLAYERS_TURN:
            player_turn_result = movement(move, game_map, player)
            player_turn_results.extend(player_turn_result)
            player.fov.calc_fov(game_map)
            game_state = GameStates.ENEMY_TURN
            if map_type == 'chunks':
                add_new_chunks(game_map, player)

        if pickup and game_state == GameStates.PLAYERS_TURN:
            if (player.x, player.y) in game_map.items:
                item = game_map.items[(player.x, player.y)]

                pickup_results = player.inventory.add_item(item)
                player_turn_results.extend(pickup_results)

            else:
                message_log.append('There is nothing here to pick up')

        if show_inventory:
            previous_game_state = game_state
            game_state = GameStates.SHOW_INVENTORY

        if drop_inventory:
            previous_game_state = game_state
            game_state = GameStates.DROP_INVENTORY

        if (inventory_index is not None
                and previous_game_state != GameStates.PLAYER_DEAD
                and inventory_index < len(player.inventory.items)):
            item = player.inventory.items[inventory_index]

            if game_state == GameStates.SHOW_INVENTORY:
                player_turn_results.extend(
                    player.inventory.use(
                        item,
                        entities=game_map.entities,
                    ))
                # fov=player.fov.fov_cells))
            elif game_state == GameStates.DROP_INVENTORY:
                player_turn_results.extend(player.inventory.drop_item(item))
        if game_state == GameStates.TARGETING:
            if left_click:
                target_x, target_y = camera.to_map_coordinates(
                    left_click[0], left_click[1])

                item_use_results = player.inventory.use(
                    player.inventory.targeting_item,
                    entities=game_map.entities,
                    target_x=target_x,
                    target_y=target_y)
                player_turn_results.extend(item_use_results)

            elif right_click:
                player_turn_results.append({'targeting_cancelled': True})

        if player_turn_results:
            state = show_result(player_turn_results, game_state, game_map,
                                message_log, player)
            if state:
                game_state = state

        if fullscreen:
            blt.set("window: fullscreen=true;")
        if exit:
            if game_state in (GameStates.SHOW_INVENTORY,
                              GameStates.DROP_INVENTORY):
                game_state = previous_game_state
            elif game_state == GameStates.TARGETING:
                player_turn_results.append({'targeting_cancelled': True})
            else:
                save_game(player, game_map, message_log, game_state, camera,
                          map_type)

                return False

        if game_state == GameStates.ENEMY_TURN:
            completed_entities = []
            entities = [
                game_map.entities.get((x, y))
                for (x,
                     y) in set(player.fov.fov_cells) & set(game_map.entities)
            ]
            for entity in entities:
                if entity is not None and entity.ai:
                    if entity not in completed_entities:
                        enemy_turn_results = entity.ai.take_turn(
                            player, game_map)
                        completed_entities.append(entity)
                        state = show_result(enemy_turn_results, game_state,
                                            game_map, message_log, entity)
                        if state:
                            game_state = state
                        if game_state == GameStates.PLAYER_DEAD:
                            break
                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN
        print('2')
        render_all(game_map,
                   player,
                   camera,
                   game_state,
                   log_frame,
                   action,
                   debug=True)
        end_loop = time()
        blt.delay(1000 // fps)
        print(end_loop - start_loop)