coeur.py

Created by schraf

Created on February 01, 2022

2.58 KB

Saurez-vous atteindre le 💕 de votre élu.e avant le 14 février ?? Découvrez mon programme hautement scientifique (et complètement inutile 😂) qui vous donnera le nombre de jours 📅 nécessaires pour y arriver.

ᴹᵃ ᶜʰᵃⁱⁿᵉ ʸᵒᵘᵗᵘᵇᵉ‧ᶜᵒᵐ/ˢᶜʰʳᵃᶠ


from math import *

from kandinsky import draw_string,fill_rect
from random import randint, random
from time import sleep, monotonic

# couleurs et dessin du coeur
BLA, ROS, ROU, NOI = (255,255,255),(210,130,180),(210,30,60),(0,0,0)
coul = [BLA, ROS, ROU, NOI]
cases = [[0,0,1,1,1,1,0,0,0,1,1,1,1,0,0],[0,1,1,1,1,1,1,0,1,1,1,1,1,1,0],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,3,3,1,3,3,1,1,1,1,1],[1,1,1,1,3,2,2,3,2,0,3,1,1,1,1],[0,1,1,1,3,2,2,2,2,2,3,1,1,1,0],[0,1,1,1,1,3,2,2,2,3,1,1,1,1,0],[0,0,1,1,1,1,3,2,3,1,1,1,1,0,0],[0,0,0,1,1,1,1,3,1,1,1,1,0,0,0],[0,0,0,0,1,1,1,1,1,1,1,0,0,0,0],[0,0,0,0,0,1,1,1,1,1,0,0,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]]

# dimensions et position du coeur
largeur, hauteur = len(cases[0]), len(cases)
gauche, haut = 8, 4

class balle(): 
 def __init__(self):
  # position initiale 
  self.x = randint(1,20)
  self.y = randint(15,20)
  # direction deplacement
  self.vx = 1 if random() < .5 else -1
  self.vy = 1
 def aff(self,x1,y1,x2,y2):
    # si deplacement, on efface case precedente et affiche suivante
    fill_rect(10 * x1, 10 * y1, 10, 10, BLA)  
    fill_rect(10 * x2, 10 * y2, 10, 10, NOI)
    self.x = x2
    self.y = y2 
 def bouge(self):
  # position suivante avec effet aleatoire
  xx = max(0, min(31,self.x + self.vx + randint(-1,1)))
  yy = max(0, min(21,self.y + self.vy))
  # bords ? Si oui rebond
  if xx == 0 or xx == 31: self.vx *= -1
  if yy == 0 or yy == 21: self.vy *= -1
  # On est dans le cadre du dessin ?
  try:
   # La case n'est pas vide ?
   if cases[yy - haut][xx - gauche] > 0:
    # rebond
    self.vx *= -1
    self.vy *= -1
    # si c'est du rose, on efface
    if cases[yy - haut][xx - gauche] == 1: cases[yy - haut][xx - gauche] = 0
   else:
    # la case est vide, on deplace la balle 
    self.aff(self.x, self.y, xx, yy)
  # on est a l'exterieur du cadre donc deplacement
  except:
   self.aff(self.x, self.y, xx, yy)   

def plateau(cases):
 for l, t in enumerate(cases):
  for c, v in enumerate(t):
   # la couleur depend de la valeur de la case 0 = BLA, 1 = ROS...
   fill_rect(10 * gauche + 10 * c, 10 * haut + 10 * l, 10, 10, coul[v])

plateau(cases)
# creation de 5 balles
balles = []
for k in range(5): balles.append(balle())
# depart chrono
depart = monotonic()
while True:
 # on bouge toutes les balles
 for b in balles: b.bouge()
 sleep(.03)
 # on compre le nb de 0
 s = sum([r.count(0) for r in cases])
 # fin du jeu
 if s == 169: 
   fill_rect(0,0,320,222,BLA)
   plateau(cases)
   break
# calcul du nb de jours et affichage
jours = int((monotonic() - depart) / 3)
draw_string(str(jours)+" jours",120,180,ROS)

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>.