setscreen ("offscreenonly,title:Snake,graphics:300;300")
var lengtx : int := 0
var lengty : int := 0
var x, y : int := 28
var die : int := 0
% drawfillbox (x, y, x2 + 9, y2 + 9, clr)
var key : array char of boolean
var move : int := 0
var dir : string := "up"
proc maze2
drawline (9, 9, 9, 290, 7)
drawline (9, 9, 290, 9, 7)
drawline (290, 9, 290, 290, 7)
drawline (9, 290, 290, 290, 7)
end maze2
maze2
var foodx, foody, food : int := 0
proc up
y += 14
if whatdotcolor (x, y) not= 1 then
drawfilloval (x, y, 7, 7, 1)
else
die := 1
end if
dir := "up"
end up
proc down
y -= 14
if whatdotcolor (x, y) not= 1 then
drawfilloval (x, y, 7, 7, 1)
else
die := 1
end if
dir := "down"
end down
proc left
x -= 14
if whatdotcolor (x, y) not= 1 then
drawfilloval (x, y, 7, 7, 1)
else
die := 1
end if
dir := "left"
end left
proc right
x += 14
if whatdotcolor (x, y) not= 1 then
drawfilloval (x, y, 7, 7, 1)
else
die := 1
end if
dir := "right"
end right
var tail : array 1 .. 200, 1 .. 2 of int
var i := 0
var snake_length : int := 2
var delx, dely : array 1 .. 1000 of int
var count : int := 0
loop
count += 1
delx (count) := x
dely (count) := y
if dir = "up" then
drawfilloval (delx (count), dely (count) - (snake_length * 14 - 14), 7, 7, 0)
elsif dir = "down" then
drawfilloval (delx (count), dely (count) + (snake_length * 14 - 14), 7, 7, 0)
elsif dir = "right" then
drawfilloval (delx (count) - (snake_length * 14 - 14), dely (count), 7, 7, 0)
elsif dir = "left" then
drawfilloval (delx (count) + (snake_length * 14 - 14), dely (count), 7, 7, 0)
end if
die := 0
if food = 0 then
if snake_length not= 0 and whatdotcolor (foodx, foody) not= 1 then
drawfilloval (foodx, foody, 7, 7, 0)
end if
loop
randint (foodx, 20, 270)
randint (foody, 20, 270)
if foodx mod 14 = 0 then
if foody mod 14 = 0 then
if whatdotcolor (foodx, foody) = 0 then
if x + 10 <= foodx and x >= foodx and y + 10 <= foody and y + 10 >= foody then
drawfilloval (foodx, foody, 7, 7, 0)
exit
else
drawfilloval (foodx, foody, 7, 7, 7)
exit
end if
end if
end if
end if
end loop
food := 1
end if
Input.KeyDown (key)
if key (KEY_UP_ARROW) and dir not= "down" then %up arrow
up
% drawfilloval (delx (count), dely (count) - (snake_length * 14 - 14), 7, 7, 0)
elsif key (KEY_UP_ARROW) and dir = "down" then
down
% drawfilloval (delx (count), dely (count) + (snake_length * 14 + 14), 7, 7, 0)
elsif key (KEY_DOWN_ARROW) and dir not= "up" then %down
down
% drawfilloval (delx (count), dely (count) + (snake_length * 14 + 14), 7, 7, 0)
elsif key (KEY_DOWN_ARROW) and dir = "up" then
up
% drawfilloval (delx (count), dely (count) - (snake_length * 14 - 14), 7, 7, 0)
elsif key (KEY_LEFT_ARROW) and dir not= "right" then %left
left
% drawfilloval (delx (count) + (snake_length * 14 + 14), dely (count), 7, 7, 0)
elsif key (KEY_LEFT_ARROW) and dir = "right" then
right
%drawfilloval (delx (count) - (snake_length * 14 - 14), dely (count), 7, 7, 0)
elsif key (KEY_RIGHT_ARROW) and dir not= "left" then %right
right
% drawfilloval (delx (count) - (snake_length * 14 - 14), dely (count), 7, 7, 0)
elsif key (KEY_RIGHT_ARROW) and dir = "left" then
left
% drawfilloval (delx (count) + (snake_length * 14 + 14), dely (count), 7, 7, 0)
else
if dir = "down" then
down
% drawfilloval (delx (count), dely (count) + (snake_length * 14 + 14), 7, 7, 0)
elsif dir = "up" then
up
% drawfilloval (delx (count), dely (count) - (snake_length * 14 - 14), 7, 7, 0)
elsif dir = "right" then
right
% drawfilloval (delx (count) - (snake_length * 14 - 14), dely (count), 7, 7, 0)
elsif dir = "left" then
left
% drawfilloval (delx (count) + (snake_length * 14 + 14), dely (count), 7, 7, 0)
end if
end if
exit when x < 10| x > 285| y < 10| y > 285
View.Update
if whatdotcolor (foodx, foody) = 1| whatdotcolor (foodx + 7, foody + 7) = 1 then
food := 0
snake_length += 1
end if
maze2
delay (100)
end loop
|