Ok, so i have been browsing these boards for a while, everyone has been great, and very helpfull, thought it about time i gave something back.
This is the code to tetris, its slighty sloppy as im still a newbie, but this is all the process of learning.
The game plays well and as far as i can see is bug free.. I havent provided no media, as the code is now it loads the following 3 images.
C:\back.bmp - 640 * 480
Background image
C:\bar.bmp - 240 * 20
White image, ya will never see this in game, its just used for collision detection
C:\tetris_tiles.bmp - 160 * 20
This is the last image containing 8 20*20 boxs used to make the tetris shapes from. Black is set as transparent colour so if you want to use otherwise then ya best change the code
I have removed the music code, Hope this is of use to someone
`loads bitmap with tetris tiles on it
load bitmap "C:\tetris_tiles.bmp",1
`load music "atari.mid",1
`load music "decision.mid",2
set current bitmap 1
`gets 2 images from the tiles sheet
get image 1,1,1,20,20,1
get image 2,21,1,40,20,1
get image 3,41,1,60,20,1
get image 4,61,1,80,20,1
get image 5,81,1,100,20,1
get image 6,101,1,120,20,1
get image 7,121,1,140,20,1
get image 8,141,1,160,20,1
`unloads the bmp
delete bitmap 1
load bitmap "C:\back.bmp",2
set current bitmap 2
get image 9,0,0,640,480
delete bitmap 2
sprite 9,0,0,9
load bitmap "C:\bar.bmp",3
set current bitmap 3
get image 10,0,0,240,20
delete bitmap 3
`sprite 1-4 will be used for the current shape
global piece_colour
global active_piece_number
dim old_x(4)
dim old_y(4)
dim sprites_to_kill(12)
`draws the boarder pieces
x_value = 170
for x = 11 to 24
sprite x, x_value,400,8
x_value = x_value + 20
next x
y_value = 400
for x = 25 to 45
y_value = y_value - 20
sprite x, 170,y_value,8
next x
y_value = 400
for x = 46 to 67
y_value = y_value - 20
sprite x, 430,y_value,8
next x
`play music 1
randomize timer()
random_number =rnd(6)+1
make_piece(random_number)
dropped_piece = 73
left_repeat = timer() : right_repeat = timer() : up_repeat = timer() : down_repeat = timer() : DropTimer = timer()
key_delay = 100 : drop_speed = 300
DO
IF KEYSTATE(200)=0 then up_key_not_pressed = 1
IF KEYSTATE(203)=0 then left_key_not_pressed = 1
IF KEYSTATE(205)=0 then right_key_not_pressed = 1
IF LEFTKEY() = 1
if left_key_not_pressed = 1
left_key_not_pressed = 0
FOR X = 1 TO 4
MY_X = SPRITE X(X)-20
mY_Y = SPRITE Y(X)
DELETE SPRITE X
SPRITE X,MY_X, MY_Y,piece_colour
NEXT X
FOR Y = 11 TO (dropped_piece - 1)
FOR X = 1 TO 4
IF SPRITE COLLISION(X,Y) = 1
FOR Z = 1 TO 4
mY_x = SPRITE X(z)+20
MY_Y = SPRITE Y(Z)
DELETE SPRITE Z
SPRITE Z,mY_X,mY_Y,piece_colour
NEXT Z
ENDIF
NEXT X
NEXT Y
ENDIF
Endif
IF RightKEY() = 1
if right_key_not_pressed = 1
right_key_not_pressed = 0
FOR X = 1 TO 4
MY_X = SPRITE X(X)+20
mY_Y = SPRITE Y(X)
DELETE SPRITE X
SPRITE X,MY_X, MY_Y,piece_colour
NEXT X
FOR Y = 11 TO (dropped_piece - 1)
FOR X = 1 TO 4
IF SPRITE COLLISION(X,Y) = 1
FOR Z = 1 TO 4
mY_x = SPRITE X(z)-20
MY_Y = SPRITE Y(Z)
DELETE SPRITE Z
SPRITE Z,mY_X,mY_Y,piece_colour
NEXT Z
ENDIF
NEXT X
NEXT Y
ENDIF
Endif
if upkey() = 1
`stops square shape rotating
if active_piece_number <> 7
if up_key_not_pressed = 1
up_key_not_pressed = 0
`loops around 4 times, once for each sprite that
`makes up the tetris shappe
for x = 1 to 4
`writes original co-ordinates to
`some variables
old_x(x) = sprite x(x)
old_y(x) = sprite y(x)
`works out how far the sprite is from sprite 2
`2 calculations one for he x and on for the y
var1 = sprite x(x)-sprite x(2) : var2 = sprite y(x)-sprite y(2)
`works out the location to move the sprite to
`this is a offset from sprite 2
var3 = sprite x(2)-var2 : var4 = sprite y(2)+var1
`deletes the old sprite
delete sprite x
`draws a new sprite
sprite x,var3,var4,piece_colour
next x
FOR Y = 11 TO (dropped_piece - 1)
FOR X = 1 TO 4
IF SPRITE COLLISION(X,Y) = 1
FOR Z = 1 TO 4
DELETE SPRITE Z
SPRITE Z,old_x(z),old_y(z),piece_colour
NEXT Z
ENDIF
NEXT X
NEXT Y
endif
endif
endif
IF DOWNKEY() = 1
FOR X = 1 TO 4
MY_X = SPRITE X(X)
mY_Y = SPRITE Y(X)+20
DELETE SPRITE X
SPRITE X,MY_X, MY_Y,piece_colour
NEXT X
FOR Y = 11 TO (dropped_piece - 1)
FOR X = 1 TO 4
IF SPRITE COLLISION(X,Y) = 1
FOR Z = 1 TO 4
mY_x = SPRITE X(z)
MY_Y = SPRITE Y(Z)-20
`ends the game if a sprite is pasted with a y
`that is less than 0
if sprite y(z) < 0 then end
DELETE SPRITE Z
SPRITE dropped_piece,mY_X,mY_Y,piece_colour
dropped_piece = dropped_piece + 1
NEXT Z
random_number=rnd(6)+1
make_piece(random_number)
ENDIF
NEXT X
NEXT Y
wait 40
ENDIF
if timer() - DropTimer => drop_speed
FOR X = 1 TO 4
MY_X = SPRITE X(X)
mY_Y = SPRITE Y(X)+20
DELETE SPRITE X
SPRITE X,MY_X, MY_Y,piece_colour
NEXT X
FOR Y = 11 TO (dropped_piece - 1)
FOR X = 1 TO 4
IF SPRITE COLLISION(X,Y) = 1
FOR Z = 1 TO 4
mY_x = SPRITE X(z)
MY_Y = SPRITE Y(Z)-20
`ends the game if a sprite is pasted with a y
`that is less than 0
if sprite y(z) < 0 then end
DELETE SPRITE Z
SPRITE dropped_piece,mY_X,mY_Y,piece_colour
dropped_piece = dropped_piece + 1
NEXT Z
random_number=rnd(6)+1
make_piece(random_number)
ENDIF
NEXT X
NEXT Y
DropTimer = timer()
endif
`loop to delete lines
for Start_y = -20 to 380 step 20
`makes check sprite if it doesnt exist
if sprite exist(68) <> 1 then sprite 68,190,Start_y,10
pieces_on_row = 0
`counts pieces on row
FOR x = 72 TO (dropped_piece - 1)
IF SPRITE COLLISION(68,x) = 1
pieces_on_row = pieces_on_row + 1
endif
next x
`starts a loop if theirs 12
`pieces on row
if pieces_on_row = 12
`deletes the row
FOR x = 72 TO (dropped_piece - 1)
IF SPRITE COLLISION(68,x) = 1
delete sprite x
endif
next x
FOR x = 72 TO (dropped_piece - 1)
if sprite exist(x)
if sprite y(x) < start_y
new_x=sprite x(x)
new_y=sprite y(x)+20
new_image=sprite image(x)
delete sprite x
sprite x,new_x,new_y,new_image
endif
endif
next x
endif
delete sprite 68
pieces_on_row = 0
next start_y
LOOP
function make_piece(piece_number)
active_piece_number = piece_number
SELECT piece_number: ` decides what piece to create
CASE 1
piece_colour=1
sprite 1, 310,(20-80),piece_colour
sprite 2, 310,(40-80),piece_colour
sprite 3, 310,(60-80),piece_colour
sprite 4, 330,(60-80),piece_colour
ENDCASE
CASE 2
piece_colour=2
sprite 1, 310,(20-80),piece_colour
sprite 2, 310,(40-80),piece_colour
sprite 3, 310,(60-80),piece_colour
sprite 4, 290,(60-80),piece_colour
ENDCASE
CASE 3
piece_colour=3
sprite 1, 290,(40-80),piece_colour
sprite 2, 310,(40-80),piece_colour
sprite 3, 330,(40-80),piece_colour
sprite 4, 310,(20-80),piece_colour
ENDCASE
CASE 4
piece_colour=4
sprite 1, 310,(20-80),piece_colour
sprite 2, 310,(40-80),piece_colour
sprite 3, 310,(60-80),piece_colour
sprite 4, 310,(80-80),piece_colour
ENDCASE
CASE 5
piece_colour=5
sprite 1, 330,(20-80),piece_colour
sprite 2, 310,(20-80),piece_colour
sprite 3, 310,(40-80),piece_colour
sprite 4, 290,(40-80),piece_colour
ENDCASE
CASE 6
piece_colour=6
sprite 1, 290,(20-80),piece_colour
sprite 2, 310,(20-80),piece_colour
sprite 3, 310,(40-80),piece_colour
sprite 4, 330,(40-80),piece_colour
ENDCASE
CASE 7
piece_colour=7
sprite 1, 290,(20-80),piece_colour
sprite 2, 290,(40-80),piece_colour
sprite 3, 310,(20-80),piece_colour
sprite 4, 310,(40-80),piece_colour
ENDCASE
ENDSELECT
endfunction
Enjoy
SAnTA