I have recently been programming Tetris in DBPro for a school CAS project, its pretty simplistic so far and is in a presentable state(don't expect to play a game of Tetris though), its just over 700 lines so far but you will only be interested in the functions that handle the core mechanics of the game, their all aptly named i hope
`Tetris
`Tetris.dba
`======================
//Makes arrays
type BoardAT
Actual as integer
Current as integer
Transform as integer
BlockID as integer
endtype
dim Board(10,20) as BoardAT
//Globals
global Level
Global LeftPress
Global RightPress
Global UpPressed
Global Tick
Global xMin
Global xMax
Global yMin
Global yMax
Global LineID
Level=1
Level=1
Tick=0
//General Setup
load dll "user32.dll",1
Width=call dll(1,"GetSystemMetrics",0)
Height=call dll(1,"GetSystemMetrics",1)
maximize window
delete dll 1
set display mode Width,Height,32,1
sync on
sync rate 60
backdrop on
Autocam off
position camera 27,-50,-110
CreateBoard()
do
RenderBoard()
UpdateTetris()
text 30,700,""+STR$(BlockCollision())
text 30,750,""+STR$(Tick)
text 60,700,""+STR$(xMin)
text 60,750,""+STR$(xMax)
text 90,700,""+STR$(yMin)
text 90,750,""+STR$(yMax)
loop
function RenderBoard()
for x=1 to 10
for y=1 to 20
text x*30,y*30,""+STR$(Board(x,y).Actual)
text 680+(x*30),y*30,""+STR$(Board(x,y).Current)
next y
next x
sync
//Colour object
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Color Object Board(x,y).BlockID,RGB(255,0,0)
else
Color Object Board(x,y).BlockID,RGB(0,255,0)
endif
next y
next x
//Show/Hide Blocks
For x=1 to 10
for y=1 to 20
if Board(x,y).Actual=1
show object Board(x,y).BlockID
else
Hide Object Board(x,y).BlockID
endif
next y
next x
endfunction
function MakeBlock()
//Clear .Current
for x=1 to 10
for y=1 to 20
Board(x,y).Current=0
next y
next x
If level=1
BlockID=RND(13)+1
//Block 1 <-Reference Block.bnp for id
if BlockID=1
Board(4,1).Current=1
Board(5,1).Current=1
Board(6,1).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(4,1).Actual=1
Board(5,1).Actual=1
Board(6,1).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
endif
//Block 2 <-Reference Block.bnp for id
if BlockID=2
Board(4,1).Current=1
Board(5,1).Current=1
Board(6,1).Current=1
Board(6,2).Current=1
Board(6,3).Current=1
Board(4,1).Actual=1
Board(5,1).Actual=1
Board(6,1).Actual=1
Board(6,2).Actual=1
Board(6,3).Actual=1
endif
//Block 3 <-Reference Block.bnp for id
if BlockID=3
Board(4,1).Current=1
Board(5,1).Current=1
Board(6,1).Current=1
Board(4,2).Current=1
Board(6,2).Current=1
Board(4,1).Actual=1
Board(5,1).Actual=1
Board(6,1).Actual=1
Board(4,2).Actual=1
Board(6,2).Actual=1
endif
//Block 4 <-Reference Block.bnp for id
if BlockID=4
Board(6,1).Current=1
Board(4,2).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(4,3).Current=1
Board(6,1).Actual=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(4,3).Actual=1
endif
//Block 5 <-Reference Block.bnp for id
if BlockID=5
Board(4,2).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(4,1).Current=1
Board(6,3).Current=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(4,1).Actual=1
Board(6,3).Actual=1
endif
//Block 6 <-Reference Block.bnp for id
if BlockID=6
Board(5,1).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(6,3).Current=1
Board(6,4).Current=1
Board(5,1).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(6,3).Actual=1
Board(6,4).Actual=1
endif
//Block 7 <-Reference Block.bnp for id
if BlockID=7
Board(5,1).Current=1
Board(5,2).Current=1
Board(4,2).Current=1
Board(4,3).Current=1
Board(4,4).Current=1
Board(5,1).Actual=1
Board(5,2).Actual=1
Board(4,2).Actual=1
Board(4,3).Actual=1
Board(4,3).Actual=1
endif
//Block 8 <-Reference Block.bnp for id
if BlockID=8
Board(5,1).Current=1
Board(4,2).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(4,3).Current=1
Board(5,1).Actual=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(4,3).Actual=1
endif
//Block 9 <-Reference Block.bnp for id
if BlockID=9
Board(5,1).Current=1
Board(4,2).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(6,3).Current=1
Board(5,1).Actual=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(6,3).Actual=1
endif
//Block 10 <-Reference Block.bnp for id
if BlockID=10
Board(5,1).Current=1
Board(4,2).Current=1
Board(5,2).Current=1
Board(6,2).Current=1
Board(5,3).Current=1
Board(5,1).Actual=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(6,2).Actual=1
Board(5,3).Actual=1
endif
//Block 11 <-Reference Block.bnp for id
if BlockID=11
Board(4,1).Current=1
Board(4,2).Current=1
Board(5,2).Current=1
Board(5,3).Current=1
Board(6,3).Current=1
Board(4,1).Actual=1
Board(4,2).Actual=1
Board(5,2).Actual=1
Board(5,3).Actual=1
Board(6,3).Actual=1
endif
//Block 12 <-Reference Block.bnp for id
if BlockID=12
Board(5,1).Current=1
Board(5,2).Current=1
Board(5,3).Current=1
Board(5,4).Current=1
Board(5,5).Current=1
Board(5,1).Actual=1
Board(5,2).Actual=1
Board(5,3).Actual=1
Board(5,4).Actual=1
Board(5,5).Actual=1
endif
//Block 13
if BlockID=13
Board(5,1).Current=1
Board(5,2).Current=1
Board(5,3).Current=1
Board(5,4).Current=1
Board(6,2).Current=1
Board(5,1).Actual=1
Board(5,2).Actual=1
Board(5,3).Actual=1
Board(5,4).Actual=1
Board(6,2).Actual=1
endif
//Block 14
if BlockID=14
Board(5,1).Current=1
Board(5,2).Current=1
Board(5,3).Current=1
Board(5,4).Current=1
Board(4,2).Current=1
Board(5,1).Actual=1
Board(5,2).Actual=1
Board(5,3).Actual=1
Board(5,4).Actual=1
Board(4,2).Actual=1
endif
endif
remstart
//Set block
Board(5,1).Current=1
Board(6,1).Current=1
Board(6,2).Current=1
Board(5,1).Actual=1
Board(6,1).Actual=1
Board(6,2).Actual=1
remend
endfunction
Function UpdateTetris()
//Decide if new block is needed
Found=0
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Found=1
endif
next y
next x
if Found=0 then MakeBlock()
Tick=Tick+1
If Tick=50
Tick=0
endif
If Tick=1
//If no collision mvoe block down
if BlockCollision()=0
BlockDown()
endif
endif
//If collision make new block
if BlockCollision()=1
MakeBLock()
endif
if Spacekey()=1
if BlockCollision()=0
BlockDown()
endif
endif
If LeftPress=0
if leftkey()=1
If BlockCollisionLeft()=0
BlockLeft()
endif
LeftPress=1
endif
endif
if Leftkey()=0
LeftPress=0
endif
If RightPress=0
if Rightkey()=1
If BlockCollisionRight()=0
BlockRight()
endif
RightPress=1
endif
endif
if RightKey()=0
RightPress=0
endif
if UpPressed=0
If Upkey()=1
RotateBlock()
UpPressed=1
Endif
endif
If Upkey()=0
UpPressed=0
endif
//Standalone Functions
ClearLines()
endfunction
Function BlockCollision()
//Check for collision downwards
collision=0
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
if y+1<21
if Board(x,y+1).Current=0
if Board(x,y+1).Actual=1
collision=1
endif
endif
endif
If y=20
Collision=1
endif
endif
next y
next x
endfunction Collision
Function BlockCollisionLeft()
collision=0
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
if x-1>0
if Board(x-1,y).Current=0
if Board(x-1,y).Actual=1
collision=1
endif
endif
endif
endif
next y
next x
endfunction collision
Function BlockCollisionRight()
collision=0
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
if x+1<11
if Board(x+1,y).Current=0
if Board(x+1,y).Actual=1
collision=1
endif
endif
endif
endif
next y
next x
endfunction collision
function BlockDown()
// Remove .Current from .Actual
for x=1 to 10
for y=1 to 20
If Board(x,y).Current=1
Board(x,y).Actual=0
endif
next y
next x
//Clear .Transform
for x=1 to 10
for y=1 to 20
Board(x,y).Transform=0
next y
next x
//Move .Current to .Transform+1
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
if y+1<21
Board(x,y).Current=0
Board(x,y+1).Transform=1
endif
endif
next y
next x
//.Transform to .Current
for x=1 to 10
for y=1 to 20
if Board(x,y).Transform=1
Board(x,y).Current=1
endif
next y
next x
//Place back .Current of .Actual
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Board(x,y).Actual=1
endif
next y
next x
endfunction
Function BlockLeft()
//Remove .Current from .Actual
For x=1 to 10
for y=1 to 20
If Board(x,y).Current=1
Board(x,y).Actual=0
endif
next y
next x
//Clear .Transform
for x=1 to 10
for y=1 to 20
Board(x,y).Transform=0
next y
next x
//Move .Current to .Transform-1
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Board(x,y).Current=0
Board(x-1,y).Transform=1
endif
next y
next x
//Move .Trnasform to .Current
for x=1 to 10
for y=1 to 20
if Board(x,y).Transform=1
Board(x,y).Current=1
Board(x,y).Actual=1
endif
next y
next x
endfunction
Function BlockRight()
//Remove .Current from .Actual
For x=1 to 10
for y=1 to 20
If Board(x,y).Current=1
Board(x,y).Actual=0
endif
next y
next x
//Clear .Transform
for x=1 to 10
for y=1 to 20
Board(x,y).Transform=0
next y
next x
//Move .Current to .Transform-1
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Board(x,y).Current=0
Board(x+1,y).Transform=1
endif
next y
next x
//Move .Trnasform to .Current
for x=1 to 10
for y=1 to 20
if Board(x,y).Transform=1
Board(x,y).Current=1
Board(x,y).Actual=1
endif
next y
next x
endfunction
Function RotateBlock()
xMin=0
xMax=0
yMin=0
yMax=0
//Determine xMin
for x=1 to 10
for y=1 to 20
if xMin<1
if Board(x,y).Current=1
xMin=x
endif
endif
next y
next x
//Determine xMax
for x=1 to 10
for y=1 to 20
xNew=11-x
if xMax<1
if Board(xNew,y).Current=1
xMax=xNew
endif
endif
next y
next x
//determin yMin
for y=1 to 20
for x=1 to 10
if yMin<1
if Board(x,y).Current=1
Ymin=y
endif
endif
next x
next y
//determin yMax
for y=1 to 20
for x=1 to 10
yNew=21-y
if yMax<1
if Board(x,yNew).Current=1
yMax=yNew
endif
endif
next x
next y
//Clear .Transform
for x=1 to 10
for y=1 to 20
Board(x,y).Transform=0
next y
next x
//Clear .Current from .Actual
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Board(x,y).Actual=0
endif
next y
next x
//Move .Current to .Transform withrotation
for x=xMin to xMax
for y=yMin to yMax
if Board(x,y).Current=1
Board(y,x).Transform=1
endif
next y
next x
//Clear .Current
for x=1 to 10
for y=1 to 20
Board(x,y).Current=0
next y
next x
//Move .Transform to .Current
for x=1 to 10
for y=1 to 20
if Board(x,y).Transform=1
Board(x,y).Current=1
endif
next y
next x
endfunction
Function ClearLines()
LineID=0
//check line if full
for y=1 to 20
Full=1
for x=1 to 10
if Board(x,y).Actual=0
Full=0
endif
next x
If Full=1
LineID=y
endif
Next y
If LineID>0
//Remove line
for x=1 to 10
Board(x,LineID).Actual=0
next x
//remove .Current from .Actual
for x=1 to 10
for y=1 to 20
if Board(x,y).Current=1
Board(x,y).Actual=0
endif
next y
next x
//Clear .Transform
for x=1 to 10
for y=1 to 20
Board(x,y).Transform=0
next y
next x
//Move .Actual to .Transform+1
for y=1 to (LineID-1)
for x=1 to 10
if Board(x,y).Actual=0
Board(x,y+1).Transform=1
endif
next x
next y
//Remove .Actual
for y=1 to (LineID-1)
for x=1 to 10
Board(x,y).Actual=0
next x
next y
//Move .Transform to .Actual
for x=1 to 10
for y=1 to 20
if Board(x,y).Transform=1
Board(x,y).Actual=1
endif
next y
next x
endif
endfunction LineID
Function CreateBoard()
//Create Box Array
for x=1 to 10
for y=1 to 20
ObjectID=FreeObject(20,1000)
Board(x,y).BlockID=ObjectID
make object cube ObjectId,4.7
Position object ObjectID,x*5,-(y*5),0
next y
next x
endfunction
function FreeObject(start,finish)
for i = start to finish
if object exist(i) = 0
freeobj = i
i = finish+1
endif
next i
endfunction freeobj
I plan to restart the project in GDK soon, provided that i can get the damn thing to compile...
I can count to banana...