Advertisement
xerpi

snake alpha1

Jul 28th, 2011
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.62 KB | None | 0 0
  1. math.randomseed(os.time()/math.pi*os.clock()/os.time())
  2.  
  3. os.cpu(111)
  4.  
  5. tile={w=20,h=20,vertical=10,horizontal=10}
  6. timers={move=0}
  7. map ={map={},image={}}
  8.     --Coordinates
  9.         map.w = tile.horizontal*tile.w
  10.         map.h = tile.vertical*tile.h
  11.         map.x = 240-map.w/2
  12.         map.y = 136-map.h/2
  13.     --Images
  14.         map.image.border = image.create(tile.w,tile.h,color.new(128,0,0))
  15.         map.image.interior = image.create(tile.w,tile.h,color.new(34,139,34))
  16.  
  17. direction_list = {"up","down","right","left"}
  18.  
  19. snake = {body = {{}}, direction = "down", color ={}, image ={},velocity=2,steps=0,score=0}
  20.     --Colors
  21.         snake.color.head = color.new(255,0,0)
  22.         snake.color.body = color.new(255,255,0)
  23.         snake.color.tail = color.new(0,255,0)
  24.     --Images
  25.         snake.image.head = image.create(tile.w,tile.h,color.new(255,0,0))
  26.         snake.image.body = image.create(tile.w,tile.h,color.new(255,255,0))
  27.         snake.image.tail = image.create(tile.w,tile.h,color.new(0,255,0))      
  28.  
  29. food = {{}}
  30.     food.image = image.create(tile.w,tile.h,color.new(0,0,255))
  31.  
  32. --Snake    
  33.         function snake.random_head_position()
  34.             snake.body[1].x = math.random(2,tile.horizontal-1)
  35.             snake.body[1].y = 1--math.random(2,tile.vertical-1)
  36.         end    
  37.            
  38.         function snake.check_tile(x,y)
  39.             for i = 1, #snake.body do
  40.                 if snake.body[i].x == x and snake.body[i].y == y then
  41.                     return true
  42.                 end
  43.             end
  44.             return false
  45.         end
  46.        
  47.         function snake.check_food(x,y)
  48.             for i = 1, #food do
  49.                 if food[i].x == snake.body[1].x and food[i].y == snake.body[1].y then
  50.                     snake.score = snake.score+10
  51.                     food.change_randomly(i)
  52.                     table.insert(snake.body,{x = snake.lastx,y = snake.lasty})
  53.                 end    
  54.             end
  55.        
  56.         end
  57.        
  58.         function snake.draw()
  59.             for i = 1, #snake.body do
  60.                 if i == 1 then
  61.                     snake.image.head:blit(map.x + (snake.body[1].x-1)*tile.w, map.y + (snake.body[1].y-1)*tile.h)
  62.                 elseif i == #snake.body then
  63.                     snake.image.tail:blit(map.x + (snake.body[i].x-1)*tile.w, map.y + (snake.body[i].y-1)*tile.h)
  64.                 else
  65.                     snake.image.body:blit(map.x + (snake.body[i].x-1)*tile.w, map.y + (snake.body[i].y-1)*tile.h)
  66.                 end
  67.             end
  68.         end
  69.  
  70.         function snake.die() --Poor snake :'(  
  71.             os.message("has perdido")
  72.             errore()           
  73.         end
  74.            
  75.         function snake.self_colision(n)
  76.             local maxn = #snake.body
  77.             if maxn>1 then
  78.                 for i = 2, maxn do
  79.                     if snake.body[1].x == snake.body[i].x and snake.body[1].y == snake.body[i].y then
  80.                         return true
  81.                     end
  82.                 end
  83.             end
  84.             return false
  85.         end
  86.  
  87.         function snake.colision(n) 
  88.             if snake.body[1].x < 0 or snake.body[1].x > tile.horizontal or snake.body[1].y < 0 or
  89.                 snake.body[1].y > tile.vertical or snake.self_colision(n) then
  90.                     snake.die()
  91.             end    
  92.         end
  93.        
  94.         function snake.move()
  95.             local maxn = #snake.body
  96.             snake.steps = snake.steps+1
  97.             if snake.steps >= 75/snake.velocity then
  98.                 snake.lastx = snake.body[#snake.body].x
  99.                 snake.lasty = snake.body[#snake.body].y
  100.                 --Move the rest of the body    
  101.                     for i = maxn, 2,-1 do
  102.                         snake.body[i].y = snake.body[i-1].y
  103.                         snake.body[i].x = snake.body[i-1].x
  104.                     end    
  105.                 --Move the head
  106.                     if snake.direction == "down" then
  107.                         snake.body[1].y = snake.body[1].y+1
  108.                     elseif snake.direction == "up" then
  109.                         snake.body[1].y = snake.body[1].y-1
  110.                     elseif snake.direction == "right" then
  111.                         snake.body[1].x = snake.body[1].x+1
  112.                     elseif snake.direction == "left" then
  113.                         snake.body[1].x = snake.body[1].x-1                
  114.                     end
  115.                 snake.steps = 0
  116.                 snake.velocity = snake.velocity+0.05               
  117.             end
  118.             --Check self colision
  119.                 snake.colision(maxn)   
  120.         end
  121.  
  122.         function snake.change_direction()
  123.             if controls.press("right") and snake.direction != "left" then
  124.                 snake.direction = "right"
  125.             elseif controls.press("left") and snake.direction != "right" then
  126.                 snake.direction = "left"
  127.             elseif controls.press("up") and snake.direction != "down" then
  128.                 snake.direction = "up"
  129.             elseif controls.press("down") and snake.direction != "up"then
  130.                 snake.direction = "down"
  131.             end
  132.         end
  133. --Food
  134.         function food.insert_randomly()
  135.             local x = math.random(1,tile.horizontal)
  136.             local y = math.random(1,tile.vertical)
  137.             while snake.check_tile(x,y) do
  138.                 x = math.random(1,tile.horizontal)
  139.                 y = math.random(1,tile.vertical)
  140.             end
  141.             food[1].x = x
  142.             food[1].y = y
  143.         end
  144.        
  145.         function food.change_randomly(i)
  146.             local x = math.random(1,tile.horizontal)
  147.             local y = math.random(1,tile.vertical)
  148.             while snake.check_tile(x,y) do
  149.                 x = math.random(1,tile.horizontal)
  150.                 y = math.random(1,tile.vertical)
  151.             end
  152.             food[i].x = x
  153.             food[i].y = y
  154.         end
  155.        
  156.         function food.draw()
  157.             for i = 1, #food do
  158.                     food.image:blit(map.x + (food[1].x-1)*tile.w, map.y + (food[1].y-1)*tile.h)
  159.             end
  160.         end
  161.        
  162.     food.insert_randomly() 
  163.  
  164. --Map
  165.    
  166.         function map.draw()
  167.             for y = 0, tile.vertical+1 do
  168.                 for x = 0, tile.horizontal+1 do
  169.                     if y==0 or y == tile.vertical+1 or x==0 or x == tile.horizontal+1 then
  170.                         map.image.border:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
  171.                     else
  172.                         map.image.interior:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
  173.                     end    
  174.                 end
  175.             end
  176.         end
  177.    
  178. snake.random_head_position()   
  179.        
  180. while true do
  181. controls.read()
  182.  
  183.  
  184.  
  185. map.draw()
  186. food.draw()
  187. snake.change_direction()
  188.  
  189. snake.move()
  190. snake.check_food(x,y)
  191.  
  192. if controls.press("r") then
  193.     table.insert(snake.body,{x = snake.lastx,y = snake.lasty})
  194. end
  195.  
  196.  
  197. snake.draw()
  198.  
  199.  
  200.  
  201. screen.print(5,5,snake.direction)
  202. screen.print(205,5,snake.score)
  203. screen.print(5,25,snake.steps)
  204. screen.print(5,45,snake.velocity)
  205. screen.print(430,3,"@"..screen.fps())
  206. screen.print(5,65,#snake.body)
  207.  
  208. if controls.select() then a() end
  209. screen.flip()
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement