Пример #1
0
    def test_create(self):
        b = Buffer()
        s = Screen()
        s.activate_buffer(b)

        self.assertEqual(s.buffer.y, 0)
        self.assertIs(s.buffer.current_line, s.buffer[0])
        self.assertEqual(s.buffer.current_line.text, '')
Пример #2
0
def main():
    cleanup_at_exit()
    pins = (11, 13, 15, 16)
    menu = Menu()
    screen = Screen(menu)
    interface = Button_Interface(pins) if is_pi() else Keys_Interface()
    lcd = LCD() if is_pi() else Mock_LCD()
    io = IO_Mgr(interface, screen, menu, lcd)
    io.listen()
Пример #3
0
def main(stdscr):
    stdscr.keypad(False)
    stdscr.clear()
    b = Buffer()
    b.filename = 'testfile'
    try:
        with open(b.filename, 'r') as f:
            for line in f.read().splitlines():
                b.append_line(line)
    except FileNotFoundError:
        pass
    height, width = stdscr.getmaxyx()
    width -= 1
    s = Screen(height=height, width=width)
    s.activate_buffer(b)
    s.redraw_lines(stdscr, range(len(s.buffer)))
    while True:
        try:
            modified = s.handle_code_point(get_code_point(stdscr))
            s.redraw_lines(stdscr, modified)
        except KeyboardInterrupt:
            s.buffer.save()
            raise
Пример #4
0
def start(player_turn=1):
    state = State()
    screen = Screen()
    agent = Agent()
    agent_turn = 3 - player_turn
    while not state.winner():
        screen.render(state)
        if state.turn == agent_turn:
            choice = agent.choice(state, agent_turn)
            try:
                state.move(choice)
            except ValueError:
                cprint('Invalid position, AI is broken!', RED)
                input()
                continue
        elif state.turn == player_turn:
            inp = input()
            if inp == '':
                continue
            inp = int(inp)
            if inp < 1 or inp > 9:
                cprint('Invalid position', RED)
                input()
                continue
            pos = map_move[inp]
            try:
                state.move(pos)
            except ValueError:
                cprint('Invalid position', RED)
                input()
                continue

    screen.render(state)
    winner = state.winner()

    if winner == -1:
        cprint(f'Tie!\n', YELLOW)
    else:
        cprint(f'Player {winner} wins!\n', GREEN)
Пример #5
0
import pygame
from julia import Julia
from display import Screen
from threading import Thread

screen_width = 500
screen_height = 500
screen_running = True
show_axis = True

p_i, q_i = -0.904, 0.224

display = Screen(screen_width, screen_height)
julia = Julia(screen_width, screen_height, [
    display.bg_color, display.RED, display.WHITE, display.YELLOW,
    display.GREEN, display.BLUE
], p_i, q_i)
display.julia = julia
display.running = screen_running
MAIN_THREAD = Thread(target=display.run)
MAIN_THREAD.setDaemon(True)
MAIN_THREAD.start()


def update():
    display.draw_background(show_axis)
    julia.calculate()
    display.show_layer(julia.image)


def p(p):
Пример #6
0
    def test_movement(self):
        b = Buffer()
        s = Screen()
        s.activate_buffer(b)

        s.mode = Mode.EDIT

        for cp in '\n'.join(('0123', '01', '0123')):
            s.handle_code_point(cp)
        s.handle_code_point('\x04')  # C-d
        self.assertEqual(s.buffer.y, 2)
        self.assertEqual(s.buffer.x, 4)

        ###

        s.handle_code_point('j')
        self.assertEqual(s.buffer.y, 2)
        self.assertEqual(s.buffer.x, 3)
        self.assertNoMovement(s, 'k')
        self.assertMovement(s, 'j', 'l', ';', 'J', ':')

        s.handle_code_point('J')
        self.assertEqual(s.buffer.y, 2)
        self.assertEqual(s.buffer.x, 0)
        self.assertNoMovement(s, 'j', 'J', 'k')
        self.assertMovement(s, 'l', ';', ':')

        s.handle_code_point(';')
        self.assertEqual(s.buffer.y, 2)
        self.assertEqual(s.buffer.x, 1)
        self.assertNoMovement(s, 'k')
        self.assertMovement(s, 'j', 'l', ';', 'J', ':')

        s.handle_code_point(':')
        self.assertEqual(s.buffer.y, 2)
        self.assertEqual(s.buffer.x, 4)
        self.assertNoMovement(s, ';', ':', 'k')
        self.assertMovement(s, 'j', 'l', 'J')

        s.handle_code_point('l')
        self.assertEqual(s.buffer.y, 1)
        self.assertEqual(s.buffer.x, 2)
        self.assertNoMovement(s, ';', ':')
        self.assertMovement(s, 'j', 'k', 'l', 'J')

        s.handle_code_point('l')
        self.assertEqual(s.buffer.y, 0)
        self.assertEqual(s.buffer.x, 2)
        self.assertNoMovement(s, 'l')
        self.assertMovement(s, 'j', 'k', ';', 'J', ':')
Пример #7
0
    def test_edit_mode(self):
        b = Buffer()
        s = Screen()
        s.activate_buffer(b)

        s.mode = Mode.EDIT

        ###

        for cp in 'abc':
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 1)
        self.assertEqual(s.buffer.y, 0)
        self.assertEqual(s.buffer.x, 3)
        self.assertEqual(s.buffer.current_line.text, 'abc')

        for cp in 'def\nghi':
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 2)
        self.assertEqual(s.buffer.y, 1)
        self.assertEqual(s.buffer.x, 3)
        self.assertEqual(s.buffer[0].text, 'abcdef')
        self.assertEqual(s.buffer.current_line.text, 'ghi')

        for cp in '\x7f' * 3:
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 2)
        self.assertEqual(s.buffer.y, 1)
        self.assertEqual(s.buffer.x, 0)
        self.assertEqual(s.buffer[0].text, 'abcdef')
        self.assertEqual(s.buffer.current_line.text, '')

        for cp in '\x7f' * 2:
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 1)
        self.assertEqual(s.buffer.y, 0)
        self.assertEqual(s.buffer.x, 5)
        self.assertEqual(s.buffer.current_line.text, 'abcde')

        for cp in '\x7f' * 5:
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 1)
        self.assertEqual(s.buffer.y, 0)
        self.assertEqual(s.buffer.x, 0)
        self.assertEqual(s.buffer.current_line.text, '')

        for cp in '\x7f' * 10:
            s.handle_code_point(cp)
        self.assertEqual(len(s.buffer), 1)
        self.assertEqual(s.buffer.y, 0)
        self.assertEqual(s.buffer.x, 0)
        self.assertEqual(s.buffer.current_line.text, '')
Пример #8
0
import pygame
from mandelbrot import Mandelbrot
from display import Screen
from threading import Thread

if __name__ == '__main__':

    size = (screen_width, screen_height) = (500, 500)
    rect = (-2,-2,2,2)
    colors = [Screen.BLACK,Screen.RED,Screen.GREEN,Screen.BLUE,Screen.YELLOW,Screen.WHITE]
    screen_running = True
    show_axis=True

    display = Screen(screen_width,screen_height)
    display.running = screen_running
    mandel = Mandelbrot(size, rect, colors)

    MAIN_THREAD = Thread(target=display.run)
    MAIN_THREAD.setDaemon(True)
    MAIN_THREAD.start()

    display.draw_background(show_axis)
    mandel.render_image()
    display.show_layer(mandel.image)
Пример #9
0
from julia import Julia
from display import Screen
from threading import Thread
import pygame

if __name__ == '__main__':

    screen_size = (screen_width, screen_height) = (1000, 500)
    fractal_size = (-2, -2, 2, 2)
    color_scheme = [
        Screen.BLACK, Screen.RED, Screen.GREEN, Screen.BLUE, Screen.YELLOW,
        Screen.WHITE
    ]
    screen_running = True

    display = Screen(screen_width, screen_height)
    display.running = screen_running
    mandel = Mandelbrot((screen_width / 2, screen_height), fractal_size,
                        color_scheme)
    julia = Julia((screen_width / 2, screen_height), fractal_size, 0, 0,
                  color_scheme)
    display.sets.append(mandel)
    display.sets.append(julia)

    MAIN_THREAD = Thread(target=display.run)
    MAIN_THREAD.setDaemon(True)
    MAIN_THREAD.start()

    display.draw_set(mandel, 0, 0)
    display.draw_set(julia, screen_width / 2, 0)