This is just a quick classic snake game I made this week. I haven't gone through it thoroughly, so if you find any bugs, tell me
Turing: | setscreen ("position:center;center,graphics:482;400,offscreenonly,nobuttonbar")
var size, counter, pX, pY, score, mx, my, click : int := 1
var boardArray : array 1 .. 30, 1 .. 20 of int
var dir, x, y : flexible array 1 .. size of int
var keys : array char of boolean
var add, gameOver : boolean := false
var speed, timer : real := 0
for i : 1 .. 30
for k : 1 .. 20
boardArray (i, k ) := 0
end for
end for
dir (1) := 4
x (1) := 15
y (1) := 10
speed := 75
timer := 100
score := 0
pX := Rand.Int (1, 30)
pY := Rand.Int (1, 20)
boardArray (pX, pY ) := 6
procedure addBlock
boardArray (x (1), y (1)) := 1
size + = 1
new dir, size
new x, size
new y, size
end addBlock
procedure newPower
score + = round (timer )
timer := 100
loop
pX := Rand.Int (1, 30)
pY := Rand.Int (1, 20)
if boardArray (pX, pY ) = 0 then
boardArray (pX, pY ) := 6
exit
end if
end loop
end newPower
loop
Input.KeyDown (keys )
counter + = 1
if timer > 0 then
timer - = . 05
end if
if speed > 10 then
speed - = . 001
end if
for i : 1 .. size
if i = 1 then
if (keys ('w') or keys (KEY_UP_ARROW)) and dir (i ) not= 1 and dir (i ) not= 2 and boardArray (x (i ), y (i )) = 1 then
dir (i ) := 1
boardArray (x (i ), y (i )) := 2
elsif (keys ('s') or keys (KEY_DOWN_ARROW)) and dir (i ) not= 1 and dir (i ) not= 1 and boardArray (x (i ), y (i )) = 1 then
dir (i ) := 2
boardArray (x (i ), y (i )) := 3
elsif (keys ('a') or keys (KEY_LEFT_ARROW)) and dir (i ) not= 3 and dir (i ) not= 4 and boardArray (x (i ), y (i )) = 1 then
dir (i ) := 3
boardArray (x (i ), y (i )) := 4
elsif (keys ('d') or keys (KEY_RIGHT_ARROW)) and dir (i ) not= 3 and dir (i ) not= 4 and boardArray (x (i ), y (i )) = 1 then
dir (i ) := 4
boardArray (x (i ), y (i )) := 5
end if
end if
if counter >= speed then
if dir (i ) = 1 then
y (i ) + = 1
if y (i ) > 20 then
y (i ) := 1
end if
if i = 1 and (boardArray (x (i ), y (i )) not= 0 and boardArray (x (i ), y (i )) not= 6) then
gameOver := true
exit
end if
if boardArray (x (i ), y (i )) = 6 then
add := true
boardArray (x (i ), y (i )) := 1
newPower
end if
if i = size and add = true then
if y (i ) > 1 then
boardArray (x (i ), y (i ) - 1) := 1
else
boardArray (x (i ), 20) := 1
end if
addBlock
dir (size ) := dir (i )
y (size ) := y (i ) - 1
x (size ) := x (i )
end if
if y (i ) > 1 and i = size then
boardArray (x (i ), y (i ) - 1) := 0
elsif i = size then
boardArray (x (i ), 20) := 0
end if
elsif dir (i ) = 2 then
y (i ) - = 1
if y (i ) < 1 then
y (i ) := 20
end if
if i = 1 and (boardArray (x (i ), y (i )) not= 0 and boardArray (x (i ), y (i )) not= 6) then
gameOver := true
exit
end if
if boardArray (x (i ), y (i )) = 6 then
add := true
boardArray (x (i ), y (i )) := 1
newPower
end if
if i = size and add = true then
if y (i ) < 20 then
boardArray (x (i ), y (i ) + 1) := 1
else
boardArray (x (i ), 1) := 1
end if
addBlock
dir (size ) := dir (i )
x (size ) := x (i )
y (size ) := y (i ) + 1
end if
if y (i ) < 20 and i = size then
boardArray (x (i ), y (i ) + 1) := 0
elsif i = size then
boardArray (x (i ), 1) := 0
end if
elsif dir (i ) = 3 then
x (i ) - = 1
if x (i ) < 1 then
x (i ) := 30
end if
if i = 1 and (boardArray (x (i ), y (i )) not= 0 and boardArray (x (i ), y (i )) not= 6) then
gameOver := true
exit
end if
if boardArray (x (i ), y (i )) = 6 then
add := true
boardArray (x (i ), y (i )) := 1
newPower
end if
if i = size and add = true then
if x (i ) < 30 then
boardArray (x (i ) + 1, y (i )) := 1
else
boardArray (1, y (i )) := 1
end if
addBlock
dir (size ) := dir (i )
x (size ) := x (i ) + 1
y (size ) := y (i )
end if
if x (i ) < 30 and i = size then
boardArray (x (i ) + 1, y (i )) := 0
elsif i = size then
boardArray (1, y (i )) := 0
end if
elsif dir (i ) = 4 then
x (i ) + = 1
if x (i ) > 30 then
x (i ) := 1
end if
if i = 1 and (boardArray (x (i ), y (i )) not= 0 and boardArray (x (i ), y (i )) not= 6) then
gameOver := true
exit
end if
if boardArray (x (i ), y (i )) = 6 then
add := true
boardArray (x (i ), y (i )) := 1
newPower
end if
if i = size and add = true then
if x (i ) > 1 then
boardArray (x (i ) - 1, y (i )) := 1
else
boardArray (30, y (i )) := 1
end if
addBlock
dir (size ) := dir (i )
x (size ) := x (i ) - 1
y (size ) := y (i )
end if
if x (i ) > 1 and i = size then
boardArray (x (i ) - 1, y (i )) := 0
elsif i = size then
boardArray (30, y (i )) := 0
end if
end if
if boardArray (x (i ), y (i )) = 2 then
dir (i ) := 1
elsif boardArray (x (i ), y (i )) = 3 then
dir (i ) := 2
elsif boardArray (x (i ), y (i )) = 4 then
dir (i ) := 3
elsif boardArray (x (i ), y (i )) = 5 then
dir (i ) := 4
end if
if boardArray (x (i ), y (i )) = 0 then
boardArray (x (i ), y (i )) := 1
end if
if i = size or (i = size - 1 and add = true) then
counter := 0
add := false
end if
end if
end for
drawbox (14, 14, 466, 316, 255)
for i : 1 .. 30
for k : 1 .. 20
if boardArray (i, k ) = 1 or boardArray (i, k ) = 2 or boardArray (i, k ) = 3 or boardArray (i, k ) = 4 or boardArray (i, k ) = 5 then
drawfillbox (i * 15, k * 15, i * 15 + 15, k * 15 + 15, 255)
drawbox (i * 15, k * 15, i * 15 + 15, k * 15 + 15, 0)
elsif boardArray (i, k ) = 6 then
drawfillbox (i * 15, k * 15, i * 15 + 15, k * 15 + 15, 0)
drawbox (i * 15, k * 15, i * 15 + 15, k * 15 + 15, 255)
end if
end for
end for
put "speed " + intstr (round (speed ))
put "bonus " + intstr (round (timer ))
put "score " + intstr (score )
exit when gameOver = true
View.Update
cls
end loop
put "game_over"
|
|