And here's already an improved version:
-- snake clone with ChipmonkeyLua
-- ------------------------------
function LoadHighscore()
filename = "snakehigh.dat"
if not fileexists (filename) then
userfile = open (filename,"w")
for i = 1, 10 do
fprint (userfile,highscore[i].score)
fprint (userfile,highscore[i].name)
fprint (userfile,highscore[i].date)
end
close (userfile)
end
if fileexists (filename) then
userfile = open (filename, "r")
for i=1,10 do
highscore[i].score = finput (userfile)
highscore[i].name = finput (userfile)
highscore[i].date = finput (userfile)
end
close (userfile)
end
end
function SaveHighscore()
filename = "snakehigh.dat"
if fileexists (filename) then
userfile = open (filename,"w")
for i = 1, 10 do
fprint (userfile,highscore[i].score)
fprint (userfile,highscore[i].name)
fprint (userfile,highscore[i].date)
end
close (userfile)
end
end
function SortHighscore()
local getauscht, i, temp, tempdate,tempstr
repeat
getauscht=false
for i=1, 9 do
if val(highscore[i].score) < val(highscore[i+1].score) then
temp =(highscore[i].score)
tempstr=highscore[i].name
tempdate=highscore[i].date
highscore[i].score=highscore[i+1].score
highscore[i].name=highscore[i+1].name
highscore[i].date=highscore[i+1].date
highscore[i+1].score=temp
highscore[i+1].name=tempstr
highscore[i+1].date=tempdate
getauscht=true
end
end
until getauscht == false
end
lowres()
CRSLEFT = 19200
CRSRIGHT = 19712
CRSUP = 18432
CRSDOWN = 20480
score = 0
snake={}
snakex={}
snakey={}
snakex[1]=5
snakey[1]=5
snake.xoffset=111
snake.yoffset=111
snake.xcount=10
snake.ycount=10
snake.width=8
snake.height=8
snake.direction ="right"
snake.length = 1
bait={}
bait.x=30
bait.y=30
bait.xoffset=111
bait.yoffset=111
bait.xcount=10
bait.ycount=10
bait.width=8
bait.height=8
speed = 50
for i=1, snake.length do
snakex[i]=5
snakey[i]=5
end
highscore={}
for i=1, 10 do
highscore[i]={}
end
for i=1,10 do
highscore[i].score = 0
highscore[i].name = "Noname"
highscore[i].date = date()
end
LoadHighscore()
SortHighscore()
SaveHighscore()
ink (65535)
cls()
locate (0,10,"")
name = input "Please enter your name: "
cls()
page = 0
endgame = false
timer1=gettickcount()
repeat
activepage (page)
clear()
if keypressed()== true then
xkey=getkey()
if xkey==27 then
endgame = true
elseif xkey==CRSLEFT then
snake.direction = "left"
elseif xkey==CRSRIGHT then
snake.direction = "right"
elseif xkey==CRSUP then
snake.direction = "up"
elseif xkey==CRSDOWN then
snake.direction = "down"
elseif xkey==112 then
inkey()
end
end
timer2=gettickcount()
if timer2-timer1 >=10000 then
bait.x = int (rnd()*72)
bait.y = int (rnd()*52)
timer1=gettickcount()
end
if (snakex[1]==bait.x) and (snakey[1]==bait.y) then
score = score + 1
bait.x = int (rnd()*72)
bait.y = int (rnd()*52)
snake.length=snake.length + 1
speed = speed - 0.2
timer1=gettickcount()
end
if snake.direction =="right" then
for i=snake.length,2,-1 do
snakex[i]=snakex[i-1]
snakey[i]=snakey[i-1]
end
snakex[1]=snakex[1] + 1
sleep (speed)
end
if snake.direction =="down" then
for i=snake.length,2,-1 do
snakex[i]=snakex[i-1]
snakey[i]=snakey[i-1]
end
snakey[1]=snakey[1] + 1
sleep (speed)
end
if snake.direction =="left" then
for i=snake.length,2,-1 do
snakex[i]=snakex[i-1]
snakey[i]=snakey[i-1]
end
snakex[1]=snakex[1] - 1
sleep (speed)
end
if snake.direction =="up" then
for i=snake.length,2,-1 do
snakex[i]=snakex[i-1]
snakey[i]=snakey[i-1]
end
snakey[1]=snakey[1] - 1
sleep (speed)
end
for i=2,snake.length do
if (snakex[1]==snakex[i]) and (snakey[1]==snakey[i]) then
endgame = true
end
end
if snakex[1] < 0 then
endgame=true
end
if snakex[1] > 72 then
endgame=true
end
if snakey[1] < 0 then
endgame=true
end
if snakey[1] > 52 then
endgame=true
end
ink (hrgb (255,0,0))
fillcircle ((bait.xoffset+bait.x*bait.xcount)+bait.width/2,(bait.yoffset+bait.y*bait.ycount)+bait.width/2,bait.width/2)
ink (hrgb (255,255,0))
for i=1,snake.length do
fillrectangle (snake.xoffset+snakex[i]*snake.xcount,snake.yoffset+snakey[i]*snake.ycount,(snake.xoffset+snakex[i]*snake.xcount)+snake.width,(snake.yoffset+snakey[i]*snake.ycount)+snake.height)
end
ink (65535)
fillrectangle (100,100,850,110)
fillrectangle (100,100,110,650)
fillrectangle (100,641,850,650)
fillrectangle (841,100,850,650)
drawtext (100,80,name.." playing")
drawtext (700,80,"Score: "..score)
sleep (15)
visualpage (page)
page = page + 1
if page > 1 then
page = 0
end
until endgame == true
ink (65535)
drawtext (screenwidth()/2-100,screenheight()/2-8,"GAME OVER!")
if val(score) > val (highscore[10].score) then
highscore[10].score = score
highscore[10].name = name
highscore[10].date = date()
SortHighscore()
SaveHighscore()
end
locate (0,0,"Press any key")
inkey()
cls()
print ("")
for i =1 , 10 do
print (i..". "..left (highscore[i].name,20).." ..... "..highscore[i].score.." ..... "..highscore[i].date)
print ("")
end
Now with highscore table ...