Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- math.randomseed(os.time()/math.pi*os.clock()/os.time())
- os.cpu(111)
- tile={w=20,h=20,vertical=10,horizontal=10}
- timers={move=0}
- map ={map={},image={}}
- --Coordinates
- map.w = tile.horizontal*tile.w
- map.h = tile.vertical*tile.h
- map.x = 240-map.w/2
- map.y = 136-map.h/2
- --Images
- map.image.border = image.create(tile.w,tile.h,color.new(128,0,0))
- map.image.interior = image.create(tile.w,tile.h,color.new(34,139,34))
- direction_list = {"up","down","right","left"}
- snake = {body = {{}}, direction = "down", color ={}, image ={},velocity=2,steps=0,score=0}
- --Colors
- snake.color.head = color.new(255,0,0)
- snake.color.body = color.new(255,255,0)
- snake.color.tail = color.new(0,255,0)
- --Images
- snake.image.head = image.create(tile.w,tile.h,color.new(255,0,0))
- snake.image.body = image.create(tile.w,tile.h,color.new(255,255,0))
- snake.image.tail = image.create(tile.w,tile.h,color.new(0,255,0))
- food = {{}}
- food.image = image.create(tile.w,tile.h,color.new(0,0,255))
- --Snake
- function snake.random_head_position()
- snake.body[1].x = math.random(2,tile.horizontal-1)
- snake.body[1].y = 1--math.random(2,tile.vertical-1)
- end
- function snake.check_tile(x,y)
- for i = 1, #snake.body do
- if snake.body[i].x == x and snake.body[i].y == y then
- return true
- end
- end
- return false
- end
- function snake.check_food(x,y)
- for i = 1, #food do
- if food[i].x == snake.body[1].x and food[i].y == snake.body[1].y then
- snake.score = snake.score+10
- food.change_randomly(i)
- table.insert(snake.body,{x = snake.lastx,y = snake.lasty})
- end
- end
- end
- function snake.draw()
- for i = 1, #snake.body do
- if i == 1 then
- snake.image.head:blit(map.x + (snake.body[1].x-1)*tile.w, map.y + (snake.body[1].y-1)*tile.h)
- elseif i == #snake.body then
- snake.image.tail:blit(map.x + (snake.body[i].x-1)*tile.w, map.y + (snake.body[i].y-1)*tile.h)
- else
- snake.image.body:blit(map.x + (snake.body[i].x-1)*tile.w, map.y + (snake.body[i].y-1)*tile.h)
- end
- end
- end
- function snake.die() --Poor snake :'(
- os.message("has perdido")
- errore()
- end
- function snake.self_colision(n)
- local maxn = #snake.body
- if maxn>1 then
- for i = 2, maxn do
- if snake.body[1].x == snake.body[i].x and snake.body[1].y == snake.body[i].y then
- return true
- end
- end
- end
- return false
- end
- function snake.colision(n)
- if snake.body[1].x < 0 or snake.body[1].x > tile.horizontal or snake.body[1].y < 0 or
- snake.body[1].y > tile.vertical or snake.self_colision(n) then
- snake.die()
- end
- end
- function snake.move()
- local maxn = #snake.body
- snake.steps = snake.steps+1
- if snake.steps >= 75/snake.velocity then
- snake.lastx = snake.body[#snake.body].x
- snake.lasty = snake.body[#snake.body].y
- --Move the rest of the body
- for i = maxn, 2,-1 do
- snake.body[i].y = snake.body[i-1].y
- snake.body[i].x = snake.body[i-1].x
- end
- --Move the head
- if snake.direction == "down" then
- snake.body[1].y = snake.body[1].y+1
- elseif snake.direction == "up" then
- snake.body[1].y = snake.body[1].y-1
- elseif snake.direction == "right" then
- snake.body[1].x = snake.body[1].x+1
- elseif snake.direction == "left" then
- snake.body[1].x = snake.body[1].x-1
- end
- snake.steps = 0
- snake.velocity = snake.velocity+0.05
- end
- --Check self colision
- snake.colision(maxn)
- end
- function snake.change_direction()
- if controls.press("right") and snake.direction != "left" then
- snake.direction = "right"
- elseif controls.press("left") and snake.direction != "right" then
- snake.direction = "left"
- elseif controls.press("up") and snake.direction != "down" then
- snake.direction = "up"
- elseif controls.press("down") and snake.direction != "up"then
- snake.direction = "down"
- end
- end
- --Food
- function food.insert_randomly()
- local x = math.random(1,tile.horizontal)
- local y = math.random(1,tile.vertical)
- while snake.check_tile(x,y) do
- x = math.random(1,tile.horizontal)
- y = math.random(1,tile.vertical)
- end
- food[1].x = x
- food[1].y = y
- end
- function food.change_randomly(i)
- local x = math.random(1,tile.horizontal)
- local y = math.random(1,tile.vertical)
- while snake.check_tile(x,y) do
- x = math.random(1,tile.horizontal)
- y = math.random(1,tile.vertical)
- end
- food[i].x = x
- food[i].y = y
- end
- function food.draw()
- for i = 1, #food do
- food.image:blit(map.x + (food[1].x-1)*tile.w, map.y + (food[1].y-1)*tile.h)
- end
- end
- food.insert_randomly()
- --Map
- function map.draw()
- for y = 0, tile.vertical+1 do
- for x = 0, tile.horizontal+1 do
- if y==0 or y == tile.vertical+1 or x==0 or x == tile.horizontal+1 then
- map.image.border:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
- else
- map.image.interior:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
- end
- end
- end
- end
- snake.random_head_position()
- while true do
- controls.read()
- map.draw()
- food.draw()
- snake.change_direction()
- snake.move()
- snake.check_food(x,y)
- if controls.press("r") then
- table.insert(snake.body,{x = snake.lastx,y = snake.lasty})
- end
- snake.draw()
- screen.print(5,5,snake.direction)
- screen.print(205,5,snake.score)
- screen.print(5,25,snake.steps)
- screen.print(5,45,snake.velocity)
- screen.print(430,3,"@"..screen.fps())
- screen.print(5,65,#snake.body)
- if controls.select() then a() end
- screen.flip()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement