LOG-O-PONG 2023

De Wiki LOGre
Aller à la navigation Aller à la recherche
Langue : Français  • English

Projet réalisé par Fred.

Abouti

Introduction

Cette version est un reboot du projet initial réalisé par Oliv' pour la Fête des Tuiles 2019.

Le LOG-O-PONG est un petit jeu à base de LED multicolores que le LOG peut emmener lors d'évènements pour montrer ce que l'on peut faire avec de l'électronique fait maison, une découpeuse laser, une imprimante 3D et un peu de code.

Règles du jeu

Un bandeau de LEDS RGB adressables (WS2812) simule le déplacement d'une balle. À chaque extrémité, une zone joueur. La balle se déplace d'un côté à l'autre, et doit être renvoyée par les joueurs/joueuses (en cliquant sur leur bouton).

Un/une joueur/joueuse perd si :

  • la balle touche son mur ;
  • il/elle appuie sur son bouton alors que la balle est dans sa moitié de jeu, mais pas encore dans sa zone.

La vitesse de la balle est modulée suivant plusieurs situations :

  • elle augmente globalement à mesure que le jeu progresse, jusqu'à un certain point ;
  • elle est boostée si le/la joueur/joueuse appuie sur le bouton lorsque la balle est à l'extrémité de sa zone (côté mur).

Lorsque la vitesse maxi de la balle est atteinte, la taille des zones se réduit progressivement à mesure des échanges.

Autres idées à creuser :

  • au bout de n échanges :
    • la taille de la zone des joueurs diminue -> fait ;
    • envoyer une balle supplémentaire ;
    • masquer la balle lorsqu'elle entre dans la zone adverse ;
  • si un/une joueur/joueuse clique n fois sur la même position de sa zone, il/elle gagne le tour ;
  • faire apparaître des objets aléatoirement : si le/la joueur/joueuse clique dessus, il/elle gagne un bonus (à déterminer) ;
  • imposer la position où cliquer dans la zone ;
  • moduler la vitesse de la balle (vu-mètre) ;
  • renvoyer la balle au/à la joueur/joueuse qui vient de frapper (à la position milieu ; changer couleur LEDs centrales) en fonction de ??? ;

Électronique

Cahier des charges

  • développement en MMBasic (PicoMite) pour faciliter les modifications in situ ;
  • PCB dédié avec connectique robuste ;
  • régulateur intégré permettant d'alimenter le jeu entre 5V et 15V ;
  • dispositif anti-arrachement des câbles des boutons ;
  • piezo intégré + sortie son pour bruitages / musique (doit pouvoir fonctionner en extérieur via un petit ampli) ;
  • montage pour utilisation verticale ou horizontale ;
  • luminosité automatique (LDR) ;
  • bouton(s) pour sélectionner les options du jeu ;

Matériel retenu

  • carte Raspberry Pi Pico ;
  • bandeau de LEDS RGB adressables WS2812 5V ;
  • module carte µ-SD pour sons et musiques ;

Logiciel

Cliquer sur le bouton à droite pour faire apparaître/disparaître le code...

<syntaxhighlight lang="BASIC"> ' LOG-O-PONG 2023 ' ' 1D PONG-like game ' ' (C) 2023 - Frederic Mantegazza ' ' Options needed: ' OPTION CPUSPEED 250000 ' OPTION SDCARD GP22, GP10, GP11, GP12 ' OPTION AUDIO GP14, GP15 ' ' Options used for editing: ' OPTION COLOURCODE ON ' OPTION CASE UPPER ' OPTION DISPLAY 40, 120

OPTION ESCAPE ' use escape chars OPTION EXPLICIT ' variables must be declared OPTION BASE 1 ' arrays start at 1 instead of 0

' I/O consts definition CONST BUTTON1_PIN = MM.INFO(PINNO GP2) CONST BUTTON2_PIN = MM.INFO(PINNO GP3) CONST BUTTON3_PIN = MM.INFO(PINNO GP4) CONST BUTTON4_PIN = MM.INFO(PINNO GP5) CONST LED_STRIP_PIN = MM.INFO(PINNO GP6) CONST LED_PIN = MM.INFO(PINNO GP25) CONST LDR_PIN = MM.INFO(PINNO GP26)

' Misc consts definition CONST ASC_BS = "\b" CONST ASC_CR = "\r"

CONST TRUE = 1 CONST FALSE = 0

' Game consts definition CONST GAME_LENGTH = 60 ' nb strip LEDs CONST ZONE_SIZE = 8 ' player zone size (at start) CONST ZONE_SIZE_MIN = 2 ' player zone size (minimum) CONST SPEED_MIN = 50 ' timing for min speed (ms) CONST SPEED_MAX = 25 ' timing for max speed (ms). NOTE: need to overclock @250MHz CONST SPEED_BOOST = 0 ' timing for boost speed (ms). NOTE: need to overclock @250MHz CONST SCORE_WIN = 5 ' points needed to win CONST HIDE_AFTER = 5 ' hide ball after n exchanges CONST REDUCE_AFTER = 5 ' reduce players zones size after n exchanges CONST BLINK_DELAY = 250 ' blinking ball delay (ms) CONST BLINK = TRUE ' CONST NO_BLINK = FALSE ' CONST VOLUME_DEFAULT = 0 ' default volume CONST BRIGHTNESS_DEFAULT = 100 ' default brightness

' Custom colours CONST COLOR_BACK = RGB(black) CONST COLOR_ZONE = RGB(blue) CONST COLOR_BALL = RGB(white) CONST COLOR_CENTER = RGB(8, 4, 0) CONST COLOR_WIN = RGB(green) CONST COLOR_LOOSE = RGB(red)

' Sounds CONST SND_START = "start.wav" CONST SND_BUMP = "bump.wav" CONST SND_LOOSE = "loose.wav" CONST SND_WIN = "win.wav"

' Internal consts definition CONST FSM_IDLE = 0 ' waiting game to start CONST FSM_DEMO = 1 ' running demo (TODO) CONST FSM_RIGHT = 2 ' ball is moving right CONST FSM_LEFT = 3 ' ball is moving left CONST FSM_GOAL = 4 ' a player scored a goal CONST FSM_SCORE = 5 ' display score (TODO) CONST FSM_WIN = 6 ' a player won

' Global vars DIM gBallPos% = 1 ' current ball position DIM gBallSpeed% = SPEED_MIN ' current ball speed DIM gZoneSize% = ZONE_SIZE ' size of players zones DIM gNbExchanges% = 0 ' number of players exchanges DIM gScorePlayer1% = 0 ' score player 1 DIM gScorePlayer2% = 0 ' score player 2 DIM gFsmState% = FSM_IDLE ' FSM state DIM gButton1State% = 0 ' button 1 state DIM gButton2State% = 0 ' button 2 state DIM gButton3State% = 0 ' button 3 state DIM gButton4State% = 0 ' button 4 state DIM gVolume% = VOLUME_DEFAULT ' curent volume (0-100) - TODO: save in flash DIM gBrightness% = BRIGHTNESS_DEFAULT ' current brightness (5-100) - TODO: save in flash

' Init system SUB InitSys

 DRIVE "B:"

END SUB

' Init I/Os SUB InitIO

 ' Init Buttons Interrupts
 EnableButton1Isr
 EnableButton2Isr
 EnableButton3Isr
 EnableButton4Isr
 ' Init strip LED
 SETPIN LED_STRIP_PIN, DOUT
 ' Init builtin LED
 SETPIN LED_PIN, DOUT
 ' Init analog input
 SETPIN LDR_PIN, AIN

END SUB


' Init sounds SUB InitSounds

 PLAY VOLUME VOLUME_DEFAULT, VOLUME_DEFAULT

END SUB


' Enable button1 interrupt SUB EnableButton1Isr

 SETPIN BUTTON1_PIN, INTL, Button1Pressed

END SUB


' Enable button2 interrupt SUB EnableButton2Isr

 SETPIN BUTTON2_PIN, INTL, Button2Pressed

END SUB


' Enable button3 interrupt SUB EnableButton3Isr

 SETPIN BUTTON3_PIN, INTB, Button3Changed

END SUB


' Enable button4 interrupt SUB EnableButton4Isr

 SETPIN BUTTON4_PIN, INTB, Button4Changed

END SUB


' Disable button1 pressed interrupt SUB DisableButton1PressedIsr

 SETPIN BUTTON1_PIN, OFF, Button1Pressed

END SUB


' Disable button2 pressed interrupt SUB DisableButton2PressedIsr

 SETPIN BUTTON2_PIN, OFF, Button2Pressed

END SUB


' Disable button3 changed interrupt SUB DisableButton3ChangedIsr

 SETPIN BUTTON3_PIN, OFF, Button3Changed

END SUB


' Disable button4 Changed interrupt SUB DisableButton4ChangedIsr

 SETPIN BUTTON4_PIN, OFF, Button4Changed

END SUB


' Button 1 pressed callback SUB Button1Pressed

 gButton1State% = 1

END SUB


' Button 2 pressed callback SUB Button2Pressed

 gButton2State% = 1

END SUB


' Button 3 changed callback SUB Button3Changed

 gButton3State% = 1 - PIN(BUTTON3_PIN)

END SUB


' Button 4 changed callback SUB Button4Changed

 gButton4State% = 1 - PIN(BUTTON4_PIN)

END SUB


' Display game field SUB ShowGame blinkFlag% ' TODO: change blinkFlag to xxx (xxx_BLINK, xxx_SOLID, xxx_HIDE...)

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 STATIC displayBall%   = TRUE   ' used to blink ball. TODO: rename to hideBall? So it can be used for gameplay.
 STATIC lastBlinkTime% = TIMER  ' last blink ball time
 ' Manage ball blinking
 IF TIMER - lastBlinkTime% >= BLINK_DELAY THEN
     IF displayBall% THEN displayBall% = FALSE ELSE displayBall% = TRUE
     lastBlinkTime% = TIMER
 ENDIF
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
   SELECT CASE i%
     ' LED color at ball position
     CASE gBallPos%
       SELECT CASE gBallPos%
         ' LED color in IDLE state
         CASE 1, GAME_LENGTH
           IF blinkFlag% AND displayBall% OR NOT blinkFlag% THEN
             leds%(i%) = COLOR_BALL
           ELSE
             leds%(i%) = COLOR_ZONE
           ENDIF
         CASE ELSE
           leds%(i%) = COLOR_BALL
       END SELECT
     ' LED color at center position
     CASE GAME_LENGTH / 2, GAME_LENGTH / 2 + 1
         leds%(i%) = COLOR_CENTER
     ' LED color in zones
     CASE IS <= gZoneSize%, IS >= GAME_LENGTH-gZoneSize%+1
       leds%(i%) = COLOR_ZONE
     ' LED colors in field
     CASE ELSE
       leds%(i%) = COLOR_BACK
   END SELECT
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Display goaler SUB ShowGoaler

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
   SELECT CASE i%
     CASE IS <= gZoneSize%
       IF gBallPos% = 1 THEN leds%(i%) = RGB(green) ELSE leds%(i%) = RGB(red)
     CASE IS >= GAME_LENGTH-gZoneSize%+1
       IF gBallPos%  = GAME_LENGTH THEN leds%(i%) = RGB(green) ELSE leds%(i%) = RGB(red)
     CASE ELSE
       leds%(i%) = RGB(black)
   END SELECT
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Display winner SUB ShowWinner

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
   SELECT CASE i%
     CASE IS <= GAME_LENGTH / 2
       IF gScorePlayer1% > gScorePlayer2% THEN leds%(i%) = RGB(green) ELSE leds%(i%) = RGB(red)
     CASE IS >= GAME_LENGTH / 2 + 1
       IF gScorePlayer2% > gScorePlayer1% THEN leds%(i%) = RGB(green) ELSE leds%(i%) = RGB(red)
     CASE ELSE
       leds%(i%) = RGB(black)
   END SELECT
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Display xxx SUB ShowXxx

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
     leds%(i%) = RGB(black)
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Display volume SUB ShowVolume

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
   SELECT CASE i%
     CASE IS <= gVolume% / 100 * GAME_LENGTH
       leds%(i%) = RGB(white)
     CASE ELSE
       leds%(i%) = RGB(black)
   END SELECT
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Display brightness SUB ShowBrightness

 LOCAL i%
 LOCAL leds%(GAME_LENGTH)
 ' Build strip LED array
 FOR i% = 1 TO GAME_LENGTH
   SELECT CASE i%
     CASE IS <= gBrightness% / 100 * GAME_LENGTH
       leds%(i%) = RGB(white)
     CASE ELSE
       leds%(i%) = RGB(black)
   END SELECT
 NEXT
 ' Output strip LED array
 BITBANG WS2812 O, LED_STRIP_PIN, GAME_LENGTH, leds%()

END SUB


' Increment/decrement ball position at correct speed SUB MoveBall increment%

 STATIC lastIncTime% = TIMER
 IF TIMER - lastIncTime% > gBallSpeed% THEN
   ' PRINT TIMER - lastIncTime% - gBallSpeed%
   INC gBallPos%, increment%
   lastIncTime% = TIMER
 ENDIF

END SUB


' Play a sound SUB PlaysSound sound$

 IF gVolume% > 0 THEN
   PLAY WAV sound$
 ENDIF

END SUB


' Manage gNbExchanges SUB IncNbExchanges

 IF gBallSpeed% = SPEED_MAX THEN
   INC gNbExchanges%
   PRINT "Nb exchanges:"; gNbExchanges%
   IF (gNbExchanges% MOD CINT(REDUCE_AFTER*(1+RND))) = 0 AND gZoneSize% > ZONE_SIZE_MIN THEN
     INC gZoneSize%, -1
     print "Zone size:"; gZoneSize%
   ENDIF
 ENDIF

END SUB


' Main loop subroutine SUB MainLoop

 STATIC savedBallSpeed% = gBallSpeed%  ' 'normal' speed storage
 DO
   SELECT CASE gFsmState%
     CASE FSM_IDLE
       ' Switch between settings and game
       IF gButton3State% = 1 OR gButton4State% = 1 THEN
         ' Xxx setting?
         IF gButton3State% = 1 AND gButton4State% = 1 THEN
           showXxx  ' highlight xxx
         ' Volume Setting?
         ELSEIF gButton3State% = 1 THEN
           IF gButton1State% = 1 AND gVolume% >= 5 THEN
             INC gVolume%, -5
             PLAY VOLUME gVolume%, gVolume%
             PRINT "Volume:"; gVolume%
             gButton1State% = 0
             PAUSE 50
           ELSEIF gButton2State% = 1 AND gVolume% =< 95 THEN
             INC gVolume%, 5
             PLAY VOLUME gVolume%, gVolume%
             PRINT "Volume:"; gVolume%
             gButton2State% = 0
             PAUSE 50
           ENDIF
           ShowVolume  ' higlight volume
         ' Brightness setting?
         ELSEIF gButton4State% = 1 THEN
           IF gButton1State% = 1 AND gBrightness% >= 10 THEN
             INC gBrightness%, -5
             PRINT "Brightness:"; gBrightness%
             gButton1State% = 0
             PAUSE 50
           ELSEIF gButton2State% = 1 AND gBrightness% =< 95 THEN
             INC gBrightness%, 5
             PRINT "Brightness:"; gBrightness%
             gButton2State% = 0
             PAUSE 50
           ENDIF
           ShowBrightness  ' higlight brightness
         ENDIF
       ELSE
         ShowGame BLINK ' display field (blinking ball)
         ' Start game?
         IF gBallPos% = 1 AND gButton1State% = 1 THEN
           PlaysSound SND_START  ' TODO: play only on player side
           gButton1State% = 0
           'gButton2State% = 0
           PRINT "Speed:"; gBallSpeed%
           gFsmState% = FSM_RIGHT
           PRINT "-> FSM_RIGHT"
         ELSEIF gBallPos% = GAME_LENGTH AND gButton2State% = 1 THEN
           PlaysSound SND_START  ' TODO: play only on player side
           'gButton1State% = 0
           gButton2State% = 0
           PRINT "Speed:"; gBallSpeed%
           gFsmState% = FSM_LEFT
           PRINT "-> FSM_LEFT"
         ENDIF
       ENDIF
     CASE FSM_RIGHT
       ShowGame NO_BLINK  ' display field (no blinking)
       MoveBall 1  ' increment ball position
       ' Player 1 scored a goal
       IF gBallPos% > GAME_LENGTH THEN
         'GOTO rightGoal:
         PlaysSound SND_LOOSE  ' TODO: play only on player side
         INC gScorePlayer1%
         gBallPos% = 1
         gFsmState% = FSM_GOAL
         PRINT "-> FSM_GOAL"
       ELSEIF gButton2State% = 1 THEN
         PRINT "Button2 clicked at position"; gBallPos%
         SELECT CASE gBallPos%
           ' Ball in player zone
           CASE IS >= GAME_LENGTH-gZoneSize%+1
             IncNbExchanges  ' Increment nb exchanges
             ' Manage ball speed depending of clicking zone
             IF gBallPos% = GAME_LENGTH THEN
               gBallSpeed% = SPEED_BOOST
               PRINT "Speed: Boost!"
             ELSE
               gBallSpeed% = savedBallSpeed%  ' retreive previous 'normal' speed
               IF gBallSpeed% >= SPEED_MAX+5 THEN INC gBallSpeed%, -5
               savedBallSpeed% = gBallSpeed%  ' save new 'normal' speed
               PRINT "Speed:"; gBallSpeed%
             ENDIF
             ' Reverse ball direction
             PlaysSound SND_BUMP  ' TODO: play only on player side
             gFsmState% = FSM_LEFT
             PRINT "-> FSM_LEFT"
           ' Ball outside player zone
           CASE IS >= GAME_LENGTH / 2 + 1

'rightGoal:

             PlaysSound SND_LOOSE  ' TODO: play only on player side
             INC gScorePlayer1%
             gBallPos% = 1
             gFsmState% = FSM_GOAL
             PRINT "-> FSM_GOAL"
         END SELECT
         gButton2State% = 0
       ENDIF
     CASE FSM_LEFT
       ShowGame NO_BLINK  ' display field (no blinking)
       MoveBall -1  ' decrement ball position
       ' Player 2 scored a goal
       IF gBallPos% < 1 THEN
         'GOTO leftGoal:
         PlaysSound SND_LOOSE  ' TODO: play only on player side
         INC gScorePlayer2%
         gBallPos% = GAME_LENGTH
         gFsmState% = FSM_GOAL
         PRINT "-> FSM_GOAL"
       ELSEIF gButton1State% = 1 THEN
         PRINT "Button1 clicked at position"; gBallPos%
         SELECT CASE gBallPos%
           ' Ball in player zone
           CASE IS <= gZoneSize%
             IncNbExchanges  ' Increment nb exchanges
             ' Manage ball speed depending of clicking zone
             IF gBallPos% = 1 THEN
               gBallSpeed% = SPEED_BOOST
               PRINT "Speed: Boost!"
             ELSE
               gBallSpeed% = savedBallSpeed%  ' retreive previous 'normal' speed
               IF gBallSpeed% >= SPEED_MAX+5 THEN INC gBallSpeed%, -5
               savedBallSpeed% = gBallSpeed%  ' save new 'normal' speed
               PRINT "Speed:"; gBallSpeed%
             ENDIF
             ' Reverse ball direction
             PlaysSound SND_BUMP  ' TODO: play only on player side
             gFsmState% = FSM_RIGHT
             PRINT "-> FSM_RIGHT"
           ' Ball outside player zone
           CASE IS <= GAME_LENGTH / 2

'leftGoal:

             PlaysSound SND_LOOSE
             INC gScorePlayer2%
             gBallPos% = GAME_LENGTH
             gFsmState% = FSM_GOAL
             PRINT "-> FSM_GOAL"
         END SELECT
         gButton1State% = 0
       ENDIF
     CASE FSM_GOAL
       PRINT "Player1:"; gScorePlayer1%; " - Player2:"; gScorePlayer2%
       IF gScorePlayer1% = SCORE_WIN OR gScorePlayer2% = SCORE_WIN THEN
         gFsmState% = FSM_WIN
         PRINT "-> FSM_WIN"
       ELSE
         ShowGoaler  ' higlight goaler
         PAUSE 1000  ' TODO: add sound
         gBallSpeed% = savedBallSpeed%
         GOTO ToIdleState  ' let's use Basic features ;o)
       ENDIF
     CASE FSM_WIN
       IF gScorePlayer1% > gScorePlayer2% THEN PRINT "Player1"; ELSE PRINT "Player2";
       PRINT " wins!"
       'PlaysSound SND_WIN
       ShowWinner  ' higlight winner
       PAUSE 3000  ' TODO: add sound and animation
       gBallSpeed% = SPEED_MIN
       savedBallSpeed% = gBallSpeed%
       gScorePlayer1% = 0
       gScorePlayer2% = 0

ToIdleState:

       gButton1State% = 0
       gButton2State% = 0
       gFsmState% = FSM_IDLE
       PRINT "-> FSM_IDLE"
     CASE ELSE
       PRINT "CRITICAL::unknow FSM state:"; gFsmState%
   END SELECT
   'MEMORY  ' debug infos
 LOOP

END SUB


' Main routine SUB Main

 InitSys
 InitIO
 InitSounds
 MainLoop

END SUB


' Run program Main END </syntaxhighlight>

Boîtier

Conçu sous Onshape pour de l'impression 3D, bien sûr !

BOM

Liens

Projets similaires

Basic