histoire_gen.py

Created by mathieu-croslacoste

Created on May 15, 2025

9.91 KB

Génère une histoire aléatoirement.


from turtle import *
from random import *
from kandinsky import *
from time import *

event=0
story_lenght=0
animation=""
auto_dice=""

speed(10)
colormode(255)
color('purple')
hideturtle()

def roll(b):
  fill_rect(0,0,330,230,color(255,255,255))
  if story_lenght>48:
    a=6
  else:
    a=b
  if animation:
    spiral()
  draw_dice(a)
  story(a)

def draw_dice(a):
  fill_rect(150,80,30,30,color(220,220,220))
  if a==1:
    dice_dots=[
    [0,0,0],
    [0,1,0],
    [0,0,0]
    ]
  elif a==2:
    dice_dots=[
    [1,0,0],
    [0,0,0],
    [0,0,1]
    ]
  elif a==3:
    dice_dots=[
    [1,0,0],
    [0,1,0],
    [0,0,1]
    ]
  elif a==4:
    dice_dots=[
    [1,0,1],
    [0,0,0],
    [1,0,1]
    ]
  elif a==5:
    dice_dots=[[1,0,1],[0,1,0],[1,0,1]]
  else:
    dice_dots=[[1,0,1],[1,0,1],[1,0,1]]
  for line in range(3):
    for column in range(3):
      if dice_dots[line][column]==1:
        sleep(0.2)
        fill_rect(152+10*column,82+10*line,6,6,color(15,15,15))

def story(a):
  global story_lenght,event
  text1=""
  text2=""
  text3=""
  text4=""
  if story_lenght>0:
    if event==1:
      if a==1 or a==2:
        event=1.1
        text1="Vous voyez un chat coincé dans"
        text2="un arbre. Vous montez le sauver."
      elif a==6:
        event=3
        text1="Vous trébuchez. Vous êtes"
        text2="hospitalisé."
        text3="L"
      else:
        event=1.2
        text1="Un chien vous poursuit."
    elif event==2:
      if a==1 or a==2:
        event=2.1
        text1="Vous prenez le téléphone de"
        text2="votre mère et prenez un taxi."
      elif a==6:
        event=3
        text1="Un seisme déchire la terre sous"
        text2="votre maison. Vous criez. Pas de"
        text3="chance !"
        text4="L"
      else:
        event=2.2
        text1="Vous prenez votre vélo."
    elif event==1.1:
      if a==1 or a==2:
        event=1.11
        text1="La proprietaire du chat vous"
        text2="invite chez elle. Vous ne pouvez"
        text3="pas refuser."
      elif a==6:
        event=3
        text1="Le chat descend quand vous êtes"
        text2="en haut et vous restez bloqué."
        text3="L"
      else:
        event=1.211
        text1="Vous continuez votre chemin."
    elif event==1.2:
      if a==1 or a==2:
        event=1.21
        text1="au bout d'un moment, le chien"
        text2="part."
      elif a==6:
        event=3
        text1="Le chien (enragé) vous mord. Vous"
        text2="êtes hospitalisé."
      else:
        event=1.22
        text1="Vous vous arrêtez et faites face"
        text2="au chien."
    elif event==2.1:
      if a==1 or a==2:
        event=3
        text1="Vous arrivez, mais en retard."
        text2="L"
      elif a==6:
        event=3
        text1="Au moment de payer, vous vous"
        text2="rendez compte qu'il n'y a pas"
        text3="d'argent sur le téléphone."
        text4="L"
      else:
        event=1.211
        text1="Vous payez, sortez et courrez."
    elif event==2.2:
      if a==1 or a==2:
        event=2.21
        text1="Une magpie vous attaque !"
      elif a==6:
        event=3
        text1="Vous crevez."
        text2="L"
      else:
        event=2.22
        text1="Vous renversez une grand-mère."
    elif event==1.11:
      if a==1 or a==2:
        event=2.1
        text1="Elle vous prête son velo."
      elif a==6:
        event=3
        text1="Vous ne voyez pas le temps"
        text2="passer."
        text3="L"
      else:
        event=1.2
        text1="Quand vous sortez, un chien vous"
        text2="poursuit."
    elif event==1.21:
      if a==1 or a==2:
        event=1.211
        text1="Vous continuez votre chemin."
      elif a==6:
        event=3
        text1="Vous continuez a courir, mais"
        text2="vous tombez et vous vous cassez"
        text3="le cou. Vous agonisez longtemps."
        text4="L"
      else:
        event=1.212
        text1="Vous faites une pause."
    elif event==1.22:
      if a==1 or a==2:
        event=1.21
        text1="Le chien part, vaincu."
      elif a==6:
        text1="Le chien vous déchire la chair."
        text2="L"
      else:
        event=1.211
        text1="Vous retournez à la réalité:vous"
        text2="ne pouvez pas le battre."
        text3="Vous fuyez."
    elif event==1.211:
      if a==1 or a==2:
        event=1.1
        text1="Vous voyez un chat coincé dans"
        text2="un arbre. Vous montez le sauver."
      elif a==6:
        text1="Vous arrivez à l'école juste quand la"
        text2="sonnerie retentit. Vous criez de joie !"
        text3="W"
      else:
        event=2.22
        text1="Vous renversez une grand-mère."
    elif event==2.21:
      event=3
      if a==1 or a==2 or a==3:
        text1="Vous accélérez pour fuir et"
        text2="arrivez à l'école."
        text2="W"
      else:
        text1="La magpie vous crève les yeux"
        text2="L"
    elif event==1.211:
      if a==1 or a==2:
        event=1.1
        text1="Vous voyez un chat coincé dans"
        text2="un arbre. Vous montez le sauver."
      elif a==6:
        text1="Vous arrivez à l'école juste quand la"
        text2="sonnerie retentit. Vous criez de joie !"
        text3="W"
      else:
        event=2.22
        text1="Vous renversez une grand-mère."
    elif event==2.22:
      if a==1 or a==2:
        event=2.221
        text1="Vous continuez en accélérant"
      elif a==6:
        text1="Vous l'avez tué. Les policiers"
        text2="vous retrouvent au même endroit,"
        text3="encore sous le choc."
        text4="L"
      else:
        event=2.222
        text1="Vous l'aidez à se relever tout"
        text2="en vous excusant."
    elif event==2.221:
      event=3
      if a==1 or a==2 or a==3:
        text1="Les policiers vous arrêtent"
        text2="L"
      else:
        text1="Vous foncez droit dans un"
        text2="poteau. C'est douloureux !"
        text3="L"
    elif event==2.222:
      if a==1 or a==2:
        event=2.2221
        text1="Elle se relève tranquillement"
        text2="et, soudain, sort son rouleau"
        text3="à patisserie et vous attaque."
      elif a==6:
        text1="Elle se relève, furibonde, et,"
        text2="avant que vous ayez le temps de"
        text3="de réagir, appelle la police."
        text4="L"
      else:
        event=1.11
        text1="Elle vous invite chez elle."
        text2="Vous n'êtes pas en état de"
        text3="refuser."
    elif event==2.2221:
      if a==1 or a==2:
        event=3
        text1="Un policier vous arrête et vous"
        text2="allez tous au poste."
        text3="L"
      elif a==6:
        text1="La grand-mère vous enfonce le rouleau dans"
        text2="la gorge. Quand les secours arrivent,"
        text3="vous êtes au portes de la mort."
        text4="L"
      else:
        event=2.22212
        text1="Vous prenez la poudre d'escampette."
        text2="La grand-mère vous poursuit."
    elif event==2.22212:#éèêà
      if a==1 or a==2:
        event=1.211
        text1="La grand-mère abandonne,"
        text2="et vous continuez votre chemin."
      elif a==6:
        text1="La grand-mère vous rattrape. Vous"
        text2="passez un mauvais quart d'heure."
      else:
        event=3
        text1="Un policier vous arrête et vous"
        text2="allez tous au poste."
        text3="L"
    elif event==1.212:
      event=3
      if a==1 or a==2:
        text1="Vous repartez, mais vous arrivez"
        text2="en retard."
        text3="L"
      else:
        text1="Le chien revient avec sa bande."
        text2="Ils n'ont aucune pitié."
        text3="Vous mourrez."
        text4="L"
  else:
    if a==1 or a==2:
      event=1
      text1="Vous vous dépéchez et partez"
      text2="en courant."
    elif a==6:
      event=3
      text1="Votre mère vous amène."
      text2="W"
    else:
      event=2
      text1="Vous prenez votre temps et"
      text2="déjeunez."
  if a==6:event=3
  story_lenght+=1
  print(text1)
  if text2!="":
    if text2=="W":
      text2="Vous avez gagné !"
    elif text2=="L":
      text2="Vous avez perdu."
    print(text2)
    if text3!="":
      if text3=="W":
        text3="Vous avez gagné !"
      elif text3=="L":
        text3="Vous avez perdu."
      print(text3)
      if text4!="":
        if text4=="W":
          text4="Vous avez gagné !"
        elif text4=="L":
          text4="Vous avez perdu."
        print(text4)
  draw_string(text1,0,140,color(50,35,100),color(255,255,255))
  draw_string(text2,0,160,color(50,35,100),color(255,255,255))
  draw_string(text3,0,180,color(50,35,100),color(255,255,255))
  draw_string(text4,0,200,color(50,35,100),color(255,255,255))
  size=len(text1+text2+text3+text4)
  sleep((size)/16)

def spiral():
  fill_rect(125,65,80,80,color(255,255,255))
  penup()
  goto(0,106)
  setheading(0)
  pendown()
  for i in range(1754):
    forward(1.55-i/1440)
    right(1)
    pensize(8-i//300)

def manualInput():
  z=0
  while not z in [1,2,3,4,5,6]:z=int(input())
  roll(z)

while True:
  fill_rect(0,0,330,230,color(0,0,0))
  draw_string("générateur à histoires",50,100,color(160,55,15),color(0,0,0))
  sleep(2)
  print("Animations ? \n('o'(oui) ou 'n'(non))")
  while not(animation=="o" or animation=="n"):animation=input()
  if animation=="o":
    animation=True
  else:
    animation=False
  print("auto-dés\n('o'(oui) ou 'n'(non))")
  while not(auto_dice=="o" or auto_dice=="n"):auto_dice=input()
  if auto_dice=="o":
    auto_dice=True
  else:
    auto_dice=False
  fill_rect(0,0,330,230,color(255,255,255))
  print("Vous devez aller au lycée, mais vous vous êtes réveillé tard.")
  draw_string("Vous devez aller au lycée, mais",0,60,color(50,35,100),color(255,255,255))
  draw_string("vous vous êtes réveillé tard.",20,80,color(50,35,100),color(255,255,255))
  sleep(3)
  if auto_dice:
    roll(randint(1,6))
  else:
    print("écrivez votre résultat")
    manualInput()
  while event!=3:
    if auto_dice:
      roll(randint(1,6))
    else:
      manualInput()
  event=0
  story_lenght=0
  print("\n")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.