Пример #1
0
def main():
    app = App(framerate=float("inf"))

    colors = [Color.rgb(r,g,b) for r in (0, 1) for g in (0, 1) for b in (0, 1)]
    pixels = [PixelData(char="▄", fg=col1, bg=col2) for col1 in colors for col2 in colors]

    t0 = perf_counter()
    n_frames = 0
    n_updates = 0
    update_time = 0

    @app.on("frame")
    def on_frame():
        nonlocal n_frames, n_updates, update_time
        for x in range(app.screen.w):
            for y in range(app.screen.h):
                app.screen[x,y].set(choice(pixels))
        app.screen.update()
        n_frames += 1
        n_updates += app.screen._update_count
        update_time += app.screen._update_duration

    app.run()

    updates_per_ms = n_updates / update_time / 1000
    updates_per_ms_real = n_updates / (perf_counter() - t0) / 1000
    print("Updates per ms (updating): {:.2f}".format(updates_per_ms))
    print("Updates per ms (real time): {:.2f}".format(updates_per_ms_real))
    print("Average update time ms: {:.2f}".format(update_time / n_frames * 1000))
    print("Average potential FPS: {:.2f}".format(n_frames / update_time))
Пример #2
0
def main():
    app = App(mouse=True, framerate=120)
    buf = Buffer(0, 0)

    dirty = True
    dragging = False
    drag_start = None
    mouse_pos = None

    def do_box(buffer):
        if drag_start is None or mouse_pos is None:
            return
        x, y, w, h = corners_to_box(*drag_start, *mouse_pos)
        # draw_box(buffer, x, y, w, h, chars=BOX_CHARS_DOUBLE, fg=Color.hsl(perf_counter(), 1.0, 0.5))
        draw_frame(buffer,
                   x,
                   y,
                   w,
                   h,
                   chars=FRAME_CHARS_DOUBLE,
                   fg=Color.hsl(perf_counter(), 1.0, 0.5))

    @app.on("start")
    @app.on("resize")
    def on_resize():
        buf.resize(app.screen.w, app.screen.h)
        for x in range(buf.w):
            for y in range(buf.h):
                col = Color.rgb(0, 0, 0) if (x + y) % 2 else Color.rgb(
                    0.1, 0.1, 0.1)
                buf.put_char(" ", x, y, bg=col)

    @app.on("mouse")
    def on_mouse(mouse):
        nonlocal dirty, dragging, drag_start, mouse_pos
        if mouse.left:
            if mouse.down:
                dragging = True
                drag_start = (mouse.x, mouse.y)
            if mouse.up:
                do_box(buf)
                dragging = False
            dirty = True
            mouse_pos = (mouse.x, mouse.y)
        else:
            dragging = False

    @app.on("frame")
    def on_frame():
        app.screen.clear()
        app.screen.blit(buf)
        if dragging:
            do_box(app.screen)
        app.screen.update()

    app.run()
Пример #3
0
def main():
    app = App(mouse=True, framerate=60)
    app.mouse_x = 0
    app.mouse_y = 0
    app.mouse_px = 0
    app.mouse_py = 0
    app.particles = []

    @app.on("mouse")
    def on_mouse(m):
        app.mouse_x = m.x
        app.mouse_y = m.y * 2
    
    @app.on("frame")
    def on_frame():
        w = app.screen.w
        h = app.screen.h
        colormap = [Color.rgb(0,0,0) for x in range(w) for y in range(h * 2)]

        dx = app.mouse_x - app.mouse_px
        dy = app.mouse_y - app.mouse_py
        l = math.sqrt(dx ** 2 + dy ** 2)
        d = math.atan2(dy, dx)

        j = min(25, int(l)) * 6
        for i in range(j):
            f = i / j
            ll = l* random.uniform(0.25, 0.5)
            dd = d + random.uniform(-0.25, 0.25)
            vx = ll * math.cos(dd)
            vy = ll * math.sin(dd)
            app.particles.append(Particle(app.mouse_px + dx * f, app.mouse_py + dy * f, vx, vy))

        app.screen.clear()
        for i, p in enumerate(app.particles):
            col = Color.hsl(i/len(app.particles), 1.0, 0.5)
            steps = math.hypot(p.vx, p.vy)
            for step in range(int(steps + 1)):
                f = step / steps
                px = int(p.x - p.vx * f)
                py = int(p.y - p.vy * f)
                if px >= 0 and py >= 0 and px < w and py < h * 2:
                    colormap[py * w + px] += RED
            p.update()
            if p.x < 0 or p.y < 0 or p.x >= w or p.y >= h * 2:
                app.particles.remove(p)
        draw_colormap_2x(app.screen, colormap, 0, 0, w=w, h=h * 2)
        app.screen.update()

        app.mouse_px = app.mouse_x
        app.mouse_py = app.mouse_y

    app.run()
Пример #4
0
def main():
    app = App(mouse=True)
    x = 0
    y = 0
    left = False
    middle = False
    right = False
    scroll_total = 0

    @app.on("mouse")
    def mouse(mouse: Mouse):
        nonlocal x, y, left, middle, right, scroll_total

        if mouse.scrollup:
            scroll_total += 1
        elif mouse.scrolldown:
            scroll_total -= 1

        if mouse.down:
            if mouse.left:
                left = True
            if mouse.middle:
                middle = True
            if mouse.right:
                right = True
        elif mouse.up:
            if mouse.left:
                left = False
            if mouse.middle:
                middle = False
            if mouse.right:
                right = False

        x = mouse.x
        y = mouse.y

    @app.on("frame")
    def frame():
        app.screen.clear()
        app.screen.print(
            "".join("#" if b else "_" for b in [left, middle, right]), 1, 1)
        app.screen.print("LMR", 1, 2)
        app.screen.print(f"scroll: {scroll_total}", 7, 2)
        app.screen.show_cursor = True
        app.screen.cursor_pos = (x, y)
        app.screen.update()

    app.run()
Пример #5
0
def main():
    app = App()
    run_count = 0

    @app.on("start")
    def start():
        nonlocal run_count
        run_count += 1
        app.screen.print("Run {}".format(run_count), 0, 0)
        app.screen.print("Press any key to restart...", 0, 1)
        app.screen.update()

    @app.on("key")
    def key(k):
        app.stop()

    app.run()
    app.run()
    app.run()
Пример #6
0
def main():
    app = App()

    @app.on("frame")  # run this function every frame
    def on_frame():
        app.screen.clear()  # remove everything from the screen
        text = "Hello world, from termpixels!"

        for i, c in enumerate(text):
            f = i / len(text)
            color = Color.hsl(f + time(), 1,
                              0.5)  # create a color from a hue value
            x = app.screen.w // 2 - len(
                text) // 2  # horizontally center the text
            offset = sin(time() * 3 + f * 5) * 2  # some arbitrary math
            y = round(app.screen.h / 2 +
                      offset)  # vertical center with an offset
            app.screen.print(c, x + i, y,
                             fg=color)  # draw the text to the screen buffer

        app.screen.update()  # commit the changes to the screen

    app.run()  # block here until the app exits (press Escape!)
Пример #7
0
                        type=float,
                        nargs="?",
                        default=SPEED_MAX,
                        help="Maximum rain speed")
    parser.add_argument("--length-min",
                        "-l",
                        type=float,
                        nargs="?",
                        default=LENGTH_MIN,
                        help="Minimum raindrop length")
    parser.add_argument("--length-max",
                        "-L",
                        type=float,
                        nargs="?",
                        default=LENGTH_MAX,
                        help="Maximum raindrop length")
    parser.add_argument("--hue",
                        "-H",
                        type=float,
                        nargs="?",
                        default=HUE,
                        help="Color hue (0 to 360)")
    ns = parser.parse_args(sys.argv[1:])
    a._framerate = ns.fps
    SPEED_MIN = ns.speed_min
    SPEED_MAX = ns.speed_max
    LENGTH_MIN = ns.length_min
    LENGTH_MAX = ns.length_max
    HUE = ns.hue
    a.run()
Пример #8
0
def main():
    app = App(framerate=20)

    ui_height = 3
    game_buffer = Buffer(0,0)

    score = 0
    hiscore = load_hiscore()
    mode = None
    worm = None
    food_x = 0
    food_y = 0
    control_x = 0
    control_y = 0

    def move_food():
        nonlocal food_x, food_y
        w = game_buffer.w // 2
        h = game_buffer.h
        positions = [(x,y) for x in range(w) for y in range(h)
                     if not worm.intersecting(x, y)]
        food_x, food_y = choice(positions)

    @app.on("start")
    def start():
        nonlocal score, mode, worm
        score = 0
        worm = Worm(randint(0, app.screen.w - 1), randint(0, app.screen.h - 1))
        mode = MODE_GAME
    
    @app.on("start")
    @app.on("resize")
    def resize():
        game_buffer.resize(app.screen.w, app.screen.h - ui_height)
        move_food()

    @app.on("key")
    def key(k):
        nonlocal control_x, control_y
        if mode == MODE_GAME:
            if k == K_UP:
                control_y = -1
            if k == K_DOWN:
                control_y = 1
            if k == K_LEFT:
                control_x = -1
            if k == K_RIGHT:
                control_x = 1
        elif mode == MODE_GAMEOVER:
            if k == "\n":
                app.emit("start")

    def frame_game():
        nonlocal control_x, control_y
        nonlocal score, hiscore
        nonlocal mode
        
        # handle input
        if control_x != 0 and not worm.intersecting(worm.x + control_x, worm.y):
            worm.vx = control_x
            worm.vy = 0
            control_x = 0
        if control_y != 0 and not worm.intersecting(worm.x, worm.y + control_y):
            worm.vy = control_y
            worm.vx = 0
            control_y = 0

        if worm.intersecting(food_x, food_y):
            score += 1
            worm.length += 1
            hiscore = max(hiscore, score)
            save_hiscore(hiscore)
            move_food()
        
        game_buffer.clear()
        game_buffer.print("░░", food_x * 2, food_y)

        try:
            worm.update(game_buffer)
        except GameOver:
            mode = MODE_GAMEOVER
            multiply_buffer(game_buffer, 0.4)
            return
        
        # draw
        app.screen.clear()
        app.screen.print("~TermWorm~", 0, 0)
        app.screen.print("Score: {}".format(score), 0, 1, fg=Color.rgb(.7,.7,.7))
        app.screen.print("  Hi: {}".format(hiscore), fg=Color.rgb(.7,.7,0))
        draw_box(app.screen, 0, 2, app.screen.w, 1, chars=BOX_CHARS_DOUBLE)
        app.screen.blit(game_buffer, 0, ui_height)
        app.screen.update()
    
    def frame_gameover():
        dark = Color.rgb(0.2,0.2,0.2)
        app.screen.clear()
        app.screen.blit(game_buffer, 0, ui_height)
        print_hcenter(app.screen, "Game over!", y=3, fg=Color.rgb(1,1,0), bg=dark)
        print_hcenter(app.screen, "Press enter to restart", y=4, fg=Color.rgb(0.75,0.75,0.75), bg=dark)
        print_hcenter(app.screen, "Press escape to exit", y=5, fg=Color.rgb(0.75,0.75,0.75), bg=dark)
        app.screen.update()

    @app.on("frame")
    def frame():
        if mode == "game":
            frame_game()
        elif mode == "gameover":
            frame_gameover()
        else:
            raise Exception("Invalid mode: {}".format(mode))
    
    @app.on("after_stop")
    def after_stop():
        print("Thanks for playing TermWorm!")

    app.run()