How does Tetris work?

Blitz3D Forums/Blitz3D Beginners Area/How does Tetris work?

You're right octo, updated version below:

Controls:
Arrow left/right = move shape left/right.
Z = Spin counter-clockwise.
X = Spin clockwise.
Space or arrow down = fast drop.
Esc = quit.

New features:
o) Colours! Woo!
o) Shapes can now land in the playarea.

Doesn't work:
x) Array bound checking (you can move too far left or right).
x) Rotation validity.
x) Line filling (i.e., the idea of the game!).


Include "keyboard.bb"

Graphics 640,480,32
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Dim colors%(6,2)
Dim shapedata%(5,3,3,3) ; 6 shapes, 4 rotations, 4x4 grid.
Dim playarea%(4,9,17) ; 5 predefined areas, 10x18 grid.

Restore colordata
For c%=0 To 6
    For rgb%=0 To 2
        Read colors%(c%,rgb%)
    Next
Next 

Restore shapedata
For shape%=0 To 5
    For angle%=0 To 3
        For y%=0 To 3
            For x%=0 To 3
                Read shapedata%(shape%,angle%,x%,y%)
            Next
        Next
    Next
Next

Restore playareadata
For area%=0 To 4
    For y%=0 To 17
        Read temp$
        For x%=0 To 9
            playarea%(area%,x%,y%)=Mid$(temp$,x%+1,1)
        Next
    Next 
Next

; Init + main loop.
playing=True
level%=2
shape%=Rand(0,5)
shape_x%=3
shape_y%=0
shape_color%=Rand(0,5)+1
angle%=0
dropspeed%=5 : timer%=dropspeed%
dx%=0
fastdrop=False

While playing

    If KeyHit(KEY_ESC) Then playing = False 

    ; Check if left/right is down or was hit.
    If KeyDown(KEY_LEFT) Or KeyHit(KEY_LEFT)
        shape_x%=shape_x%-1
    EndIf 
    If KeyDown(KEY_RIGHT) Or KeyHit(KEY_RIGHT)
        shape_x%=shape_x%+1
    EndIf

    If KeyHit(KEY_Z)
        angle%=(angle% - 1) And 3
    EndIf 
    If KeyHit(KEY_X)
        angle%=(angle% + 1) And 3
    EndIf

    If KeyDown(KEY_SPACE) Or KeyDown(KEY_DOWN) ; User fast-dropping piece.
        fastdrop=True
    Else
        fastdrop=False
    EndIf 
    
    Gosub drawscreen
    
    Delay 75    ; This is why, above, we have to check if a key was hit
                ; and not just that it happened to be down at the time.
    
    timer%=timer%-1

    If timer%=0 Or fastdrop
        landed=False    ; This is where we check to see if the shape landed.
        For y%=0 To 3
            For x%=0 To 3
                If shapedata%(shape%,angle%,x%,y%) = 1
                    below% = shape_y% + y% + 1
                    If below% = 18  ; Did we hit the bottom of the playarea?
                        landed = True
                    Else
                        If playarea%(level%,shape_x%+x%,below%) > 0
                            landed = True ; We landed on an old shape.
                        EndIf
                    EndIf
                EndIf
            Next
        Next

        If landed ; 'set' the shape into the playarea.
            For y%=0 To 3
                For x%=0 To 3
                    If shapedata%(shape%,angle%,x%,y%) = 1
                        playarea%(level%,shape_x%+x%,shape_y%+y%) = shape_color%
                    EndIf
                Next
            Next

            ; New shape
            shape%=Rand(0,5)
            shape_x%=3
            shape_y%=-1 ; Because it gets incremented almost immediately.
                        ; A nasty kludge. Don't try this at home, kids.
            shape_color%=Rand(0,5)+1
            angle%=0
            fastdrop=False
        EndIf   
                
        shape_y%=shape_y%+1
        timer%=dropspeed%
    EndIf
    
Wend

.drawscreen
;Cls
; Draw the playing area 'container'
Color $aa,$aa,$aa
Rect 240-4,64,(16*10)+8,(16*18)+4,True
Color 0,0,0
For y%=0 To 17
    For x%=0 To 9
        Color colors%(playarea%(level%,x%,y%),0),colors%(playarea%(level%,x%,y%),1),colors%(playarea%(level%,x%,y%),2)
        Rect 240+(x%*16),64+(y%*16),16,16
    Next
Next 

; Draw shape.
Color colors%(shape_color%,0),colors%(shape_color%,1),colors%(shape_color%,2)
For y%=0 To 3
    For x%=0 To 3
        If shapedata%(shape%,angle%,x%,y%)=1
            Rect 240+(shape_x%*16)+(x%*16),64+(shape_y%*16)+(y%*16),16,16
        EndIf 
    Next
Next
Flip False
Return 


.shapedata
; I
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,1,0

Data 0,0,0,0
Data 1,1,1,1
Data 0,0,0,0
Data 0,0,0,0

Data 0,0,1,0
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,1,0

Data 0,0,0,0
Data 1,1,1,1
Data 0,0,0,0
Data 0,0,0,0

; Z
Data 0,0,0,1
Data 0,0,1,1
Data 0,0,1,0
Data 0,0,0,0

Data 0,1,1,0
Data 0,0,1,1
Data 0,0,0,0
Data 0,0,0,0

Data 0,0,0,1
Data 0,0,1,1
Data 0,0,1,0
Data 0,0,0,0

Data 0,1,1,0
Data 0,0,1,1
Data 0,0,0,0
Data 0,0,0,0

; S
Data 0,0,1,0
Data 0,0,1,1
Data 0,0,0,1
Data 0,0,0,0

Data 0,0,1,1
Data 0,1,1,0
Data 0,0,0,0
Data 0,0,0,0

Data 0,0,1,0
Data 0,0,1,1
Data 0,0,0,1
Data 0,0,0,0

Data 0,0,1,1
Data 0,1,1,0
Data 0,0,0,0
Data 0,0,0,0

; T
Data 0,0,1,0
Data 0,1,1,1
Data 0,0,0,0
Data 0,0,0,0

Data 0,0,1,0
Data 0,0,1,1
Data 0,0,1,0
Data 0,0,0,0

Data 0,0,0,0
Data 0,1,1,1
Data 0,0,1,0
Data 0,0,0,0

Data 0,0,1,0
Data 0,1,1,0
Data 0,0,1,0
Data 0,0,0,0

; L
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,1,1
Data 0,0,0,0

Data 0,0,0,0
Data 0,1,1,1
Data 0,1,0,0
Data 0,0,0,0

Data 0,1,1,0
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,0,0

Data 0,0,0,1
Data 0,1,1,1
Data 0,0,0,0
Data 0,0,0,0

; J
Data 0,0,1,0
Data 0,0,1,0
Data 0,1,1,0
Data 0,0,0,0

Data 0,1,0,0
Data 0,1,1,1
Data 0,0,0,0
Data 0,0,0,0

Data 0,0,1,1
Data 0,0,1,0
Data 0,0,1,0
Data 0,0,0,0

Data 0,0,0,0
Data 0,1,1,1
Data 0,0,0,1
Data 0,0,0,0

.playareadata
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "

Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data " 1  1  1  "
Data "  1  1  1 "
Data "          "

Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "23 4  4 32"
Data "          "
Data "  43  34  "
Data " 5      5 "

Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "  22  22  "
Data "    11    "
Data " 33333333 "
Data "    44    "
Data "  12  21  "
Data "          "
Data "          "

Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data "          "
Data " 3234     "
Data " 53 23    "
Data " 4234     "
Data " 22 12    "
Data " 3314     "
Data "          "
Data "    3143  "
Data "    53 15 "
Data "    3243  "
Data "    22 22 "
Data "    1231  "
Data "          "

.colordata
Data $00,$00,$22    ; Playarea background colour.
Data $00,$00,$ff
Data $00,$ff,$00
Data $00,$ff,$ff
Data $ff,$00,$00
Data $ff,$00,$ff
Data $ff,$ff,$00