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