2049.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:colorful2048 作者: liuyuxuan123 项目源码 文件源码
def draw(self,screen):
        help_string1 = '(W)Up (S)Down (A)Left (D)Right'
        help_string2 = '     (R)Restart (Q)Exit'
        gameover_string = '           GAME OVER'
        win_string = '          YOU WIN!'

        def cast(string):
            screen.addstr(string + "\n")

        def cast_pawn(string):
            curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
            curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE)
            curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_WHITE)
            curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_WHITE)
            word_list = string.split() 
            for element in word_list:
                if element in "0123456789":
                    if int(element) == 2:
                        screen.addstr(element,curses.color_pair(1))
                    elif int(element) == 4:
                        screen.addstr(element,curses.color_pair(2))
                    elif int(element) == 8:
                        screen.addstr(element,curses.color_pair(3))
                    else:
                        screen.addstr(element,curses.color_pair(4))
                else:
                    screen.addstr(element)
            screen.addstr("\n")


        def draw_hor_separator():
            line = "+" + ("+------" * self.width + "+")[1:]
            separator = defaultdict(lambda:line)
            if not hasattr(draw_hor_separator,"counter"):
                draw_hor_separator.counter = 0
            cast(separator[draw_hor_separator.counter])
            draw_hor_separator.counter += 1

        def draw_row(row):
            cast_pawn(''.join('|{: ^5} '.format(num) if num > 0 else '|      ' for num in row) + '|')

        screen.clear()
        cast("SCORE: " + str(self.score))
        if 0 != self.high_score:
            cast("HIGH SCORE " + str(self.high_score))
        for row in self.field:
            draw_hor_separator()
            draw_row(row)
        draw_hor_separator()
        if self.is_win():
            cast(win_string)
        else:
            if self.is_gameover():
                cast(gameover_string)
            else:
                cast(help_string1)
        cast(help_string2)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号