Final version of the game with all codes!
new features:
multi-player!
both play can see each others screen.
when one player gets rid of 2~4 lines at once the other person will 'gain' lines
try to play with a friend in Turing class!
single player mode is still possible.
some bug still exist.
but was never able to find it. it rarely occurs.
Turing: | View.Set ("title:Tetris Final")
const x := 10
const y := 10
const Ex := 310
const Ey := 10
const borderC := 25
const emptyBC := 30
const autoMove := 6
const UP_ARROW : char := chr (200)
const LEFT_ARROW : char := chr (203)
const RIGHT_ARROW : char := chr (205)
const DOWN_ARROW : char := chr (208)
const SPACE : char := chr (32)
var pPic, ePic : int
var frame := 0
var startTime := 0. 0
var freeze := 0
var cShape := 1
var center := 0
var canPress := 0
var temp1, temp2, temp3 : int
var Bcolor : int
var count := 0
var key : array char of boolean
var eBlock, block : array - 36 .. 288 of int
var map : array 1 .. 288 of int
var eMap : array 1 .. 288 of int
var movel, mover := 1
var movecount := 0
var b1, b2 := 0
var keyHold := 0
var score := 0. 0
var speed := 20. 0
var bug := 0
var safe := 0
var eTotalB := 0
for i : 1 .. 288
eMap (i ) := emptyBC
map (i ) := emptyBC
end for
for i : - 36 .. 288
block (i ) := emptyBC
eBlock (i ) := emptyBC
end for
var BC : array 1 .. 7 of int := init (4, 5, 2, 3, 6, 22, 103)
var blocks1 : array 1 .. 28 of string := init ("1000100010001000", "1100110000000000", "1000110001000000", "0100110010000000", "0100011100000000", "0100111000000000", "0010111000000000",
"1111000000000000", "1100110000000000", "0110110000000000", "1100011000000000", "0011001000100000", "0100011001000000", "0100010001100000",
"1000100010001000", "1100110000000000", "1000110001000000", "0100110010000000", "0000011100010000", "0000111001000000", "0000111010000000",
"1111000000000000", "1100110000000000", "0110110000000000", "1100011000000000", "0010001001100000", "0100110001000000", "1100010001000000")
var newadd, youradd : string
var net : int
var port : int
var choice : char
color (red)
put "Play at you own risks."
put "1-single, 2- two players"
loop
choice := getchar
exit when choice = "1" or choice = "2"
end loop
View.Set ("graphics:300;400,nobuttonbar")
if choice = "2" then
View.Set ("graphics:500;400,nobuttonbar")
loop
put "1-make server, 2- connect server"
choice := getchar
exit when choice = "1" or choice = "2"
end loop
if choice = "1"
then
youradd := Net.LocalAddress
put "Your machine address is ", youradd
put "input port number: " ..
get port
net := Net.WaitForConnection (port, newadd )
put "cool!! connected to ", newadd
choice := "2"
elsif choice = "2"
then
cls
put "input port number: " ..
get port
put "Other person's IP: " ..
get newadd
net := Net.OpenConnection (newadd, port )
put "connection to: ", newadd
if net <= 0 then
choice := "1"
return
end if
end if
end if
View.Set ("offscreenonly")
color (black)
proc moreLine (var somemap : array 1 .. 288 of int, howmany : int)
var where := Rand.Int (0, 11)
for decreasing i : 288 .. howmany * 12 + 1
somemap (i ) := somemap (i - 12 * howmany )
somemap (i - 12 * howmany ) := emptyBC
end for
for i : 0 .. howmany - 1
for j : 1 .. 12
if j not= where then
somemap (i * 12 + j ) := 4
end if
end for
end for
end moreLine
%
proc send (var somemap : array 1 .. 288 of int)
for i : 1 .. 288
put : net, somemap (i )
end for
end send
%
fcn totalB (var somemap : array 1 .. 288 of int) : int
var total := 0
for i : 1 .. 288
if somemap (i ) not= emptyBC then
total := total + 1
end if
end for
result total
end totalB
%
proc drawMap (startx : int, starty : int, var somemap : array 1 .. 288 of int, size : int)
for i : 0 .. 287
drawfillbox (startx + (i mod 12) * size, starty + (i div 12) * size, startx + (i mod 12) * size + size, starty + (i div 12) * size + size, somemap (i + 1))
drawbox (startx + (i mod 12) * size, starty + (i div 12) * size, startx + (i mod 12) * size + size, starty + (i div 12) * size + size, borderC )
end for
end drawMap
%
proc getPic (x : int, y : int, size : int, var id : int)
id := Pic.New (x, y, x + 12 * size, y + 24 * size )
end getPic
%
proc drawBlock (startx : int, starty : int, var someblock : array - 36 .. 288 of int, size : int)
for i : 0 .. 287
if someblock (i + 1) not= emptyBC then
drawfillbox (startx + (i mod 12) * size, starty + (i div 12) * size, startx + (i mod 12) * size + size, starty + (i div 12) * size + size, someblock (i + 1))
end if
end for
end drawBlock
%
proc combine (var somemap : array 1 .. 288 of int, var someblock : array - 36 .. 288 of int)
for i : 1 .. 288
if someblock (i ) not= emptyBC and somemap (i ) = emptyBC then
somemap (i ) := someblock (i )
end if
end for
end combine
%
fcn situation (var somemap : array 1 .. 288 of int, var someblock : array - 36 .. 288 of int) : int
for i : - 36 .. 288 - 12
if i <= 0 and someblock (i ) not= emptyBC then
result 3
elsif i > 0 and (someblock (i ) not= emptyBC ) and (somemap (i ) not= emptyBC ) then
result 2
end if
end for
for i : - 36 .. 288 - 12
if i > 0 and (someblock (i + 12) not= emptyBC and somemap (i ) not= emptyBC ) or (someblock (i ) not= emptyBC and i < 13) then
result 1
end if
end for
result 0
end situation
%
proc cBlock (var someblock : array - 36 .. 288 of int)
for i : - 36 .. 288
someblock (i ) := emptyBC
end for
for j : 0 .. 15
temp2 := strint (blocks1 (b1 + cShape * 7) (j + 1))
if temp2 not= 0 then
temp3 := (center - (j div 4) * 12 + (j mod 4) + 2)
if temp3 < 288 and temp3 > - 36 then
someblock (temp3 ) := BC (b1 )
end if
end if
end for
end cBlock
%
proc killLine (var somemap : array 1 .. 288 of int)
var yay : int
for decreasing i : 23 .. 0
yay := 0
for j : 1 .. 12
if somemap (i * 12 + j ) not= emptyBC then
yay := yay + 1
end if
end for
if yay = 12 then
score := score + (20 - speed ) * 50
if speed > 1 then
speed := speed * 0. 97
end if
for j : (i * 12) .. 274
somemap (j + 1) := somemap (j + 13)
end for
end if
end for
end killLine
%
fcn sideCheck (var someblock : array - 36 .. 288 of int) : int
var r, l := false
for i : 0 .. 23
if someblock (12 * i + 1) not= emptyBC then
r := true
end if
if someblock (12 * i ) not= emptyBC then
l := true
end if
end for
if l = true and r = true then
result 3
elsif l = true then
result 2
elsif r = true then
result 1
else
result 0
end if
end sideCheck
%
proc recieve (var somemap : array 1 .. 288 of int)
for i : 1 .. 288
get : net, somemap (i )
end for
drawMap (Ex, Ey, eMap, 10)
getPic (Ex, Ey, 10, ePic )
if eTotalB - totalB (somemap ) > 36 then
moreLine (map, 4)
elsif eTotalB - totalB (somemap ) < 48 and eTotalB - totalB (somemap ) > 24 then
moreLine (map, 2)
elsif eTotalB - totalB (somemap ) < 36 and eTotalB - totalB (somemap ) > 12 then
moreLine (map, 1)
end if
eTotalB := totalB (somemap )
end recieve
%
drawMap (Ex, Ey, eMap, 10)
getPic (Ex, Ey, 10, ePic )
startTime := Time.Elapsed
loop
combine (map, block )
killLine (map )
drawMap (x, y, map, 15)
getPic (x, y, 15, pPic )
if choice = "2"
then
send (map )
end if
center := 280
if b1 = 0 then
randint (b1, 1, upper (BC ))
else
b1 := b2
end if
randint (b2, 1, upper (BC ))
cShape := 0
cBlock (block )
if situation (map, block ) = 2 then
put "game over"
View.Update
exit
end if
loop
cls
frame + = 1
Pic.Draw (pPic, x, y, picCopy)
drawBlock (x, y, block, 15)
if movecount > speed and freeze = 0 then
center := center - 12
cBlock (block )
movecount := 0
end if
put "Load time: ", (Time.Elapsed - startTime ) / frame - (20 - round (1. 1 ** (20 - speed ))) : 1 : 1, " score: ", round (score ), " Speed: ", round (20 - speed ) %%%%
var yy := 30
for i : 0 .. 3
locate (i + 5, yy )
for j : 1 .. 4
if blocks1 (b2 ) (4 * i + j ) = "0" then
put " " ..
else
put "#" ..
end if
end for
put ""
end for
if choice = "2" then
if Net.CharAvailable (net ) then
safe := safe + 1
if safe > 20 then
recieve (eMap )
safe := 0
end if
end if
Pic.Draw (ePic, Ex, Ey, picCopy)
end if
View.Update
delay (20 - round (1. 1 ** (20 - speed )))
Input.KeyDown (key )
if key (UP_ARROW ) then
if canPress = 1 then
temp1 := sideCheck (block )
if cShape = 3 then
cShape := - 1
end if
cShape := cShape + 1
cBlock (block )
if situation (map, block ) = 3 or situation (map, block ) = 2 then
cShape := cShape - 1
cBlock (block )
elsif sideCheck (block ) = 3 and temp1 not= 1 then
center := center - 1
cBlock (block )
if sideCheck (block ) = 3 and temp1 not= 1 then
center := center - 1
cBlock (block )
if sideCheck (block ) = 3 and temp1 not= 1 then
center := center - 1
cBlock (block )
end if
end if
elsif sideCheck (block ) = 3 and temp1 = 1 then
center := center + 1
cBlock (block )
end if
canPress := 0
end if
elsif key (DOWN_ARROW ) then
if canPress = 1 or keyHold > autoMove then
exit when situation (map, block ) = 1
center := center - 12
cBlock (block )
keyHold + = 1
end if
elsif key (LEFT_ARROW ) then
if (canPress = 1 or keyHold > autoMove ) and sideCheck (block ) not= 1 then
center := center - 1
cBlock (block )
if situation (map, block ) = 2 then
center := center + 1
cBlock (block )
end if
end if
canPress := 0
keyHold + = 1
elsif key (RIGHT_ARROW ) then
if (canPress = 1 or keyHold > autoMove ) and sideCheck (block ) not= 2 then
center := center + 1
cBlock (block )
if situation (map, block ) = 2 then
center := center - 1
cBlock (block )
end if
end if
canPress := 0
keyHold + = 1
elsif key (SPACE ) and canPress = 1 then
loop
exit when situation (map, block ) = 1
center := center - 12
cBlock (block )
end loop
canPress := - 10
exit
else
if canPress < 1 then
canPress + = 1
end if
keyHold := 0
end if
movecount + = 1
if situation (map, block ) = 1 then
freeze + = 1
else
freeze := 0
end if
exit when freeze > speed * 2
exit when center < 0
end loop
score := score + (20 - speed )
end loop
|
Spoiler: | you can cheat in multi-player mode by pausing the game. | |