Exemplo n.º 1
0
Arquivo: wnd.py Projeto: hihihippp/kaa
    def do_input(self, nonblocking):
        """Get an input from curses"""

        self._panel.top()

        if nonblocking:
            self._cwnd.timeout(0)
        else:
            self._cwnd.timeout(-1)
#            self._cwnd.timeout(self.WAITINPUTFOR)

        no_trailing_char = False  # true if esc key pressed
        try:
            curses.panel.update_panels()
            curses.doupdate()

            c = self._cwnd.get_wch()
            if c == '\x1b':
                self._cwnd.timeout(0)
                try:
                    c2 = self._cwnd.get_wch()
                    # has trailing character.
                    curses.unget_wch(c2)
                except curses.error:
                    # no trailing character.
                    no_trailing_char = True

        except curses.error as e:
            if repr(e.args) != self._lasterror:
                if e.args[0] != 'no input':
                    log.debug('Error in get_wch()', exc_info=True)
                self._lasterror = repr(e.args)
            else:
                self._skipped += 1
                if self._skipped % 500 == 0:
                    log.debug(
                        'Too much input-error skips!: {} times'.format(self._skipped))
            return []

        self._lasterror = ''
        self._skipped = 0

        if c == curses.KEY_MOUSE:
            mouseid, x, y, z, bstate = curses.getmouse()
            keys = [keydef.MouseEvent(self, mouseid, x, y, z, bstate)]
        else:
            keys = [keydef.KeyEvent(self, k, no_trailing_char)
                    for k in keydef.convert_registered_key(c)]

#        for k in keys:
#            _trace('{!r}'.format(k))

        return keys
Exemplo n.º 2
0
 def unget(self):
     curses.unget_wch(self.key)
Exemplo n.º 3
0
 def unget(self):
     curses.unget_wch(self.key)
Exemplo n.º 4
0
def main():
    autoQueen0 = [0, 1, 2, 3, 4, 5, 6, 7]
    autoQueen1 = [56, 57, 58, 59, 60, 61, 62, 63]
    stdscr = curses.initscr()
    curses.start_color()
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED)
    curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_BLUE)
    curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_BLACK)
    curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_WHITE)
    curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_GREEN)
    curses.init_pair(6, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(7, curses.COLOR_BLUE, curses.COLOR_BLACK)
    curses.raw()
    curses.noecho()
    curses.cbreak()
    stdscr.nodelay(True)
    stdscr.keypad(True)
    curses.curs_set(0)
    maxyx = stdscr.getmaxyx()
    if (maxyx[0] < 60 or maxyx[1] < 80):
        screenTooSmall(stdscr)
        curses.endwin()
        exit(1)
    chessPiece = [0, 0, 0]
    chessBoard = genChessBoard()
    moves = genMoves(chessBoard, chessPiece)
    frame = 0
    checkX = 0
    checkX2 = 0
    checkO = 0
    checkO2 = 0
    ifMoves = 0
    seconds = 0
    blink = 0
    offset = 30
    whosTurnIsIt = 0
    chessCurs = 35
    drawTitle(stdscr)
    drawControls(stdscr)
    while (1):
        if (frame > 59):
            frame = 0
            seconds += 1
        else:
            frame += 1
        #draw frame
        drawBoard(stdscr, chessBoard, chessPiece, chessCurs, blink, ifMoves,
                  moves)
        drawUI(stdscr, whosTurnIsIt, chessPiece, checkX, checkO)
        #keyboard input
        try:
            c = stdscr.get_wch()
        except:
            c = 0
        #moving, quitting, and all that stuff
        if (c == 'q'):
            gameover(stdscr, whosTurnIsIt)
            break
        elif (c == 'm'):
            if (ifMoves == 0):
                ifMoves = 1
            else:
                ifMoves = 0
        elif (c == ' ' and chessBoard[chessCurs][1] == whosTurnIsIt + 1):
            chessPiece = [
                chessBoard[chessCurs][0], chessBoard[chessCurs][1], chessCurs
            ]
        elif (not chessPiece == [0, 0, 0] and (c == chr(10) or c == chr(13))
              and chessCurs in moves):
            if (whosTurnIsIt == 0):
                whosTurnIsIt = 1
            else:
                whosTurnIsIt = 0
            chessBoard[chessPiece[2]] = [0, 0]
            chessBoard[chessCurs] = [chessPiece[0], chessPiece[1]]
            chessPiece = [0, 0, 0]
            checkX2 = 0
            checkO2 = 0
            for num, i in enumerate(chessBoard):
                if (i[1] == 1):
                    tmpMov = genMoves(chessBoard, [i[0], i[1], num])
                    if (chessBoard.index([6, 2]) in tmpMov):
                        checkO2 = 1
                elif (i[1] == 2):
                    tmpMov = genMoves(chessBoard, [i[0], i[1], num])
                    if (chessBoard.index([6, 1]) in tmpMov):
                        checkX2 = 1
            if (checkX == 1 and checkX2 == 1):
                checkmate(stdscr, 1)
                break
            if (checkO == 1 and checkO2 == 1):
                checkmate(stdscr, 0)
                break
        elif (c == curses.KEY_UP):
            frame = 50
            if (chessCurs > 7):
                chessCurs -= 8
        elif (c == curses.KEY_DOWN):
            frame = 50
            if (chessCurs < 56):
                chessCurs += 8
        elif (c == curses.KEY_LEFT):
            frame = 50
            if (chessCurs > 0):
                chessCurs -= 1
        elif (c == curses.KEY_RIGHT):
            frame = 50
            if (chessCurs < 63):
                chessCurs += 1
        #game loop stuff
        if (frame < offset):
            blink = 1
        else:
            blink = 0
        checkX = 0
        checkO = 0
        for num, i in enumerate(chessBoard):
            if (i[1] == 1):
                tmpMov = genMoves(chessBoard, [i[0], i[1], num])
                if (chessBoard.index([6, 2]) in tmpMov):
                    checkO = 1
            elif (i[1] == 2):
                tmpMov = genMoves(chessBoard, [i[0], i[1], num])
                if (chessBoard.index([6, 1]) in tmpMov):
                    checkX = 1
        for num, i in enumerate(chessBoard):
            if (i[0] == 1 and i[1] == 1 and num in autoQueen0):
                i[0] = 5
            elif (i[0] == 1 and i[1] == 2 and num in autoQueen1):
                i[0] = 5
        if (whosTurnIsIt == 0):
            moves = genMoves(chessBoard, chessPiece)
        elif (whosTurnIsIt == 1):
            chessBoard, chessPiece, chessCurs, moves = genAIMove(
                chessBoard, chessPiece, chessCurs, checkO)
            curses.unget_wch(chr(10))
            frame = 50
        for i in moves:
            if (i == chessBoard.index([6, 1])):
                moves.pop(moves.index(i))
            elif (i == chessBoard.index([6, 2])):
                moves.pop(moves.index(i))
        #
        stdscr.refresh()
        time.sleep(0.016)
        stdscr.erase()
    curses.endwin()