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