Here a little Tetris which is very basic and quite simple to play Moreover, it seems it perfectly works ! I hope you'll enjoy it, and I wish it will usefull for people who wants to make this kind of game.
I have not seen bugs, tell me if you see any of them.
This Code has been written in DB Pro using the last available version .
Have Fun ! ( sorry, the comments are in french )
`**********************************
`| TersaTris Par Tersaken ;)
`| Ecrit avec DB Pro Version 6.
`***********************************
` Init **********************************************
set display mode 800,600,32 : sync on : sync rate 30
`Variables / Tableaux ********************************************
Dim Carte(9,20)
Dim piece(2,2,8)
Type Pieces
x as integer
y as integer
c as integer : ` ( c = couleur )
p as integer : ` Piece utilisée.
endtype
randomize timer()
perso.p = 2+rnd(6)
perso.c = rgb(255,0,255)
`Global ***********************************************************
Global Perso as Pieces
Global Pushed as Integer
Global Duree as integer : Duree = 800`( en milli-seconde ) ( temps de descente d'une piece )
Global Temps as integer
Global Score as integer
`*** on charge Les Pieces
restore Pieces
for z = 2 to 8
for y = 0 to 2
for x = 0 to 2
read piece(x,y,z)
next x
next y
next z
LoadPiece()
`boucle principale ***************************************************
do
cls
controle()
affiche()
sync
loop
`fonctions ************************************************************
`Round - Permet d'arrondir les valeurs
function round(V#)
if v# < 0 : a = -1 : else : a = 1 : endif
v# = abs(v#)
if v#-int(v#) >= 0.5
exitfunction (int(v#)+1)*a
else
exitfunction (int(v#))*a
endif
endfunction 0
`Permet de faire subir des rotations a la piece active
function rotation( Ang )
for y = 0 to 2 : for x = 0 to 2
`on calcule l'angle actuel
distance# = sqrt( (x-1)^2 + (y-1)^2 )
angle = acos( (x-1)/distance# )
if ( 1 - y ) > 0 : angle = 360-angle : endif
`on deplace le point
angle = angle+ang
x_ = round(cos(angle)*distance#)+1
y_ = round(sin(angle)*distance#)+1
piece(x_,y_,perso.p) = piece(x,y,0)
next x : next y
LoadPiece()
endfunction
`fonction qui permet d'afficher mais egalement de vider les lignes " pleines "
function Affiche()
`la carte ********************************
for y = 0 to 18 ; for x = 0 to 9
ink carte(x,y),0
box x*32,y*32,x*32+32,y*32+32
`verifie si la ligne est complete
if carte(x,y) <> 0
a = a+1
if y < 2 : cls : text 360,400,"G@me Over" : repeat : sync : until scancode() : end : endif
endif
if A = 10
for B = Y To 1 Step -1
for A = 0 To 9
Carte(A,B) = Carte(A,B-1)
next A
next B
Score = Score + 1000
endif
next x : a = 0 : next y
`la piece ********************************
ink perso.c,0
for y = 0 to 2
for x = 0 to 2
if piece(x,y,0) = 1
box (Perso.x+x)*32,(Perso.y+y)*32,(Perso.x+x)*32+32,(Perso.y+y)*32+32
endif
next x
next y
`interface ********************************
ink rgb(125,255,75),0
line 320,0,320,599
text 340,50,"Score : "+str$(Score)
text 640,580,"By Tersaken Studio©"
endfunction
`controle & Collisions
function Controle()
`si rien n'est touché ...
if scancode() = 0 : Pushed = 0 : endif
`les anciennes positions avant deplacement
oldx = perso.x
oldy = perso.y
`on fait descendre la piece tout les X secondes
if Timer() - Temps > Duree
perso.y = perso.y+1
Temps = Timer()
endif
`on deplace la piece vers le bas plus rapidement avec la fleche bas du clavier
if downkey() and pushed = 0 : perso.y = perso.y + 1 : pushed = 1 : endif
`collision ?
if collision() = 1
perso.y = oldy
for y = 0 to 2 : for x = 0 to 2
if piece(x,y,0) = 1
Carte(Perso.x+x,Perso.y+y) = Piece(x,y,0)*Perso.c
endif
next x : next y
Perso.y = 0 : Perso.x = rnd(6)
perso.c = rgb( 125+rnd(125), 125+rnd(125), 125+rnd(125) )
perso.p = 2+rnd(6) : loadpiece()
Score = Score + Perso.p*10
endif
`rotation avec la touche Haut
if Upkey()
if Pushed = 0 : rotation(90) : rotate = 1 : endif
pushed = 1
endif
`collision apres rotation ?
if collision() = 1 : rotation(-90) : endif
`deplacement lateral
if leftkey() and pushed = 0 : perso.x = perso.x-1 : pushed = 1 : endif
if rightkey() and pushed = 0 : perso.x = perso.x+1 : pushed = 1 : endif
` collision ???
if collision() : Perso.x = OldX : endif
endfunction
`charge Une piece dans la piece active
function LoadPiece()
for a = 0 to 2 : for b = 0 to 2
piece(a,b,0) = piece(a,b,perso.p)
next b : next a
endfunction
`permet de savoir si la piece active est en collision avec le reste du décor.
function collision()
for y = 0 to 2 : for x = 0 to 2
if piece(x,y,0) = 1
if perso.y+y > 18 : exitfunction 1 : endif
if perso.x+x < 0 or perso.x+x > 9 : exitfunction 1 : endif
if Carte(Perso.x+x,Perso.y+y) <> 0 : exitfunction 1 : endif
endif
next x : next y
endfunction 0
`Les Pieces du Jeu
Pieces:
data 1,0,0
data 1,1,0
data 0,1,0
data 0,0,1
data 0,1,1
data 0,1,0
data 1,0,0
data 1,0,0
data 1,1,0
data 0,1,0
data 0,1,0
data 0,1,0
data 1,0,0
data 1,0,0
data 1,1,0
data 0,0,1
data 0,0,1
data 0,1,1
data 1,1,1
data 1,0,1
data 0,0,0
Athlon Barton 2800+ ; 640 Mo DDR2 ; ati radeon 9600 Pro 256 Mo - 120 Go HD.