Page 1 of 1

Joyeux Noël 2021 sur TI-Planet !

Unread postPosted: 25 Dec 2021, 00:02
by critor
15017
A tous nos membres, partenaires, contacts, amis, et même lecteurs anonymes, et au nom de toute l'équipe, nous vous souhaitons un joyeux Noël 2021 avec plein de calculatrices sous le sapin ! :ange:

Que la magie de Noël soit au rendez-vous et vous fasse oublier, au moins pour un temps, le contexte sanitaire. :favorite:




Code: Select all
from math import pi, sin, cos, exp, sqrt
from time import sleep, ticks_ms
from random import *
from ti_innovator import send
from ti_system import *
import ti_draw

screen_w, screen_h = ti_draw.get_screen_dim()
font_h = 12

#function to send the micro:bit Python code to run
def send_microbit(cmd):
  send("\x04")
  send(cmd)
  send("\x05")

def transform(x, y):
  f = screen_h * 45 // 64
  return (x*f,screen_h-1-y*f)

def fill_circle(x, y, r, c):
  ti_draw.set_color(tuple(c))
  ti_draw.fill_circle(x, y, r)

def draw_circle(x, y, r, c):
  ti_draw.set_color(tuple(c))
  ti_draw.draw_circle(x, y, r)

def fill_rect(x, y, w, h, c):
  ti_draw.set_color(tuple(c))
  ti_draw.fill_rect(x, y, w, h)

def draw_line(x1, y1, x2, y2, c):
  ti_draw.set_color(tuple(c))
  ti_draw.draw_line(x1, y1, x2, y2)

def set_pixel(x, y, c):
  ti_draw.set_color(tuple(c))
  ti_draw.fill_rect(x, y, 1, 1)

def draw_ellipse(x, y, rx, ry, c):
  for h in range(-int(ry), int(ry)+1):
    w = sqrt(max(0, rx*rx*(1-h*h/ry/ry)))
    x1, x2 = int(x - w), int(x + w)
    yc = int(y + h)
    set_pixel(x1, yc, c)
    set_pixel(x2, yc, c)
  for w in range(-int(rx), int(rx)+1):
    h = sqrt(max(0, ry*ry*(1-w*w/rx/rx)))
    y1, y2 = int(y - h), int(y + h)
    xc = int(x + w)
    set_pixel(xc, y1, c)
    set_pixel(xc, y2, c)

def fill_ellipse(x, y, rx, ry, c):
  for h in range(-int(ry), int(ry)+1):
    w = sqrt(max(0, rx*rx*(1-h*h/ry/ry)))
    x1, x2 = int(x - w), int(x + w)
    yc = int(y + h)
    draw_line(x1, yc, x2, yc, c)

def draw_circle(x, y, r, c):
  draw_ellipse(x, y, r, r, c)

def fill_circle(x, y, r, c):
  fill_ellipse(x, y, r, r, c)

def horiz_gradient(x, y, w, h, col1, col2):
  pass
  for k in range(h):
    fill_rect(x, y + k, w, 1, [col1[i] + (col2[i] - col1[i])*k//(h-1) for i in range(3)])

def cmath_exp(a):
  return exp(a.real) * (cos(a.imag) + 1j*sin(a.imag))

def similitude(u, v):
    v = 1j * (u - v)
    return lambda z: v*z + u

def generer_arbre(n, x0, dy, c, zoom):
  lf = (
    similitude(.2j, .2j + .5*cmath_exp(1j * pi / 7)),
    similitude(.22j, .22j + .45j*cmath_exp(1j * pi / 3)),
    similitude(.55j, .55j + .35*cmath_exp(1j * pi / 6)),
    similitude(.57j, .57j + .3j*cmath_exp(1j * pi / 3)),
    similitude(.7j, 1.2j - .01)
  )
  lz1 = []
  for _ in range(n+1):
    if len(lz1):
      lz2 = []
      for f in lf:
        lz2.extend([f(z) for z in lz1])
    else:
      lz2 = [0j, 0.7j]

    for k in range(0, len(lz2), 2):
      x1, y1 = transform(lz2[k].real*zoom, lz2[k].imag*zoom)
      x2, y2 = transform(lz2[k+1].real*zoom, lz2[k+1].imag*zoom)
      draw_line(x1 + x0, y1 - dy, x2 + x0, y2 - dy, c)
    lz1 = lz2
  lz2 = [v*zoom for v in lz2]
  return lz2

def rotate_color(c):
  return (c[1], c[2], c[0])

def rangee_arbres(dy, n, d, c, zoom):
  dx = screen_w / n // 2
  for k in range(n):
    x0 = int(screen_w * k / n)
    lz = generer_arbre(d, x0 + dx, dy, c, zoom)
  return lz

def trace(nb_rows, nb_stars, nb_flakes):

  color_black = (0,) * 3
  color = (255,) * 3

  # fait tomber la nuit
  colors = (color_black, (0, 0, 127), (0, 127, 255))
  dy = screen_h / (len(colors))
  for k in range(len(colors) - 1):
    horiz_gradient(0, round(dy*k), screen_w, round(dy), colors[k], colors[k + 1])
  horiz_gradient(0, screen_h  - round(dy), screen_w, round(dy), (0, 63, 127), color_black)

  # allume les etoiles
  for k in range(nb_stars):
    set_pixel(randint(0, screen_w - 1), randint(0, screen_h - 1 - round(dy)), color)

  # plante une foret de sapins
  for k in range(nb_rows, -1, -1):
    lz = rangee_arbres(dy*k//nb_rows, 2**k, nb_rows-k, (0, 200*(nb_rows-k)//nb_rows, 0), .8*(1-k/nb_rows))

  # saupoudre de neige
  for k in range(nb_flakes):
    fill_circle(randint(0, screen_w - 1), randint(0, screen_h - 1), 1, color)

  return lz

nb_rows = 5
nb_stars = 100
nb_flakes = 40
nb_balls = 30

ti_draw.use_buffer()

lz = trace(nb_rows, nb_stars, nb_flakes)

x0 = screen_w // 2
lz, r, color_in, color_out = lz[1::max(1, len(lz)//nb_balls)], 2, (0, 255, 255), (0, 0, 255)

def animate(line_de, line_en, line_fr):
  global lz, r, color_in, color_out
  for z in lz:
    x, y = transform(z.real, z.imag)
    x += x0
    fill_circle(x, y, r, color_in)
    draw_circle(x, y, r, color_out)
    color_in, color_out = rotate_color(color_in), rotate_color(color_out)
  ti_draw.set_color(255, 255, 255)
  ti_draw.fill_rect(0, 0, screen_w, 3*font_h)
  ti_draw.set_color(255, 0, 0)
  ti_draw.draw_text(0,3*font_h,line_en)
  ti_draw.set_color(0, 127, 0)
  ti_draw.draw_text(0,2*font_h,line_de)
  ti_draw.set_color(0, 0, 255)
  ti_draw.draw_text(0,font_h,line_fr)
  ti_draw.paint_buffer()

nop = lambda: None

def play_melody_on_microbit(mus, durat_bytes, animate, l_animate, lyrics_de, lyrics_en, lyrics_fr):
  t2, durat, i, deltat_anim = ticks_ms(), 0, 0, 0
  t1 = t2
  lyrics_de.append("")
  lyrics_en.append("")
  lyrics_fr.append("")
  auto_silence = .1
  r = 2 ** (1 / 12)
  i_note = 0
  i_lyrics = 1
  animate(lyrics_de[0], lyrics_en[0], lyrics_fr[0])
  while i < len(mus):
    t1, t2 = t2, ticks_ms()
    deltat = max(0, (t2 - t1) / 1000 - deltat_anim - durat)
    note = mus[i]
    i += note < 0x80
    durat = mus[i] & ((note ^ 0x80) | 0x7F)
    i += 1
    if durat_bytes > 1:
      durat |= int.from_bytes(mus[i:i + durat_bytes - 1],'little') << (8 - (note >= 0x80))
      i += durat_bytes - 1
    durat = max(1, durat) / 1000
    durat_note = max(durat - auto_silence, 0)
    send_microbit("music.pitch("+str(round((note < 0x80) and 440 * r**(note - 57)))+", "+str(int(durat_note*1000))+", wait=False)")
    if i_note in l_animate:
      t = ticks_ms()
      animate(lyrics_de[i_lyrics], lyrics_en[i_lyrics], lyrics_fr[i_lyrics])
      i_lyrics += 1
      deltat_anim = (ticks_ms() - t)/1000
    else:
      deltat_anim = 0
    i_note += 1
    sleep(max(0, durat - deltat - deltat_anim))

# your melody data
melody = (
b"\xee\x02<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x025w\x01Hw\x01Hw\x01Ew\x01Je\x04Hw\x01H3\x02F\xbc\x00Fe\x04Fw\x01Fw\x01Cw\x01He\x04Fw\x01F3\x02E\xbc\x00E\xee\x02<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x02"
#b"<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x025w\x01Hw\x01Hw\x01Ew\x01Je\x04Hw\x01H3\x02F\xbc\x00Fe\x04Fw\x01Fw\x01Cw\x01He\x04Fw\x01F3\x02E\xbc\x00E\xee\x02<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x02<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x025w\x01Hw\x01Hw\x01Ew\x01Je\x04Hw\x01H3\x02F\xbc\x00Fe\x04Fw\x01Fw\x01Cw\x01He\x04Fw\x01F3\x02E\xbc\x00E\xee\x02<w\x01A3\x02A\xbc\x00Ae\x04Cw\x01E3\x02E\xbc\x00Ee\x04Ew\x01Cw\x01Ew\x01F\xee\x02@\xee\x02C\xee\x02A\xee\x02"
)

lyrics_de = (
  [
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Wie grün sind deine Blätter.",
    "Du grünst nicht nur",
    "zur Sommerzeit,",
    "Nein auch im Win-",
    "-ter wenn es schneit,",
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Wie grün sind deine Blätter !",
  ],
  [
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Du kannst mir sehr gefallen.",
    "Wie oft hat schon",
    "zur Weihnachtszeit,",
    "Ein Baum von dir",
    "mich hoch erfreut,",
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Du kannst mir sehr gefallen !",
  ],
  [
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Dein Kleid will mich was lehren.",
    "Die Hoffnung und",
    "Beständigkeit,",
    "Gibt Mut und Kraft",
    "zu jeder Zeit,",
    "O Tannenbaum,",
    "o Tannenbaum,",
    "Dein Kleid will mich was lehren !",
  ],
)

lyrics_en = (
  [
    "O Christmas Tree,",
    "o Christmas Tree,",
    "How steadfast are your branches.",
    "Your boughs are green",
    "in summer's clime,",
    "And through the snows",
    "of wintertime,",
    "O Christmas Tree,",
    "o Christmas Tree,",
    "How steadfast are your branches !",
  ],
  [
    "O Christmas Tree,",
    "o Christmas Tree,",
    "What happiness befalls me.",
    "When oft at joy-",
    "-ous Christmas-time,",
    "Your form inspires",
    "my song and rhyme,",
    "O Christmas Tree,",
    "o Christmas Tree,",
    "What happiness befalls me !",
  ],
  [
    "O Christmas Tree,",
    "o Christmas Tree,",
    "Your boughs can teach a lesson.",
    "That constant faith",
    "and hope sublime,",
    "Lend strength and com-",
    "-fort through all time,",
    "O Christmas Tree,",
    "o Christmas Tree,",
    "Your boughs can teach a lesson !",
  ],
)

lyrics_fr = (
  [
    "Mon beau sapin,",
    "roi des forêts,",
    "Que j'aime ta verdure.",
    "Quand par l'hiver",
    "bois et guérêts,",
    "Sont dépouillés",
    "de leurs attraits,",
    "Mon beau sapin,",
    "roi des forêts,",
    "Tu gardes ta parure !",
  ],
  [
    "Toi que Noël,",
    "planta chez nous,",
    "Au saint anniversaire.",
    "Joli sapin",
    "comme ils sont doux,",
    "Et tes bonbons",
    "et tes joujoux,",
    "Toi que Noël,",
    "planta chez nous,",
    "Par les mains de ma mère !",
  ],
  [
    "Mon beau sapin,",
    "tes verts sommets,",
    "Et leur fidèle ombrage.",
    "De la foi qui",
    "ne ment jamais,",
    "De la constance",
    "et de la paix,",
    "Mon beau sapin,",
    "tes verts sommets,",
    "M'offrent la douce image !",
  ],
)

l_pauses = [4,8,16,20,24,28,32,36,40,47]

send_microbit("import music")
send_microbit("display.show(Image.XMAS)")

for k in range(3):
  play_melody_on_microbit(melody, 2, animate, l_pauses, lyrics_de[k], lyrics_en[k], lyrics_fr[k])


Téléchargement : archives_voir.php?id=2833119

Re: Joyeux Noël 2021 sur TI-Planet !

Unread postPosted: 26 Dec 2021, 09:12
by SlyVTT
Coucou les Amis,

J’espère que vous avez passé un bon réveillon et une belle journée de Noël.
Merci encore une fois à Critor pour nous publier un joli programme Python de circonstance.

A bientôt et soyez sages, que le Père Fouettard ne vienne pas reprendre vos cadeaux :sob:

Ciao

Sly