forme_rebondissante.py

Created by laurent-cortot

Created on February 03, 2020

2.75 KB

Indication d’une vitesse et orientation via vecteur, facteur de ralentissement puis lancement animation avec rebond sur les bords


from kandinsky import *
from random import *
import time

# Configuration espace NumWorks
xMax = 320
yMax = 240

# Retourne une liste de liste vide
# de carrés de c = tCase
def fondVide(tCase):
  fill_rect(0,0,xMax,yMax,color(255,255,255))

# Trace un carré par case ; 1 => noir
def traceGrille(grille, tCase, x, y):
  # boucle sur chaque ligne
  for iL in range(len(grille)):
    # boucle sur chaque ligne
    for iC in range(len(grille[iL])):
      if grille[iL][iC] == 1:
        couleurC = color(0,0,0)
      else:
        couleurC = color(255,255,255)
      fill_rect(x+tCase*iC,y+tCase*iL, \
                tCase,tCase,couleurC)

# Motif animation via 10 états (%10)
# avec effet cyclique
def motifAnime(num):
  if num == 0:
    L01 = [0,0,0,0,0]
    L02 = [0,0,0,0,0]
    L03 = [0,0,1,0,0]
    L04 = [0,0,0,0,0]
    L05 = [0,0,0,0,0]
  elif num == 1:
    L01 = [0,0,0,0,0]
    L02 = [0,1,0,1,0]
    L03 = [0,0,1,0,0]
    L04 = [0,1,0,1,0]
    L05 = [0,0,0,0,0]
  elif num == 2:
    L01 = [1,0,0,0,1]
    L02 = [0,1,0,1,0]
    L03 = [0,0,1,0,0]
    L04 = [0,1,0,1,0]
    L05 = [1,0,0,0,1]
  elif num == 3:
    L01 = [0,1,0,1,0]
    L02 = [1,1,0,1,1]
    L03 = [0,0,1,0,0]
    L04 = [1,1,0,1,1]
    L05 = [0,1,0,0,0]  
  elif num == 4:
    L01 = [1,0,1,0,1]
    L02 = [0,1,0,1,0]
    L03 = [1,0,1,0,1]
    L04 = [0,1,0,1,0]
    L05 = [1,0,1,0,1]  
  elif num == 5:
    L01 = [1,1,1,1,1]
    L02 = [1,1,1,1,1]
    L03 = [1,1,1,1,1]
    L04 = [1,1,1,1,1]
    L05 = [1,1,1,1,1]  
  # Maintenant on assure l'animation en inverse
  elif num == 6:
    return motifAnime(4)
  elif num == 7:
    return motifAnime(3)
  elif num == 8:
    return motifAnime(2)
  elif num == 9:
    return motifAnime(1)
  return [L01,L02,L03,L04,L05]


""" Programme principal """
tailleCase = 10
print("Vecteur pour le mouvement :")
xV = int(input("xVecteur (0 - "+str(xMax)+") ? "))%xMax
yV = int(input("yVecteur (0 - "+str(yMax)+") ? "))%yMax
nbMvt = int(input("Nombre de mvt ? "))

# positionnement aléatoire
x = randint(0,xMax)
y = randint(0,yMax)

numMotif = 0
# lancement mouvement :
# arrêt quand vecteur dpt = vecteur nul
#while (xV != 0) and (yV != 0):
while nbMvt != 0:
  nbMvt = nbMvt - 1
  # efface trace précédente
  fondVide(tailleCase)
  traceGrille(motifAnime(numMotif), tailleCase,x,y)
  time.sleep(0.1)
  # application vecteur mouvement
  x = x + xV
  y = y + yV
  # application coef ralentissement

  # Assure le rebond sur bord droit
  if x > xMax:
    x = xMax - (x - xMax)
    xV = -xV
  # Assure le rebond sur bord gauche
  if x < 0:
    x = - x
    xV = -xV
  # Assure le rebond sur bord bas
  if y > yMax:
    y = yMax - (y - yMax)
    yV = -yV
  # Assure le rebond sur bord haut
  if y < 0:
    y = - y
    yV = -yV
  # passe au motif suivant
  numMotif = (numMotif + 1) % 10

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.