Creació de jocs amb PyBadge

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

Mata-marcians

En aquest joc tenim un personatge que va corrent i ha de saltar o ajupir-se per evitar obstacles. Hi ha dos nivells, que corresponen a dues dificultats diferents.

Les imatges que fa servir el joc són les que comentem tot seguit. La primera mostra el fons del joc, la segona la pantalla d'inici i la tercera la pantalla que surt a l'acabar la partida. També tenim la pantalla de pausa i la que permet triar la modalitat de joc.

espai       inici       final

pausa       menú

D'altra banda, tenim els elements petits; com la nau de l'usuari, l'explosió, la bala, les naus enemigues.

nau 0       explosió       bala       nau 1

El programa també fa servir quatre fitxers de so, un per a l'inici, inici.wav, un per a l'explosió, boom.wav, un per al dispar, pew.wav i un per al final, gameover.wav.

El programa és el següent:

import board
import displayio
import adafruit_imageload
import time
import ugame
import terminalio
import random
import adafruit_display_shapes
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
sorollinici = open("inici.wav", 'rb')
sorollgameover = open("gameover.wav", 'rb')
sorollexplosio = open("boom.wav", 'rb')
sorolldispar = open("pew.wav", 'rb')
sound = ugame.audio
sound.mute(False)
# Creem un objecte per a la pantalla
pant = board.DISPLAY
# Carreguem el fitxer amb la imatge
imexp, pal4 = adafruit_imageload.load("/explosio.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette) #imagen explosión
imnau, pal2 = adafruit_imageload.load("/dolent1.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)#imagen nave
imnaubona, pal3 = adafruit_imageload.load("/naubona.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
imespai,pal5=adafruit_imageload.load("/espai.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette) #imagen fondo
imbala,pal6=adafruit_imageload.load("/bala.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
iminici,pal7=adafruit_imageload.load("/inici.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
imgameover,pal8=adafruit_imageload.load("/gameover.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
impausa,pal9=adafruit_imageload.load("/pausa.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
immodojuego,pal10=adafruit_imageload.load("/modojuego.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette)
# Creem l'sprites/Mosaic
fondo= displayio.TileGrid(imespai, pixel_shader=pal5,
                            width =1 , height = 1,
                            tile_width = 160, tile_height = 128)  #(tile_width y tile_height es el numero de píxels que tiene cada sprite)
inici= displayio.TileGrid(iminici, pixel_shader=pal7,
                            width =1 , height = 1,
                            tile_width = 160, tile_height = 128)       
modojuego= displayio.TileGrid(immodojuego, pixel_shader=pal10,
                            width =1 , height = 1,
                            tile_width = 160, tile_height = 128)
pausa= displayio.TileGrid(impausa, pixel_shader=pal9,
                            width =1 , height = 1,
                            tile_width = 160, tile_height = 128)
gameover= displayio.TileGrid(imgameover, pixel_shader=pal8,
                            width =1 , height = 1,
                            tile_width = 160, tile_height = 128)
naubona= displayio.TileGrid(imnaubona, pixel_shader=pal3,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
dolent1 = displayio.TileGrid(imnau, pixel_shader=pal2,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
dolent2 = displayio.TileGrid(imnau, pixel_shader=pal2,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
dolent3 = displayio.TileGrid(imnau, pixel_shader=pal2,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
dolent4 = displayio.TileGrid(imnau, pixel_shader=pal2,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
bala = displayio.TileGrid(imbala, pixel_shader=pal6,
                            width =1 , height = 1,
                            tile_width = 6, tile_height = 3)
bala2 = displayio.TileGrid(imbala, pixel_shader=pal6,
                            width =1 , height = 1,
                            tile_width = 6, tile_height = 3)
explosio = displayio.TileGrid(imexp, pixel_shader=pal4,
                            width =1 , height = 1,
                            tile_width = 25, tile_height = 15)
#crear los grupos
grup = displayio.Group()
grup_mapa = displayio.Group()
grup_mapa.append(fondo)
grup = displayio.Group()
grup_modojuego = displayio.Group()
grup_modojuego.append(modojuego)
grup = displayio.Group()
grup_pausa = displayio.Group()
grup_pausa.append(pausa)
grup = displayio.Group()
grup_inici = displayio.Group()
grup_inici.append(inici)
grup = displayio.Group()
grup_gameover = displayio.Group()
grup_gameover.append(gameover)
grup = displayio.Group()
grup_bala = displayio.Group()
grup_bala.append(bala)
(tx, grup_bala.x) = (1,-25)
(ty, grup_bala.y) = (1,64)
grup = displayio.Group()
grup_nave = displayio.Group()
grup_nave.append(naubona)
gruptotal = displayio.Group()
# Afegim els grups anteriors al global
gruptotal.append(grup_mapa)
(tx, grup_nave.x) = (1,1)
(ty, grup_nave.y) = (1,64)
gruptotal.append(grup_nave)
grup = displayio.Group()
grup_explosio = displayio.Group()
grup_explosio.append(explosio)
(tx, grup_explosio.x) = (1,-25)
(ty, grup_explosio.y) = (1,1)
grup = displayio.Group()
grup_dolent1 = displayio.Group()
grup_dolent1.append(dolent1)
posrand1 = random.randint(150,250)
(tx, grup_dolent1.x) = (1,posrand1)
(ty, grup_dolent1.y) = (1,1)
grup = displayio.Group()
grup_dolent2 = displayio.Group()
grup_dolent2.append(dolent2)
posrand2 = random.randint(150,250)
(tx, grup_dolent2.x) = (1,posrand2)
(ty, grup_dolent2.y) = (1,35)
grup = displayio.Group()
grup_dolent3 = displayio.Group()
grup_dolent3.append(dolent3)
posrand3 = random.randint(150,250)
(tx, grup_dolent3.x) = (1,posrand3)
(ty, grup_dolent3.y) = (1,70)
grup = displayio.Group()
grup_dolent4 = displayio.Group()
grup_dolent4.append(dolent4)
posrand4 = random.randint(150,250)
(tx, grup_dolent4.x) = (1,posrand4)
(ty, grup_dolent4.y) = (1,105)
gruptotal.append(grup_dolent1)
gruptotal.append(grup_dolent2)
gruptotal.append(grup_dolent3)
gruptotal.append(grup_dolent4)
gruptotal.append(grup_bala)
gruptotal.append(grup_explosio)
darrer_valor = time.monotonic()
velocitat = 0.05
jugador_perdio = False
c = False
gameover = False
inici = True
cont1 = 0
cont2 = 0
cont3 = 0
cont4 = 0
kills = 0
d = True
exp = 0
l = 1
cont = 0
soroll = 'on'
contsoroll = 0
z = 0
s = 1
recordjoc = 0
hardmode = False
a = 1
while True:
    buttons = ugame.buttons.get_pressed()   
    if buttons & ugame.K_X:
        soroll = 'off'
    if buttons & ugame.K_SELECT:
        s = 2
        while True:
            pant.show(grup_pausa)
            time.sleep(0.6)
            pant.show(gruptotal)
            time.sleep(0.6)
            buttons = ugame.buttons.get_pressed()
            if buttons & ugame.K_START:
                s=1
                time.sleep(1)
                pant.show(gruptotal)
                break
    exp += 1
    cont += 1
    jugador_perdio = False
    if inici == True:
        pant.show(grup_inici)
        if soroll == 'on':
            sound.play(sorollinici)
        while True:
            buttons = ugame.buttons.get_pressed()
            if buttons & ugame.K_START:
                time.sleep(1)
                while True:
                    pant.show(grup_modojuego)
                    buttons = ugame.buttons.get_pressed()
                    a = 2
                    if buttons & ugame.K_O:
                        hardmode = True
                        pant.show(gruptotal)
                        print('hardmode')
                        time.sleep(0.2)
                        break
                    if buttons & ugame.K_X:
                        hardmode = False
                        pant.show(gruptotal)
                        break
                        a = 2
                if a ==2:
                    break
    inici = False
    if exp > 65:
        explosio.x = explosio.x - 1700
    if dolent1.x < -posrand1-25 :
        jugador_perdio = True
    if dolent2.x < -posrand2-25 :
        jugador_perdio = True
    if dolent3.x < -posrand3-25 :
        jugador_perdio = True
    if dolent4.x < -posrand4-25 :
        jugador_perdio = True
    if dolent1.x + posrand1 - 25 <= naubona.x <= dolent1.x + posrand1 + 20:
        if -64 <= naubona.y <= -49:
            jugador_perdio = True
    if dolent2.x + posrand2 - 25 <= naubona.x <= dolent2.x + posrand2 + 20:
        if -42 <= naubona.y <= -16:
            jugador_perdio = True
    if dolent3.x + posrand3 - 25 <= naubona.x <= dolent3.x + posrand3 + 20:
        if -9 <= naubona.y <= 21:
            jugador_perdio = True
    if dolent4.x + posrand4 - 23 <= naubona.x <= dolent4.x + posrand4 + 20:
        if 27 <= naubona.y <= 52:
            jugador_perdio = True
    if jugador_perdio:
        if soroll == 'on':
            sound.play(sorollgameover)
        time.sleep(1)
        pant.show(grup_gameover)
        time.sleep(2)
        while True:
            if kills > recordjoc:
                recordjoc = kills
            font = bitmap_font.load_font("/font/ib8x16u.bdf")
            req_text = label.Label(font, text='SCORE:' + str(kills) + '\n' + 'TOP SCORE:' +  str(recordjoc) + '\n' + '\n' +   '    PRESS START', color=0xFFFF00)
            req_text.x = 5
            req_text.y = 20
            time.sleep(2)
            pant.show(req_text)
            buttons = ugame.buttons.get_pressed()
            if buttons & ugame.K_START:
                if soroll == 'on':
                    sound.play(sorollinici)
                pant.show(gruptotal)
                cont1 = 0
                cont2 = 0
                cont3 = 0
                cont4 = 0
                kills = 0
                naubona.x = 0
                naubona.y = 0
                dolent1.x = random.randrange(50,100,2)
                dolent2.x = random.randrange(50,100,2)
                dolent3.x = random.randrange(50,100,2)
                dolent4.x = random.randrange(50,100,2)
                break
    buttons = ugame.buttons.get_pressed()
    if c== True:
        bala.x += 2
    if  5 < bala.x  <150:
        d= False
        print
    if  bala.x  > 150 or bala.x == -25 :
        d = True
        l = 1
    if buttons & ugame.K_O  and d == True and l == 1 and cont > 20:
        bala.x = naubona.x + 52
        bala.y = naubona.y + 6
        time.sleep(0.01)
        c = True
        l = 0
        cont = 0
        if soroll == 'on':
            sound.play(sorolldispar)
    if  dolent1.x + posrand1 - 25 < bala.x - 52  <  dolent1.x + posrand1 - 15 and cont1 < 3:
        if -64 <= bala.y <= -49:
            bala.x = -25
            c = False
            explosio.x = dolent1.x + posrand1 + 27
            explosio.y = dolent1.y
            dolent1.x = random.randrange(50,100,2)
            cont1 +=1
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent1.x + posrand1 - 25 < bala.x - 52  <  dolent1.x + posrand1 - 15 and cont1 >=3:
        if -64 <= bala.y <= -49:
            bala.x = -25
            c = False
            explosio.x = dolent1.x + posrand1 + 27
            explosio.y = dolent1.y
            dolent1.x = random.randrange(-10,5,2)
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent2.x + posrand2 - 25 < bala.x - 52  <  dolent2.x + posrand2 - 15 and cont2 < 3:
        if -30 <= bala.y <= -16:
            bala.x = -25
            c = False
            explosio.x = dolent2.x + posrand2 + 27
            explosio.y = dolent2.y + 35
            dolent2.x = random.randrange(50,100,2)
            cont2 += 1
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent2.x + posrand2 - 25 < bala.x - 52  <  dolent2.x + posrand2 - 15 and cont2 >= 3:
        if -30 <= bala.y <= -16:
            bala.x = -25
            c = False
            explosio.x = dolent2.x + posrand2 + 27
            explosio.y = dolent2.y + 35
            dolent2.x = random.randrange(-10,5,2)
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent3.x + posrand3 - 25 < bala.x - 52  <  dolent3.x + posrand3 - 15 and cont3 < 3:
        if 3 <= bala.y <= 21:
            bala.x = -25
            c = False
            explosio.x = dolent3.x + posrand3 + 27
            explosio.y = dolent3.y +70
            dolent3.x = random.randrange(50,100,2)
            cont3 += 1
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent3.x + posrand3 - 25 < bala.x - 52  <  dolent3.x + posrand3 - 15 and cont3 >= 3:
        if 3 <= bala.y <= 21:
            bala.x = -25
            c = False
            explosio.x = dolent3.x + posrand3 + 27
            explosio.y = dolent3.y +70
            dolent3.x = random.randrange(-5,5,2)
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent4.x + posrand4 - 25 < bala.x - 52  <  dolent4.x + posrand4 - 15 and cont4 < 3:
        if 38 <= bala.y <= 52:
            bala.x = -25
            c = False
            explosio.x = dolent4.x + posrand4 + 27
            explosio.y = dolent4.y + 105
            dolent4.x = random.randrange(50,100,2)
            cont4 += 1
            kills += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    if  dolent4.x + posrand4 - 25 < bala.x - 52  <  dolent4.x + posrand4 - 15 and cont4 >= 3:
        if 38 <= bala.y <= 52:
            bala.x = -25
            c = False
            explosio.x = dolent4.x + posrand4 + 27
            explosio.y = dolent4.y + 105
            dolent4.x = random.randrange(-10,5,2)
            kills += 1
            cont4 += 1
            exp = 0
            if soroll == 'on':
                sound.play(sorollexplosio)
    buttons = ugame.buttons.get_pressed()
    if buttons & ugame.K_UP and naubona.y >-128 +64:
        if hardmode and cont4 < 7:# Comprobamos si el sprite está en el borde superior de la pantalla
            naubona.y -= 1
        if hardmode == False:
            naubona.y -= 1
    elif buttons & ugame.K_DOWN and naubona.y < (128-64 -15):  # Comprobamos si el sprite está en el borde inferior de la pantalla
        if hardmode and cont4 < 7:# Comprobamos si el sprite está en el borde superior de la pantalla
            naubona.y += 1
        if hardmode == False:
            naubona.y += 1
    if buttons & ugame.K_UP and naubona.y >-128 +64 and cont4 >= 7 and hardmode:  # Comprobamos si el sprite está en el borde superior de la pantalla
        naubona.y -= 2
    elif buttons & ugame.K_DOWN and naubona.y < (128-64 -15) and cont4 >= 7 and hardmode:  # Comprobamos si el sprite está en el borde inferior de la pantalla
        naubona.y += 2   
    if buttons & ugame.K_RIGHT and naubona.x < 160 - 25:
        if hardmode and cont4 < 7:# Comprobamos si el sprite está en el borde superior de la pantalla
            naubona.x += 1
        if hardmode == False:
            naubona.x += 1
    elif buttons & ugame.K_LEFT and naubona.x > 0 :
        if hardmode and cont4 < 7:# Comprobamos si el sprite está en el borde superior de la pantalla
            naubona.x -= 1
        if hardmode == False:
            naubona.x -= 1
    if buttons & ugame.K_RIGHT and naubona.x < 160 - 25 and cont4>=7 and hardmode:
        naubona.x += 2
    elif buttons & ugame.K_LEFT and naubona.x > 0 and cont4 >= 7 and hardmode:
        naubona.x -= 2
    time.sleep(0.01)
    ara = time.monotonic()
    temps = ara - darrer_valor
    # MOURE ELS ENEMICS CAPA A L'ESQUERRA
    if temps >= velocitat and s==1:
        if hardmode == True and cont4 < 3:
            print('vel1')
            dolent1.x -= 1
            dolent2.x -= 1
            dolent3.x -= 1
            dolent4.x -= 1
        if hardmode == False:
            dolent1.x -= 1
            dolent2.x -= 1
            dolent3.x -= 1
            dolent4.x -= 1
        # reiniciar el temporizador
        darrer_valor = ara
    if temps >= velocitat and s==1 and cont4 >=3 and cont4 < 7 and hardmode:
        print('vel2')
        dolent1.x -= 2
        dolent2.x -= 2
        dolent3.x -= 2
        dolent4.x -= 2
        # reiniciar el temporizador
        darrer_valor = ara
    if temps >= velocitat and s==1 and cont4 >=7 and hardmode:       
        print('vel3')
        dolent1.x -= 3
        dolent2.x -= 3
        dolent3.x -= 3
        dolent4.x -= 3
        # reiniciar el temporizador
        darrer_valor = ara

 

 

 

 

 

 

 

 

 

 

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