Blog de Robert Sebille


Vers mon curriculum vitae - Conception

Accueil > Technique > Informatique > Python > Cinéma ascii > Le bonhomme

Le bonhomme

vendredi 5 avril 2019, par Robert

La technique est différente du roflcopter ou du lancer du marteau. Ici, il y a un fichier par frame.

Screencast

Télécharger

Code source

bonhomme.py :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
#
#  bonhomme.py
#
#  Copyright 2019 Robert Sebille <robert@sebille.name>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#


import os
import time as t
from platform import system

from couleurs import Ccxy1, Ccxy2, Ccu, Ccf, Ccb, Ccd, Ccls, Ccll
from couleurs import dessine_carre
from couleurs import Csrs, Csbr, Csd, Csu, Csbl, Csrv, Csn

# efface l'écran suivant le système'
def cls():
   if system() == 'Windows':
       os.system('cls')
   else:
       os.system('clear')


# imprime un message dans un cadre
def message(texte, hg, bd, x, y):
   cls()
   dessine_carre(hg, bd, Csrv, " ")
   #print(Ccxy2(hg[0] + x, bd[1] + y) + texte)
   print(Ccxy2(hg[0] + x, hg[1] + y) + texte)


# Lit les données
frms = []
for fichier in os.listdir('frames'):
   with (open('frames/' + fichier)) as f:
       frms.append(f.read())
frames = []
for f in frms:
   frames.append(f.split("\n"))



# init
dep = 60
i = 0
y = 1
# Pub debian
cls()
with open('debian_pub_en.txt') as d:
   p = d.readlines()
for l in p:
   print(l, end='')
t.sleep(2)
cls()

# main loop
while True:
   
   try:
       message("En avant !", (2,2), (20, 10), 4, 3)
       t.sleep(1)
       while i <= dep:
           for f in frames:
               cls()
               for l in range(len(f)):
                   print(Ccxy2(i, y + l) + f[l])
               print("CTRL + C = Fin")
               t.sleep(0.2)
               i += 1
       message("En arrière !", (dep,2), (dep + 18, 10), 4, 3)
       t.sleep(1)
       while i > 0:
           for f in frames:
               cls()
               for l in range(len(f)):
                   print(Ccxy2(i, y + l) + f[l])
               print("CTRL + C = Fin")
               t.sleep(0.2)
               i -= 1
   except KeyboardInterrupt:
       print("\nReçu CTRL + C")
       input("Pressez entrée pour sortir.")
       exit("Bye !")

# local encoding
# import sys
# print(sys.getdefaultencoding())

# from platform import system
# if system() == 'Windows':
#    os.system("pause")

couleurs.py :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
#
#  sans titre.py
#
#  Copyright 2019 Robert Sebille <robert@sebille.name>
#
#  This program is free software; you can redistribute it and/orm"odify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
# m"ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# m"A 02110-1301, USA.
#
#

###############
# Version 1.0 #
###############

# ref: https://gitlab.com/dslackw/colored

# imports
# from couleurs import Csrs, Csbr, Csd, Csu, Csbl, Csrv, Csn
# from couleurs import Cfba, Cfbu, Cfr, Cfy, Cfc, Cfg, Cfm, Cfw, Cfrs
# from couleurs import Cbba, Cbbu, Cbr, Cby, Cbc, Cbg, Cbm, Cbw, Cbrs
# from couleurs import Ccxy1, Ccxy2, Ccu, Ccf, Ccb, Ccd, Ccls, Ccll
# from couleurs import dessine_carre

Csrs = "\033[0m"       # reset all (colors and brightness)
Csbr = "\033[1m"       # bright
Csd = "\033[2m"       # dim (looks same as normal brightness)
Csu = "\033[4m"       # UNDERLINED
Csbl = "\033[5m"      # BLINK,
Csrv = "\033[7m"      # REVERSE
Csn = "\033[22m"      # normal brightness

# FOREGROUND:
Cfba = "\033[30m"      # black
Cfr = "\033[31m"      # red
Cfg = "\033[32m"      # green
Cfy = "\033[33m"      # yellow
Cfbu = "\033[34m"      # blue
Cfm = "\033[35m"      # magenta
Cfc = "\033[36m"      # cyan
Cfw = "\033[37m"      # white
Cfrs = "\033[39m"      # reset

# BACKGROUND
Cbba = "\033[40m"      # black
Cbr = "\033[41m"      # red
Cbg = "\033[42m"      # green
Cby = "\033[43m"      # yellow
Cbbu = "\033[44m"      # blue
Cbm = "\033[45m"      # magenta
Cbc = "\033[46m"      # cyan
Cbw = "\033[47m"      # white
Cbrs = "\033[49m"      # reset


# cursor positioning
def Ccxy1(coords):                     # position cursor at x across, y down
   return "\033["+str(coords[1])+";"+str(coords[0])+"H"


def Ccxy2(x, y):     # position cursor at x across, y down
   return "\033["+str(y)+";"+str(x)+"f"


def Ccu(n):
   return "\033["+str(n)+"A"       # move cursor n lines up


def Ccd(n):
   return "\033["+str(n)+"B"       # move cursor n lines down


def Ccf(n):
   return "\033["+str(n)+"C"       # move cursor n characters forward


def Ccb(n):
   return "\033["+str(n)+"D"       # move cursor n characters backward


# clear the screen
Ccls = "\033[2J"   # clear the screen

# clear the line
Ccll = "\033[K"    # clear the line


def dessine_carre(hg, bd, coul=Csrv, car='·', bordure=True):
   """dessine_carre(hg, bd, coul=Csrv, car='·') dessine un rectangle,
   et retourne un dict avec les param du rectangle
   """

   dictparam = {
       'coinexthg': hg,
       'coinextbd': bd,
       'coinexthd': (bd[0], hg[1]),
       'coinextbg': (hg[0], bd[1]),
       'coininthg': (hg[0] + 1, hg[1] + 1),
       'coinintbd': (bd[0] - 1, bd[1] - 1),
       'coininthd': (bd[0] - 1, hg[1] + 1),
       'coinintbg': (hg[0] + 1, bd[1] - 1),
       'hauteur': bd[1] - hg[1] + 1,
       'longueur': bd[0] - hg[0] + 1,
       'limiteg': hg[0],
       'limited': bd[0],
       'limiteh': hg[1],
       'limiteb': bd[1]
       }

   if bordure:
       bhb = "-"
       bgd = '|'
       coin = '+'
   else:
       bhb = car
       bgd = car
       coin = car
       
   for j in range(0, dictparam['hauteur']):
       for i in range(0, dictparam['longueur']):
           #print(i)
           if j == 0 or j == (dictparam['hauteur'] - 1):
               print(coul + Ccxy2(hg[0], hg[1] + j) + coin + bhb*(dictparam['longueur']-2) + coin, sep='', end='')
           else:
               print(Ccxy2(hg[0] + i, hg[1] + j), sep='', end='')
               if i == 0 or i == (dictparam['longueur'] - 1):
                   print(coul + bgd + Csrs, sep='', end='')
               else:
                   print(car, sep='', end='')
       print()
           #if j == 0 or j == (dictparam['hauteur'] - 1):
           #    print(coul, sep='', end='')
   print(Csrs, end='')
   return dictparam


if __name__ == '__main__':
   pass

4 fichiers frames : frames/bon00, frames/bon10, frames/bon20, frames/bon30

frames/bon00:
    __
  _/&_\_   
  (;; o\   
   \  <   
    | /    
   / \   
    ||    
   /@|    
   (++   
    ||
    ||    
    -=-   

frames/bon10:
    __
  _/&_\)   
  (;; o\   
   \  <   
    | /    
   / \   
    ||\    
   /@| @    
   (++   
    |\
    |\\    
    -=-='

frames/bon20:
    __
  _/&_\)   
  (:: o\   
   \  =   
    | /    
   / \   
  //||\    
 @ / | @    
   (++   
   / \\
  //  \\    
 `=-   -='

frames/bon30:
    __
  _/&_\_   
  (:: o\   
   \  =   
    | /    
   / \   
  //||    
 @ / |    
   (++   
   /||
  // |    
 `=- -=-

debian_pub_en.txt

        _,met$$$$$gg.
     ,g$$$$$$$$$$$$$$$P.                       |   |  __|  _ \
   ,g$$P""       """Y$$.".                     |   |\__ \  __/
  ,$$P'              `$$$.                    \__,_|____/\___|
',$$P       ,ggs.     `$$b:
`d$$'     ,$P"'   .    $$$          _,           _,      ,'`.
 $$P      d$'     ,    $$P        `$$'         `$$'     `.  ,'       HEX
 $$:      $$.   -    ,d$$'         $$           $$        `'  64 65 62 69 61 6E
 $$;      Y$b._   _,d$P'           $$           $$         _,           _
 Y$$.    `.`"Y$$$$P"'        ,d$$$g$$  ,d$$$b.  $$,d$$$b.`$$' g$$$$$b.`$$,d$$b.
 `$$b      "-.__            ,$P'  `$$ ,$P' `Y$. $$$'  `$$ $$  "'   `$$ $$$' `$$
  `Y$$b                     $$'    $$ $$'   `$$ $$'    $$ $$  ,ggggg$$ $$'   $$
   `Y$$.                    $$     $$ $$ggggg$$ $$     $$ $$ ,$P"   $$ $$    $$
     `$$b.                  $$    ,$$ $$.       $$    ,$P $$ $$'   ,$$ $$    $$
       `Y$$b.               `$g. ,$$$ `$$._ _., $$ _,g$P' $$ `$b. ,$$$ $$    $$
         `"Y$b._             `Y$$P'$$. `Y$$$$P',$$$$P"'  ,$$. `Y$$P'$$.$$.  ,$$.
             `""""

Un message, un commentaire ?

modération a priori

Ce forum est modéré a priori : votre contribution n’apparaîtra qu’après avoir été validée par un administrateur du site.

Qui êtes-vous ?

Votre message

Pour créer des paragraphes, laissez simplement des lignes vides.