city.py

Created by schraf

Created on October 24, 2022

1.41 KB

Création d'une ville

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

# Fond gris foncé
fill_rect(0,0,320,222,(40,40,40))

# remplissage parallélogramme (begin_fill n'existe pas sur la NUMWORKS)
def remplir(coul, *u):
 # On récupère les coordonnées des 4 points
 (ax,ay),(bx,by),(cx,cy),(dx,dy)=u[0]
 # On parcourt les pixels du rectangle contenant le parallélogramme
 for col in range(min(ax,bx,cx,dx), max(ax,bx,cx,dx)+1):
  for lig in range(min(ay,by,cy,dy), max(ay,by,cy,dy)+1):
   # Si c'est un pixel de l'écran
   if 0 <= col <= 320 and 0 <= lig <= 222:
   # On teste si le point est dans le parallélogramme
    xa,xb,xc,xd = ax-col,bx-col,cx-col,dx-col 
    ya,yb,yc,yd = ay-lig,by-lig,cy-lig,dy-lig
    # xa*yb-ya*xb permet de déterminer de quel côté on est de la droite AB
    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
   # Si tous identiques on est dans le parallélogramme
    if u == v == w == t:
    # On met la bordure en noir
     co = (0,0,0) if d1*d2*d3*d4 == 0 else coul
     set_pixel(col,222 - lig,co)

# Plus z (profondeur) est grand, plus la couleur sera sombre
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
 # Description immeubles et rues
 # Testez avec d'autres fonctions
 if ty <= int(5+5*sin(tz)*cos(tx/2)) and (x % 10 > 2) and (z % 10 > 3):
  # dessus - devant - cote
  face(x,y,z,((1,0,0),(0,0,-1),(-1,0,0)),light(z,280))
  face(x,y,z,((0,-1,0),(0,0,-1),(0,1,0)),light(z,60))
  face(x2,y2,z2,((1,0,0),(0,-1,0),(-1,0,0)),light(z,140))

# Une face
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)

# Coord 3D vers 2D
def pos2D(x, y, z):
 t = x+z+2*y
 w = 450/(110+x+z)
 return (int(140+(x-z)*3*w), int(-70+t*w))

# Les etoiles
for c in range(320):
 for _ in range(5):
  set_pixel(c


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

fill_rect(0,0,320,222,(40,)*3)

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):
  for lig in range(min(ay,by,cy,dy), max(ay,by,cy,dy)+1):
   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 = (0,0,0) 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
 if ty <= int(5+5*sin(tz)*cos(tx/2)) and (x % 10 > 2) and (z % 10 > 3):
  face(x,y,z,((1,0,0),(0,0,-1),(-1,0,0)),light(z,280))
  face(x,y,z,((0,-1,0),(0,0,-1),(0,1,0)),light(z,60))
  face(x2,y2,z2,((1,0,0),(0,-1,0),(-1,0,0)),light(z,140))

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+2*y
 w = 450/(110+x+z)
 return (int(140+(x-z)*3*w), int(-70+t*w))

for c in range(320):
 for _ in range(5):
  set_pixel(c,randint(0,90),(randint(50,180),)*3)

for p in range(50, 0, -1):
 for h in range(10):
  for z in range(80, 0, -1):
   cube(p, h, z)

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.