Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Tetris - my second 2d game -Xmas day game

Author
Message
mmouse
8
Years of Service
User Offline
Joined: 6th Nov 2015
Location:
Posted: 26th Dec 2015 06:56
Rem Project: tetris
Rem Created: Friday, December 25, 2015

Rem ***** Main Source File *****
REM a shape is 3x3
rem the grid is 30 height and 10 width, 0 is at the top
rem the speed is set by the inkey$() number

set display mode 800,600,32 : sync rate 60: sync on : backdrop off
bg = 1

load image "media\bg.jpg",bg
global csa `current shape active
global h = 30
global w = 10
global level =1
global blankleft
global blankright
global blanktop
global blankbottom
global lastshape
global score = 0
lastshape = -1
dim color(9)
color(0) = rgb(0,0,0)
color(1) = rgb(255,0,0)
color(2) = rgb(0,255,0)
color(3) = rgb(0,0,255)
color(4) = rgb(0,255,255)
color(5) = rgb(255,255,255)
color(6) = rgb(255,255,0)
color(7) = rgb(127,127,127)
color(8) = rgb(255,127,0)
color(9) = rgb(255,0,127)
dim grid(20,40) `give extra room at the bottom
dim shape(2,2)
dim temp(2,2)

for x = 1 to 20
for y = 1 to 30
grid(x,y) = 0
next
NEXT


type current
x as integer
y as integer
active
color
no
r
endtype

cas as current
cas.x = 10
cas.y = 0
cas.active = 0
cas.no = 0


rem game variables
time = 0
randomize timer()

REM **************MAIN GAME LOOP****************
while escapekey() = 0
cls
if cas.active = 0 then gosub new_shape
`paste image 1,0,0 `set the back drop
gosub draw_fallen `draw the fallen shapes
gosub draw_shape
gosub check_line
if downkey() = 1 and keydown = 0 then templevel = 20
if leftkey() = 1 and keydown = 0
ok = 1
dec cas.x
if cas.x < 0 and blankleft = 0 and blankleft2 = 0 then cas.x = 0
if cas.x < -1 and blankleft = 1 and blankleft2 = 0 then cas.x =-1
if cas.x < -2 and blankleft = 1 and blankleft2 = 1 then cas.x =-2
for x = 0 to 2
for y = 0 to 2
if (grid(cas.x+X,cas.y+y) > 0 and shape(x,y) >0 ) or cas.y > h-3 then ok =0
next
next
if ok = 0 then inc cas.x
endif

if rightkey() = 1 and keydown = 0
ok = 1
inc cas.x
if cas.x > w-3 and blankright = 0 and blankright2 = 0 then cas.x = w-3
if cas.x > w-2 and blankright = 1 and blankright2 = 0 then cas.x = w-2
if cas.x > w-1 and blankright = 1 and blankright2 = 1 then cas.x = w-1
for x = 0 to 2
for y = 0 to 2
if (grid(cas.x+X,cas.y+y) >0 and shape(x,y) >0 ) or cas.y > h-3 then ok =0
next
next
if ok = 0 then dec cas.x
endif

if upkey() = 1 and keydown = 0 and rotate = 0 then gosub rotate_shape

inc time
if time > 20-templevel then gosub down_shape : time = 0 : rotate = 0
keydown = leftkey() + rightkey()+upkey()+ downkey()
if inkey$() = "q" and q = 0
q = 1
gosub new_game
endif
if inkey$() <>"q" then q = 0

ink rgb(255,255,255),rgb(255,255,255)
set text size 32
text 400,0,"Level:"+str$(level)
text 400,40,"Score:"+str$(score)
text 400,100,"Next Level in:"+str$(10-lines)

sync
ENDWHILE

REM **************END MAIN GAME LOOP****************



REM check for a line
check_line:
for y = 30 to 0 step -1
cs = 0
for x = 0 to 10
if grid(x,y) <> 0 then inc cs
next
if cs > 9 `full line
inc score, 10
inc lines
if lines = 11 then lines = 0 : inc level
for y1 = y to 1 step -1
for x1 = 0 to 10
grid(x1,y1) = grid(x1,y1-1)
next
next
endif
next
return

REM SHAPE FALLS
down_shape:
inc cas.y
for x = 0 to 2
for y = 0 to 2
if (grid(cas.x+X,cas.y+y) > 0 and shape(x,y) >0) then cas.active = 0
next
next

if (cas.y > 27 and blankbottom =0 and blankbottom2 = 0) and cas.active = 1 then cas.active = 0
if (cas.y > 28 and blankbottom =1 and blankbottom2 = 0) and cas.active = 1 then cas.active = 0
if (cas.y > 29 and blankbottom =1 and blankbottom2 = 1) and cas.active = 1 then cas.active = 0

IF CAS.ACTIVE = 0
dec cas.y
for x = 0 to 2
for y = 0 to 2
if grid(x+cas.x,y+cas.y) = 0 then grid(x+cas.x,y+cas.y)= shape(x,y)
next
NEXT
ENDIF

return

REM DRAW FALLEN AND the number GRID and square lines
draw_fallen:
for x = 0 to w
for y = 0 to h
ink color(grid(x,y)),color(grid(x,y)) : box x*20,y*20,(x*20)+20,(Y*20)+20
next
next

ink rgb(255,255,255),rgb(255,255,255)
for y = 0 to h
line 0,y*20,200,y*20
next

for x = 0 to w
line x*20,0,x*20,600
next
return

REM DRAW SHAPE
draw_shape:
for y = 0 to 2
for x = 0 to 2
if shape(x,y) > 0 then ink color(shape(x,y)),color(shape(x,y)) : box (cas.x+x)*20,(cas.y+y)*20,20+((cas.x+x)*20),20+((cas.y+y)*20)
next
NEXT
return

REM CHECK FOR LOSS USING CURRENT SHAPE
check_loss:
if cas.y-3 < 0
set text size 24
text 200,200,"End game"
text 0,250,"Press any key to start"
while inkey$() = "" and kd = 0
sync
kd = downkey() + upkey()+ leftkey() + rightkey()
ENDWHILE
gosub new_game
endif
return

REM NEW SHAPE
new_shape:
gosub check_loss


blankleft =0
blankright = 0
blanktop =0
blankbottom = 0
rndx = rnd(6)+1
while rndx = lastshape
rndx = rnd(6)+1
endwhile
cas.x = 4
cas.y = 0
rotate = 0
cas.no = rndx
templevel = level

rem 0 = Z
if rndx = 1
cas.no = 1
cas.color = 1
shape(0,0) = 1
shape(0,1) = 1
shape(0,2) = 0
shape(1,0) = 0
shape(1,1) = 1
shape(1,2) = 0
shape(2,0) = 0
shape(2,1) = 1
shape(2,2) = 1
ENDIF
rem 1 = L
if rndx = 2
cas.no = 2
cas.color = 2
shape(0,0) = 2
shape(0,1) = 0
shape(0,2) = 0
shape(1,0) = 2
shape(1,1) = 0
shape(1,2) = 0
shape(2,0) = 2
shape(2,1) = 2
shape(2,2) = 0
ENDIF

rem T
if rndx = 3
cas.no = 3
cas.color = 3
shape(0,0) = 3
shape(0,1) = 3
shape(0,2) = 3
shape(1,0) = 0
shape(1,1) = 3
shape(1,2) = 0
shape(2,0) = 0
shape(2,1) = 0
shape(2,2) = 0
blankright = 1
ENDIF

rem block
if rndx = 4
cas.no = 4
cas.color = 4
shape(0,0) = 4
shape(0,1) = 4
shape(0,2) = 0
shape(1,0) = 4
shape(1,1) = 4
shape(1,2) = 0
shape(2,0) = 0
shape(2,1) = 0
shape(2,2) = 0
blankbottom = 1
blankright = 1
ENDIF

rem I
if rndx = 5
cas.no = 5
cas.color = 5
shape(0,0) = 5
shape(0,1) = 5
shape(0,2) = 5
shape(1,0) = 0
shape(1,1) = 0
shape(1,2) = 0
shape(2,0) = 0
shape(2,1) = 0
shape(2,2) = 0
blankright = 1
blankright2 = 1
ENDIF

rem reverse L
if rndx = 6
cas.no = 6
cas.color = 6
shape(0,0) = 0
shape(0,1) = 0
shape(0,2) = 0
shape(1,0) = 0
shape(1,1) = 0
shape(1,2) = 6
shape(2,0) = 6
shape(2,1) = 6
shape(2,2) = 6
blankleft = 1
ENDIF

rem reverse Z
if rndx = 7
cas.no = 7
cas.color = 7
shape(0,0) = 7
shape(0,1) = 0
shape(0,2) = 0
shape(1,0) = 7
shape(1,1) = 7
shape(1,2) = 7
shape(2,0) = 0
shape(2,1) = 0
shape(2,2) = 7
ENDIF

cas.active = 1
lastshape = rndx
return

rem rotating shapes
rotate_shape:
blankleft =0
blankright = 0
blanktop =0
blankbottom = 0
blankleft2 =0
blankright2 = 0
blanktop2 =0
blankbottom2 = 0
if cas.x < 0 then cas.x = 0
if cas.x > 7 then cas.x = 7
rotate = 1
for x = 0 to 2
for y = 0 to 2
temp(x,y) = shape(x,y)
next
NEXT
shape(0,0)= temp(2,0)
shape(1,0) =temp(2,1)
shape(2,0) =temp(2,2)

shape(0,1)= temp(1,0)
shape(1,1) =temp(1,1)
shape(2,1) =temp(1,2)

shape(0,2)= temp(0,0)
shape(1,2) =temp(0,1)
shape(2,2) =temp(0,2)

if shape(0,0) + shape(0,1)+shape(0,2) = 0 then blankleft = 1
if shape(1,0) + shape(1,1)+shape(1,2) = 0 and blankleft =1 then blankleft2 = 1

if shape(0,0) + shape(1,0)+shape(2,0) = 0 then blanktop = 1
if shape(0,1) + shape(1,1)+shape(2,1) = 0 and blanktop =1 then blanktop2 = 1

if shape(0,2) + shape(1,2)+shape(2,2) = 0 then blankbottom = 1
if shape(0,1) + shape(1,1)+shape(2,1) = 0 and blankbottom =1 then blankbottom2 = 1

if shape(2,0) + shape(2,1)+shape(2,2) = 0 then blankright = 1
if shape(1,0) + shape(1,1)+shape(1,2) = 0 and blankright = 1 then blankright2 = 1

return

new_game:
level = 0
score = 0
score = 0
level = 0
lines = 0
rem reset grid
for x = 0 to 10
for y= 0 to 30
grid(x,y) =0
next
NEXT
cas.active = 0
return:

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-04-18 17:33:09
Your offset time is: 2024-04-18 17:33:09