사용자 도구

사이트 도구

English

cublocapp:an32008:index

AN32008 - LED 메트릭스 테트리스 게임

우선 동영상을 먼저 보세요.

보시는 것처럼 CB210과 LED 도트 메트릭스 2개, 그리고 스위치 몇개로 테트리스를 구현한 것입니다.

저희 회사의 프랑스 대리점에서 만든 프로그램입니다.

결선도

게임 요령

2 개의 버튼은 조각을 수평으로 (오른쪽에서 왼쪽으로 그리고 왼쪽에서 오른쪽으로) 움직이는데 사용됩니다.

다른 두 개의 버튼은 부품을 90 ° (시계 방향으로 또는 그 반대로) 회전시키는 데 사용됩니다.

마지막 버튼은 부품의 낙하를 가속하는 데 사용됩니다. 전원이 켜지면 어레이의 모든 LED가 켜집니다. 버튼을 누르면 LED가 사라지고 게임이 시작됩니다. 이 단계에서 부품은 면 상단에 나타나고 하단으로 부드럽게 내려갑니다. 부품 모델의 선택은 무작위입니다. 부품을 가능한 최상의 위치로 이동시켜 부품을 채워서 선을 만드세요. 이 프로그램의 원리는 매우 간단하며, 플레이 영역을 차지하는 배열 타입 변수를 사용했습니다.

소스 프로그램

tetris.zip

'###############################################################
' Jeu video avec 2 mini matrice a leds
' @Lextronic 2012 - 14/07/2012 - version Cubloc Studio 3.3.4
'
' Programme donne a titre d'exemple. A utiliser dans le cadre d'une initiation aux modules CUBLOC
' (l utilisation commerciale de ce programme est interdite). Lextronic decline toute responsabilite en
' cas de prejudice resultant de l'utilisation de cette note d'application.
'###############################################################  
 
 Const Device = CB210          ' Initialisation de modele de Cubloc
 
 Dim mat1(12,19) As Byte       ' Memoire RAM matrices du jeu
 Dim piece(5,5) As Byte        ' Matrice piece
 Dim piecem(5,5) As Byte       ' Matrice memoire piece
 Dim x As Byte
 Dim y As Byte
 Dim xfig As Byte               ' Position figure
 Dim yfig As Byte
 Dim xlpiece As Byte            ' Taille matrice piece
 Dim ylpiece As Byte 
 Dim posxpiece As Byte          ' Position x de la piece
 Dim posypiece As Byte          ' Position y de la piece
 Dim oldx As Byte               ' Ancienne position x de la piece
 Dim oldy As Byte               ' Ancienne Position y de la piece
 Dim pbplace As Byte            ' Variable probleme positionnement piece
 Dim finjeu As Byte             ' Variable signalant la fin du jeu       
 Dim  Aleatoire1 As Integer  
 Dim typepiece As Byte          ' Type de piece
 Dim Direction As Byte          ' Direction de deplacement
 Dim vitesse As Byte            ' Vitesse du jeu
 Dim difficulte As Byte         ' Niveau du jeu
 Dim i As Byte
 Dim j As Byte
 Dim k As Byte
 Dim tstligne As Byte           ' Variable de test de ligne pleine
 Dim pasniv As Byte             ' Vaiable de test de passage au niveau plus rapide
 Dim errorcom As Byte           ' Registre erreur communication I2C
 Dim DATA As Byte               ' Donnee stockage Led pour la matrice
 
   Input 0             ' Port 0 en entree 
   Input 1             ' Port 1 en entree 
   Input 2             ' Port 2 en entree 
   Input 3             ' Port 3 en entree 
   Input 4             ' Port 4 en entree
 
   Set I2c 8,9         ' Configure ports pour liaison I2C
   Delay 500 
 
 ' Initialisation Matrice 1
 matrice &HE0,&H21                            ' Activation oscillateur interne de la matrice
 matrice &HE0,&HE0                            ' Relage luminosite
 matrice &HE0,&H81                            ' Display ON - pas de clignotement
 
 ' Initialisation Matrice 2
 matrice &HE2,&H21                            ' Activation oscillateur interne de la matrice
 matrice &HE2,&HE0                            ' Relage luminosite
 matrice &HE2,&H81                            ' Display ON - pas de clignotement
 
 '------------- Ecran de presentation -----------
 
 For yfig = 2 To 17     ' Efface les matrices
   For xfig = 3 To 10
     mat1(xfig,yfig)=0 
   Next
 Next  
 Gosub  affmat1           ' Raffraichissement de la matrice
 For yfig = 2 To 17       ' Affiche matrice en violet
   For xfig = 3 To 10
     mat1(xfig,yfig)=6
   Next
   Gosub  affmat1         ' Raffraichissement de la matrice   
 Next
 
  '----------------------- Attend debut du jeu ------------------------
 
 Do                       ' Attend sollicitation touche pour debut jeu
    Aleatoire1 = Rnd(0)
    If In(0) = 0 Then Exit Do
    If In(1) = 0 Then Exit Do
 Loop 
 
 For yfig = 17 To 2 Step-1    ' Efface progressivement la matrice
   For xfig = 3 To 10
     mat1(xfig,yfig)=0
   Next
   Gosub  affmat1             ' Raffraichissement de la matrice   
 Next  
 Delay 100
 
 '----------------------- Initialisation des variables ------------------------
 
 For yfig = 1 To 17           ' Positionne delimitations bord ecran droite/gauche 
   mat1(2,yfig) = 1 
   mat1(11,yfig) = 1
 Next
 For xfig = 1 To 10
   mat1 (xfig,1) = 1
   mat1 (xfig,18) = 1
 Next
 finjeu = 0                   ' Le jeu demarre
 vitesse = 30                 ' Initialise vitesse du jeu
 difficulte = 30
 Direction = 0
 pasniv = 5
 Gosub selfig                 ' Va choisir une piece aleatoire
 Gosub  affmat1               ' Raffraichissement de la matrice   
 
 '------------------- Recupere etat des bouton-poussoirs --------------------
 
 Do 
    Direction = 0
    If Keyinh(0,15) = 0 Then Direction = 2
    If Keyinh(1,15) = 0 Then Direction = 1
    If Keyinh(2,15) = 0 Then Direction = 3
    If Keyinh(3,15) = 0 Then Direction = 4 
    If Keyinh(4,15) = 0 Then Direction = 5 
 
    oldx = posxpiece           ' Memorise ancienne position x
      oldy = posypiece         ' Memorise ancienne position y
 
    vitesse  = vitesse-1
    If vitesse = 0 Then          ' Teste si la piece doit tomber d un etage
       vitesse = difficulte  ' initialise variable vitesse
       posypiece = posypiece + 1   ' Fait tomber la piece d un etage
       Gosub deplace   
       If pbplace = 1 Then         ' Test si la piece est bloquee ?
          If posypiece = 1 Then  ' Est-on sur la premier ligne ? -> oui alors fin de partie
          Reset
       Else
 
'-------------------- Test si des lignes ne sont pas pleines ? ----------------------------------
 
      For j = 17 To 2 Step-1
       tstligne = 0 
testagain: 
       For i = 3 To 10
         If mat1(i,j) = 0 Then tstligne = 1
       Next
       If tstligne = 0 Then      ' Efface la ligne pleine
       For i = 3 To 10
             mat1(i,j) = 0
       Next   
       Gosub  affmat1         ' Raffraichissement de la matrice 
       Delay 500
 
       For k = j To 2 Step -1   ' Decalage de tout l'ecran vers le bas
          For i = 3 To 10
             mat1(i,k) = mat1(i,k-1)
          Next
       Next
       For i = 3 To 10
          mat1(i,2) = 0          ' La premiere ligne se vide
       Next  
       Gosub  affmat1       ' Raffraichissement de la matrice
 
          pasniv = pasniv - 1    ' Regarde si augmente la vitesse ?
          If pasniv = 0 Then
            pasniv = 5
            difficulte = difficulte - 1
            If difficulte = 0 Then difficulte = 1
            Endif 
          Endif      
          If tstligne = 0 Then Goto testagain 
      Next
      Direction = 0
        Gosub selfig              ' Alors on selctionne une autre figure
      Gosub  affmat1              ' Raffraichissement de la matrice 
    Endif  
   Endif  
 Endif  
 
 
 '------------------------------ Deplacement de la piece -------------------------------- 
 
    oldx = posxpiece               ' Memorise ancienne position x
    oldy = posypiece               ' Memorise ancienne position y
    Select Case Direction        
      Case 1                       ' Deplacement piece vers la gauche
         posxpiece = posxpiece + 1 ' Essai de deplacer piece vers la droite
         Gosub deplace                 ' Gestion deplacement horizontal
 
      Case 2                       ' Deplacement piece vers la gauche
         posxpiece = posxpiece - 1 ' Essai de deplacer piece vers la gauche
         Gosub deplace                 ' Gestion deplacement horizontal  
 
      Case 3                         ' Rotation piece vers la droite
         Gosub rotation  
 
      Case 4                         ' Rotation piece vers la gauche
         Gosub rotation  
 
      Case 5                       ' Lache la piece plus vite
         vitesse = 1     
 
    End Select
  Gosub affmat1
 Loop
 
 
    '--------------------- Rafraichissement de la matrice -----------------------
affmat1:  
    I2cstart                     ' Condition Start I2C matrice 1
    errorcom = I2cwrite (&HE0)   ' Adresse I2C de la matrice
    errorcom = I2cwrite (&H00)   ' Adresse debut RAM matrice
    For y = 3 To 10
      DATA.BIT0 = mat1(y,3)
      DATA.BIT1 = mat1(y,4)
      DATA.BIT2 = mat1(y,5)
      DATA.BIT3 = mat1(y,6)
      DATA.BIT4 = mat1(y,7)
      DATA.BIT5 = mat1(y,8)   
      DATA.BIT6 = mat1(y,9)
      DATA.BIT7 = mat1(y,2)
      errorcom = I2cwrite (DATA) 
      errorcom = I2cwrite (DATA)
    Next
    I2cstop  
 
    I2cstart                      ' Condition Start I2C matrice 1
    errorcom = I2cwrite (&HE2)    ' Adresse I2C de la matrice
    errorcom = I2cwrite (&H00)    ' Adresse debut RAM matrice
    For y = 3 To 10
      DATA.BIT0 = mat1(y,11)
      DATA.BIT1 = mat1(y,12)
      DATA.BIT2 = mat1(y,13)
      DATA.BIT3 = mat1(y,14)
      DATA.BIT4 = mat1(y,15)
      DATA.BIT5 = mat1(y,16)   
      DATA.BIT6 = mat1(y,17)
      DATA.BIT7 = mat1(y,10)
      errorcom = I2cwrite (DATA) 
      errorcom = I2cwrite (DATA)
     Next
     I2cstop  
 Return
     '--------------------- Selection d'une piece -----------------------
selfig:
    Aleatoire1 = Rnd(0)     ' Gneration piece aleatoire 
    typepiece = Aleatoire1/4285
    If typepiece = 0 Then typepiece = 1
    If typepiece > 7 Then typepiece = 2
 
    For x= 1 To 4           ' Efface matrice piece
      For y = 1 To 4
        piece(x,y) = 0
      Next
    Next   
 
  Select Case typepiece
 
  Case 1                ' Piece orange
     xlpiece = 3         ' Taille matrice piece
     ylpiece = 3
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(2,2) = 4      ' Memorise 
     piece(3,2) = 4
     piece(1,3) = 4
     piece(2,3) = 4
 
  Case 2                ' Piece en forme de T rouge
     xlpiece = 3         ' Taille matrice piece
     ylpiece = 3
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,2) = 1      ' Memorise 
     piece(2,2) = 1
     piece(3,2) = 1
     piece(2,3) = 1
 
  Case 3                ' Piece verte
     xlpiece = 3         ' Taille matrice piece
     ylpiece = 3
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,2) = 2      ' Memorise 
     piece(2,2) = 2
     piece(3,2) = 2
     piece(3,3) = 2
 
  Case 4                ' Piece Bleu foncee
     xlpiece = 3         ' Taille matrice piece
     ylpiece = 3
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,2) = 5      ' Memorise 
     piece(2,2) = 5
     piece(2,3) = 5
     piece(3,3) = 5
 
  Case 5                ' Piece Bleu foncee
     xlpiece = 3         ' Taille matrice piece
     ylpiece = 3
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,2) = 3      ' Memorise 
     piece(2,2) = 3
     piece(3,2) = 3
     piece(1,3) = 3
 
  Case 6                ' Piece Bleu foncee
     xlpiece = 2         ' Taille matrice piece
     ylpiece = 2
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,1) = 6      ' Memorise 
     piece(2,1) = 6
     piece(1,2) = 6
     piece(2,2) = 6
 
  Case 7                ' Piece Bleu foncee
     xlpiece = 4         ' Taille matrice piece
     ylpiece = 4
     posxpiece = 3       ' Position x initiale de la piece
     posypiece = 0       ' Position y initiale de la piece
     piece(1,2) = 7      ' Memorise 
     piece(2,2) = 7
     piece(3,2) = 7
     piece(4,2) = 7 
 
  End Select
 
  '--------------------- Test si la piece peut etre affichee si pas fin partie -----------
 
  For y = 1 To ylpiece 
     For x = 1 To xlpiece
       If piece(x,y) <> 0 Then
       If mat1(x+posxpiece,y) <> 0 Then finjeu = 1
       Endif
     Next
  Next
 
  If finjeu = 0 Then Gosub placepiece    ' La piece peut etre placee
  Return
 
 
    '--------------------- Place la piece a l ecran -----------  
placepiece:
     For y = 1 To ylpiece 
        For x = 1 To xlpiece
          If piece(x,y) <> 0 Then mat1(x+posxpiece,y+posypiece) = piece(x,y)       
        Next
     Next 
  Return 
 
   '-------------------- Routine deplacement horizontal de la piece -----------------------      
deplace:     
  For y = 1 To ylpiece      ' Efface piece de la memoire
    For x = 1 To xlpiece
       If piece(x,y) <> 0 Then mat1(x+oldx,y+oldy) = 0
    Next  
  Next             
  pbplace = 0
  For y = 1 To ylpiece      ' Regarde si on peut la mettre a la nouvelle place ?
     For x = 1 To xlpiece
        If piece(x,y) <> 0 Then
         If mat1(x+posxpiece,y+posypiece) <> 0 Then pbplace = 1
         Endif
     Next
  Next
  If pbplace = 0 Then       ' Oui -> On la place !
     Gosub placepiece
  Else   
     For y = 1 To ylpiece                ' Non -> On replace piece a l'ancienne place
        For x = 1 To xlpiece
         If piece(x,y) <> 0 Then mat1(x+oldx,y+oldy) = piece(x,y)       
          Next
         posxpiece=oldx        ' Recupere ancienne place
      Next      
  Endif   
  Return
 
 
    '--------------------- Essai la rotation de la piece -----------  
rotation:
 
  For y = 1 To ylpiece      ' Efface piece de la memoire
    For x = 1 To xlpiece
       If piece(x,y) <> 0 Then mat1(x+oldx,y+oldy) = 0
    Next  
  Next             
  pbplace = 0   
 
  '---------------------- Rotation de la matrice memoire ---------------
  If Direction = 4 Then     ' selectionne sens de la rotation
    For y = 1 To ylpiece      
       For x = 1 To xlpiece
          piecem(x,y)=piece(y,xlpiece-x+1)
       Next 
    Next
      Else
    For y = 1 To ylpiece      
       For x = 1 To xlpiece
          piecem(x,y)=piece(ylpiece-y+1,x)
       Next 
    Next  
  Endif
 
  '---------------------- Regarde si on peut la mettre a la nouvelle place ? ---------------
 
  For y = 1 To ylpiece      
    For x = 1 To xlpiece
      If piecem(x,y) <> 0 Then
        If mat1(x+posxpiece,y+posypiece) <> 0 Then pbplace = 1
     Endif
    Next
  Next
  If pbplace = 0 Then       ' Oui -> On la place !
   For y = 1 To ylpiece     ' Recupere nouvelle position de la matrice     
       For x = 1 To xlpiece
        piece(x,y)=piecem(x,y)
     Next 
     Next
     Gosub placepiece
  Else   
     For y = 1 To ylpiece   ' Non -> On replace piece a l'ancienne place
        For x = 1 To xlpiece
         If piece(x,y) <> 0 Then mat1(x+oldx,y+oldy) = piece(x,y)       
          Next
      Next      
  Endif   
  Return 
 
 End
 
 '####### Routine envoye donnee I2C a la matrice #########
 
 Sub matrice (DATA2 As Byte,DATA3 As Byte)
   I2cstart                             ' Condition Start I2C
   errorcom = I2cwrite (DATA2)          ' Adresse I2C de la matrice
   errorcom = I2cwrite (DATA3)          ' Activation oscillateur interne
   I2cstop 
 End Sub

큐블록 어플리케이션 노트

cublocapp/an32008/index.txt · 마지막으로 수정됨: 2017/10/17 20:11 저자 Comfile Technology