pixelart.py

Created by schraf

Created on December 17, 2020

1.4 KB

Explications en vidéo ici

Dans la vidéo j’utilise monotonic pour temporiser le jeu, dans la version ci-dessous j’ai simplifié en ne mettant que sleep.

Pixel Art

  • Restez appuyé sur une des touches 7, 8 ou 9 en vous déplaçant pour obtenir une frise colorée
  • Appuyez sur 2 flèches de direction pour vous déplacer en diagonal
  • Changez la valeur de d = 8 par une autre valeur pour obtenir un quadrillage plus gros ou plus petit. Essayez par exemple avec d = 2 !
  • N’oubliez pas de prendre en photo vos oeuvres avant de sortir du programme ;-)


from kandinsky import fill_rect, get_pixel
from ion import *
from time import sleep

BL = (248,255,248)
NR = (0,)*3
GR = (220,)*3
d = 8

def grille():
  for i in range(320//d):
    fill_rect(d*i,0,1,d*(220//d),GR)
    if d*i <= 220:fill_rect(0,d*i,320,1,GR)

def dec(c,n):
  v = list(c)
  if n == 4: v = [max(0, e - 51) for e in v]
  elif n == 6: v = [min(255, e + 51) for e in v]
  else: v[n] = v[n] + 51 % 306
  return tuple(v)

def touche(x,y,e,coul):
 c = coul if e else get_pixel(x+1,y+1)
 etat = 0
 while True:
  sleep(.08)
  dx,dy = keydown(KEY_RIGHT)-keydown(KEY_LEFT), keydown(KEY_DOWN)-keydown(KEY_UP)
  if keydown(KEY_ZERO): e = 0 
  if keydown(KEY_ONE): e,c = 1,(255,0,0)
  if keydown(KEY_TWO): e,c = 1,(0,255,0)
  if keydown(KEY_THREE): e,c = 1,(0,0,255)
  if keydown(KEY_FOUR): e,c = 1,dec(c,4)
  if keydown(KEY_SIX): e,c = 1,dec(c,6)
  if keydown(KEY_SEVEN): e,c = 1,dec(c,0)
  if keydown(KEY_EIGHT): e,c = 1,dec(c,1)
  if keydown(KEY_NINE): e,c = 1,dec(c,2)
  if keydown(KEY_FIVE): e,c = 1,BL
  if dx != 0 or dy != 0: 
   rect(x,y,c)
   return [dx, dy, e, c]
  etat = 1 - etat
  if e:rect(x,y,c)
  else:rect(x,y,c if etat else GR)
    
def rect(x,y,c):
  fill_rect(x+1,y+1,d-1,d-1,c)

def pixelart():
  grille()
  x,y,t = d*(160//d),d*(100//d),0
  ecrire, coul = 0, NR
  while True:
    t = touche(x,y,ecrire,coul)
    x += d * t[0]
    y += d * t[1]
    ecrire = t[2]
    coul = t[3]
    
pixelart()

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.