Creació de jocs amb PyBadge

Programació Dades pràctiques     Recursos CITCEA
Tutorial Exemples Projectes   Inici

Pac-Man

El Pac-Man és un joc d'un jugador, el qual controla el personatge principal, Pac-Man, que apareix en un laberint i el seu objectiu és menjar-se totes les boles que hi ha, mentre evita els quatre fantasmes que l'intenten atrapar. Hi ha boles de dues mides diferents. Quan el Pac-Man menja una bola gran, tots els fantasmes es tornen blaus i el Pac-Man se'ls pot menjar per un temps limitat. Si és capaç de menjar-se'n algun, aquest fantasma torna a la casella d'inici. Un cop transcorregut aquest temps, el joc continua amb les normes inicials, on són els fantasmes els que s'han de menjar al Pac-Man.

Les imatges que fa servir el joc són les que comentem tot seguit. La primera mostra el taulell del joc i la segona la pantalla d'inici. També tenim les imatges que surten a l'acabar el joc, segons si es guanya o es perd.

fons       inici       guanya       perd

D'altra banda, tenim les quatre orientacions del personatge i les boles.

personatge       personatge       personatge       personatge       bola petita       bola grossa

També els cinc colors de fantasmes.

fantasma       fantasma       fantasma       fantasma       fantasma

El programa és el següent:

import board
import displayio
import ugame
import adafruit_imageload
import time
import random
import digitalio
import audioio
import audio_lib
import neopixel
pant = board.DISPLAY
# Creem l'sprite (personatge)
pers_sprite, paleta_pacman= adafruit_imageload.load("/PACMAN.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
pacman = displayio.TileGrid(pers_sprite,pixel_shader=paleta_pacman,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_pacman.make_transparent(0)  #Fem que el fons sigui transparent
pers_spriteD, paleta_pacmanD= adafruit_imageload.load("/PACMAND.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
pacmanD = displayio.TileGrid(pers_spriteD,pixel_shader=paleta_pacmanD,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_pacmanD.make_transparent(0)
pers_spriteL, paleta_pacmanL= adafruit_imageload.load("/PACMANL.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
pacmanL = displayio.TileGrid(pers_spriteL,pixel_shader=paleta_pacmanL,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_pacmanL.make_transparent(0)
pers_spriteU, paleta_pacmanU= adafruit_imageload.load("/PACMANU.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
pacmanU = displayio.TileGrid(pers_spriteU,pixel_shader=paleta_pacmanU,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_pacmanU.make_transparent(0)
#Creem als fanstames
grup_fant = displayio.Group(scale=1)
#Vermell
fantVer_sprite, paleta_fantVer= adafruit_imageload.load("/RED.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
Vermell = displayio.TileGrid(fantVer_sprite,pixel_shader=paleta_fantVer,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_fantVer.make_transparent(0)
grup_fant.append(Vermell)
#Blau
fantBBlau_sprite, paleta_fantBBlau= adafruit_imageload.load("/BBLUE.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
BBlau = displayio.TileGrid(fantBBlau_sprite,pixel_shader=paleta_fantBBlau,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_fantBBlau.make_transparent(0)
grup_fant.append(BBlau)
#Lila
fantLila_sprite, paleta_fantLila= adafruit_imageload.load("/PURPLE.bmp",
                                bitmap=displayio.Bitmap,
                                palette=displayio.Palette)
Lila = displayio.TileGrid(fantLila_sprite,pixel_shader=paleta_fantLila,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_fantLila.make_transparent(0)
grup_fant.append(Lila)
#Rosa
fantRosa_sprite, paleta_fantRosa= adafruit_imageload.load("/PINK.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
Rosa = displayio.TileGrid(fantRosa_sprite,pixel_shader=paleta_fantRosa,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
paleta_fantRosa.make_transparent(0)
grup_fant.append(Rosa)
#Blau fosc
fantBlau_sprite, paleta_fantBlau= adafruit_imageload.load("/BLUE.bmp",
                                            bitmap=displayio.Bitmap,
                                            palette=displayio.Palette)
paleta_fantBlau.make_transparent(0)
for fantBlau in range(0,4):
    Blau = displayio.TileGrid(fantBlau_sprite,pixel_shader=paleta_fantBlau,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
    grup_fant.append(Blau)
# Creem l'sprite boles petites
lcoord = []
lnoboles = [[0, 0], [0,112], [144,0], [144,112], [64, 0], [80, 0], [16, 16], [32, 16], [112, 16],
[128, 16], [16, 48], [16, 64], [128, 48], [128, 64], [48, 48], [48, 64], [64, 48], [64, 64],
[80, 48], [80, 64], [96, 48], [96, 64], [0,96], [16, 96], [48, 96], [64, 96], [80, 96], [96, 96],
[128, 96], [144, 96], [80, 80]]
for ele in range(0,10):
    for el in range(0,8):
        if [ele*16,el*16] not in lnoboles:
            lcoord.append([ele*16,el*16])
grup_bola = displayio.Group(scale=1)
#Si lcoord te 48 llistes dins ______
#Fem un for per no posar 20 vegades el mateix
bola_sprite, paleta_bola= adafruit_imageload.load("/bola2.bmp",
                                        bitmap=displayio.Bitmap,
                                        palette=displayio.Palette)
paleta_bola.make_transparent(0) #Fem el fons de les boles transparent
for ele in range(0,49):  #Aquí també posem 48 ______
    bola = displayio.TileGrid(bola_sprite,pixel_shader=paleta_bola,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
    grup_bola.append(bola)
#Col·loquem les boles
for el in range(len(lcoord)):
    grup_bola[el].x = lcoord[el][0]
    grup_bola[el].y = lcoord[el][1]
# Creem l'sprite boles grans
#Si aquí posem 4
lbolag = [[0, 0], [0,112], [144,0], [144,112]]
grup_bolaG = displayio.Group(scale=1)
bolaG_sprite, paleta_bolaG= adafruit_imageload.load("/BOLA.bmp",
                                        bitmap=displayio.Bitmap,
                                        palette=displayio.Palette)
paleta_bolaG.make_transparent(0) #Fem el fons de les boles transparent
for eleG in range(0,4):  #Aquí també posem 48
    bolaG = displayio.TileGrid(bolaG_sprite,pixel_shader=paleta_bolaG,
                                width = 1, height = 1,
                                tile_width = 16, tile_height = 16)
    grup_bolaG.append(bolaG)
# Taulell
taulell_sprite, paleta_taulell= adafruit_imageload.load("/fonsB.bmp",
                            bitmap=displayio.Bitmap,
                            palette=displayio.Palette)
taulell = displayio.TileGrid(taulell_sprite,pixel_shader=paleta_taulell,
                            width = 1, height = 1,
                            tile_width = 160, tile_height = 128)
#Taulell d'inici
taulellI_sprite, paleta_taulellI= adafruit_imageload.load("/INICIO.bmp",
                            bitmap=displayio.Bitmap,
                            palette=displayio.Palette)
taulellI = displayio.TileGrid(taulellI_sprite,pixel_shader=paleta_taulellI,
                            width = 1, height = 1,
                            tile_width = 160, tile_height = 128)
taulellW_sprite, paleta_taulellW= adafruit_imageload.load("/WINNER.bmp",
                            bitmap=displayio.Bitmap,
                            palette=displayio.Palette)
paleta_taulellW.make_transparent(0)
taulellW = displayio.TileGrid(taulellW_sprite,pixel_shader=paleta_taulellW,
                            width = 1, height = 1,
                            tile_width = 130, tile_height = 60)
taulellD_sprite, paleta_taulellD= adafruit_imageload.load("/DEAD.bmp",
                            bitmap=displayio.Bitmap,
                            palette=displayio.Palette)
paleta_taulellD.make_transparent(0)
taulellD = displayio.TileGrid(taulellD_sprite,pixel_shader=paleta_taulellD,
                            width = 1, height = 1,
                            tile_width = 130, tile_height = 60)
# Definim un grup per al personatge
grup_pers = displayio.Group(scale=1)
grup_pers.append(pacman)
grup_pers.append(pacmanD)
grup_pers.append(pacmanL)
grup_pers.append(pacmanU)
# Definim un grup per al taulell
grup_taulell = displayio.Group(scale=1)
grup_taulell.append(taulell)
grup_taulellI = displayio.Group(scale=1)
grup_taulellI.append(taulellI)
#Creem un grup de tot
grup = displayio.Group()
# Afegim els grups anteriors al global
grup.append(grup_taulell)   # Primer el taulell
grup.append(grup_bola)      #Després les boles
grup.append(grup_bolaG)
grup.append(grup_fant)      # Seguidament els fantsames
grup.append(grup_pers)      # Finalment el personatge
grup.append(taulellD)
grup.append(taulellW)
#Creem la classe Casella per fer que cada casella tingui les condicions que volem
class Casella:
    def __init__(self, bola, tipus, posicio, x, y, sensefantasma):
        self.tipus = tipus
        self.posicio = posicio
        self.bola = bola #0 vacia, 1 bola pequeña, 2 bola grande
        self.x = x
        self.y = y
        self.sensefantasma = sensefantasma
matriu = [[],[],[],[],[],[],[],[],[],[],[],[]]
for matx in range(-1,11):
    for maty in range(-1,9):
        matriu[matx+1].append(Casella(0, True,[matx*16,maty*16], matx + 1, maty + 1, True))
#On no volem que pugui anar el pacman
lno = [[-16, -16], [0, -16], [16, -16], [32, -16], [48, -16], [64, -16], [80, -16], [96, -16], [112, -16],
[128, -16], [144, -16], [160, -16], [-16, -16], [-16, 0], [-16, 16], [-16, 32], [-16, 48], [-16, 64],
[-16, 96], [-16, 112], [-16, 128], [160, -16], [160, 0], [160, 16], [160, 32], [160, 48], [160, 64],
[160, 96], [160, 112], [160, 128], [-16, 128], [0, 128], [16, 128], [32, 128], [48, 128], [64, 128],
[80, 128], [96, 128], [112, 128], [128, 128], [144, 128], [160, 128], [64, 0], [80, 0], [16, 16], [32, 16],
[112, 16], [128, 16], [16, 48], [16, 64], [48, 48], [48, 64], [64, 48], [64, 64], [80, 48], [80, 64],
[96, 48], [96, 64], [128, 48], [128,64], [0, 96], [16, 96], [48, 96], [64, 96], [80, 96], [96, 96],
[128, 96], [144, 96]]
for paret in matriu:
    for paret2 in paret:
        if paret2.posicio in lno:
            paret2.tipus = False
            #print(paret2.posicio)
        if paret2.posicio in lcoord:
            paret2.bola = 1
        if paret2.posicio in lbolag:
            paret2.bola = 2
class Pers:
    def __init__(self, grup):
        self.grup = grup
        self.pos = [80,80]
        self.estat = "Quiet"
        self.casella = matriu[6][6]
pers = Pers(grup_pers[0])
class Fantasma:
    def __init__(self, grup, pos, casella):
        self.grup = grup
        self.pos = pos
        self.casella = casella
#Definim cada fantasma
fantVerm = Fantasma(grup_fant[0], [48,64], matriu[4][5])
fantBBlau = Fantasma(grup_fant[1], [64,48], matriu[5][4])
fantLila = Fantasma(grup_fant[2], [80,64], matriu[6][5])
fantRosa = Fantasma(grup_fant[3], [96,48], matriu[7][4])
estat = 'Inici'
#Cada random serà el pròxim moviment de cada fantasma
randV = random.randint(0,3); randB = random.randint(0,3); randL = random.randint(0,3); randR = random.randint(0,3)
vidas = 3
contadorA = 0 #Contador de temps quan els fantasmes es tornen blau fosc
bolasF = 0 #Contador de quantes boles porta menjades el pacman
led = 0
while True:
    if estat == 'Inici':
        grup[5].x = -30; grup[5].y = -60; grup[6].x = -30; grup[6].y = -60
        fantVerm = Fantasma(grup_fant[0], [48,64], matriu[4][5])
        fantBBlau = Fantasma(grup_fant[1], [64,48], matriu[5][4])
        fantLila = Fantasma(grup_fant[2], [80,64], matriu[6][5])
        fantRosa = Fantasma(grup_fant[3], [96,48], matriu[7][4])
        pers = Pers(grup_pers[0])
        #Fem que tots els sprites que no utilitzem estiguin fora del taulell
        grup_fant[4].x = -16; grup_fant[4].y = -16
        grup_fant[5].x = -16; grup_fant[5].y = -16
        grup_fant[6].x = -16; grup_fant[6].y = -16
        grup_fant[7].x = -16; grup_fant[7].y = -16
        grup_pers[1].x = -16; grup_pers[1].y = -16
        grup_pers[2].x = -16; grup_pers[2].y = -16
        grup_pers[3].x = -16; grup_pers[3].y = -16
        #Cada fantasma comença en un temps diferent
        comenV = 0; comenB = 0; comenL = 0; comenR = 0
        contadorA = 0 #Contador de temps quan els fantasmes es tornen blau fosc
        iniciP = time.monotonic()
        iniciF = time.monotonic()
        iniciI = time.monotonic()
        estatAnt = 'Right'
        if vidas == 3:
            pant.show(grup_taulellI)
            boto = ugame.buttons.get_pressed()
            #Col·loquem les boles en pantalla
            for el in range(len(lcoord)):
                grup_bola[el].x = lcoord[el][0]
                grup_bola[el].y = lcoord[el][1]
            for elG in range(len(lbolag)):
                grup_bolaG[elG].x = lbolag[elG][0]
                grup_bolaG[elG].y = lbolag[elG][1]
            #Tornem a crear la matriu en cas de tornar a començar la partida
            matriu = [[],[],[],[],[],[],[],[],[],[],[],[]]
            for matx in range(-1,11):
                for maty in range(-1,9):
                    matriu[matx+1].append(Casella(0, True,[matx*16,maty*16], matx + 1, maty + 1, True))
            for paret in matriu:
                for paret2 in paret:
                    if paret2.posicio in lno:
                        paret2.tipus = False
                    if paret2.posicio in lcoord:
                        paret2.bola = 1
                    if paret2.posicio in lbolag:
                        paret2.bola = 2
            bolasF = 0 #Contador de quantes boles porta menjades el pacman
            minn = 0
            smV = 0; smB = 0; smL = 0; smR = 0; tcf = 0
            estatAnt = 'Right'
            if boto == ugame.K_O:
                estat = 'Jugant'
                velocitat = 0.5
            elif boto == ugame.K_X:
                estat = 'Jugant'
                velocitat = 0.3
        else:
            if vidas == 0:
                tempsM = time.monotonic()
                estat = 'Gameover'
            if(time.monotonic() > iniciI):
                estat = 'Jugant'
                for matx in range(len(matriu)):
                    for maty in range(len(matriu[0])):
                        matriu[matx][maty].sensefantasma = True
    if estat == 'Jugant':
        pant.show(grup)
        llposvalV = [0,1,2,3]; llposvalB = [0,1,2,3]; llposvalL = [0,1,2,3]; llposvalR = [0,1,2,3]
        pers.grup.x = pers.pos[0]; pers.grup.y = pers.pos[1]
        fantVerm.grup.x = fantVerm.pos[0]; fantVerm.grup.y = fantVerm.pos[1]
        fantBBlau.grup.x = fantBBlau.pos[0]; fantBBlau.grup.y = fantBBlau.pos[1]
        fantLila.grup.x = fantLila.pos[0]; fantLila.grup.y = fantLila.pos[1]
        fantRosa.grup.x = fantRosa.pos[0]; fantRosa.grup.y = fantRosa.pos[1]
        boto = ugame.buttons.get_pressed()
        if bolasF == 53:
            estat = 'Winner'
            tempsM = time.monotonic()
        if(time.monotonic() > iniciP + 0.5):
            if boto == ugame.K_LEFT and matriu[pers.casella.x - 1][pers.casella.y].tipus:
                if estatAnt == 'Right':
                    grup_pers[0].x = -16; grup_pers[0].y = -16
                elif estatAnt == 'Up':
                    grup_pers[3].x = -16; grup_pers[3].y = -16
                elif estatAnt == 'Down':
                    grup_pers[1].x = -16; grup_pers[1].y = -16
                estatAnt = 'Left'
                pers.grup = grup_pers[2]
                if pers.casella.x == 0:
                    pers.casella.x = 11
                if matriu[pers.casella.x - 1][pers.casella.y].bola == 1:
                    n = lcoord.index(matriu[pers.casella.x - 1][pers.casella.y].posicio)
                    grup_bola[n].x = -16; grup_bola[n].y = -16
                    matriu[pers.casella.x - 1][pers.casella.y].bola = 0
                    bolasF += 1
                if matriu[pers.casella.x - 1][pers.casella.y].bola == 2:
                    bolasF += 1
                    contadorA = 1
                    fantVerm.grup = grup_fant[4]; fantBBlau.grup = grup_fant[5]; fantLila.grup = grup_fant[6]; fantRosa.grup = grup_fant[7]
                    grup_fant[0].x = -32; grup_fant[0].y = -32
                    grup_fant[1].x = -32; grup_fant[1].y = -32
                    grup_fant[2].x = -32; grup_fant[2].y = -32
                    grup_fant[3].x = -32; grup_fant[3].y = -32
                    n = lbolag.index(matriu[pers.casella.x - 1][pers.casella.y].posicio)
                    grup_bolaG[n].x = -16; grup_bolaG[n].y = -16
                    matriu[pers.casella.x - 1][pers.casella.y].bola = 0
                pers.casella = matriu[pers.casella.x - 1][pers.casella.y]
            elif boto == ugame.K_RIGHT and matriu[pers.casella.x + 1][pers.casella.y].tipus:
                if estatAnt == 'Left':
                    grup_pers[2].x = -16; grup_pers[2].y = -16
                elif estatAnt == 'Up':
                    grup_pers[3].x = -16; grup_pers[3].y = -16
                elif estatAnt == 'Down':
                    grup_pers[1].x = -16; grup_pers[1].y = -16
                estatAnt = 'Right'
                pers.grup = grup_pers[0]
                if matriu[pers.casella.x + 1][pers.casella.y].bola == 1:
                    bolasF += 1
                    n = lcoord.index(matriu[pers.casella.x + 1][pers.casella.y].posicio)
                    grup_bola[n].x = -16; grup_bola[n].y = -16
                    matriu[pers.casella.x + 1][pers.casella.y].bola = 0
                if matriu[pers.casella.x + 1][pers.casella.y].bola == 2:
                    bolasF += 1
                    contadorA = 1
                    fantVerm.grup = grup_fant[4]; fantBBlau.grup = grup_fant[5]; fantLila.grup = grup_fant[6]; fantRosa.grup = grup_fant[7]
                    grup_fant[0].x = -32; grup_fant[0].y = -32
                    grup_fant[1].x = -32; grup_fant[1].y = -32
                    grup_fant[2].x = -32; grup_fant[2].y = -32
                    grup_fant[3].x = -32; grup_fant[3].y = -32
                    n = lbolag.index(matriu[pers.casella.x + 1][pers.casella.y].posicio)
                    grup_bolaG[n].x = -16; grup_bolaG[n].y = -16
                    matriu[pers.casella.x + 1][pers.casella.y].bola = 0
                pers.casella = matriu[pers.casella.x + 1][pers.casella.y]
                if pers.casella.x == 11:
                    pers.casella.x = 0
            elif boto == ugame.K_UP and matriu[pers.casella.x][pers.casella.y - 1].tipus:
                if estatAnt == 'Right':
                    grup_pers[0].x = -16; grup_pers[0].y = -16
                elif estatAnt == 'Left':
                    grup_pers[2].x = -16; grup_pers[2].y = -16
                elif estatAnt == 'Down':
                    grup_pers[1].x = -16; grup_pers[1].y = -16
                estatAnt = 'Up'
                pers.grup = grup_pers[3]
                if matriu[pers.casella.x][pers.casella.y - 1].bola == 1:
                    bolasF += 1
                    n = lcoord.index(matriu[pers.casella.x][pers.casella.y - 1].posicio)
                    grup_bola[n].x = -16; grup_bola[n].y = -16
                    matriu[pers.casella.x][pers.casella.y - 1].bola = 0
                if matriu[pers.casella.x][pers.casella.y - 1].bola == 2:
                    bolasF += 1
                    contadorA = 1
                    fantVerm.grup = grup_fant[4]; fantBBlau.grup = grup_fant[5]; fantLila.grup = grup_fant[6]; fantRosa.grup = grup_fant[7]
                    grup_fant[0].x = -32; grup_fant[0].y = -32
                    grup_fant[1].x = -32; grup_fant[1].y = -32
                    grup_fant[2].x = -32; grup_fant[2].y = -32
                    grup_fant[3].x = -32; grup_fant[3].y = -32
                    n = lbolag.index(matriu[pers.casella.x][pers.casella.y - 1].posicio)
                    grup_bolaG[n].x = -16; grup_bolaG[n].y = -16
                    matriu[pers.casella.x][pers.casella.y - 1].bola = 0
                pers.casella = matriu[pers.casella.x][pers.casella.y - 1]
            elif boto == ugame.K_DOWN and matriu[pers.casella.x][pers.casella.y + 1].tipus:
                if estatAnt == 'Right':
                    grup_pers[0].x = -16; grup_pers[0].y = -16
                elif estatAnt == 'Up':
                    grup_pers[3].x = -16; grup_pers[3].y = -16
                elif estatAnt == 'Left':
                    grup_pers[2].x = -16; grup_pers[2].y = -16
                estatAnt = 'Down'
                pers.grup = grup_pers[1]
                if matriu[pers.casella.x][pers.casella.y + 1].bola == 1:
                    bolasF += 1
                    n = lcoord.index(matriu[pers.casella.x][pers.casella.y + 1].posicio)
                    grup_bola[n].x = -16; grup_bola[n].y = -16
                    matriu[pers.casella.x][pers.casella.y + 1].bola = 0
                if matriu[pers.casella.x][pers.casella.y + 1].bola == 2:
                    bolasF += 1
                    contadorA = 1
                    fantVerm.grup = grup_fant[4]; fantBBlau.grup = grup_fant[5]; fantLila.grup = grup_fant[6]; fantRosa.grup = grup_fant[7]
                    grup_fant[0].x = -32; grup_fant[0].y = -32
                    grup_fant[1].x = -32; grup_fant[1].y = -32
                    grup_fant[2].x = -32; grup_fant[2].y = -32
                    grup_fant[3].x = -32; grup_fant[3].y = -32
                    n = lbolag.index(matriu[pers.casella.x][pers.casella.y + 1].posicio)
                    grup_bolaG[n].x = -16; grup_bolaG[n].y = -16
                    matriu[pers.casella.x][pers.casella.y + 1].bola = 0
                pers.casella = matriu[pers.casella.x][pers.casella.y + 1]
            #else:
                #print(pers.casella.posicio)
                #print(pers.casella.bola)
            iniciP = time.monotonic()
            pers.pos = pers.casella.posicio
            if matriu[pers.casella.x][pers.casella.y].sensefantasma == False:
                if contadorA == 0:
                    estat = 'Inici'
                    iniciI = time.monotonic()
                    vidas -= 1
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
        if(time.monotonic() > iniciF + velocitat):
    #VERMELL
            anteriorV = randV
            llposvalV.append(anteriorV)
            if comenV == 3:
                while smV == 0 and tcf < 30:
                    randV = random.choice(llposvalV)
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    if randV == 0 and matriu[fantVerm.casella.x][fantVerm.casella.y + 1].tipus and matriu[fantVerm.casella.x][fantVerm.casella.y + 1].sensefantasma:
                        matriu[fantVerm.casella.x][fantVerm.casella.y + 1].sensefantasma = False
                        fantVerm.casella = matriu[fantVerm.casella.x][fantVerm.casella.y + 1]
                        smV = 1
                    elif randV == 1 and matriu[fantVerm.casella.x][fantVerm.casella.y - 1].tipus and matriu[fantVerm.casella.x][fantVerm.casella.y - 1].sensefantasma:
                        matriu[fantVerm.casella.x][fantVerm.casella.y - 1].sensefantasma = False
                        fantVerm.casella = matriu[fantVerm.casella.x][fantVerm.casella.y - 1]
                        smV = 1
                    elif randV == 2:
                        if fantVerm.casella.x == 11:
                            fantVerm.casella.x = 0
                        elif matriu[fantVerm.casella.x + 1][fantVerm.casella.y].tipus and matriu[fantVerm.casella.x + 1][fantVerm.casella.y].sensefantasma:
                            matriu[fantVerm.casella.x + 1][fantVerm.casella.y].sensefantasma = False
                            fantVerm.casella = matriu[fantVerm.casella.x + 1][fantVerm.casella.y]
                            smV = 1
                    elif randV == 3 and matriu[fantVerm.casella.x - 1][fantVerm.casella.y].tipus and matriu[fantVerm.casella.x - 1][fantVerm.casella.y].sensefantasma:
                        matriu[fantVerm.casella.x - 1][fantVerm.casella.y].sensefantasma = False
                        fantVerm.casella = matriu[fantVerm.casella.x - 1][fantVerm.casella.y]
                        smV = 1
                    tcf += 1
                smV = 0
                tcf = 0
            else:
                if comenV < 1:
                    comenV += 1
                else:
                    fantVerm.casella = matriu[fantVerm.casella.x][fantVerm.casella.y - 1]
                    comenV += 1
    #BBLAU
            anteriorB = randB
            llposvalB.append(anteriorB)
            if comenB == 6:
                while smB == 0 and tcf < 30:
                    randB = random.choice(llposvalB)
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    if randB == 0 and matriu[fantBBlau.casella.x][fantBBlau.casella.y + 1].tipus and matriu[fantBBlau.casella.x][fantBBlau.casella.y + 1].sensefantasma:
                        matriu[fantBBlau.casella.x][fantBBlau.casella.y + 1].sensefantasma = False
                        fantBBlau.casella = matriu[fantBBlau.casella.x][fantBBlau.casella.y + 1]
                        smB = 1
                    elif randB == 1 and matriu[fantBBlau.casella.x][fantBBlau.casella.y - 1].tipus and matriu[fantBBlau.casella.x][fantBBlau.casella.y - 1].sensefantasma:
                        matriu[fantBBlau.casella.x][fantBBlau.casella.y - 1].sensefantasma = False
                        fantBBlau.casella = matriu[fantBBlau.casella.x][fantBBlau.casella.y - 1]
                        smB = 1
                    elif randB == 2:
                        if fantBBlau.casella.x == 11:
                            fantBBlau.casella.x = 0
                        elif matriu[fantBBlau.casella.x + 1][fantBBlau.casella.y].tipus and matriu[fantBBlau.casella.x + 1][fantBBlau.casella.y].sensefantasma:
                            matriu[fantBBlau.casella.x + 1][fantBBlau.casella.y].sensefantasma = False
                            fantBBlau.casella = matriu[fantBBlau.casella.x + 1][fantBBlau.casella.y]
                            smB = 1
                    elif randB == 3 and matriu[fantBBlau.casella.x - 1][fantBBlau.casella.y].tipus and matriu[fantBBlau.casella.x - 1][fantBBlau.casella.y].sensefantasma:
                        matriu[fantBBlau.casella.x - 1][fantBBlau.casella.y].sensefantasma = False
                        fantBBlau.casella = matriu[fantBBlau.casella.x - 1][fantBBlau.casella.y]
                        smB = 1
                    tcf += 1
                smB = 0
                tcf = 0
            else:
                if comenB < 5:
                    comenB += 1
                else:
                    fantBBlau.casella = matriu[fantBBlau.casella.x][fantBBlau.casella.y - 1]
                    comenB += 1
    #Lila
            anteriorL = randL
            llposvalL.append(anteriorL)
            if comenL == 10:
                while smL == 0 and tcf < 30:
                    randL = random.choice(llposvalL)
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    if randL == 0 and matriu[fantLila.casella.x][fantLila.casella.y + 1].tipus and matriu[fantLila.casella.x][fantLila.casella.y + 1].sensefantasma:
                        matriu[fantLila.casella.x][fantLila.casella.y + 1].sensefantasma = False
                        fantLila.casella = matriu[fantLila.casella.x][fantLila.casella.y + 1]
                        smL = 1
                    elif randL == 1 and matriu[fantLila.casella.x][fantLila.casella.y - 1].tipus and matriu[fantLila.casella.x][fantLila.casella.y - 1].sensefantasma:
                        matriu[fantLila.casella.x][fantLila.casella.y - 1].sensefantasma = False
                        fantLila.casella = matriu[fantLila.casella.x][fantLila.casella.y - 1]
                        smL = 1
                    elif randL == 2:
                        if fantLila.casella.x == 11:
                            fantLila.casella.x = 0
                        elif matriu[fantLila.casella.x + 1][fantLila.casella.y].tipus and matriu[fantLila.casella.x + 1][fantLila.casella.y].sensefantasma:
                            matriu[fantLila.casella.x + 1][fantLila.casella.y].sensefantasma = False
                            fantLila.casella = matriu[fantLila.casella.x + 1][fantLila.casella.y]
                            smL = 1
                    elif randL == 3 and matriu[fantLila.casella.x - 1][fantLila.casella.y].tipus and matriu[fantLila.casella.x - 1][fantLila.casella.y].sensefantasma:
                        matriu[fantLila.casella.x - 1][fantLila.casella.y].sensefantasma = False
                        fantLila.casella = matriu[fantLila.casella.x - 1][fantLila.casella.y]
                        smL = 1
                    tcf += 1
                smL = 0
                tcf = 0
            else:
                if comenL < 8:
                    comenL += 1
                else:
                    fantLila.casella = matriu[fantLila.casella.x][fantLila.casella.y - 1]
                    comenL += 1
    #Rosa
            anteriorR = randR
            llposvalR.append(anteriorR)
            if comenR == 14:
                while smR == 0 and tcf < 30:
                    randR = random.choice(llposvalR)
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
                    if randR == 0 and matriu[fantRosa.casella.x][fantRosa.casella.y + 1].tipus and matriu[fantRosa.casella.x][fantRosa.casella.y + 1].sensefantasma:
                        matriu[fantRosa.casella.x][fantRosa.casella.y + 1].sensefantasma = False
                        fantRosa.casella = matriu[fantRosa.casella.x][fantRosa.casella.y + 1]
                        smR = 1
                    elif randR == 1 and matriu[fantRosa.casella.x][fantRosa.casella.y - 1].tipus and matriu[fantRosa.casella.x][fantRosa.casella.y - 1].sensefantasma:
                        matriu[fantRosa.casella.x][fantRosa.casella.y - 1].sensefantasma = False
                        fantRosa.casella = matriu[fantRosa.casella.x][fantRosa.casella.y - 1]
                        smR = 1
                    elif randR == 2:
                        if fantRosa.casella.x == 11:
                            fantRosa.casella.x = 0
                        elif matriu[fantRosa.casella.x + 1][fantRosa.casella.y].tipus and matriu[fantRosa.casella.x + 1][fantRosa.casella.y].sensefantasma:
                            matriu[fantRosa.casella.x + 1][fantRosa.casella.y].sensefantasma = False
                            fantRosa.casella = matriu[fantRosa.casella.x + 1][fantRosa.casella.y]
                            smR = 1
                    elif randR == 3 and matriu[fantRosa.casella.x - 1][fantRosa.casella.y].tipus and matriu[fantRosa.casella.x - 1][fantRosa.casella.y].sensefantasma:
                        matriu[fantRosa.casella.x - 1][fantRosa.casella.y].sensefantasma = False
                        fantRosa.casella = matriu[fantRosa.casella.x - 1][fantRosa.casella.y]
                        smR = 1
                    tcf += 1
                smR = 0
                tcf = 0
            else:
                if comenR < 10:
                    comenR += 1
                else:
                    fantRosa.casella = matriu[fantRosa.casella.x][fantRosa.casella.y - 1]
                    comenR += 1
            if (fantVerm.casella.x == pers.casella.x) and (fantVerm.casella.y == pers.casella.y):
                if contadorA == 0:
                    estat = 'Inici'
                    iniciI = time.monotonic()
                    vidas -= 1
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
                else:
                    fantVerm.casella = matriu[4][5]
                    comenV = 0
                    fantVerm.grup = grup_fant[0]
                    grup_fant[4].x = -32; grup_fant[4].y = -32
            if (fantBBlau.casella.x == pers.casella.x) and (fantBBlau.casella.y == pers.casella.y):
                if contadorA == 0:
                    estat = 'Inici'
                    iniciI = time.monotonic()
                    vidas -= 1
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
                else:
                    fantBBlau.casella = matriu[5][4]
                    comenB = 4
                    fantBBlau.grup = grup_fant[1]
                    grup_fant[5].x = -32; grup_fant[5].y = -32
            if (fantLila.casella.x == pers.casella.x) and (fantLila.casella.y == pers.casella.y):
                if contadorA == 0:
                    estat = 'Inici'
                    iniciI = time.monotonic()
                    vidas -= 1
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
                else:
                    fantLila.casella = matriu[6][5]
                    comenL = 7
                    fantLila.grup = grup_fant[2]
                    grup_fant[6].x = -32; grup_fant[6].y = -32
            if (fantRosa.casella.x == pers.casella.x) and (fantRosa.casella.y == pers.casella.y):
                if contadorA == 0:
                    estat = 'Inici'
                    iniciI = time.monotonic()
                    vidas -= 1
                    matriu[fantVerm.casella.x][fantVerm.casella.y].sensefantasma = True
                    matriu[fantBBlau.casella.x][fantBBlau.casella.y].sensefantasma = True
                    matriu[fantLila.casella.x][fantLila.casella.y].sensefantasma = True
                    matriu[fantRosa.casella.x][fantRosa.casella.y].sensefantasma = True
                else:
                    fantRosa.casella = matriu[7][4]
                    comenR = 9
                    fantRosa.grup = grup_fant[3]
                    grup_fant[7].x = -32; grup_fant[7].y = -32
            fantVerm.pos = fantVerm.casella.posicio; fantBBlau.pos = fantBBlau.casella.posicio; fantLila.pos = fantLila.casella.posicio; fantRosa.pos = fantRosa.casella.posicio
            iniciF = time.monotonic()
            if contadorA > 0:
                contadorA+= 1
                if contadorA > 8:
                    contadorA = 0
                    fantVerm.grup = grup_fant[0]; fantBBlau.grup = grup_fant[1]; fantLila.grup = grup_fant[2]; fantRosa.grup = grup_fant[3]
                    grup_fant[4].x = -32; grup_fant[4].y = -32
                    grup_fant[5].x = -32; grup_fant[5].y = -32
                    grup_fant[6].x = -32; grup_fant[6].y = -32
                    grup_fant[7].x = -32; grup_fant[7].y = -32
    if estat == 'Gameover':
        grup[5].x = 15; grup[5].y = 30
        pant.show(grup)
        vidas = 3
        if(time.monotonic() > tempsM + 5):
            estat = 'Inici'
    if estat == 'Winner':
        grup[6].x = 15; grup[6].y = 30
        pant.show(grup)
        vidas = 3
        if(time.monotonic() > tempsM + 5):
            estat = 'Inici'
    pass
while True:
    pass

 

 

 

 

 

 

 

 

 

 

Licencia de Creative Commons
Esta obra de Oriol Boix está licenciada bajo una licencia no importada Creative Commons Reconocimiento-NoComercial-SinObraDerivada 3.0.