Creació de jocs amb PyBadge

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

Quatre en línia

El Quatre en línia és un joc d'estratègia en el qual participen dos jugadors. Es juga sobre un tauler vertical de sis files i set columnes. Cada jugador té fitxes d'un color i el seu principal objectiu és alinear quatre de les seves fitxes; ja sigui en vertical, en horitzontal o en diagonal, abans que l'altre jugador. Els jugadors posen, per torns, una de les seves fitxes en una de les columnes, la qual caurà a la filera més baixa disponible d'aquella columna. El joc acaba quan un jugador aconsegueix alinear quatre fitxes o si el tauler s'omple sense que cap jugador guanyi (que es considera un empat).

Les imatges que fa servir el joc són les que comentem tot seguit. La primera correspon al títol del joc, la segona al botó d'inici i les següents són indicacions per al joc.

títol       inici       Indicacions       Indicacions

D'altra banda, tenim la que conté els modes de joc, la que té les caselles buides i les fitxes del joc i, per acabar, la que té els elements per al marcador.

Modes de joc       fitxes       marcador

El programa també fa servir dos fitxers de so, un per al dispar, pew.wav, i un per quan guanya un jugador, victoria.wav.

El programa és el següent:

import ugame
import board
import displayio
import terminalio
import adafruit_imageload
from adafruit_display_text import label
import time
import random
import neopixel
# Creem un objecte per a la pantalla
pant = board.DISPLAY
font = terminalio.FONT
#sonido
victory_sound = open("victoria.wav", 'rb')
ficha_sound = open("pew.wav", 'rb')
sound = ugame.audio
sound.mute(False)
#llum
llum_led = neopixel.NeoPixel(board.NEOPIXEL, 5, brightness=0.1)
imatge, palette = adafruit_imageload.load("/prueba.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge2, palette2 = adafruit_imageload.load("/marcador.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge3, palette3 = adafruit_imageload.load("/start.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge4, palette4 = adafruit_imageload.load("/4.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge5, palette5 = adafruit_imageload.load("/modo_juego1.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge6, palette6 = adafruit_imageload.load("/indicacions.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
imatge7, palette7 = adafruit_imageload.load("/indicacions2.bmp",
                                                bitmap=displayio.Bitmap,
                                                palette=displayio.Palette)
# Creem l'sprite (ficha)
pers = displayio.TileGrid(imatge, pixel_shader=palette,
                            width = 1, height = 1,
                            tile_width = 18, tile_height = 18)
# Creem un mosaic per al taulell
taulell = displayio.TileGrid(imatge, pixel_shader=palette,
                            width = 7, height = 6,
                            tile_width = 18, tile_height = 18)
#Creem el sprite (start)
start = displayio.TileGrid(imatge3, pixel_shader=palette3,
                            width=1, height=1,
                            tile_width = 132, tile_height = 38)
#Creem el sprite (4 en raya)
enraya = displayio.TileGrid(imatge4, pixel_shader=palette4,
                            width=1, height=1,
                            tile_width = 110, tile_height = 50)
# Creem un sprite per al marcador
marcador = displayio.TileGrid(imatge2, pixel_shader=palette2,
                            width = 1, height = 8,
                            tile_width = 16, tile_height = 16)
#Sprites per escollir jugadors
jug1 = displayio.TileGrid(imatge, pixel_shader=palette,
                            width = 1, height = 1,
                            tile_width = 18, tile_height = 18)
jug2 = displayio.TileGrid(imatge, pixel_shader=palette,
                            width = 1, height = 1,
                            tile_width = 18, tile_height = 18)
#Creem el sprite indicacions
indicacions = displayio.TileGrid(imatge6, pixel_shader=palette6,
                            width=1, height=1,
                            tile_width = 160, tile_height = 30)
#Creem el sprite indicacions2
indicacions2 = displayio.TileGrid(imatge7, pixel_shader=palette7,
                            width=1, height=1,
                            tile_width = 160, tile_height = 20)
# Creem un sprite per al modo de juego
modo_juego1 = displayio.TileGrid(imatge5, pixel_shader=palette5,
                            width = 1, height = 1,
                            tile_width = 140, tile_height = 25)
modo_juego2 = displayio.TileGrid(imatge5, pixel_shader=palette5,
                            width = 1, height = 1,
                            tile_width = 140, tile_height = 25)
flecha = displayio.TileGrid(imatge2, pixel_shader=palette2,
                            width = 1, height = 1,
                            tile_width = 16, tile_height = 16)
# Creem els grups
# Creem un grup per tot el menu
grup2 = displayio.Group()
# Afegim els sprites anteriors al global del menu
grup2.append(start)
grup2.append(enraya)
start.x=15
start.y=80
enraya.x=25
enraya.y=15
# Creem un grup per escollir modo de juego
grup_modojuego = displayio.Group()
# Afegim els sprites anteriors al global de la partida
grup_modojuego.append(modo_juego1)
grup_modojuego.append(modo_juego2)
grup_modojuego.append(flecha)
grup_modojuego.append(indicacions)
indicacions.x=60
indicacions.y=98
modo_juego1.x=20
modo_juego1.y=30
modo_juego2.x=20
modo_juego2.y=69
flecha[0]=6
#Creem un grup per les indicacions
grup_indicacions=displayio.Group()
#Afegim el sprite
# Creem un grup per escollir colors
grup_jug1 = displayio.Group(scale=2)
grup_jug2 = displayio.Group(scale=2)
grup_jug1.append(jug1)
grup_jug2.append(jug2)
grup_esc = displayio.Group()
# Afegim els grups anteriors al global per escollir colors
grup_esc.append(grup_jug1)
grup_esc.append(grup_jug2)
grup_esc.append(indicacions2)
indicacions2.x=52
indicacions2.y=108
jug1.x=10
jug1.y=23
jug2.x=500
jug2.y=23
colors=[1,3,5,7]
# Creem un grup per tota la partida
grup = displayio.Group()
# Afegim els sprites anteriors al global de la partida
grup.append(pers)
grup.append(taulell)
taulell.x=0
taulell.y=20
grup.append(marcador)
marcador.y=0
marcador.x=160-16
palette.make_transparent(0)
palette2.make_transparent(0)
palette3.make_transparent(0)
palette4.make_transparent(0)
palette5.make_transparent(0)
palette6.make_transparent(0)
palette7.make_transparent(0)
def dib_marcador():
    for n  in range(0,8):
        marcador[n]= 7
    marcador[0]=4
    marcador[4]=5
def dib_taulell():
    # Dibuixem el taulell
    for x in range(0, 7):
        for y in range(0, 6):
            taulell[x, y] = 0
    # Mostra el grup a la pantalla
    pant.show(grup)
    #Crea la matriu que representara el taulell
    mat=[]
    for i in range(6):
        f=[]
        for j in range(7):
            f.append(0)
        mat.append(f)
    return mat
#Retorna la matriiu modificada i el valor de y en la matriu i taule on s'ha colocat la peça
def nou_taulell(ex):
    for ie in range(0,6):
        if mat_taulell[ie][ex]==0:
            n=ie
    return n
#Comproba si s'ha guanyat
def comproba():
    comprobante=[0,0,0,0,0]
    for opcionesy in range(0,3): ##las diagonales
        for opcionesx in range(0,4):
            for casillas in range(0,4):
                comprobante[2]+=mat_taulell[5-opcionesy-casillas][opcionesx+casillas]
                comprobante[3]+=mat_taulell[opcionesy+casillas][opcionesx+casillas]
            if comprobante[2]==4 or comprobante[3]==4:
                ganador[0]+=1
                return (2,comprobante)
            if comprobante[2]==-4 or comprobante[3]==-4:
                ganador[1]+=1
                return (3,comprobante)
            comprobante=[0,0,0,0,0]
    for opcionesy in range(0,6): #horizontales
        for opcionesx in range(0,4):
            for casillas in range(0,4):
                comprobante[0]+=mat_taulell[5-opcionesy][opcionesx+casillas]
            if comprobante[0]==4:
                ganador[0]+=1
                return (2,comprobante)
            if comprobante[0]==-4:
                ganador[1]+=1
                return (3,comprobante)
            comprobante=[0,0,0,0,0]
    for opcionesy in range(0,3): #verticales
        for opcionesx in range(0,7):
            for casillas in range(0,4):
                comprobante[1]+=mat_taulell[5-opcionesy-casillas][opcionesx]
            if comprobante[1]==4:
                ganador[0]+=1
                return (2,comprobante)
            if comprobante[1]==-4:
                ganador[1]+=1
                return (3,comprobante)
            comprobante=[0,0,0,0,0]
    empate=0
    for col in range(0,7):
        if mat_taulell[0][col]!=0:
            empate+=1
        if empate==7:
            return (4,comprobante)
    return (0,comprobante)
def pos_maquina():
    #mira donde ganar
    comprobante=[0,0,0,0]
    for opcionesy in range(0,3): ##las diagonales
        for opcionesx in range(0,4):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #diagonales arriba
                comprobante[0]+=mat_taulell[5-opcionesy-casillas][opcionesx+casillas]
                if mat_taulell[5-opcionesy-casillas][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=5-opcionesy-casillas
                    comprobante[3]=1
            if comprobante[0]==-3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #diagonales abajo
                comprobante[0]+=mat_taulell[0+opcionesy+casillas][opcionesx+casillas]
                if mat_taulell[0+opcionesy+casillas][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=0+opcionesy+casillas
                    comprobante[3]=1
            if comprobante[0]==-3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    for opcionesy in range(0,6): #horizontales
        for opcionesx in range(0,4):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #1
                comprobante[0]+=mat_taulell[5-opcionesy][opcionesx+casillas]
                if mat_taulell[5-opcionesy][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=5-opcionesy
                    comprobante[3]=1
            if comprobante[0]==-3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    for opcionesy in range(0,3): #verticales
        for opcionesx in range(0,7):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #3
                comprobante[0]+=mat_taulell[5-opcionesy-casillas][opcionesx]
                if mat_taulell[5-opcionesy-casillas][opcionesx]==0:
                    comprobante[1]=opcionesx
                    comprobante[2]=5-opcionesy-casillas
                    comprobante[3]=1
            if comprobante[0]==-3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    ##mira donde bloquearte
    comprobante=[0,0,0,0]
    for opcionesy in range(0,3): ##las diagonales
        for opcionesx in range(0,4):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #diagonales arriba
                comprobante[0]+=mat_taulell[5-opcionesy-casillas][opcionesx+casillas]
                if mat_taulell[5-opcionesy-casillas][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=5-opcionesy-casillas
                    comprobante[3]=1
            if comprobante[0]==3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #diagonales abajo
                comprobante[0]+=mat_taulell[0+opcionesy+casillas][opcionesx+casillas]
                if mat_taulell[0+opcionesy+casillas][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=0+opcionesy+casillas
                    comprobante[3]=1
            if comprobante[0]==3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    for opcionesy in range(0,6): #horizontales
        for opcionesx in range(0,4):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #1
                comprobante[0]+=mat_taulell[5-opcionesy][opcionesx+casillas]
                if mat_taulell[5-opcionesy][opcionesx+casillas]==0:
                    comprobante[1]=opcionesx+casillas
                    comprobante[2]=5-opcionesy
                    comprobante[3]=1
            if comprobante[0]==3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    for opcionesy in range(0,3): #verticales
        for opcionesx in range(0,7):
            comprobante=[0,0,0,0]
            for casillas in range(0,4): #3
                comprobante[0]+=mat_taulell[5-opcionesy-casillas][opcionesx]
                if mat_taulell[5-opcionesy-casillas][opcionesx]==0:
                    comprobante[1]=opcionesx
                    comprobante[2]=5-opcionesy-casillas
                    comprobante[3]=1
            if comprobante[0]==3 and comprobante[3]==1:
                if comprobante[2]==5:
                    return comprobante[1]
                elif mat_taulell[comprobante[2]+1][comprobante[1]]!=0:
                    return comprobante[1]
    return random.randint(0,6)
def get_color_led(c):
    if c==1:
        return (0,0,255)
    elif c==3:
        return (255,0,0)
    elif c==5:
        return (0,255,0)
    elif c==7:
        return (255,255,0)
#Inicialitza les variables del programa
abans=0
partida=-1
estat=1
inici=0
req_text = label.Label(font, text="Projecte 1 - GETI - UPC", color=0x99ccff)
req_text.x = 0
req_text.y = 5
jugador=6 #jugador=3 vermell i jugador=4 groc
pers[0]=jugador
ex=0
ey=0
col1=0
jug1[0]=colors[col1]
col2=4
jug2[0]=0
mat_taulell=0
juga_maquina=False
modo_facil=False
while True:
    while inici==-1:#pantalla menu
        pant.show(req_text)
        boto = ugame.buttons.get_pressed()
        if not abans & ugame.K_START and boto & ugame.K_START or not abans & ugame.K_SELECT and boto & ugame.K_SELECT:
            inici=0
        abans = boto
    while inici==0:#pantalla menu
        dib_marcador()
        partida=-1
        ganador=[0,0]
        jug2.x=500
        jug2.y=23
        col1=0
        jug1[0]=colors[col1]
        col2=4
        jug2[0]=0
        pant.show(grup2)
        boto = ugame.buttons.get_pressed()
        if not abans & ugame.K_START and boto & ugame.K_START:
            inici=1
            pos_flecha=0
            flecha.x=5
            flecha.y=34
        if not abans & ugame.K_SELECT and boto & ugame.K_SELECT:
            inici=-1
            pos_flecha=0
        abans = boto
    while inici==1: #modo one player o two players
        modo_juego1[0]=0
        modo_juego2[0]=1
        pant.show(grup_modojuego)
        boto = ugame.buttons.get_pressed()
        if not abans & ugame.K_O and boto & ugame.K_O:
            inici=2
            if pos_flecha==0:
                juga_maquina=True
            else:
                juga_maquina=False
                inici=3
            flecha.y=34
        if not abans & ugame.K_UP and boto & ugame.K_UP and pos_flecha==1:
            pos_flecha-=1
            flecha.y=34
        if not abans & ugame.K_DOWN and boto & ugame.K_DOWN and pos_flecha==0:
            pos_flecha+=1
            flecha.y=73
        abans = boto
    while inici==2: # modo easy o hard
        modo_juego1[0]=2
        modo_juego2[0]=3
        pant.show(grup_modojuego)
        boto = ugame.buttons.get_pressed()
        if not abans & ugame.K_O and boto & ugame.K_O:
            inici=3
            if pos_flecha==0:
                modo_facil=True
            else:
                modo_facil=False
            flecha.y=34
        if not abans & ugame.K_UP and boto & ugame.K_UP and pos_flecha==1:
            pos_flecha-=1
            flecha.y=34
        if not abans & ugame.K_DOWN and boto & ugame.K_DOWN and pos_flecha==0:
            pos_flecha+=1
            flecha.y=73
        abans = boto
    while inici==3: #colores
        pant.show(grup_esc)
        boto = ugame.buttons.get_pressed()
        if not abans & ugame.K_X and boto & ugame.K_X:
            col1+=1
            if col1==col2:
                col1+=1
            if col1>3:
                col1=0
                if col1==col2:
                    col1+=1
            jug1[0]=colors[col1]
        if not abans & ugame.K_O and boto & ugame.K_O:
            jug2.x=50
            jug2.y=23
            col2+=1
            if col1==col2:
                col2+=1
            if col2>3:
                col2=0
                if col1==col2:
                    col2+=1
            jug2[0]=colors[col2]
        if not abans & ugame.K_START and boto & ugame.K_START and col2!=4:
            inici=0
            jugador=colors[col1]
            pers[0]=jugador
            partida=0
        abans = boto
    mat_taulell=dib_taulell()
    while partida==0:
        estat=1
        while estat==1:
            #si le toca a la maquina
            if jugador==colors[col2] and juga_maquina:
                if modo_facil:
                    ex= random.randint(0,6)
                else:
                    ex=pos_maquina()
                pers.x = ex*18
            #Jugador li toca colocar
            boto = ugame.buttons.get_pressed()
            if not abans & ugame.K_RIGHT and boto & ugame.K_RIGHT and pers.x<108:
                ex += 1
                pers.x = ex*18
            if not abans & ugame.K_LEFT and boto & ugame.K_LEFT and pers.x>0:
                ex -=1
                pers.x = ex*18
            if not abans & ugame.K_START and boto & ugame.K_START:
                partida=-1
                estat=0
            if not abans & ugame.K_O and boto & ugame.K_O and mat_taulell[0][ex]==0 or mat_taulell[0][ex]==0 and jugador==colors[col2] and juga_maquina: #or jugando contra la maquina and pos maquina libre
                #canvia la matriu del taulell
                n=nou_taulell(ex)
                #while per l'animació de baixar la peça
                while pers.y<=110-(5-n)*18:
                    pers.y +=1
                    time.sleep(0.01)
                sound.play(ficha_sound)
                #Recoloca la fitxa
                ey=0
                pers.y=ey*18
                #Canvi de jugador i actualiza la pantall
                if jugador==colors[col1]: #jugador 3 vermell i jugador 4 groc
                    jugador=colors[col2]
                    pers[0]=jugador
                    taulell[ex,n]=colors[col1]+1
                    mat_taulell[n][ex]=1
                else:
                    jugador=colors[col1]
                    pers[0]=jugador
                    taulell[ex,n]=colors[col2]+1
                    mat_taulell[n][ex]=-1
                #Comproba si la partida ha finalitzat
                estat,com=comproba()
            abans = boto
        if estat==2 and ganador[0]!=3: #ha guanyat el jugador1
            print(com)
            print(mat_taulell)
            for y in range(0, 6):
                for x in range(0, 7):
                    taulell[x, y] = colors[col1]+1
            llum_led[0]=get_color_led(colors[col1])
            sound.play(victory_sound)
            time.sleep(2)
            print([ganador[0],col1])
            marcador[ganador[0]]=col1
            estat=1
            mat_taulell=dib_taulell()
        if estat==3 and ganador[1]!=3: #ha guanyat el jugador2
            print(com)
            print(mat_taulell)
            for y in range(0, 6):
                for x in range(0, 7):
                    taulell[x, y] = colors[col2]+1
            llum_led[0]=get_color_led(colors[col2])
            sound.play(victory_sound)
            time.sleep(2)
            marcador[ganador[1]+4]=col2
            estat=1
            mat_taulell=dib_taulell()
        if estat==4: #empat
            estat=1
            mat_taulell=dib_taulell()
            time.sleep(2)
        if ganador[0]==3:
            for y in range(0, 6):
                for x in range(0, 7):
                    taulell[x, y] = colors[col1]+1
                    time.sleep(0.1)
            marcador[ganador[0]]=col1
            sound.play(victory_sound)
            for i in range(20):
                llum_led[0]=get_color_led(colors[col1])
                time.sleep(0.25)
                llum_led[0]=(0,0,0)
                time.sleep(0.25)
            partida=-1
            inici=-1
        if ganador[1]==3:
            for y in range(0, 6):
                for x in range(0, 7):
                    taulell[x, y] = colors[col2]+1
                    time.sleep(0.1)
            marcador[ganador[1]+4]=col2
            sound.play(victory_sound)
            for i in range(20):
                llum_led[0]=get_color_led(colors[col2])
                time.sleep(0.25)
                llum_led[0]=(0,0,0)
                time.sleep(0.25)
            partida=-1
            inici=-1
        llum_led[0]=(0,0,0)

 

 

 

 

 

 

 

 

 

 

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