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/07 13:12] Labomediajeu_de_la_vie [2019/10/18 15:08] Mushussu
Ligne 59: Ligne 59:
 Un code python pour la prise en compte des billes et l'affichage du jeu de la vie Un code python pour la prise en compte des billes et l'affichage du jeu de la vie
 OS Raspbian version Buster (10.4 (?)) OS Raspbian version Buster (10.4 (?))
-Modifications de l'OS: +Modifications de l'OS:\\ 
--prise en compte du bouton marche/arrêt par un démon +-prise en compte du bouton marche/arrêt par un démon\\ 
--lancement du programme Jeu de la vie au démarrage de l'OS +-lancement du programme Jeu de la vie au démarrage de l'OS\\ 
-Sauvegarde du système complet sur une carte SD de secours: procédure +Sauvegarde du système complet sur une carte SD de secours: procédure\\ 
--copie de la carte SD d'origine +-copie de la carte SD d'origine\\ 
--copie sur une autre carte SD +-copie sur une autre carte SD\\ 
-Liste exhaustive du matériel: +Liste exhaustive du matériel:\\ 
-Photos +Photos\\ 
-Codes +Codes\\ 
-Schéma électrique pour les FSR +Le code a été écrit en python et utilise la bibliothèque pygame. Il s'agit de l'adaptation du code de Trevor Appleton, que l'on peut trouver détaillé ici:\\ 
-Schéma d'intégration  +http://trevorappleton.blogspot.com/2013/07/python-game-of-life.html\\ 
-Schéma de câblage sur la Raspberry+ 
 + 
 +======================================================= 
 + 
 +<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)}\\ 
 +assert len(GPIOCasesInit)==16\\ 
 +assert len(set(GPIOCasesInit.values()))==16\\ 
 +GPIO.setmode(GPIO.BCM)\\ 
 +'' 
 +'' 
 +for n in GPIOCasesInit: 
 +    GPIO.setup(n, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
 +    GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
 +    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\\ 
 +WINDOWWIDTH = 1920\\ 
 +WINDOWHEIGHT = 1080\\ 
 +CELLSIZE = 40\\ 
 + 
 +#Check to see if the width and height are multiples of the cell size.\\ 
 +assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size"\\ 
 +assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size"\\ 
 + 
 +#Determine number of cells in horizonatl and vertical plane\\ 
 +CELLWIDTH = WINDOWWIDTH / CELLSIZE # number of cells wide\\ 
 +CELLHEIGHT = WINDOWHEIGHT / CELLSIZE # Number of cells high\\ 
 + 
 +# set up the colours\\ 
 +BLACK =    (0,  0,  0)\\ 
 +WHITE =    (255,255,255)\\ 
 +DARKGRAY = (40, 40, 40)\\ 
 +GREEN =    (50,255,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)}\\ 
 + 
 +def text_objects(text, font): \\ 
 +    textSurface = font.render(text, True, DARKGRAY)\\ 
 +    return textSurface, textSurface.get_rect()\\ 
 + 
 +def afficheInit(text, x, y):\\ 
 +    largeText = pygame.font.Font('freesansbold.ttf',75)\\ 
 +    TextSurf, TextRect = text_objects(text, largeText)\\ 
 +    TextRect.center = (x,y)\\ 
 +    DISPLAYSURF.blit(TextSurf, TextRect)\\ 
 +    pygame.display.update()\\ 
 +    #time.sleep(2)\\ 
 + 
 +#Draws the grid lines\\ 
 +def drawGrid():\\ 
 +    for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines\\ 
 +        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x,0),(x,WINDOWHEIGHT))\\ 
 +    for y in range (0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines\\ 
 +        pygame.draw.line(DISPLAYSURF, DARKGRAY, (0,y), (WINDOWWIDTH, y))\\ 
 + 
 + 
 +def drawGridExample():\\ 
 +    for n in BoxExample:\\ 
 +    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\\ 
 +def colourGrid(item, lifeDict):\\ 
 +    x = item[0]\\ 
 +    y = item[1]\\ 
 +    y = y * CELLSIZE # translates array into grid size\\ 
 +    x = x * CELLSIZE # translates array into grid size\\ 
 +    if lifeDict[item] == 0:\\ 
 +        pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, CELLSIZE, CELLSIZE))\\ 
 +    if lifeDict[item] == 1:\\ 
 +        pygame.draw.rect(DISPLAYSURF, GREEN, (x, y, CELLSIZE, CELLSIZE))\\ 
 + #   pygame.draw.rect(DISPLAYSURF, BLACK, (8*CELLSIZE, 3*CELLSIZE, CELLSIZE, CELLSIZE))\\ 
 +    return lifeDict[item]\\ 
 + 
 +#Creation un dictionnaire de l'ensemble des cellules\\ 
 +#Toutes les cellules sont "mortes" (valeur 0)\\ 
 +def blankGrid():\\ 
 +    gridDict = {}\\ 
 +    #Creation d un dictionnaire pour toutes les cellules de la grille\\ 
 +    for y in range (CELLHEIGHT):\\ 
 +        for x in range (CELLWIDTH):\\ 
 +            gridDict[x,y] = 0 #Toutes les cellules sont "mortes"\\  
 +    return gridDict\\ 
 + 
 +def startingGridInit(lifeDict, GPIOCasesInit):\\ 
 +    for ncapt in GPIOCasesInit:\\ 
 + if(GPIO.input(ncapt)!=0):\\ 
 +    lifeDict[GPIOCasesInit[ncapt]] = 1\\ 
 + # print('pion')\\ 
 + # print(ncapt)\\ 
 +    return lifeDict\\ 
 + 
 +#Determines how many alive neighbours there are around each cell\\ 
 +def getNeighbours(item,lifeDict):\\ 
 +    neighbours = 0\\ 
 +    for x in range (-1,2):\\ 
 +        for y in range (-1,2):\\ 
 +            checkCell = (item[0]+x,item[1]+y)\\ 
 +            if checkCell[0] < CELLWIDTH  and checkCell[0] >=0:\\ 
 +                if checkCell [1] < CELLHEIGHT and checkCell[1]>= 0:\\ 
 +                    if lifeDict[checkCell] == 1:\\ 
 +                        if x == 0 and y == 0: # negate the central cell\\ 
 +                            neighbours += 0\\ 
 +                        else:\\ 
 +                            neighbours += 1\\ 
 +    return neighbours\\ 
 + 
 +#determines the next generation by running a 'tick'\\ 
 +def tick(lifeDict):\\ 
 +    newTick = {}\\ 
 +    for item in lifeDict:\\ 
 +        #get number of neighbours for that item\\ 
 +        numberNeighbours = getNeighbours(item, lifeDict)\\ 
 +        if lifeDict[item] == 1: # For those cells already alive\\ 
 +            if numberNeighbours < 2: # kill under-population\\ 
 +                newTick[item] = 0\\ 
 +            elif numberNeighbours > 3: #kill over-population\\ 
 +                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\\ 
 +def main():\\ 
 + 
 +    ps14=0\\ 
 +    ps15=0\\ 
 +    cs14=0\\ 
 +    cs15=0\\ 
 +#    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)}\\ 
 +    etat=0\\ 
 +    FLAG_PUSH = 0\\ 
 +    pygame.init()\\ 
 +    global DISPLAYSURF\\ 
 +    FPSCLOCK = pygame.time.Clock()\\ 
 +    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.FULLSCREEN)\\ 
 +    pygame.display.set_caption('Game of Life')\\ 
 +    DISPLAYSURF.fill(WHITE)\\ 
 +#    GPIOCasesInit=valCaptInit()\\ 
 +    lifeDict = blankGrid() #Creation un dictionnaire de cellules, initialisation a zero\\ 
 +    #lifeDict = startingGridInit(lifeDict,GPIOCasesInit) # Remplissage du dictionnaire de depart\\ 
 +    #Colours the live cells, blanks the dead\\ 
 +    for item in lifeDict:\\ 
 +        colourGrid(item, lifeDict)\\ 
 +    drawGrid()\\ 
 +    pygame.display.update()\\ 
 +    while True: #main game loop\\ 
 +        for event in pygame.event.get():\\ 
 +        if event.type == QUIT:\\ 
 +                pygame.quit()\\ 
 +                sys.exit()\\ 
 + if event.type == pygame.KEYUP:\\ 
 + DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT), pygame.RESIZABLE)\\ 
 + cs14=GPIO.input(14)\\ 
 + cs15=GPIO.input(15)\\ 
 + if ps14!=cs14: \\ 
 + print('ps14', ps14)\\ 
 + print('cs14', cs14)\\ 
 + if(etat==0):\\ 
 + afficheInit('Disposez les pions sur le plateau', WINDOWWIDTH/2, WINDOWHEIGHT/2-300)\\ 
 + afficheInit('et appuyez sur SUIVANT', WINDOWWIDTH/2, (WINDOWHEIGHT/2-200)) \\ 
 + afficheInit('Quelques exemples:', WINDOWWIDTH/2, WINDOWHEIGHT/2-100)\\ 
 + drawGridExample()\\ 
 + pygame.display.update()\\ 
 + #passage a etat suivant\\ 
 +# if GPIO.event_detected(14):\\ 
 + 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 
 + lifeDict=blankGrid() 
 + if(ps14==1 and cs14==0): 
 +# if GPIO.event_detected(14): 
 +        #runs a tick 
 + nb_viv=0 
 + lifeDict = tick(lifeDict) 
 + print('suite'
 + for item in lifeDict: 
 + if(lifeDict[item]): 
 + nb_viv=nb_viv+1 
 + print(nb_viv) 
 + if(nb_viv==0): 
 + print('vide'
 + etat=0 
 +        #Colours the live cells, blanks the dead 
 +        for item in lifeDict: 
 +            colourGrid(item, lifeDict) 
 + drawGrid() 
 +        pygame.display.update()  
 + #if(nb_viv==0): 
 + # etat=0   
 + ps14=cs14 
 + ps15=cs15 
 + sleep(0.1)  
 +        #FPSCLOCK.tick(FPS) 
 +         
 +if __name__=='__main__': 
 +    main() 
 + 
 + 
 +'' 
 +</code> 
 +======================================================= 
 + 
 + 
 + 
 +Schéma électrique pour les FSR\\ 
 +Schéma d'intégration \\ 
 +Schéma de câblage sur la Raspberry\\
  
 =====Retour d'expérience===== =====Retour d'expérience=====
jeu_de_la_vie.txt · Dernière modification : 2020/10/29 14:03 de serge