cube222.py

Created by schraf

Created on November 06, 2022

3.18 KB

Quelques explications sur le programme

Au démarrage la calculatrice mélange le cube puis voici les touches à utiliser :

  • 4 flèches du haut : faire tourner le cube entier
  • 1 et 4 : Face de Gauche (L = Left) 4 pour aller dans le sens 1 vers 4 et 1 pour sens inverse
  • 8 et 9 : Face du Haut (U = Up) 9 pour aller dans le sens 8 vers 9 (horaire)
  • 6 et 5 : Face Avant (F)
  • x et + : Face de Droite (R = Right)
  • . et x10^x : Face du Bas (B)
  • et / : Face arrière


from kandinsky import *
from time import sleep
from random import choice
from ion import *

MVT = ((8,9,11,10),(4,0,17,13),(5,1,16,12)),((4,5,7,6),(3,10,13,23),(1,11,15,22)), \
      ((17,16,18,19),(9,0,20,14),(8,2,21,12)),((0,1,3,2),(8,4,22,18),(10,6,20,16)), \
      ((20,22,23,21),(2,6,15,19),(3,7,14,18)),((13,12,14,15),(7,11,17,21),(5,9,19,23))

TOUCHES = (32,31,39,45,42,36,38,37,50,49,40,46,0,1,2,3)
AIDE = ("1",20,20),("4",55,12),("8   9",140,10),("-",250,95),("/",280,105),\
      (".",105,185),("^",135,205),("6",105,132),("5",135,143),("x",185,140),("+",215,130)
COUL="RBWOVJ"
RVB={"W":(235,235,235), "R":(180,30,40), "B":(44,100,200), "V":(40,175,40),"O":(240,100,0),"J":(240,210,0)}
F, H, R = ((1,0,0),(0,0,-1),(-1,0,0)),((0,-1,0),(0,0,-1),(0,1,0)),((1,0,0),(0,-1,0),(-1,0,0))

FACES = ((0,0,0),H),((0,0,-1.1),H),((0,-1.1,0),H),((0,-1.1,-1.1),H), \
        ((0,0,-2.15),R),((1.05,0,-2.15),R),((0,-1.1,-2.15),R),((1.05,-1.1,-2.15),R),\
        ((0,0.1,0),F),((1.1,0.1,0),F),((0,0.1,-1.1),F),((1.1,0.1,-1.1),F), \
        ((2.4,1,-2),H),((2.4,1,-3.1),H),((2.4,-.1,-2),H),((2.4,-.1,-3.1),H), \
        ((-2.5,1,0),R),((-1.45,1,0),R),((-2.5,-.1,0),R),((-1.45,-.1,0),R), \
        ((0,-4,0),F),((1.1,-4,0),F),((0,-4,-1.1),F),((1.1,-4,-1.1),F)

def key(t):
  while True:
    for (i, k) in enumerate(t):
      if keydown(k): return i

def inv(l): return [tuple(reversed(t)) for t in reversed(l)]

def permu(mvt, pos):
  suiv = list(pos)
  for t in mvt:
    u = (t[-1],)+t
    for i,v in enumerate(t): suiv[v] = pos[u[i]]
  return suiv
  
def fin(pos):
 c, nb = pos[0], 1
 for v in pos:
  if v != c: 
    c = v
    nb += 1
 return nb != 6

def remplir(coul, *u):
 (ax,ay),(bx,by),(cx,cy),(dx,dy)=u[0]
 for col in range(min(ax,bx,cx,dx), max(ax,bx,cx,dx)-1,2):
  for lig in range(min(ay,by,cy,dy), max(ay,by,cy,dy)-1,2):
    xa,xb,xc,xd = ax-col,bx-col,cx-col,dx-col 
    ya,yb,yc,yd = ay-lig,by-lig,cy-lig,dy-lig
    d1,d2,d3,d4 = xa*yb-ya*xb,xb*yc-yb*xc,xc*yd-yc*xd,xd*ya-yd*xa
    u,v,w,t = d1>=0,d2>=0,d3>=0,d4>=0
    if u == v == w == t: fill_rect(col,222 - lig,2,2,coul)

def face(n,coul):
 (x,y,z),dd = FACES[n]
 coord = (pos2D(x,y,z),)
 for d in dd:
  x,y,z = x+d[0],y+d[1],z+d[2]
  coord += (pos2D(x, y, z),)
 remplir(coul, coord)

def pos2D(x, y, z):
 t = x+z+1*3*y
 w = 1200/(110+x+z)
 return (int(95+(x-z)*3*w), int(170+t*w))

def init():
 cube = ""
 for c in COUL: cube += c * 4
 for (t,x,y) in AIDE:
  draw_string(t,x,y,(255,)*3,(0,)*3)
 return list(cube)

def aff(suiv,pos,force=0):
 for n,c in enumerate(suiv):
  if pos[n] != c or force: face(n,RVB[c])

def melange():
 pos = init()
 for _ in range(20):
  pos = permu(choice(MVT), pos)
 aff(pos,pos,1)
 return pos

def choix(k, pos):
 if k < 12:
  m = MVT[k//2]
  if k % 2: m = inv(m)
  return permu(m, pos)
 else:
  for v in ((0,4,4,4),(1,2,2,2),(1,1,1,2),(0,0,0,4))[k-12]:
    pos = permu(MVT[v], pos)
  return pos

def arret():
  while True:
    draw_string("BRAVO !",20,150,RVB['W'],RVB[choice(list(RVB)[1:])])
    sleep(.2)
    if keydown(4): return

while True:
  fill_rect(0,0,320,222,(0,)*3)
  pos, jouer = melange(), True
  while jouer:
    suiv = choix(key(TOUCHES), pos)
    aff(suiv,pos)
    pos = list(suiv)  
    sleep(.2)
    jouer = fin(pos)
  arret()

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.