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 14:50] – [Logiciel] Camille | jeu_de_la_vie [2019/10/21 09:21] – Camille | ||
---|---|---|---|
Ligne 74: | Ligne 74: | ||
======================================================= | ======================================================= | ||
+ | <code python> | ||
+ | | ||
+ | from pygame.locals import * | ||
+ | | ||
+ | | ||
+ | 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: |
- | import pygame, sys\\ | + | # vérification du nombre de capteurs définis |
- | from pygame.locals import *\\ | + | |
- | import random\\ | + | |
- | import RPi.GPIO as GPIO\\ | + | |
- | 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\\ | + | |
- | assert len(set(GPIOCasesInit.values()))==16\\ | + | |
- | GPIO.setmode(GPIO.BCM)\\ | + | |
- | '' | + | # les broches 14 et 15 détecteront un front descendant |
- | '' | + | |
- | for n in GPIOCasesInit: | + | |
- | GPIO.setup(n, | + | #nombre d' |
- | GPIO.setup(14, | + | FPS = 10 |
- | GPIO.setup(15, | + | |
- | '' | + | |
- | '' | + | |
- | GPIO.add_event_detect(14, | + | |
- | GPIO.add_event_detect(15, | + | |
- | #Number of frames per second\\ | + | |
- | FPS = 10\\ | + | |
- | ###Sets size of grid\\ | + | ###configure la taille de la grille |
- | WINDOWWIDTH = 1920\\ | + | |
- | WINDOWHEIGHT = 1080\\ | + | |
- | CELLSIZE = 40\\ | + | |
- | #Check to see if the width and height are multiples | + | #les dimensions de la grille sont-elles des multiples |
- | assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple | + | |
- | assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple | + | |
- | # | + | # |
- | CELLWIDTH = WINDOWWIDTH / CELLSIZE # number of cells wide\\ | + | |
- | CELLHEIGHT = WINDOWHEIGHT / CELLSIZE # Number of cells high\\ | + | |
- | # set up the colours\\ | + | # fabrication des couleurs |
- | BLACK = (0, 0, 0)\\ | + | BLACK = (0, 0, 0) |
- | WHITE = (255, | + | WHITE = (255, |
- | DARKGRAY = (40, 40, 40)\\ | + | |
- | GREEN = (50, | + | GREEN = (50, |
- | 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: | + | 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: |
- | def text_objects(text, | + | def text_objects(text, |
- | textSurface = font.render(text, | + | |
- | return textSurface, | + | |
- | def afficheInit(text, | + | def afficheInit(text, |
- | largeText = pygame.font.Font(' | + | |
- | TextSurf, TextRect = text_objects(text, | + | |
- | TextRect.center = (x,y)\\ | + | |
- | DISPLAYSURF.blit(TextSurf, | + | |
- | pygame.display.update()\\ | + | |
- | # | + | |
- | #Draws the grid lines\\ | + | #Dessin de la grille |
- | def drawGrid():\\ | + | def drawGrid(): |
- | for x in range(0, WINDOWWIDTH, | + | |
- | pygame.draw.line(DISPLAYSURF, | + | |
- | for y in range (0, WINDOWHEIGHT, | + | |
- | pygame.draw.line(DISPLAYSURF, | + | |
- | def drawGridExample(): | + | def drawGridExample(): |
- | for n in BoxExample:\\ | + | |
- | pygame.draw.rect(DISPLAYSURF, | + | pygame.draw.rect(DISPLAYSURF, |
- | #Colours the cells green for life and white for no life\\ | + | #Colore en vert les cellules vivantes et en blancs les " |
- | def colourGrid(item, | + | def colourGrid(item, |
- | 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, | + | |
- | if lifeDict[item] == 1:\\ | + | |
- | pygame.draw.rect(DISPLAYSURF, | + | |
- | # | + | |
- | | + | |
- | #Creation un dictionnaire de l' | + | #Creation un dictionnaire de l' |
- | #Toutes les cellules sont " | + | # |
- | def blankGrid(): | + | 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, | + | |
- | return gridDict\\ | + | |
- | def startingGridInit(lifeDict, | + | def startingGridInit(lifeDict, |
- | for ncapt in GPIOCasesInit: | + | |
- | if(GPIO.input(ncapt)!=0): | + | if(GPIO.input(ncapt)!=0): |
- | lifeDict[GPIOCasesInit[ncapt]] = 1\\ | + | lifeDict[GPIOCasesInit[ncapt]] = 1 |
- | # | + | |
- | # | + | |
- | | + | |
- | #Determines how many alive neighbours there are around each cell\\ | + | #Calcul du nombre de voisins vivants autour de chaque cellule |
- | def getNeighbours(item, | + | def getNeighbours(item, |
- | neighbours = 0\\ | + | |
- | for x in range (-1,2):\\ | + | |
- | for y in range (-1,2):\\ | + | |
- | checkCell = (item[0]+x, | + | |
- | if checkCell[0] < CELLWIDTH | + | |
- | if checkCell [1] < CELLHEIGHT and checkCell[1]> | + | |
- | if lifeDict[checkCell] == 1:\\ | + | |
- | if x == 0 and y == 0: # negate the central cell\\ | + | |
- | neighbours += 0\\ | + | |
- | else:\\ | + | |
- | neighbours += 1\\ | + | |
- | return neighbours\\ | + | |
+ | #Calcul de la nouvelle génération par appel à la fonction ' | ||
+ | def tick(lifeDict): | ||
+ | | ||
+ | for item in lifeDict: | ||
+ | # | ||
+ | | ||
+ | if lifeDict[item] == 1: # Pour les cellules encore vivantes | ||
+ | if numberNeighbours < 2: # mort par sous-population | ||
+ | | ||
+ | elif numberNeighbours > 3: #mort par surpopulation | ||
+ | | ||
+ | else:\\ | ||
+ | | ||
+ | elif lifeDict[item] == 0: | ||
+ | if numberNeighbours == 3: # naissance d'une cellule | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
- | #determines the next generation by running a ' | + | #fonction princpale |
- | def tick(lifeDict):\\ | + | |
- | | + | etat=0 |
- | for item in lifeDict: | + | FLAG_PUSH |
- | #get number of neighbours for that item\\ | + | pygame.init() |
- | numberNeighbours = getNeighbours(item, | + | global DISPLAYSURF |
- | if lifeDict[item] == 1: # For those cells already alive\\ | + | FPSCLOCK |
- | if numberNeighbours < 2: # kill under-population\\ | + | DISPLAYSURF |
- | newTick[item] | + | pygame.display.set_caption(' |
- | elif numberNeighbours > 3: #kill over-population\\ | + | DISPLAYSURF.fill(WHITE) |
- | newTick[item] | + | |
- | | + | |
- | newTick[item] = 1 # keep status quo (life)\\ | + | |
- | elif lifeDict[item] == 0:\\ | + | |
- | if numberNeighbours | + | |
- | | + | |
- | | + | |
- | | + | |
- | return newTick\\ | + | |
- | #main function\\ | + | |
- | def main():\\ | + | |
- | ps14=0\\ | + | #Coloration des cellules |
- | ps15=0\\ | + | |
- | cs14=0\\ | + | |
- | cs15=0\\ | + | |
- | # | + | |
- | etat=0\\ | + | |
- | 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, | + | # |
- | pygame.display.set_caption(' | + | if event.type == pygame.KEYUP: |
- | DISPLAYSURF.fill(WHITE)\\ | + | DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, |
- | # GPIOCasesInit=valCaptInit()\\ | + | cs14=GPIO.input(14) |
- | lifeDict = blankGrid() #Creation un dictionnaire de cellules, initialisation a zero\\ | + | cs15=GPIO.input(15) |
- | # | + | if ps14!=cs14: |
- | #Colours the live cells, blanks the dead\\ | + | print(' |
- | | + | print(' |
- | colourGrid(item, | + | if(etat==0): |
- | drawGrid()\\ | + | afficheInit(' |
- | pygame.display.update()\\ | + | afficheInit(' |
- | while True: #main game loop\\ | + | afficheInit(' |
- | 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, | + | 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(' | + | nb_viv+=1 |
- | print(' | + | print(GPIOCasesInit[elem]) |
- | if(etat==0): | + | print(' |
- | afficheInit(' | + | if doit_demarrer: |
- | afficheInit(' | + | etat=1 |
- | afficheInit(' | + | lifeDict = startingGridInit(lifeDict, |
- | 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(' | + | |
- | if doit_demarrer: | + | |
- | etat=1\\ | + | |
- | lifeDict = startingGridInit(lifeDict, | + | |
- | + | ||
- | 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): | + | |
- | #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() | ||
- | + | </ | |
- | '' | + | |
======================================================= | ======================================================= | ||
jeu_de_la_vie.txt · Dernière modification : 2020/10/29 14:03 de serge