jeucubes3d.py

Created by schraf

Created on January 10, 2023

2.32 KB

Petit jeu inspiré d’un Tweet de NUMWORKS


from kandinsky import fill_rect, set_pixel
from math import *
from random import randint
from time import sleep

COUL = ((157,183,216),(200,218,254),(175,203,249))

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)):
  for lig in range(min(ay,by,cy,dy), max(ay,by,cy,dy)):
   if 0 <= col <= 320 and 0 <= lig <= 222:
    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:
     co = (255,)*3 if d1*d2*d3*d4 == 0 else coul
     set_pixel(col,222 - lig,co)

def light(z,g): return ((max(0,min(255,g-1.1*z)),)*3)

def cube(x, y, z):
 tx,ty,tz = 50-x,y,80-z
 x2,y2,z2 = x,y,z-1
 face(x,y,z,((1,0,0),(0,0,-1),(-1,0,0)),COUL[1])
 face(x,y,z,((0,-1,0),(0,0,-1),(0,1,0)),COUL[0])
 face(x2,y2,z2,((1,0,0),(0,-1,0),(-1,0,0)),COUL[2])

def face(x,y,z,dd,coul):
 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+3*y
 w = 1200/(110+x+z)
 return (int(130+(x-z)*3*w), int(40+t*w))

def rotate_matrix(matrix):
    n = len(matrix)
    for layer in range(n // 2):
        first = layer
        last = n - 1 - layer
        for i in range(first, last):
            offset = i - first
            top = matrix[first][i]
            matrix[first][i] = matrix[last - offset][first]
            matrix[last - offset][first] = matrix[last][last - offset]
            matrix[last][last - offset] = matrix[i][last]
            matrix[i][last] = top
    return matrix
 
hauteurs = [[randint(0,4) for _ in range(4)] for _ in range(4)]
total = sum([sum(v) for v in hauteurs])
tour = 0

while tour < 5:
  fill_rect(0,0,320,222,(255,)*3)
  for p in range(4, 0, -1):
   for h in range(4):
    for z in range(4, 0, -1):
     if h < hauteurs[p-1][z - 1]: cube(p, h, z)
  sleep(2)
  hauteurs = rotate_matrix(hauteurs)
  tour += 1
res = int(input("Combien de cubes ? "))
if res == total:
  print("Incroyable !")
elif abs(res-total) < .2 * total:
  print("Pas mal ! il y en avait {}".format(total))
elif abs(res-total) < .5 * total:
  print("Bof ! il y en avait {}".format(total))
else:
  print("Il va falloir s'entrainer !")  
  print("Il y en avait {}".format(total))

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.