Basic water physics like minecraft in 2d.
There are problem with this snippet R1 to R3 add and delete to fast.
warning 800+ lines snippet
Most of the size is the water checks.
/////////////////////////////////////////////////////////
// program name : water //
// Program Version : 1 //
/////////////////////////////////////////////////////////
// written by : Joseph Allen ( ALIEN ) //
// date started: 28/07/2011 //
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
// MAIN INIT //
/////////////////////////////////////////////////////////
sync on
sync rate 60
autocam off
hide mouse
color backdrop 0, 0
rem disable escapekey
set text font "Arial"
set text size 16
set text transparent
randomize timer()
gosub Sub_Veribles
FUN_Defalt_Map( )
/////////////////////////////////////////////////////////
// MAIN LOOP //
/////////////////////////////////////////////////////////
do
cls RGB(0,0,0)
FUN_Map_Controls( )
IF timer( ) >= Water_TIMER + 1000
Water_TIMER = timer()
FUN_Water_Controls( )
endif
If controlkey( ) = 0 then control_Key_hold = 0
If controlkey( ) = 1 and control_Key_hold = 0
control_Key_hold = 1
rem clear data
FOR Y = 1 to 30
FOR X = 1 to 30
World_data( X , Y ) = 0
next X
next Y
FUN_Defalt_Map( )
endif
If spacekey( ) = 0 then space_Key_hold = 0
If spacekey( ) = 1 and space_Key_hold = 0
space_Key_hold = 1
randomize timer()
rem clear data
FOR Y = 1 to 30
FOR X = 1 to 30
World_data( X , Y ) = 0
next X
next Y
rem add stone
FOR N = 1 to 200
TEMP_RND_X = rnd( 30 )
TEMP_RND_Y = rnd( 30 )
World_data( TEMP_RND_X , TEMP_RND_Y ) = 1
next N
rem add water
FOR N = 1 to 10
TEMP_RND_X = rnd( 30 )
TEMP_RND_Y = rnd( 30 )
World_data( TEMP_RND_X , TEMP_RND_Y ) = 2
next N
endif
FUN_Draw_World( )
ink RGB(255,0,0) , 0
text 526 , 0 , "fps: " + str$( screen fps() )
text 526 , 20 , "Selected: " + str$( Selected_Item )
IF Selected_Item = 1 then text 526 , 40 , "ROCK"
IF Selected_Item = 2 then text 526 , 40 , "WATER"
Text 526 , 60 , "1 / 2 to change selected Block"
Text 526 , 100 , "space bar to randomize the map"
Text 526 , 120 , "control key to change to defalt map"
center text screen width() / 2 , screen height() - 20 , "Water Version: 1 By Joseph Allen ( ALIEN )"
FUN_Draw_Mouse()
sync
loop
/////////////////////////////////////////////////////////
// SUBS //
/////////////////////////////////////////////////////////
Sub_Veribles:
DIm World_data( 30 , 30 )
global Selected_Item as integer
Selected_Item = 1
global Water_TIMER as integer
Water_TIMER = 0
return
/////////////////////////////////////////////////////////
// FUNCTIONS //
/////////////////////////////////////////////////////////
Function FUN_Water_Controls( )
FOR Y = 30 to 1 step -1
FOR X = 1 to 30
Rem add water flowwing down below souce water
IF Y + 1 <= 30
If World_data( X , Y ) = 2
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem remove water if souce water not above it
IF Y - 1 >= 1
IF World_data( X , Y ) = 3
IF World_data( X , Y - 1 ) = 0
World_data( X , Y ) = 0
endif
endif
endif
Rem add water flowwing down below water flowwing down
IF Y + 1 <= 30
If World_data( X , Y ) = 3
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem remove water flowing down if dtone above it
IF Y - 1 >= 1
IF World_data( X , Y ) = 3
IF World_data( X , Y - 1 ) = 1
World_data( X , Y ) = 0
endif
endif
endif
Rem add Diangle to flowing water if stome below it and no block below diangle position
rem left diangle
IF X - 1 >= 1 and Y + 1 <= 30
IF World_data( X , Y ) = 3 or World_data( X , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y ) = 0
IF World_data( X - 1 , Y + 1 ) = 0 or World_data( X - 1 , Y + 1 ) = 2 or World_data( X - 1 , Y + 1 ) >= 4
World_data( X - 1 , Y ) = 4
endif
endif
endif
endif
endif
rem Ridht diangle
IF X + 1 <= 30 and Y + 1 <= 30
IF World_data( X , Y ) = 3 or World_data( X , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y ) = 0
IF World_data( X + 1 , Y + 1 ) = 0 or World_data( X + 1 , Y + 1 ) = 2 or World_data( X + 1 , Y + 1 ) >= 4
World_data( X + 1 , Y ) = 5
endif
endif
endif
endif
endif
Rem add water below diangle left
IF Y + 1 <= 30
IF World_data( X , Y ) = 4
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
Rem add water below diangle Right
IF Y + 1 <= 30
IF World_data( X , Y ) = 5
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem remove diangle left if no water flowing down right position
IF X + 1 <= 30
IF World_data( X , Y ) = 4
IF World_data( X + 1 , Y ) = 0
World_data( X , Y ) = 0
endif
endif
endif
rem remove diangle right if no water flowing down right position
IF X - 1 >= 1
IF World_data( X , Y ) = 5
IF World_data( X - 1 , Y ) = 0
World_data( X , Y ) = 0
endif
endif
endif
Rem add diangle left if souce water on the right
IF X - 1 >= 1 and Y + 1 <= 30
IF World_data( X , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y ) = 0
IF World_data( X - 1 , Y + 1 ) = 0
World_data( X - 1 , Y ) = 4
endif
endif
endif
endif
endif
Rem add diangle Right if souce water on the left
IF X + 1 <= 30 and Y + 1 <= 30
IF World_data( X , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y ) = 0
IF World_data( X + 1 , Y + 1 ) = 0
World_data( X + 1 , Y ) = 5
endif
endif
endif
endif
endif
rem remove diangle left if stone right position
IF X + 1 <= 30
IF World_data( X , Y ) = 4
IF World_data( X + 1 , Y ) = 1
World_data( X , Y ) = 0
endif
endif
endif
rem remove diangle right if stone right position
IF X - 1 >= 1
IF World_data( X , Y ) = 5
IF World_data( X - 1 , Y ) = 1
World_data( X , Y ) = 0
endif
endif
endif
rem add Left 3 if below if stone and souce water on the right and stone below souce water
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 6
endif
endif
endif
endif
endif
endif
rem add Left 3 if below if stone and Flowwing water down on the right and stone below Flowwing water down
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 3
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 6
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 3
If Y + 1 <= 30
If World_data( X , Y ) = 6
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem add right 3 if below if stone and souce water on the left and stone below souce water
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 2
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 9
endif
endif
endif
endif
endif
endif
rem add right 3 if below if stone and Flowwing water down on the left and stone below Flowwing water down
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 3
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 9
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 3
If Y + 1 <= 30
If World_data( X , Y ) = 9
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem add Left 2 if below if stone and souce water on the right and stone below souce water
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 6
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 7
endif
endif
endif
endif
endif
endif
rem add Left 2 if below if stone and Flowwing water down on the right and stone below Flowwing water down
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 6
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 7
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 2
If Y + 1 <= 30
If World_data( X , Y ) = 7
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem add right 2 if below if stone and souce water on the left and stone below souce water
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 9
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 10
endif
endif
endif
endif
endif
endif
rem add right 2 if below if stone and Flowwing water down on the left and stone below Flowwing water down
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 9
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 10
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 2
If Y + 1 <= 30
If World_data( X , Y ) = 10
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem add Left 1 if below if stone and souce water on the right and stone below souce water
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 7
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 8
endif
endif
endif
endif
endif
endif
rem add Left 1 if below if stone and Flowwing water down on the right and stone below Flowwing water down
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 7
IF World_data( X , Y + 1 ) = 1
IF World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 8
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 1
If Y + 1 <= 30
If World_data( X , Y ) = 8
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem add right 1 if below if stone and souce water on the left and stone below souce water
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 10
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 11
endif
endif
endif
endif
endif
endif
rem add right 1 if below if stone and Flowwing water down on the left and stone below Flowwing water down
If Y + 1 <= 30
IF X - 1 >= 1
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 10
IF World_data( X , Y + 1 ) = 1
IF World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 11
endif
endif
endif
endif
endif
endif
rem add flowwing water down below left 1
If Y + 1 <= 30
If World_data( X , Y ) = 11
IF World_data( X , Y + 1 ) = 0
World_data( X , Y + 1 ) = 3
endif
endif
endif
rem remove left 3 of no flowing water down or stone block is there
IF X + 1 <= 30
IF World_data( X , Y ) = 6
IF World_data( X + 1 , Y ) = 0 or World_data( X + 1 , Y ) = 1 or World_data( X + 1 , Y ) = 6
World_data( X , Y ) = 0
endif
endif
endif
rem remove left 2 of no flowing water down or stone block is there
IF X + 1 <= 30
IF World_data( X , Y ) = 7
IF World_data( X + 1 , Y ) = 0 or World_data( X + 1 , Y ) = 1 or World_data( X + 1 , Y ) = 3
World_data( X , Y ) = 0
endif
endif
endif
rem remove left 1 of no flowing water down or stone block is there
IF X + 1 <= 30
IF World_data( X , Y ) = 8
IF World_data( X + 1 , Y ) = 0 or World_data( X + 1 , Y ) = 1 or World_data( X + 1 , Y ) = 3
World_data( X , Y ) = 0
endif
endif
endif
rem remove right 3 of no flowing water down or stone block is there
IF X - 1 >= 1
IF World_data( X , Y ) = 9
IF World_data( X - 1 , Y ) = 0 or World_data( X - 1 , Y ) = 1 or World_data( X - 1 , Y ) = 9
World_data( X , Y ) = 0
endif
endif
endif
rem remove right 2 of no flowing water down or stone block is there
IF X - 1 >= 1
IF World_data( X , Y ) = 10
IF World_data( X - 1 , Y ) = 0 or World_data( X - 1 , Y ) = 1 or World_data( X - 1 , Y ) = 3
World_data( X , Y ) = 0
endif
endif
endif
rem remove right 1 of no flowing water down or stone block is there
IF X - 1 >= 1
IF World_data( X , Y ) = 11
IF World_data( X - 1 , Y ) = 0 or World_data( X - 1 , Y ) = 1 or World_data( X - 1 , Y ) = 3
World_data( X , Y ) = 0
endif
endif
endif
rem add left 2 if below is no block and right is left 3
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 6
If World_data( X , Y + 1 ) = 0
If World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 7
endif
endif
endif
endif
endif
endif
rem add left 1 if below is no block and right is left 2
If Y + 1 <= 30
IF X + 1 <= 30
If World_data( X , Y ) = 0
If World_data( X + 1 , Y ) = 7
If World_data( X , Y + 1 ) = 0
If World_data( X + 1 , Y + 1 ) = 1
World_data( X , Y ) = 8
endif
endif
endif
endif
endif
endif
rem add right 2 if below is no block and left is right 3
If Y + 1 <= 30
IF X - 1 <= 30
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 9
If World_data( X , Y + 1 ) = 0
If World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 10
endif
endif
endif
endif
endif
endif
rem add right 1 if below is no block and left is right 2
If Y + 1 <= 30
IF X - 1 <= 30
If World_data( X , Y ) = 0
If World_data( X - 1 , Y ) = 10
If World_data( X , Y + 1 ) = 0
If World_data( X - 1 , Y + 1 ) = 1
World_data( X , Y ) = 11
endif
endif
endif
endif
endif
endif
rem change left 1 to 3 and right 1 to 3 to flowing water down if flowing water above
FOR N = 6 to 11
If Y - 1 >= 1
IF World_data( X , Y ) = N
IF World_data( X , Y - 1 ) = 3
World_data( X , Y ) = 3
endif
endif
endif
next N
Rem change diangle left anf right to flowing water if flowing water above
IF Y - 1 >= 1
IF World_data( X , Y ) = 4 or World_data( X , Y ) = 5
IF World_data( X , Y - 1 ) = 3
World_data( X , Y ) = 3
endif
endif
endif
rem change diangle left to water flowing down if diangle right is above it
IF Y - 1 >= 1
IF World_data( X , Y ) = 4
IF World_data( X , Y - 1 ) = 5
World_data( X , Y ) = 3
endif
endif
endif
rem change diangle right to water flowing down if diangle left is above it
IF Y - 1 >= 1
IF World_data( X , Y ) = 5
IF World_data( X , Y - 1 ) = 4
World_data( X , Y ) = 3
endif
endif
endif
next X
next Y
endfunction
Function FUN_Map_Controls( )
IF keystate( 2 ) = 1
Selected_Item = 1
endif
IF keystate( 3 ) = 1
Selected_Item = 2
endif
IF mouseclick( ) > 0
FOR Y = 1 to 30
FOR X = 1 to 30
TEMP_X = 16 + ( X * 16 )
TEMP_Y = 16 + ( Y * 16 )
IF FUN_point_within_Box( mousex( ) + 16 , mouseY( ) + 16 , TEMP_X , TEMP_Y , 15 , 15 ) = 1
IF mouseclick( ) = 1
World_data( X , Y ) = Selected_Item
endif
IF mouseclick( ) = 2
World_data( X , Y ) = 0
endif
endif
next X
next Y
endif
endfunction
Function FUN_Defalt_Map( )
FOR Y = 15 to 30
FOR X = 1 to 30
World_data( X , Y ) = 1
next X
next Y
endfunction
Function FUN_Draw_Mouse( )
ink RGB(255,255,255) , 0
Temp_Size = 5
Temp_X = mousex( )
Temp_Y = mousey( )
BOX Temp_X , Temp_Y , Temp_X + Temp_Size , Temp_Y + Temp_Size
endfunction
Function FUN_Draw_World( )
ink RGB(255,0,0) , 0
TEmp_Border_Width = 2
TEMP_X = 16 - TEmp_Border_Width
TEMP_Y = 16 - TEmp_Border_Width
TEMP_Width = ( 30 * 16 ) + TEmp_Border_Width
TEMP_Height = ( 30 * 16 ) + TEmp_Border_Width
BOX TEMP_X , TEMP_Y , TEMP_X + TEMP_Width , TEMP_Y + TEMP_Height
FOR Y = 1 to 30
FOR X = 1 to 30
rem blank
IF World_data( X , Y ) = 0
ink RGB(0,0,0) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
endif
rem stone
IF World_data( X , Y ) = 1
ink RGB(128,128,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
endif
rem water souce
IF World_data( X , Y ) = 2
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "S"
endif
rem water flow down
IF World_data( X , Y ) = 3
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "B"
endif
rem water flow Diangle left
IF World_data( X , Y ) = 4
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "L"
endif
rem water flow Diangle Right
IF World_data( X , Y ) = 5
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "R"
endif
rem water flow Flat left 3
IF World_data( X , Y ) = 6
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "L3"
endif
rem water flow Flat left 2
IF World_data( X , Y ) = 7
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "L2"
endif
rem water flow Flat left 1
IF World_data( X , Y ) = 8
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "L1"
endif
rem water flow Flat Right 3
IF World_data( X , Y ) = 9
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "R3"
endif
rem water flow Flat Right 2
IF World_data( X , Y ) = 10
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "R2"
endif
rem water flow Flat Right 1
IF World_data( X , Y ) = 11
ink RGB(0,0,128) , 0
TEMP_X = X * 16
TEMP_Y = Y * 16
BOX TEMP_X , TEMP_Y , TEMP_X + 14 , TEMP_Y + 14
ink RGB(255,255,255) , 0
Text Temp_X , TEMP_Y , "R1"
endif
next X
next Y
endfunction
function FUN_point_within_Box( Point_X , Point_Y , Box_Left , Box_Top , Box_Width , Box_Height )
Return_Value = 0
IF Point_X >= Box_Left
IF Point_X <= Box_Left + Box_Width
IF Point_Y >= Box_Top
IF Point_Y <= Box_Top + Box_Height
Return_Value = 1
endif
endif
endif
endif
endfunction Return_Value
/////////////////////////////////////////////////////////
// TERMINATE //
/////////////////////////////////////////////////////////
`end of code
/////////////////////////////////////////////////////////
i'm starting working on a more complex version of water physics.
i will post it in a day ot 2.
The attachment is the snippet in a zip file.