Creació de jocs amb PyBadge

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

DualBall

Aquest joc combina un aspecte futbolístic amb una mecànica de joc similar al tennis de taula. El joc fa servir cinc botons:

Polsador Funció
START Inici de la partida
Moviment amunt costat esquerre
Moviment avall costat esquerre
A Moviment amunt costat dret
B Moviment avall costat dret

Pantalla del joc

Es fan servir tres fitxers de gràfics: seminum1.bmp, semi2.bmp i semipilota.bmp.

Gràfics         Gràfics         Gràfics

El programa és el següent:

import ugame
import stage
import time
import board
import neopixel
def colision(sprite1, sprite2):
    return (
        sprite1.x < sprite2.x + 16 and
        sprite1.x + 16 > sprite2.x and
        sprite1.y < sprite2.y + 16 and
        sprite1.y + 16 > sprite2.y
    )
cadena = neopixel.NeoPixel(board.NEOPIXEL, 5, brightness=0.05)
banc = stage.Bank.from_bmp16("semi2.bmp")
banc1 = stage.Bank.from_bmp16("seminum1.bmp")
pilota = stage.Bank.from_bmp16("semipilota.bmp")
fons = stage.Grid(banc, 10, 8)  # 160x128
#definicions objectes
bloc = stage.Sprite(pilota, 1, 80, 40)
#pales cadascu
porteria1 = stage.Sprite(banc, 4, 0, 60)
porteria2 = stage.Sprite(banc, 5, 144, 60)
#marcador
marcador1 = stage.Sprite(banc1, 1, 50, 10)
marcador2 = stage.Sprite(banc1, 1, 100, 10)
l = []
for elem in range(8):
    for i in range(6):
        n = elem*16
        m = i*32
        l = l + [stage.Sprite(banc, 1, m, n)]
joc = stage.Stage(ugame.display, 12)
joc.layers = [bloc, porteria1, porteria2, marcador1, marcador2]+ l + [fons]
joc.render_block()
incx = 4
incy = 1
n = 1
m = 1
k = 1
running = True
while running ==True:
    bloc.move(bloc.x + incx, bloc.y + incy)
    if not 0 < bloc.y < 112:
        incy = -incy
    #moviment
    boto = ugame.buttons.get_pressed()
    if boto & ugame.K_UP and porteria1.y > 2:
        porteria1.move(porteria1.x, porteria1.y - 2)
    if boto & ugame.K_DOWN and porteria1.y < 112:
        porteria1.move(porteria1.x, porteria1.y + 2)
    if boto & ugame.K_X and porteria2.y > 2:
        porteria2.move(porteria2.x, porteria2.y - 2)
    if boto & ugame.K_O and porteria2.y < 112:
        porteria2.move(porteria2.x, porteria2.y + 2)
    #se la para
    if colision(bloc, porteria1):
        incx = -incx
        k = k+1
        if k == 3:
            incx = incx + 1
            k = 1
            incy = incy + 2
    if colision(bloc, porteria2):
        incx = -incx
    #gol
    if bloc.x <0:
        for elem in range(9):
            cadena[0] = (0, 0, 255)
            time.sleep(0.1)
            cadena[0] = (255, 255, 255)
            time.sleep(0.1)
        cadena[0] = (0, 0, 0)     # Pixel 0 vermell
        bloc.move(bloc.x + 60, 60)
        time.sleep(0.4)
        n = n+1
        marcador2.set_frame(n, 0)
        incx = 4
    if bloc.x > 144:
        for elem in range(9):
            cadena[0] = (0, 0, 255)
            time.sleep(0.1)
            cadena[0] = (255, 0, 0)
            time.sleep(0.1)
        cadena[0] = (0, 0, 0)
        bloc.move(bloc.x - 60 , 60)
        time.sleep(0.4)
        m = m+1
        marcador1.set_frame(m, 0)
        incx = -4
    if m == 5 or n == 5:
        texto = stage.Text(30, 60, 0)
        texto.move(10, 60)
        if m == 5:
            texto.text("Jugador 1 guanya!")
            p = 0
        if n == 5:
            texto.text("Jugador 2 guanya!")
            p = 255
        texto1 = stage.Text(30, 60, 0)
        texto1.text("Prem START per")
        texto2 = stage.Text(30, 60, 0)
        texto2.move(0, 10)
        texto2.text("tornar a jugar")
        joc = stage.Stage(ugame.display, 12)
        joc.layers = [texto, texto1, texto2] + l + [fons]
        joc.render_block()
        while True:
            cadena[0] = (0, 0, 255)
            time.sleep(0.2)
            cadena[0] = (255, p, p)
            time.sleep(0.1)
            boto1 = ugame.buttons.get_pressed()
            if boto1 & (ugame.K_START):
                import supervisor
                supervisor.reload()
            time.sleep(0.1)
    joc.render_sprites([bloc, porteria1, porteria2, marcador1, marcador2])
    joc.tick()

 

 

 

 

 

 

 

 

 

 

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