Page 1 sur 2

jeu de type match3

Publié : jeu. 03/nov./2016 16:39
par blendman
salut

savez-vous s'il existe quelque part un exemple de jeu de type match3 (comme Candy crush, charm king, etc...) ?

Car j'aimerais en réaliser un avec SB ou PB (mais plutôt SB) mais je ne sais pas du tout comment faire ça, je parle notamment de la gestion de la grille interne :
- si trois sprite identiques (horizontal) sont cote à cote, ils disparaissent et alors, les sprites au dessus tombent ainsi que 3 nouveaux sprites
- si trois sprite identiques (horizontal) + 2 sprites identiques verticales sont alignés, idem
etc...

Une idée de comment faire ça ou un exemple pour ce type de petit jeu ?

Re: jeu de type match3

Publié : jeu. 03/nov./2016 18:56
par Patrick88
exemple pas à pas :http://rembound.com/articles/how-to-mak ... ml5-canvas en javascript ?

pat

Re: jeu de type match3

Publié : ven. 04/nov./2016 1:03
par Ar-S
Excellent ce site merci pour le lien.

Re: jeu de type match3

Publié : ven. 04/nov./2016 9:12
par blendman
merci, je vais regarder ça ;)

Edit : bon, j'ai regardé, mais c'est du javascript, et moi, je ne connais pas du tout javascript ^^.
Je vais essayer de traduire ça en purebasic :)

Re: jeu de type match3

Publié : ven. 04/nov./2016 9:48
par Ar-S
Moi c'est pareil Blendman, la conversion n'a pas l'air trop difficile.
Pour m'entrainer je suis en train de convertir le BreakiT qui est plus simple.

Re: jeu de type match3

Publié : dim. 06/nov./2016 10:08
par blendman
salut

vous savez comment voncertir ça :

Code : Tout sélectionner

   var moves = [];     // { column1, row1, column2, row2 }
 
    // Game states
    var gamestates = { init: 0, ready: 1, resolve: 2 };
En purebasic ?

J'ai mis ça :

Code : Tout sélectionner

Structure sMove
 
    column1.i
    row1.i
    column2.i
    row2.i
    
EndStructure
Global Moves.sMove

Global gamestates
ça vous semble ok ?

Merci :)

Re: jeu de type match3

Publié : dim. 06/nov./2016 10:21
par comtois
Je ferais plutôt comme ça

Code : Tout sélectionner

Structure move
  column1.i
  row1.i
  column2.i
  row2.i   
EndStructure

; Array of moves
Global NewList moves.move()

    ; Game states
    Structure GameState
      init.i
      ready.i
      resolve.i
    EndStructure
    
    Global gamestates.gamestate
    gamestates\init = 0
    gamestates\ready = 1
    gamestates\resolve = 2
    
    Global gamestate = gamestates\init

Re: jeu de type match3

Publié : dim. 06/nov./2016 10:47
par falsam
Comtois je suis d'accord avec toi sauf que je pense que c'est un tableau à deux dimensions. Ceci dit je ne connais le code complet et je peux me tromper bien sur.
var moves = []; // { column1, row1, column2, row2 }
moves = [] c'est en javascript un tableau apparemment de deux dimensions Column et Row = column1, row1, column2, row2 .... et peut être columnN, rowN

quote]var gamestates = { init: 0, ready: 1, resolve: 2 };[/quote]ici on a effectivement une structure. La solution de Comtois est la bonne.

Ar-s tu vas bientôt aimer JavaScript ^^

Re: jeu de type match3

Publié : dim. 06/nov./2016 11:19
par blendman
Merci comtois et falsam, le code complet est celui-ci :

https://github.com/rembound/Match-3-Gam ... example.js

(c'est tiré de l'exemple dont le lien est donné un peu plus haut.)

Re: jeu de type match3

Publié : dim. 06/nov./2016 15:44
par blendman
J'ai encore une question, comment puis convertir ces termes :

Code : Tout sélectionner

// Find clusters in the level
    function findClusters() {
        // Reset clusters
        clusters = []
        
        // Find horizontal clusters
        for (var j=0; j<level.rows; j++) {
            // Start with a single tile, cluster of 1
            var matchlength = 1;
            for (var i=0; i<level.columns; i++) {
                var checkcluster = false;
                
                if (i == level.columns-1) {
                    // Last tile
                    checkcluster = true;
                } else {
                    // Check the type of the next tile
                    if (level.tiles[i][j].type == level.tiles[i+1][j].type &&
                        level.tiles[i][j].type != -1) {
                        // Same type as the previous tile, increase matchlength
                        matchlength += 1;
                    } else {
                        // Different type
                        checkcluster = true;
                    }
                }
                
                // Check if there was a cluster
                if (checkcluster) {
                    if (matchlength >= 3) {
                        // Found a horizontal cluster
                        clusters.push({ column: i+1-matchlength, row:j,
                                        length: matchlength, horizontal: true });
                    }
                    
                    matchlength = 1;
                }
            }
        }

        // Find vertical clusters
        for (var i=0; i<level.columns; i++) {
            // Start with a single tile, cluster of 1
            var matchlength = 1;
            for (var j=0; j<level.rows; j++) {
                var checkcluster = false;
                
                if (j == level.rows-1) {
                    // Last tile
                    checkcluster = true;
                } else {
                    // Check the type of the next tile
                    if (level.tiles[i][j].type == level.tiles[i][j+1].type &&
                        level.tiles[i][j].type != -1) {
                        // Same type as the previous tile, increase matchlength
                        matchlength += 1;
                    } else {
                        // Different type
                        checkcluster = true;
                    }
                }
                
                // Check if there was a cluster
                if (checkcluster) {
                    if (matchlength >= 3) {
                        // Found a vertical cluster
                        clusters.push({ column: i, row:j+1-matchlength,
                                        length: matchlength, horizontal: false });
                    }
                    
                    matchlength = 1;
                }
            }
        }
    }
    
    // Find available moves
    function findMoves() {
        // Reset moves
        moves = []
        
        // Check horizontal swaps
        for (var j=0; j<level.rows; j++) {
            for (var i=0; i<level.columns-1; i++) {
                // Swap, find clusters and swap back
                swap(i, j, i+1, j);
                findClusters();
                swap(i, j, i+1, j);
                
                // Check if the swap made a cluster
                if (clusters.length > 0) {
                    // Found a move
                    moves.push({column1: i, row1: j, column2: i+1, row2: j});
                }
            }
        }
        
        // Check vertical swaps
        for (var i=0; i<level.columns; i++) {
            for (var j=0; j<level.rows-1; j++) {
                // Swap, find clusters and swap back
                swap(i, j, i, j+1);
                findClusters();
                swap(i, j, i, j+1);
                
                // Check if the swap made a cluster
                if (clusters.length > 0) {
                    // Found a move
                    moves.push({column1: i, row1: j, column2: i, row2: j+1});
                }
            }
        }
        
        // Reset clusters
        clusters = []
}

Je bloque notamment sur :
moves.push({column1: i, row1: j, column2: i+1, row2: j});
swap(i, j, i, j+1);

Merci ;)


EDIT :
voilà ce que j'ai obtenu :

Code : Tout sélectionner




; Find available moves
Procedure  findMoves() 
    
    ; Reset moves    
    ClearList(moves()) 
    
    ; Check horizontal swaps
    For j=0 To level\rows
        For  i=0 To level\columns-1
            
            ; Swap, find clusters And Swap back
           
            ;Swap(i, j, i+1, j);
            ;findClusters();
            ;Swap(i, j, i+1, j);
            
            ; Check If the Swap made a cluster
            If (ListSize(clusters()) > 0) 
                ; Found a move
                ;moves.push({column1: i, row1: j, column2: i+1, row2: jendif);
            EndIf
        Next
    Next
    
    ; Check vertical swaps
    For i=0 To level\columns
        For j=0 To level\rows - 1
            ; Swap, find clusters And Swap back
            ;Swap(i, j, i, j+1);
            ;findClusters();
            ;Swap(i, j, i, j+1);
            
            ; Check If the Swap made a cluster
            If (ListSize(clusters()) > 0) 
                ; Found a move
                ;moves.push({column1: i, row1: j, column2: i, row2: j+1endif);
            EndIf
        Next
    Next
    
    ; Reset clusters    
    ClearList(clusters()) 
    
    ; temporaire
    AddElement(Moves())
    
    
EndProcedure


; Find clusters IN the level
Procedure  findClusters() 
    
    ; Reset clusters   
    ClearList(clusters()) 
    
    ; Find horizontal clusters
    For  j=0 To level\rows
        ; Start With a single tile, cluster of 1
        matchlength = 1;
        For  i=0   To level\columns
            checkcluster = 0; false;
            
            If (i = level\columns-1) 
                ; Last tile
                checkcluster = 1;
            Else 
                ; Check the Type of the Next tile
                If (Level\Tiles(i,j)\Typ = level\tiles(i+1,j)\Typ And level\tiles(i,j)\Typ <> -1) 
                    ; Same Type As the previous tile, increase matchlength
                    matchlength + 1;
                Else 
                    ; Different Type
                    checkcluster = 1;
                EndIf
            EndIf
            
            ; Check If there was a cluster
            If (checkcluster) 
                If (matchlength >= 3) 
                    ; Found a horizontal cluster
                    ; clusters.push({ column: i+1-matchlength, row:j,length: matchlength, horizontal: true );
                EndIf
            EndIf
            
            matchlength = 1;
        Next
    Next

    
    ; Find vertical clusters
    For  i=0 To level\columns
        
        ; Start With a single tile, cluster of 1
        matchlength = 1;
        
        For  j=0  To level\rows
            checkcluster = 0
            
            If (j = level\rows-1) 
                ; Last tile
                checkcluster = 1
            Else 
                ; Check the Type of the Next tile
                If (level\tiles(i,j)\Typ = level\tiles(i,j+1)\Typ And level\tiles(i,j)\Typ <> -1) 
                    ; Same Type As the previous tile, increase matchlength
                    matchlength + 1;
                Else 
                    ; Different Type
                    checkcluster = 1;
                EndIf
            EndIf
            
            ; Check If there was a cluster
            If (checkcluster) 
                If (matchlength >= 3) 
                    ; Found a vertical cluster
                    ;clusters.push({ column: i, row:j+1-matchlength,length: matchlength, horizontal: false );
                EndIf
                
            EndIf
            
            matchlength = 1
            
        Next
    Next
    
EndProcedure


Re: jeu de type match3

Publié : dim. 06/nov./2016 16:49
par comtois

Code : Tout sélectionner

    ;Find clusters in the level
    Procedure findClusters() 
      Protected checkcluster
        ; Reset clusters
        ResetList(clusters())
 
        ; Find horizontal clusters
        For j=0 To level\rows-1
            ; Start With a single tile, cluster of 1
             matchlength = 1;
            For  i=0 To level\columns-1
                 checkcluster = #False
 
                If (i = level\columns-1) 
                    ; Last tile
                    checkcluster = #True
                 Else 
                    ; Check the type of the Next tile
                    If level\tiles(i,j)\type = level\tiles(i+1,j)\type And level\tiles(i,j)\type <> -1
                        ; Same type As the previous tile, increase matchlength
                        matchlength + 1
                    Else 
                        ; Different type
                        checkcluster = #True
                    EndIf
                EndIf
 
                ; Check If there was a cluster
                If checkcluster
                    If matchlength >= 3
                        ; Found a horizontal cluster
                      AddElement(clusters())
                      clusters()\column = i+1-matchlength
                       clusters()\row = j
                       clusters()\length = matchlength
                       clusters()\horizontal  = #True
                    EndIf
 
                    matchlength = 1;
                EndIf
            Next
        Next
 
        ; Find vertical clusters
        For i=0 To level\columns-1
            ; Start With a single tile, cluster of 1
            matchlength = 1
            For j=0 To level\rows-1
                 checkcluster = #False
 
                If j = level\rows-1
                    ; Last tile
                    checkcluster = #True
                 Else 
                    ; Check the type of the Next tile
                    If level\tiles(i,j)\type = level\tiles(i,j+1)\type And level\tiles(i,j)\type <> -1
                        ; Same type As the previous tile, increase matchlength
                        matchlength + 1
                    Else 
                        ; Different type
                        checkcluster = #True
                    EndIf
                EndIf
 
                ; Check If there was a cluster
                If checkcluster 
                    If matchlength >= 3 
                      ; Found a vertical cluster
                       AddElement(clusters())
                      clusters()\column = i
                      clusters()\row = j+1-matchlength
                      clusters()\length = matchlength
                      clusters()\horizontal  = #False
                       
          
                    EndIf
 
                    matchlength = 1
                EndIf
            Next
        Next
      EndProcedure  

Re: jeu de type match3

Publié : dim. 06/nov./2016 17:20
par blendman
merci Comtois ;)

Lorsque j'ajoute un debug à la fin de cette procédure, j'ai un nombre de clusters (possibilités) énorme certaines fois, c'est étrange, non ?


Voici mon code pour le moment :

Code : Tout sélectionner




;{ infos

; match 3
; date : 4/11/2016
; blendman
; purebasic 5.50 beta 1

; 4, 5 /11/2016 : conversion du code trouvé sur le net : https://github.com/rembound/Match-3-Game-HTML5/blob/master/match3-example.js#L85

;}

;{ on initilialise les encode/decorder

If UseJPEGImageDecoder() =0 Or UseJPEGImageEncoder()=0 Or UsePNGImageDecoder()=0 Or UsePNGImageEncoder()=0
    MessageRequester("Error", "unable To open the encoder/decorder")
    End
EndIf

InitSprite()


;}


Enumeration ;  window   
    #Win
EndEnumeration



;{ Levels 
Structure Sselectedtile
    selected.b
    column.a
    row.a
EndStructure

Structure sTile
    
    Typ.i
    shift.i
    
EndStructure

Structure SLevel
    
    x.i
    y.i
    Columns.b
    rows.b
    tilewidth.i  ; ; Visual width of a tile
    tileheight.i ; ; Visual height of a tile
    Array tiles.sTile(100,100) ;  ; The two-dimensional tile Array
    selectedtile.Sselectedtile ; { selected: false, column: 0, row: 0 endif
    
EndStructure
Global Level.sLevel

With level
    
    \x = 250
    \y = 113
    \Columns = 8
    \Rows = 8
    \tilewidth= 40  ;; Visual width of a tile
    \tileheight= 40           
    \selectedtile\selected=-1 ; selected: false, column: 0, row: 0 
    \selectedtile\column = 0
    \selectedtile\row = 0 
    
EndWith
;}

;{ Colors 
Global Maxcol = 5

Global Dim col(5)
Col(0) = RGB(255, 128, 128)
Col(1) = RGB(128, 255, 128)
Col(2) = RGB(128, 128, 255)
Col(3) = RGB(255, 255, 128)
Col(4) = RGB(255, 128, 255)
Col(5) = RGB(128, 255, 255)


;}

;{ Clusters And moves that were found

Structure move
    
  column1.i
  row1.i
  column2.i
  row2.i
  
EndStructure

; Array of moves
Global NewList moves.move()

; Game states
Structure GameState
    init.i
    ready.i
    resolve.i
EndStructure

Global gamestates.gamestate
gamestates\init = 0
gamestates\ready = 1
gamestates\resolve = 2

Global gamestate = gamestates\init


; Current move
Global NewList currentmove.move() 




Structure sClusters
    
    Length.i
    
    column.i
    row.i
    
    horizontal.i
    
EndStructure

Global NewList clusters.sClusters() ; ; { column, row, length, horizontal endif




; Swap two tiles IN the level
Procedure Swap_(x1, y1, x2, y2) 
    
    typeswap = level\tiles(x1,y1)\Typ
    level\tiles(x1,y1)\Typ = level\tiles(x2,y2)\Typ
    level\tiles(x2,y2)\Typ = typeswap 
    
EndProcedure
                

; Find clusters IN the level
Procedure findClusters0() 
    
    ; Reset clusters   
    ClearList(clusters()) 
    
    ; Find horizontal clusters
    Debug "horizontal ----"
    For  j=0 To level\rows
        
        ; Start With a single tile, cluster of 1
        matchlength = 1;
        For  i=0  To level\columns
            checkcluster = 0; false;
            
            If (i = level\columns-1) 
                ; Last tile
                checkcluster = 1;
            Else 
                ; Check the Type of the Next tile
                If (Level\Tiles(i,j)\Typ = level\tiles(i+1,j)\Typ And level\tiles(i,j)\Typ <> -1) 
                    
                    ; Same Type As the previous tile, increase matchlength
                    matchlength + 1;
                    If matchlength>=2
                        
                        ;Debug ""+Level\Tiles(i,j)\Typ+"\"+Level\Tiles(i+1,j)\Typ
                        Debug ""+i+"\"+j
                        Debug ""+Str(i+1)+"\"+Str(j)
                        Debug matchlength
                    EndIf
                Else 
                    ; Different Type
                    checkcluster = 1;
                EndIf
                
            EndIf
            
            ; Check If there was a cluster
            If (checkcluster) 
                If (matchlength >= 3) 
                    ; Found a horizontal cluster
                    ; clusters.push({ column: i+1-matchlength, row:j,length: matchlength, horizontal: true );
                    
                    AddElement(clusters())
                    clusters()\column = i+1-matchlength
                    clusters()\row = j
                    clusters()\Length = matchlength
                    clusters()\horizontal = 1
                    
                EndIf
            EndIf
            
            matchlength = 1;
        Next
        
    Next

    
    ; Find vertical clusters
    For  i=0 To level\columns
        
        ; Start With a single tile, cluster of 1
        matchlength = 1;
        
        For  j=0  To level\rows
            checkcluster = 0
            
            If (j = level\rows-1) 
                ; Last tile
                checkcluster = 1
            Else 
                ; Check the Type of the Next tile
                If (level\tiles(i,j)\Typ = level\tiles(i,j+1)\Typ And level\tiles(i,j)\Typ <> -1) 
                    ; Same Type As the previous tile, increase matchlength
                    matchlength + 1;
                Else 
                    ; Different Type
                    checkcluster = 1;
                EndIf
            EndIf
            
            ; Check If there was a cluster
            If (checkcluster) 
                If (matchlength >= 3) 
                    
                    ; Found a vertical cluster
                    ;clusters.push({ column: i, row:j+1-matchlength,length: matchlength, horizontal: false );
                    AddElement(clusters())
                    clusters()\column = i
                    clusters()\row = j+1-matchlength
                    clusters()\Length = matchlength
                    clusters()\horizontal = 0
                    
                EndIf
                
            EndIf
            
            matchlength = 1
            
        Next
        
    Next
    
   ;  Debug ListSize(clusters())
    
    
    
    
EndProcedure

;Find clusters in the level
Procedure findClusters()
    
    Protected checkcluster
    ; Reset clusters
    ResetList(clusters())
    
    ; Find horizontal clusters
    For j=0 To level\rows-1
        ; Start With a single tile, cluster of 1
        matchlength = 1;
        For  i=0 To level\columns-1
            checkcluster = #False
            
            If (i = level\columns-1)
                ; Last tile
                checkcluster = #True
            Else
                ; Check the type of the Next tile
                If level\tiles(i,j)\typ = level\tiles(i+1,j)\typ And level\tiles(i,j)\typ <> -1
                    ; Same type As the previous tile, increase matchlength
                    matchlength + 1
                    
                    If matchlength>=3
                        
                        ;Debug ""+Level\Tiles(i,j)\Typ+"\"+Level\Tiles(i+1,j)\Typ
;                         Debug ""+i+"\"+j
;                         Debug ""+Str(i+1)+"\"+Str(j)
;                         Debug matchlength
                    EndIf
                Else
                    ; Different type
                    checkcluster = #True
                EndIf
            EndIf
            
            ; Check If there was a cluster
            If checkcluster
                If matchlength >= 3
                    ; Found a horizontal cluster
                    AddElement(clusters())
                    clusters()\column = i+1-matchlength
                    clusters()\row = j
                    clusters()\length = matchlength
                    clusters()\horizontal  = #True
                EndIf
                
                matchlength = 1;
            EndIf
        Next
    Next
    
    ; Find vertical clusters
    For i=0 To level\columns-1
        ; Start With a single tile, cluster of 1
        matchlength = 1
        For j=0 To level\rows-1
            checkcluster = #False
            
            If j = level\rows-1
                ; Last tile
                checkcluster = #True
            Else
                ; Check the type of the Next tile
                If level\tiles(i,j)\typ = level\tiles(i,j+1)\typ And level\tiles(i,j)\typ <> -1
                    ; Same type As the previous tile, increase matchlength
                    matchlength + 1
                Else
                    ; Different type
                    checkcluster = #True
                EndIf
            EndIf
            
            ; Check If there was a cluster
            If checkcluster
                If matchlength >= 3
                    ; Found a vertical cluster
                    AddElement(clusters())
                    clusters()\column = i
                    clusters()\row = j+1-matchlength
                    clusters()\length = matchlength
                    clusters()\horizontal  = #False
                    
                    
                EndIf
                
                matchlength = 1
            EndIf
            
        Next
        
    Next
    
    
    Debug ListSize(clusters())
    
EndProcedure  


; Find available moves
Procedure  findMoves() 
    
    ; Reset moves    
    ClearList(moves()) 
    
    ; Check horizontal swaps
    For j=0 To level\rows-1
        
        For  i=0 To level\columns-1
            
            ; Swap, find clusters And Swap back
           
            Swap_(i, j, i+1, j);
            
            findClusters();
            Swap_(i, j, i+1, j);
            
            ; Check If the Swap made a cluster
            If (ListSize(clusters()) > 0) 
                ; Found a move
                ;moves.push({column1: i, row1: j, column2: i+1, row2: j );
                
                AddElement(moves())
                Moves()\column1 = i
                Moves()\row1 = j
                Moves()\column2 = i+1
                Moves()\row2 = j
                
            EndIf
            
        Next
        
    Next
    
    ; Check vertical swaps
    For i=0 To level\columns-1
        
        For j=0 To level\rows - 1
            
            ; Swap, find clusters And Swap back
            Swap_(i, j, i, j+1);
            findClusters();
            Swap_(i, j, i, j+1);
            
            ; Check If the Swap made a cluster
            If (ListSize(clusters()) > 0) 
                ; Found a move
                ;moves.push({column1: i, row1: j, column2: i, row2: j+1);
                
                AddElement(moves())
                Moves()\column1 = i
                Moves()\row1 = j
                Moves()\column2 = i
                Moves()\row2 = j+1
                
            EndIf
            
        Next
        
    Next
    
    ; Reset clusters    
    ClearList(clusters()) 
    
    ; temporaire
    ; AddElement(Moves())
    Debug "Moves : "+ListSize(moves())
    
EndProcedure

;}



;{ Game prop


; Timing And frames per second
Global lastframe = 0;
Global fpstime = 0  ;
Global framecount = 0;
Global fps = 0       ;

; Mouse dragging
Global drag = #False;


; Score
Global score = 0;

; Animation variables
Global animationstate = 0;
Global animationtime = 0 ;
Global animationtimetotal.f = 0.3;

; Show available moves
Global showmoves = #False;

; The AI bot
Global aibot = #False;

; Game Over
Global gameover = #False;

;}


;{ Gui buttons & Score
Structure sButton
    x.i
    y.i
    w.i
    h.i
    t.s
EndStructure
Global Dim buttons.sButton(2)  


Procedure SetButton(i,u$)
    
    buttons(i)\x = Val(StringField(u$,1,","))
    buttons(i)\y = Val(StringField(u$,2,","))
    buttons(i)\w = Val(StringField(u$,3,","))
    buttons(i)\h = Val(StringField(u$,4,","))
    buttons(i)\t = StringField(u$,5,",")
    CreateSprite(10+i,buttons(i)\w,buttons(i)\h,#PB_Sprite_AlphaBlending)
    
    If StartDrawing(SpriteOutput(10+i))
        
       
        Box (0,0,buttons(i)\w,buttons(i)\h,RGB(50,50,50))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawText(10,10,buttons(i)\t)
        StopDrawing()
        
    EndIf
    

EndProcedure

Dim u$(2)
u$(0)="30,240,150,50,New Game"
u$(1)="30,300,150,50,Show Moves"
u$(2)="30,360,150,50, Enable AI Bot"


; Score
Global ScoreW, ScoreH
ScoreW = 250
ScoreH = 30

Procedure UpdateScore()
    
    ; si on doit updater le score 
    If StartDrawing(SpriteOutput(20))
               
        Box (0,0,ScoreW,ScoreH,RGB(50,50,50))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawText(5,5,"Score : "+Str(score))
        StopDrawing()
        
    EndIf
    
EndProcedure

Procedure InitScore()
    
    ; pour initialiser le score
    CreateSprite(20,ScoreW,ScoreH,#PB_Sprite_AlphaBlending)
    UpdateScore()
    
EndProcedure


;}


;{ Initialize the game
Procedure init() 
    ;  
    ; ; Initialize the two-dimensional tile Array
    ;For i=0 To level\Columns
       ; For j=0 To level\rows
            ;level\tiles(i,j)
        ;Next 
    ;Next 
    
    For i=0 To level\columns
        ;             ; leveltiles[i] (0);
        For j=0 To level\rows
            ;                 ; Define a tile Type And a shift parameter For animation
            level\tiles(i,j)\Typ = 0
            level\tiles(i,j)\shift = 0
            
        Next
    Next
    
EndProcedure


Procedure GetRandomTile()
    
    
    ProcedureReturn Random(Maxcol)
    
EndProcedure


;  Create a random level
Procedure CreateLevel() 
    
    done = 0
    
    ; Keep generating levels Until it is correct
    While done = 0 
        
        ; Create a level With random tiles
        For i=0 To level\columns            
            For j=0 To level\rows
                level\tiles(i,j)\Typ = GetRandomTile()
            Next
        Next
        
        ; Resolve the clusters
        ; resolveClusters();
        
        ; Check If there are valid moves
        FindMoves();
        
        ; Done when there is a valid move
        If ListSize(moves())>0
            done = 1
        EndIf
        
    Wend
    
EndProcedure

;}


;{ render

Procedure DrawButtons()
    
    For i=0 To 2
        DisplayTransparentSprite(10+i, buttons(i)\x, buttons(i)\y)
    Next

EndProcedure

Procedure DrawScore()
    
    DisplayTransparentSprite(20,5,5)
    
    
EndProcedure

Procedure Render()
    
    ClearScreen(RGB(100,100,100))
    
    
    
    ; the tiles 
    u = 68
    e = 100 ; sprite de départ des tiles
    w = 200
    For i=0 To level\columns
        
        For j=0 To level\rows
             
            DisplayTransparentSprite(e,w+i*u,50+j*u,255,Col(level\tiles(i,j)\Typ))
            
            e=e+1
            
        Next   
    Next   
    
    
    ; draw score
    DrawScore()
    
    
    ; draw buttons
    DrawButtons()
    
    
    
    
    FlipBuffers()
    
EndProcedure

;}





Init()
CreateLevel() 

;{ openwindow
screenwidth = 1024
screenheight = 768

If OpenWindow(0, 0, 0, screenwidth, screenheight, "Hit Shiby", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) : EndIf


If OpenWindowedScreen(WindowID(0),0,0,screenwidth, screenheight)=0
  MessageRequester("Error", "Can't Open Screen!", 0)
  End
EndIf

; on crée les sprites 


; on crée les buttons
For i=0 To 2
    SetButton(i,u$(i))
Next

; score 
InitScore()


; les tiles 
e=100
For i=0 To level\columns
    For j=0 To level\rows
        CreateSprite(e,64,64,#PB_Sprite_AlphaBlending)
        e=e+1
    Next
Next

;}

Repeat
    
    Repeat
        
        EventID     = WindowEvent()
        
        Select EventID
                
            Case #PB_Event_CloseWindow 
                End
                
        EndSelect
        
    Until event = 0
        
    Render()
   
    
Until Quit = 1





Re: jeu de type match3

Publié : dim. 06/nov./2016 18:59
par comtois
ne pas confondre ResetList(clusters()) avec ClearList(clusters())

Code : Tout sélectionner

Procedure findClusters()
   
    Protected checkcluster
    ; Reset clusters
    ClearList(clusters())
[EDIT]
Je viens de m'apercevoir que c'est moi qui avais fait l'erreur dans la procédure précédente :oops:

Re: jeu de type match3

Publié : lun. 07/nov./2016 8:17
par blendman
Ah oui, ça marche mieux avec ClearList(), merci ;)

Je vais m'attaquer à la suite :mrgreen:

[EDIT]
ah non, j'ai encore parfois des chiffres trop élevé :(
si je fais debug listsize(Moves())
j'obtiens des fois plus de 100 possibilités, mais ça me semble énorme (surtout lorsque je compare avec la grille ^^)

[EDIT 2 ]
Par exemple, sur ce screenshot, on voit 122 déplacements possibles, mais ils sont où dans la grille ? :D

Image

Re: jeu de type match3

Publié : lun. 07/nov./2016 10:05
par blendman
Bon, pour la suite, il y encore des choses en javascript que je ne sais pas du tout traduire en pb, notamment ça :

Code : Tout sélectionner

  function loopClusters(func) 

  func(i, cluster.column+coffset, cluster.row+roffset, cluster);

  loopClusters(function(index, column, row, cluster) { level.tiles[column][row].type = -1; });


dans ce code-ci :

Code : Tout sélectionner


 // Loop over the cluster tiles and execute a function
    function loopClusters(func) {
        for (var i=0; i<clusters.length; i++) {
            //  { column, row, length, horizontal }
            var cluster = clusters[i];
            var coffset = 0;
            var roffset = 0;
            for (var j=0; j<cluster.length; j++) {
                func(i, cluster.column+coffset, cluster.row+roffset, cluster);
                
                if (cluster.horizontal) {
                    coffset++;
                } else {
                    roffset++;
                }
            }
        }
    }
    
    // Remove the clusters
    function removeClusters() {
        // Change the type of the tiles to -1, indicating a removed tile
        loopClusters(function(index, column, row, cluster) { level.tiles[column][row].type = -1; });

        // Calculate how much a tile should be shifted downwards
        for (var i=0; i<level.columns; i++) {
            var shift = 0;
            for (var j=level.rows-1; j>=0; j--) {
                // Loop from bottom to top
                if (level.tiles[i][j].type == -1) {
                    // Tile is removed, increase shift
                    shift++;
                    level.tiles[i][j].shift = 0;
                } else {
                    // Set the shift
                    level.tiles[i][j].shift = shift;
                }
            }
        }
}

Visiblement, en javascrpit on peut mettre une fonction dans une fonction en paramètre (ou un truc du genre), mais alors comment faire ça en pb ? aucune idée ^^.