jeu_de_la_vie
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteProchaine révisionLes deux révisions suivantes | ||
jeu_de_la_vie [2019/10/18 15:08] – Mushussu | jeu_de_la_vie [2019/10/21 09:26] – Camille | ||
---|---|---|---|
Ligne 30: | Ligne 30: | ||
L' | L' | ||
Il était initialement question de pions qui se poseraient dans les trous du plateau de jeu. Le choix s'est porté sur des capteurs de pression (Force Sensing Resistance) pour la détection des pions. \\ | Il était initialement question de pions qui se poseraient dans les trous du plateau de jeu. Le choix s'est porté sur des capteurs de pression (Force Sensing Resistance) pour la détection des pions. \\ | ||
- | Un bouton pour amorcer le jeu et le faire et évoluer et un bouton pour revenir à la configuration initiale ont été ajoutés sur les GPIO du micro-iordinateur. \\ | + | Un bouton pour amorcer le jeu et le faire et évoluer et un bouton pour revenir à la configuration initiale ont été ajoutés sur les GPIO du micro-ordinateur. \\ |
- | Enfin, un bouton marche/ | + | Enfin, un bouton marche/ |
Montage des capteurs dans le plateau de jeu: | Montage des capteurs dans le plateau de jeu: | ||
Ligne 75: | Ligne 75: | ||
<code python> | <code python> | ||
- | '' | + | import pygame, sys |
- | import pygame, sys\\ | + | from pygame.locals import * |
- | from pygame.locals import *\\ | + | |
- | import random\\ | + | |
- | import RPi.GPIO as GPIO\\ | + | from time import sleep |
- | 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: | + | |
- | assert len(GPIOCasesInit)==16\\ | + | |
- | assert len(set(GPIOCasesInit.values()))==16\\ | + | |
- | GPIO.setmode(GPIO.BCM)\\ | + | |
- | '' | + | |
- | '' | + | |
- | for n in GPIOCasesInit: | + | |
- | GPIO.setup(n, | + | |
- | GPIO.setup(14, | + | |
- | GPIO.setup(15, | + | |
- | '' | + | |
- | '' | + | |
- | GPIO.add_event_detect(14, | + | |
- | GPIO.add_event_detect(15, | + | |
- | #Number of frames per second\\ | + | |
- | FPS = 10\\ | + | |
- | ###Sets size of grid\\ | + | |
- | WINDOWWIDTH | + | # vérification du nombre de capteurs définis |
- | WINDOWHEIGHT | + | |
- | CELLSIZE | + | |
+ | | ||
+ | # 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' | ||
+ | # Les broches 14 et 15 (sur lesquelles sont câblés des boutons) sont au niveau électrique haut par défaut | ||
+ | for n in GPIOCasesInit: | ||
+ | | ||
+ | GPIO.setup(14, | ||
+ | | ||
+ | # les broches 14 et 15 détecteront un front descendant | ||
+ | | ||
+ | | ||
+ | # | ||
+ | FPS = 10 | ||
- | #Check to see if the width and height are multiples of the cell size.\\ | + | ###configure la taille de la grille |
- | assert | + | |
- | assert | + | |
+ | | ||
- | #Determine number of cells in horizonatl and vertical plane\\ | + | #les dimensions de la grille sont-elles des multiples du nombre de cellules |
- | CELLWIDTH = WINDOWWIDTH | + | assert |
- | CELLHEIGHT | + | |
- | # set up the colours\\ | + | #Determine le nombre de cellules sur l'axe vertical et l'axe horizontal\\ |
- | BLACK = | + | CELLWIDTH |
- | WHITE = | + | CELLHEIGHT |
- | DARKGRAY = (40, 40, 40)\\ | + | |
- | GREEN = (50, | + | |
- | RED = (255, 0, 100)\\ | + | |
- | BoxExample | + | # fabrication des couleurs |
+ | | ||
+ | WHITE = | ||
+ | | ||
+ | GREEN = | ||
+ | RED = | ||
- | def text_objects(text, font): \\ | + | |
- | textSurface = font.render(text, True, DARKGRAY)\\ | + | |
- | return textSurface, textSurface.get_rect()\\ | + | |
- | def afficheInit(text, | + | def text_objects(text, |
- | | + | textSurface |
- | TextSurf, TextRect = text_objects(text, | + | return textSurface, textSurface.get_rect() |
- | TextRect.center = (x,y)\\ | + | |
- | | + | |
- | pygame.display.update()\\ | + | |
- | #time.sleep(2)\\ | + | |
- | #Draws the grid lines\\ | + | def afficheInit(text, x, y): |
- | def drawGrid():\\ | + | largeText = pygame.font.Font(' |
- | for x in range(0, WINDOWWIDTH, | + | |
- | pygame.draw.line(DISPLAYSURF, DARKGRAY, (x,0),(x,WINDOWHEIGHT))\\ | + | |
- | for y in range (0, WINDOWHEIGHT, | + | DISPLAYSURF.blit(TextSurf, TextRect) |
- | pygame.draw.line(DISPLAYSURF, | + | |
+ | # | ||
+ | def drawGrid(): | ||
+ | for x in range(0, WINDOWWIDTH, | ||
+ | | ||
+ | for y in range (0, WINDOWHEIGHT, | ||
+ | | ||
- | def drawGridExample(): | ||
- | for n in BoxExample: | ||
- | pygame.draw.rect(DISPLAYSURF, | ||
- | #Colours the cells green for life and white for no life\\ | + | def drawGridExample(): |
- | def colourGrid(item, lifeDict):\\ | + | for n in BoxExample: |
- | x = item[0]\\ | + | pygame.draw.rect(DISPLAYSURF, |
- | 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, | + | |
- | if lifeDict[item] == 1:\\ | + | |
- | pygame.draw.rect(DISPLAYSURF, | + | |
- | # | + | |
- | return lifeDict[item]\\ | + | |
- | #Creation un dictionnaire de l' | + | #Colore en vert les cellules |
- | # | + | |
- | def blankGrid():\\ | + | x = item[0] |
- | | + | y = item[1] |
- | #Creation d un dictionnaire pour toutes les cellules | + | y = y * CELLSIZE |
- | for y in range (CELLHEIGHT):\\ | + | x = x * CELLSIZE # transforme le tableau de dimension de la grille |
- | for x in range (CELLWIDTH):\\ | + | if lifeDict[item] == 0: |
- | | + | pygame.draw.rect(DISPLAYSURF, |
- | return | + | if lifeDict[item] == 1: |
+ | | ||
+ | | ||
- | def startingGridInit(lifeDict, GPIOCasesInit):\\ | + | # |
- | for ncapt in GPIOCasesInit:\\ | + | # |
- | if(GPIO.input(ncapt)!=0):\\ | + | def blankGrid(): |
- | | + | gridDict = {} |
- | # print(' | + | # |
- | # | + | for y in range (CELLHEIGHT): |
- | return | + | for x in range (CELLWIDTH): |
+ | gridDict[x,y] = 0 #Toutes les cellules sont " | ||
+ | | ||
- | #Determines how many alive neighbours there are around each cell\\ | + | def startingGridInit(lifeDict, GPIOCasesInit): |
- | def getNeighbours(item,lifeDict):\\ | + | |
- | | + | if(GPIO.input(ncapt)!=0): |
- | | + | lifeDict[GPIOCasesInit[ncapt]] = 1 |
- | for y in range (-1,2):\\ | + | |
- | checkCell | + | |
- | if checkCell[0] < CELLWIDTH | + | |
- | if checkCell | + | |
- | if lifeDict[checkCell] == 1:\\ | + | |
- | if x == 0 and y == 0: # negate the central cell\\ | + | |
- | neighbours += 0\\ | + | |
- | else:\\ | + | |
- | neighbours += 1\\ | + | |
- | | + | |
- | #determines the next generation by running a ' | + | #Calcul du nombre de voisins vivants autour de chaque cellule |
- | def tick(lifeDict): | + | def getNeighbours(item, |
- | newTick = {}\\ | + | |
- | for item in lifeDict:\\ | + | for x in range (-1,2): |
- | #get number of neighbours for that item\\ | + | for y in range (-1,2): |
- | numberNeighbours = getNeighbours(item, | + | |
- | if lifeDict[item] == 1: # For those cells already alive\\ | + | if checkCell[0] < CELLWIDTH |
- | if numberNeighbours < 2: # kill under-population\\ | + | if checkCell [1] < CELLHEIGHT and checkCell[1]> |
- | newTick[item] = 0\\ | + | if lifeDict[checkCell] == 1: |
- | elif numberNeighbours > 3: #kill over-population\\ | + | if x == 0 and y == 0: # la cellule centrale n'est pas prise en compte |
- | newTick[item] = 0\\ | + | |
- | else:\\ | + | |
- | newTick[item] = 1 # keep status quo (life)\\ | + | |
- | elif lifeDict[item] == 0:\\ | + | |
- | if numberNeighbours == 3: # cell reproduces\\ | + | #Calcul de la nouvelle génération par appel à la fonction |
- | newTick[item] = 1\\ | + | def tick(lifeDict): |
- | else:\\ | + | |
- | newTick[item] = 0 # keep status quo (death)\\ | + | |
- | return newTick\\ | + | |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
- | #main function\\ | + | #fonction princpale |
- | def main():\\ | + | def main(): |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
- | ps14=0\\ | + | lifeDict = blankGrid() #Creation un dictionnaire de cellules, initialisation a zero |
- | 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: | + | |
- | etat=0\\ | + | |
- | FLAG_PUSH = 0\\ | + | |
- | pygame.init()\\ | + | |
- | global DISPLAYSURF\\ | + | |
- | FPSCLOCK = pygame.time.Clock()\\ | + | |
- | DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, | + | |
- | pygame.display.set_caption(' | + | |
- | DISPLAYSURF.fill(WHITE)\\ | + | |
- | # GPIOCasesInit=valCaptInit()\\ | + | |
- | | + | |
- | #lifeDict = startingGridInit(lifeDict, | + | |
- | #Colours the live cells, blanks the dead\\ | + | |
- | for item in lifeDict: | + | |
- | colourGrid(item, | + | |
- | 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, | + | |
- | cs14=GPIO.input(14)\\ | + | |
- | cs15=GPIO.input(15)\\ | + | |
- | if ps14!=cs14: | + | |
- | print(' | + | |
- | print(' | + | |
- | if(etat==0): | + | |
- | afficheInit(' | + | |
- | afficheInit(' | + | |
- | afficheInit(' | + | |
- | 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(' | + | |
- | if doit_demarrer: | + | |
- | etat=1\\ | + | |
- | lifeDict = startingGridInit(lifeDict, | + | |
- | else:\\ | + | # |
- | etat=0\\ | + | for item in lifeDict: |
- | elif(etat==1):\\ | + | |
- | if(ps15==1 and cs15==0):\\ | + | |
- | # if GPIO.event_detected(15): | + | |
+ | while True: #main game loop | ||
+ | for event in pygame.event.get(): | ||
+ | if event.type == QUIT: | ||
+ | | ||
+ | | ||
+ | # | ||
+ | if event.type == pygame.KEYUP: | ||
+ | | ||
+ | | ||
+ | | ||
+ | if ps14!=cs14: | ||
+ | print(' | ||
+ | | ||
+ | if(etat==0): | ||
+ | afficheInit(' | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | doit_demarrer=False | ||
+ | | ||
+ | | ||
+ | if(GPIO.input(elem)): | ||
+ | doit_demarrer | ||
+ | nb_viv+=1 | ||
+ | print(GPIOCasesInit[elem]) | ||
+ | print(' | ||
+ | if doit_demarrer: | ||
+ | etat=1 | ||
+ | lifeDict = startingGridInit(lifeDict, | ||
+ | else: | ||
+ | etat=0 | ||
+ | elif(etat==1): | ||
+ | if(ps15==1 and cs15==0): | ||
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): | + | |
- | #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() | ||
- | # | ||
- | # | ||
ps14=cs14 | ps14=cs14 | ||
ps15=cs15 | ps15=cs15 | ||
Ligne 304: | Ligne 287: | ||
main() | main() | ||
- | |||
- | '' | ||
</ | </ | ||
======================================================= | ======================================================= | ||
Ligne 316: | Ligne 297: | ||
=====Retour d' | =====Retour d' | ||
+ | |||
+ | Il semble qu'il ne soit pas nécessaire de protéger la carte SD d'une extinction intempestive, | ||
+ | |||
+ | Par précaution, | ||
+ | |||
+ | Ce clone servira à graver une nouvelles carte SD si celle employée ne fonctionne plus. | ||
+ | |||
+ | |||
FSR: | FSR: | ||
jeu_de_la_vie.txt · Dernière modification : 2020/10/29 14:03 de serge