Outils pour utilisateurs

Outils du site


jeu_de_la_vie

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
Prochaine révisionLes deux révisions suivantes
jeu_de_la_vie [2019/10/18 14:50] – [Logiciel] Camillejeu_de_la_vie [2019/10/21 09:21] Camille
Ligne 74: Ligne 74:
 ======================================================= =======================================================
  
 +<code python>
 + import pygame, sys
 + from pygame.locals import *
 + import random
 + import RPi.GPIO as GPIO
 + from time import sleep
  
-'' + GPIOCasesInit = {17:(9,4), 27:(10,4), 22:(11,4), 5:(12,4), 6:(9,5), 13:(10,5), 19:(11,5), 26:(12,5), 18:(9,6), 24:(10,6),   23:(11,6), 25:(12,6), 12:(9,7), 16:(10,7), 20:(11,7), 21:(12,7)} 
-import pygame, sys\\ + # vérification du nombre de capteurs définis 
-from pygame.locals import *\\ + assert len(GPIOCasesInit)==16 
-import random\\ + assert len(set(GPIOCasesInit.values()))==16 
-import RPi.GPIO as GPIO\\ + GPIO.setmode(GPIO.BCM) 
-from time import sleep\\ +# les broches sur lesquelles se trouvent les capteurs, ainsi que les broches 14 et 15, sont configurées en entrée. 
-'' +# Par défaut, les broches des capteurs sont à l'état bas (niveau électrique 0V) 
-'' +# Les broches 14 et 15 (sur lesquelles sont câblés des boutons) sont au niveau électrique haut par défaut 
-GPIOCasesInit = {17:(9,4), 27:(10,4), 22:(11,4), 5:(12,4), 6:(9,5), 13:(10,5), 19:(11,5), 26:(12,5), 18:(9,6), 24:(10,6), 23:(11,6), 25:(12,6), 12:(9,7), 16:(10,7), 20:(11,7), 21:(12,7)}\\ + for n in GPIOCasesInit:  
-assert len(GPIOCasesInit)==16\\ +     GPIO.setup(n, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
-assert len(set(GPIOCasesInit.values()))==16\\ +     GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
-GPIO.setmode(GPIO.BCM)\\ +     GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
-'' + # les broches 14 et 15 détecteront un front descendant  
-'' + GPIO.add_event_detect(14, GPIO.FALLING) 
-for n in GPIOCasesInit: + GPIO.add_event_detect(15, GPIO.FALLING) 
-    GPIO.setup(n, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + #nombre d'images par seconde 
-    GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP) + FPS = 10
-    GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP) +
-'' +
-'' +
-GPIO.add_event_detect(14, GPIO.FALLING)\\ +
-GPIO.add_event_detect(15, GPIO.FALLING)\\ +
-#Number of frames per second\\ +
-FPS = 10\\+
  
-###Sets size of grid\\ + ###configure la taille de la grille 
-WINDOWWIDTH = 1920\\ + WINDOWWIDTH = 1920 
-WINDOWHEIGHT = 1080\\ + WINDOWHEIGHT = 1080 
-CELLSIZE = 40\\+ CELLSIZE = 40
  
-#Check to see if the width and height are multiples of the cell size.\\ + #les dimensions de la grille sont-elles des multiples du nombre de cellules 
-assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size"\\ + assert WINDOWWIDTH % CELLSIZE == 0, "la largeur des cellules doit être un multiple de la taille des cellules
-assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size"\\+ assert WINDOWHEIGHT % CELLSIZE == 0, "la hauteur des cellules doit être un multiple de la taille des cellules"
  
-#Determine number of cells in horizonatl and vertical plane\\ + #Determine le nombre de cellules sur l'axe vertical et l'axe horizontal\\ 
-CELLWIDTH = WINDOWWIDTH / CELLSIZE # number of cells wide\\ + CELLWIDTH = WINDOWWIDTH / CELLSIZE # nombre de cellules en largeur 
-CELLHEIGHT = WINDOWHEIGHT / CELLSIZE # Number of cells high\\+ CELLHEIGHT = WINDOWHEIGHT / CELLSIZE # nombre de cellules en hauteur
  
-set up the colours\\ + fabrication des couleurs 
-BLACK =    (0,  0,  0)\\ + BLACK =    (0,  0,  0) 
-WHITE =    (255,255,255)\\ + WHITE =    (255,255,255) 
-DARKGRAY = (40, 40, 40)\\ + DARKGRAY = (40, 40, 40) 
-GREEN =    (50,255,100)\\ + GREEN =    (50,255,100) 
-RED =  (255, 0, 100)\\+ RED =  (255, 0, 100)
  
-BoxExample = {1:(3,10), 2:(15,10), 3:(2,11), 4:(4,11), 5:(10,11), 6:(14,11), 7:(16,11), 8:(3,12), 9:(9,12), 10:(10,12), 11:(11,12), 12:(14,12), 13:(16,12), 14:(15,13), 15:(4,16), 16:(9,16), 17:(10,16), 18:(14,16), 19:(16,16), 20:(17,16), 21:(5,17), 22:(8,17), 23:(11,17), 24:(14,17), 25:(15,17), 26:(17,17), 27:(3,18), 28:(4,18), 29:(5,18), 30:(9,18), 31:(11,18), 32:(10,19), 33:(25,17),34:(25,18),35:(26,18)}\\+ BoxExample = {1:(3,10), 2:(15,10), 3:(2,11), 4:(4,11), 5:(10,11), 6:(14,11), 7:(16,11), 8:(3,12), 9:(9,12), 10:(10,12),  11:(11,12), 12:(14,12), 13:(16,12), 14:(15,13), 15:(4,16), 16:(9,16), 17:(10,16), 18:(14,16), 19:(16,16), 20:(17,16), 21:(5,17), 22:(8,17), 23:(11,17), 24:(14,17), 25:(15,17), 26:(17,17), 27:(3,18), 28:(4,18), 29:(5,18), 30:(9,18), 31:(11,18), 32:(10,19), 33:(25,17),34:(25,18),35:(26,18)}
  
-def text_objects(text, font): \\ + def text_objects(text, font):  
-    textSurface = font.render(text, True, DARKGRAY)\\ +     textSurface = font.render(text, True, DARKGRAY) 
-    return textSurface, textSurface.get_rect()\\+     return textSurface, textSurface.get_rect() 
  
-def afficheInit(text, x, y):\\ + def afficheInit(text, x, y): 
-    largeText = pygame.font.Font('freesansbold.ttf',75)\\ +     largeText = pygame.font.Font('freesansbold.ttf',75) 
-    TextSurf, TextRect = text_objects(text, largeText)\\ +     TextSurf, TextRect = text_objects(text, largeText) 
-    TextRect.center = (x,y)\\ +     TextRect.center = (x,y) 
-    DISPLAYSURF.blit(TextSurf, TextRect)\\ +     DISPLAYSURF.blit(TextSurf, TextRect) 
-    pygame.display.update()\\ +     pygame.display.update()
-    #time.sleep(2)\\+
  
-#Draws the grid lines\\ + #Dessin de la grille 
-def drawGrid():\\ + def drawGrid(): 
-    for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines\\ +     for x in range(0, WINDOWWIDTH, CELLSIZE): # trace des lignes verticales 
-        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x,0),(x,WINDOWHEIGHT))\\ +         pygame.draw.line(DISPLAYSURF, DARKGRAY, (x,0),(x,WINDOWHEIGHT)) 
-    for y in range (0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines\\ +     for y in range (0, WINDOWHEIGHT, CELLSIZE): # trace des lignes horizontales 
-        pygame.draw.line(DISPLAYSURF, DARKGRAY, (0,y), (WINDOWWIDTH, y))\\+         pygame.draw.line(DISPLAYSURF, DARKGRAY, (0,y), (WINDOWWIDTH, y))
  
  
-def drawGridExample():\\ + def drawGridExample(): 
-    for n in BoxExample:\\ +     for n in BoxExample: 
-     pygame.draw.rect(DISPLAYSURF, RED, (BoxExample[n][0]*CELLSIZE, BoxExample[n][1]*CELLSIZE, CELLSIZE, CELLSIZE))\\+     pygame.draw.rect(DISPLAYSURF, RED, (BoxExample[n][0]*CELLSIZE, BoxExample[n][1]*CELLSIZE, CELLSIZE, CELLSIZE))
  
-#Colours the cells green for life and white for no life\\ + #Colore en vert les cellules vivantes et en blancs les "non-vivantes" 
-def colourGrid(item, lifeDict):\\ + def colourGrid(item, lifeDict): 
-    x = item[0]\\ +     x = item[0] 
-    y = item[1]\\ +     y = item[1] 
-    y = y * CELLSIZE # translates array into grid size\\ +     y = y * CELLSIZE # transforme le tableau de dimension de la grille 
-    x = x * CELLSIZE # translates array into grid size\\ +     x = x * CELLSIZE # transforme le tableau de dimension de la grille 
-    if lifeDict[item] == 0:\\ +     if lifeDict[item] == 0: 
-        pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, CELLSIZE, CELLSIZE))\\ +         pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, CELLSIZE, CELLSIZE)) 
-    if lifeDict[item] == 1:\\ +     if lifeDict[item] == 1: 
-        pygame.draw.rect(DISPLAYSURF, GREEN, (x, y, CELLSIZE, CELLSIZE))\\ +         pygame.draw.rect(DISPLAYSURF, GREEN, (x, y, CELLSIZE, CELLSIZE)) 
- #   pygame.draw.rect(DISPLAYSURF, BLACK, (8*CELLSIZE, 3*CELLSIZE, CELLSIZE, CELLSIZE))\\ +     return lifeDict[item]\\
-    return lifeDict[item]\\+
  
-#Creation un dictionnaire de l'ensemble des cellules\\ + #Creation un dictionnaire de l'ensemble des cellules 
-#Toutes les cellules sont "mortes" (valeur 0)\\ + #Toutes les cellules sont "mortes" (valeur 0) 
-def blankGrid():\\ + def blankGrid(): 
-    gridDict = {}\\ +     gridDict = {} 
-    #Creation d un dictionnaire pour toutes les cellules de la grille\\ +     #Creation d un dictionnaire pour toutes les cellules de la grille 
-    for y in range (CELLHEIGHT):\\ +     for y in range (CELLHEIGHT): 
-        for x in range (CELLWIDTH):\\ +         for x in range (CELLWIDTH): 
-            gridDict[x,y] = 0 #Toutes les cellules sont "mortes"\\  +             gridDict[x,y] = 0 #Toutes les cellules sont "mortes"  
-    return gridDict\\+     return gridDict 
  
-def startingGridInit(lifeDict, GPIOCasesInit):\\ + def startingGridInit(lifeDict, GPIOCasesInit): 
-    for ncapt in GPIOCasesInit:\\ +     for ncapt in GPIOCasesInit: 
- if(GPIO.input(ncapt)!=0):\\ +  if(GPIO.input(ncapt)!=0): 
-     lifeDict[GPIOCasesInit[ncapt]] = 1\\ +     lifeDict[GPIOCasesInit[ncapt]] = 1 
- # print('pion')\\ +     return lifeDict 
- # print(ncapt)\\ +
-    return lifeDict\\+
  
-#Determines how many alive neighbours there are around each cell\\ + #Calcul du nombre de voisins vivants autour de chaque cellule 
-def getNeighbours(item,lifeDict):\\ + def getNeighbours(item,lifeDict): 
-    neighbours = 0\\ +     neighbours = 0 
-    for x in range (-1,2):\\ +     for x in range (-1,2): 
-        for y in range (-1,2):\\ +         for y in range (-1,2): 
-            checkCell = (item[0]+x,item[1]+y)\\ +             checkCell = (item[0]+x,item[1]+y) 
-            if checkCell[0] < CELLWIDTH  and checkCell[0] >=0:\\ +             if checkCell[0] < CELLWIDTH  and checkCell[0] >=0: 
-                if checkCell [1] < CELLHEIGHT and checkCell[1]>= 0:\\ +                 if checkCell [1] < CELLHEIGHT and checkCell[1]>= 0: 
-                    if lifeDict[checkCell] == 1:\\ +                     if lifeDict[checkCell] == 1: 
-                        if x == 0 and y == 0: # negate the central cell\\ +                         if x == 0 and y == 0: # la cellule centrale n'est pas prise en compte 
-                            neighbours += 0\\ +                             neighbours += 0 
-                        else:\\ +                         else: 
-                            neighbours += 1\\ +                             neighbours += 1 
-    return neighbours\\+     return neighbours 
 +      #Calcul de la nouvelle génération par appel à la fonction 'tick' 
 + def tick(lifeDict): 
 +     newTick = {} 
 +     for item in lifeDict: 
 +         #Obtention du nombre de voisins pour cet élément 
 +         numberNeighbours = getNeighbours(item, lifeDict) 
 +         if lifeDict[item] == 1: # Pour les cellules encore vivantes 
 +             if numberNeighbours < 2: # mort par sous-population 
 +                 newTick[item] = 0 
 +             elif numberNeighbours > 3: #mort par surpopulation 
 +                 newTick[item] = 0 
 +             else:\\ 
 +                 newTick[item] = 1 # ne rien faire (cellule vivante) 
 +         elif lifeDict[item] == 0: 
 +             if numberNeighbours == 3: # naissance d'une cellule 
 +                 newTick[item] = 1 
 +             else: 
 +                 newTick[item] = 0 # ne rien faire (cellule morte) 
 +     return newTick
  
-#determines the next generation by running a 'tick'\\ + #fonction princpale 
-def tick(lifeDict):\\ + def main(): 
-    newTick = {}\\ +     etat=0 
-    for item in lifeDict:\\ +     FLAG_PUSH = 0 
-        #get number of neighbours for that item\\ +     pygame.init() 
-        numberNeighbours = getNeighbours(item, lifeDict)\\ +     global DISPLAYSURF 
-        if lifeDict[item] == 1: # For those cells already alive\\ +     FPSCLOCK pygame.time.Clock() 
-            if numberNeighbours < 2: # kill under-population\\ +     DISPLAYSURF pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.FULLSCREEN) 
-                newTick[item] = 0\\ +     pygame.display.set_caption('Game of Life') 
-            elif numberNeighbours > 3: #kill over-population\\ +     DISPLAYSURF.fill(WHITE)
-                newTick[item] = 0\\ +
-            else:\\ +
-                newTick[item] = 1 # keep status quo (life)\\ +
-        elif lifeDict[item] == 0:\\ +
-            if numberNeighbours == 3: # cell reproduces\\ +
-                newTick[item] 1\\ +
-            else:\\ +
-                newTick[item] = 0 # keep status quo (death)\\ +
-    return newTick\\+
  
-#main function\\ +     lifeDict = blankGrid() #Creation un dictionnaire de cellules, initialisation a zero
-def main():\\+
  
-    ps14=0\\ +     #Coloration des cellules vivantes 
-    ps15=0\\ +     for item in lifeDict: 
-    cs14=0\\ +         colourGrid(item, lifeDict) 
-    cs15=0\\ +     drawGrid() 
-   GPIOCasesInit = {17:(9,4), 27:(10,4), 22:(11,4), 5:(12,4), 6:(9,5), 13:(10,5), 19:(11,5), 26:(12,5), 12:(9,6), 23:(11,6), 25:(12,6), 21:(9,7), 16:(10,7), 20:(11,7)}\\ +     pygame.display.update() 
-    etat=0\\ +     while True: #main game loop 
-    FLAG_PUSH = 0\\ +         for event in pygame.event.get(): 
-    pygame.init()\\ +         if event.type == QUIT: 
-    global DISPLAYSURF\\ +                 pygame.quit() 
-    FPSCLOCK = pygame.time.Clock()\\ +                 sys.exit() 
-    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.FULLSCREEN)\\ +                 #Indispensable: prévoir de pouvoir quitter le mode plein écran  
-    pygame.display.set_caption('Game of Life')\\ +  if event.type == pygame.KEYUP: 
-    DISPLAYSURF.fill(WHITE)\\ +  DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.RESIZABLE) 
-#    GPIOCasesInit=valCaptInit()\\ +  cs14=GPIO.input(14) 
-    lifeDict = blankGrid() #Creation un dictionnaire de cellules, initialisation a zero\\ +  cs15=GPIO.input(15) 
-    #lifeDict = startingGridInit(lifeDict,GPIOCasesInit) # Remplissage du dictionnaire de depart\\ +  if ps14!=cs14:  
-    #Colours the live cells, blanks the dead\\ +  print('ps14', ps14) 
-    for item in lifeDict:\\ +  print('cs14', cs14) 
-        colourGrid(item, lifeDict)\\ +  if(etat==0): 
-    drawGrid()\\ +  afficheInit('Disposez les pions sur le plateau', WINDOWWIDTH/2, WINDOWHEIGHT/2-300) 
-    pygame.display.update()\\ +  afficheInit('et appuyez sur SUIVANT', WINDOWWIDTH/2, (WINDOWHEIGHT/2-200))  
-    while True: #main game loop\\ +  afficheInit('Quelques exemples:', WINDOWWIDTH/2, WINDOWHEIGHT/2-100) 
-       for event in pygame.event.get():\\ +  drawGridExample() 
-         if event.type == QUIT:\\ +  pygame.display.update() 
-                 pygame.quit()\\ +  #passage a etat suivant 
-                 sys.exit()\\ +  if(ps14==1 and  cs14==0): 
- if event.type == pygame.KEYUP:\\ +  doit_demarrer=False 
- DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.RESIZABLE)\\ +  nb_viv=0 
- cs14=GPIO.input(14)\\ +  for elem in GPIOCasesInit: 
- cs15=GPIO.input(15)\\ +  if(GPIO.input(elem)): 
- if ps14!=cs14: \\ + doit_demarrer = True 
- print('ps14', ps14)\\ + nb_viv+=1 
- print('cs14', cs14)\\ + print(GPIOCasesInit[elem]) 
- if(etat==0):\\ + print('nb_viv', nb_viv) 
- afficheInit('Disposez les pions sur le plateau', WINDOWWIDTH/2, WINDOWHEIGHT/2-300)\\ + if doit_demarrer:  
- afficheInit('et appuyez sur SUIVANT', WINDOWWIDTH/2, (WINDOWHEIGHT/2-200)) \\ + etat=1 
- afficheInit('Quelques exemples:', WINDOWWIDTH/2, WINDOWHEIGHT/2-100)\\ + lifeDict = startingGridInit(lifeDict, GPIOCasesInit) 
- drawGridExample()\\ + else: 
- pygame.display.update()\\ + etat=0 
- #passage a etat suivant\\ + elif(etat==1): 
-# if GPIO.event_detected(14):\\ + if(ps15==1 and cs15==0):
- if(ps14==1 and  cs14==0):\\ +
- doit_demarrer=False\\ +
- nb_viv=0\\ +
- for elem in GPIOCasesInit:\\ +
- if(GPIO.input(elem)):\\ +
- doit_demarrer = True\\ +
- nb_viv+=1\\ +
- print(GPIOCasesInit[elem])\\ +
- print('nb_viv', nb_viv)\\ +
- if doit_demarrer: \\  +
- etat=1\\ +
- lifeDict = startingGridInit(lifeDict, GPIOCasesInit)\\ +
- +
- else:\\ +
- etat=0\\ +
- elif(etat==1):\\ +
- if(ps15==1 and cs15==0):\\ +
-# if GPIO.event_detected(15):+
  etat=0  etat=0
  lifeDict=blankGrid()  lifeDict=blankGrid()
  if(ps14==1 and cs14==0):  if(ps14==1 and cs14==0):
-# if GPIO.event_detected(14): +        #Calcul de l'état suivant
-        #runs a tick+
  nb_viv=0  nb_viv=0
  lifeDict = tick(lifeDict)  lifeDict = tick(lifeDict)
Ligne 294: Ligne 279:
  drawGrid()  drawGrid()
         pygame.display.update()          pygame.display.update() 
- #if(nb_viv==0): 
- # etat=0   
  ps14=cs14  ps14=cs14
  ps15=cs15  ps15=cs15
Ligne 304: Ligne 287:
     main()     main()
  
- +</code>
-''+
 ======================================================= =======================================================
  
jeu_de_la_vie.txt · Dernière modification : 2020/10/29 14:03 de serge