serpent.py

Created by arthurmichel-scolaire

Created on January 12, 2026

2.46 KB


from kandinsky import *
from math import sin

def d(x,y,r,c):
  # Optimisation pour ne pas dessiner hors ecran
  if x+r<0 or x-r>320 or y+r<0 or y-r>222: return
  for i in range(-r,r+1):
    for j in range(-r,r+1):
      if i*i+j*j<=r*r:
        set_pixel(int(x+i),int(y+j),c)

# --- COULEURS NOCTURNES ---
Nuit     = (20, 20, 50)       # Fond bleu tres sombre
Lune     = (240, 240, 255)    # Blanc pale
Cratere  = (200, 200, 220)    # Gris lune
RocherS  = (60, 60, 70)       # Rocher sombre
RocherC  = (90, 90, 100)      # Reflet rocher
VertFluo = (50, 255, 50)      # Serpent qui ressort bien
VertD    = (0, 100, 0)        # Dos du serpent
Or       = (255, 215, 0)      # Yeux/Lucioles

# 1. FOND DE NUIT
fill_rect(0,0,320,222,Nuit)

# 2. ETOILES (Petits disques blancs aleatoires via maths)
for i in range(40):
  # Pseudo-aleatoire basique
  sx = (i * 97) % 320
  sy = (i * 43) % 150
  d(sx, sy, 1, (255,255,255))

# 3. LA LUNE (Remplace le soleil)
d(270, 40, 26, Lune)
# Craters (Disques gris sur la lune)
d(260, 35, 6, Cratere)
d(280, 50, 4, Cratere)
d(275, 25, 3, Cratere)

# 4. SOL ROCHEUX (Serie de grands disques)
# On fait varier la hauteur avec un sinus pour un sol inegal
for k in range(-20, 340, 20):
  hy = 210 + 10 * sin(k/40)
  d(k, hy, 25, RocherS)     # Ombre
  d(k+5, hy-5, 15, RocherC) # Lumiere lune

# 5. LE SERPENT (Ondulation)
# Equation: y = base + amplitude * sin(frequence)
for x in range(-10, 250, 4):
  # Le serpent suit la forme des rochers mais en flottant un peu
  y = 170 + 20 * sin(x / 25)
  
  # Gestion de la queue (rayon progressif)
  r = 13 if x > 30 else x/2.5
  if r < 1: r = 1
  
  # Corps bicolore (Dos sombre / Ventre fluo)
  d(x, y-2, r, VertD)
  d(x, y+2, r-2, VertFluo)
  
  # Taches sur le dos (tous les 20 pixels)
  if x % 20 < 5:
    d(x, y-4, 3, (150, 255, 150))

# 6. TETE DU SERPENT
hx = 250
hy = 170 + 20 * sin(250 / 25)

d(hx, hy, 14, VertD)        # Cou
d(hx+12, hy-2, 15, VertFluo)# Crane
d(hx+12, hy-5, 12, VertD)   # Haut du crane

# Oeil (Grand, jaune, pupille fente)
d(hx+14, hy-4, 5, Or)
d(hx+14, hy-4, 1, (0,0,0))       # Pupille centre
d(hx+14, hy-6, 1, (0,0,0))       # Pupille haut
d(hx+14, hy-2, 1, (0,0,0))       # Pupille bas

# Langue
lx = hx+28; ly = hy+2
d(lx, ly, 2, (255,0,0))
d(lx+3, ly-2, 1, (255,0,0))

# 7. LUCIOLES (Ambiance magique)
positions = [(50,120), (100,160), (300,190), (180, 100)]
for px, py in positions:
  d(px, py, 2, Or)          # Coeur
  d(px, py, 6, (40,40,80))  # Halo (bleu nuit clair pour transparence simulee)

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.