personnage.py

Created by ph-moutou

Created on June 19, 2018

2.32 KB

Ce programme dessine une animation d’un personnage marchant. Taper marche(n), sur la calculatrice uniquement, pour obtenir n fois le passage du personnage. Le mouvement des membres est un peu rudimentaire: quelqu’un pourra t-il proposer un meilleur scénario?


from kandinsky import *
from math import *

def cercle(x0,y0,r,c):
  for i in range(4):
    xd=x0-int((r-i*0.5)/sqrt(2))
    xf=x0+int((r-i*0.5)/sqrt(2))
    for x in range(xd,xf+1):
      x1=x
      y1=y0+int(sqrt((r-i*0.5)**2-(x-x0)**2))
      set_pixel(x,y1,c)
      for j in range(3):
        x2=x0+y1-y0
        y2=y0+x0-x1
        set_pixel(x2,y2,c)
        x1,y1=x2,y2
        
def seg(xa,ya,xb,yb,c):
  if abs(yb-ya)<abs(xb-xa):
    if xb<xa:xa,xb,ya,yb=xb,xa,yb,ya
    m=(yb-ya)/(xb-xa)
    for i in range(xb-xa):
      set_pixel(xa+i,int(m*i+ya),c)
  else:
    if yb<ya:ya,yb,xa,xb=yb,ya,xb,xa
    m=(xb-xa)/(yb-ya)
    for i in range(yb-ya):
      set_pixel(int(m*i+xa),ya+i,c)

def personnage(p,c):
  gus=[[1,2],[2,3],[3,4],[2,5],
  [5,6],[2,7],[7,8],[8,9],[9,10],
  [7,11],[11,12],[12,13]]
  cercle(p[0][0],p[0][1],20,c)
  cercle(p[7][0],p[7][1],4,c)
  for i in range(len(gus)):
    seg(p[gus[i][0]][0],p[gus[i][0]][1],
    p[gus[i][1]][0],p[gus[i][1]][1],c)

def marche(n):
  for k in range(n):
    P=[[-20,37],[-18,57],[-17,63],#tete
    [-30,90],[-25,125],#bras gauche
    [-15,90],[-10,125],#bras droit
    [-15,130],#bassin
    [-35,171],[-40,199],[-35,208],#j.gau.
    [0,171],[-10,199],[-5,208]]#j.dr.
    R1,R2,R3,R4=30.0,62.514,45.62,73.39
    th1,th2,th3,th4=-0.45,0.32,0.1,-0.1
    c=999
    for t in range(80):
      personnage(P,color(255,255,255))
      for i in [0,1,2,7]:P[i][0]+=5
      co=cos((pi*t)/8)
      P[3][0]=P[2][0]+int(R1*sin(th1*co))
      P[3][1]=P[2][1]+int(R1*cos(th1*co))
      P[4][0]=P[2][0]+int(R2*sin(th2+th1*co))
      P[4][1]=P[2][1]+int(R2*cos(th2+th1*co))
      P[5][0]=P[2][0]+int(R1*sin(-th1*co))
      P[5][1]=P[2][1]+int(R1*cos(-th1*co))
      P[6][0]=P[2][0]+int(R2*sin(th2-th1*co))
      P[6][1]=P[2][1]+int(R2*cos(th2-th1*co))
      P[8][0]=P[7][0]+int(R3*sin(th3+th1*co))
      P[8][1]=P[7][1]+int(R3*cos(th3+th1*co))
      P[9][0]=P[7][0]+int(R4*sin(th4+th1*co))
      P[9][1]=P[7][1]+int(R4*cos(th4+th1*co))
      P[11][0]=P[7][0]+int(R3*sin(th3-th1*co))
      P[11][1]=P[7][1]+int(R3*cos(th3-th1*co))
      P[12][0]=P[7][0]+int(R4*sin(th4-th1*co))
      P[12][1]=P[7][1]+int(R4*cos(th4-th1*co))
      P[10][0]=P[9][0]+10
      P[10][1]=P[9][1]
      P[13][0]=P[12][0]+10
      P[13][1]=P[12][1]
      personnage(P,color(0,0,0))
      for i in range(999):c=(c**2)%123

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.