simon.py

Created by schraf

Created on October 24, 2022

1.1 KB

Petit jeu type “simon” : Touche 0 pour lancer un tour et touches de 1 à 9 pour répéter le motif.

En cas d’erreur la partie est terminée et votre score s’affiche.

Ma chaine Youtube consacrée aux maths, à la programmation en Python, aux calculatrices, Excel, etc.

Description du programme

from random import randint
from ion import *
from kandinsky import fill_rect,draw_string
from time import sleep

VE, RO, BL, NR = (0,255,0), (255,0,0), 3 * (255,), (0,0,0)     # couleurs

def eff_ecran():        # efface écran et dessin de la grille
 fill_rect(0,0,320,222,BL)
 for i in range(4):
  fill_rect(130 + 60 * i, 20, 1, 180, NR)
  fill_rect(130, 20 + 60 * i, 180, 1, NR)

def wait(*dg):       # Attente d'une touche numérique
 while True:
  for k in dg:       # on parcourt les codes attendus
   if keydown(k):    # si touche enfoncée
    while keydown(k): continue   # on attend qu'elle soit relâchée
    return (1 + (k + 3) % 9) * (k < 48)   # retour de la valeur correspondante

def aff(txt, attend = 0):   # affichage d'un texte
 fill_rect(0,0,129,222,BL)
 draw_string(txt, 60 - 5 * len(txt), 100)
 if attend: wait(48)        # Attente appui touche "0"

def rect(v,coul = NR):      # rectangle dans la grille
 x, y = 135 + 60 * ((v - 1) % 3), 25 + 60 * (2 - (v - 1) // 3)
 fill_rect(x, y, 50, 50, coul)  # en couleur
 sleep(.3)
 fill_rect(x, y, 50, 50, BL)    # on l'efface
 sleep(.3)
 
def go():
 while True:    
  simon = []     # contenu du simon
  tour = 1
  eff_ecran()
  while tour > 0:
   aff('Tour '+str(tour), 1)
   simon.append(randint(1, 9))  #  ajout d'une valeur à simon
   for v in simon: rect(v)      # animation
   aff('A toi !')
   correct = True      # Pour le moment le joueur a bon
   i = 0
   while correct:
    t = wait(42,43,44,36,37,38,30,31,32) # on attend un chiffre entre 1 et 9
    if t == simon[i]:  # si correct
     rect(t, VE)       # rectangle vert
  


from random import randint
from ion import *
from kandinsky import fill_rect as rec,draw_string
from time import sleep

VE,RO,BL,NR=(0,255,0),(255,0,0),3*(255,),(0,0,0)

def eff_ecran():
 rec(0,0,320,222,BL)
 for i in range(4):
  rec(130+60*i,20,1,180,NR)
  rec(130,20+60*i,180,1,NR)

def wait(*dg): 
 while True:
  for k in dg:
   if keydown(k):
    while keydown(k): continue
    return (1+(k+3)%9)*(k<48) 

def aff(txt, attend = 0):
 rec(0,0,129,222,BL)
 draw_string(txt,60-5*len(txt),100)
 if attend: wait(48)

def rect(v,coul = NR):
 x,y=135+60*((v-1)%3),25+60*(2-(v-1)//3)
 for c in [coul, BL]:
  rec(x,y,50,50,c)
  sleep(.3)
 
def go():
 while True:
  simon,tour=[],1
  eff_ecran()
  while tour:
   aff('Tour '+str(tour),1)
   simon.append(randint(1,9))
   for v in simon:rect(v)
   aff('A toi !')
   correct,i=True,0
   while correct:
    t=wait(42,43,44,36,37,38,30,31,32)
    if t==simon[i]:
     rect(t, VE)
     i+=1
     if i==len(simon):break
    else:
     rect(t,RO)
     correct=False
     break
   if correct:tour+=1   
   else:
    aff('Perdu !',1)
    aff('Score = '+str(tour),1)
    tour=0

go()

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.