Advertisement
xerpi

snake alpha

Jul 28th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 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.move()
  71.             snake.steps = snake.steps+1
  72.             if snake.steps >= 75/snake.velocity then
  73.                 snake.lastx = snake.body[#snake.body].x
  74.                 snake.lasty = snake.body[#snake.body].y
  75.                 --Move the rest of the body    
  76.                     for i = #snake.body, 2,-1 do
  77.                         snake.body[i].y = snake.body[i-1].y
  78.                         snake.body[i].x = snake.body[i-1].x
  79.                     end    
  80.                 --Move the head
  81.                     if snake.direction == "down" then
  82.                         snake.body[1].y = snake.body[1].y+1
  83.                     elseif snake.direction == "up" then
  84.                         snake.body[1].y = snake.body[1].y-1
  85.                     elseif snake.direction == "right" then
  86.                         snake.body[1].x = snake.body[1].x+1
  87.                     elseif snake.direction == "left" then
  88.                         snake.body[1].x = snake.body[1].x-1                
  89.                     end
  90.                 snake.steps = 0
  91.                 snake.velocity = snake.velocity+0.05               
  92.             end
  93.         end
  94.  
  95.         function snake.change_direction()
  96.             if controls.press("right") then
  97.                 snake.direction = "right"
  98.             elseif controls.press("left") then
  99.                 snake.direction = "left"
  100.             elseif controls.press("up") then
  101.                 snake.direction = "up"
  102.             elseif controls.press("down") then
  103.                 snake.direction = "down"
  104.             end
  105.         end
  106. --Food
  107.         function food.insert_randomly()
  108.             local x = math.random(1,tile.horizontal)
  109.             local y = math.random(1,tile.vertical)
  110.             while snake.check_tile(x,y) do
  111.                 x = math.random(1,tile.horizontal)
  112.                 y = math.random(1,tile.vertical)
  113.             end
  114.             food[1].x = x
  115.             food[1].y = y
  116.         end
  117.        
  118.         function food.change_randomly(i)
  119.             local x = math.random(1,tile.horizontal)
  120.             local y = math.random(1,tile.vertical)
  121.             while snake.check_tile(x,y) do
  122.                 x = math.random(1,tile.horizontal)
  123.                 y = math.random(1,tile.vertical)
  124.             end
  125.             food[i].x = x
  126.             food[i].y = y
  127.         end
  128.        
  129.         function food.draw()
  130.             for i = 1, #food do
  131.                     food.image:blit(map.x + (food[1].x-1)*tile.w, map.y + (food[1].y-1)*tile.h)
  132.             end
  133.         end
  134.        
  135.     food.insert_randomly() 
  136.  
  137. --Map
  138.    
  139.         function map.draw()
  140.             for y = 0, tile.vertical+1 do
  141.                 for x = 0, tile.horizontal+1 do
  142.                     if y==0 or y == tile.vertical+1 or x==0 or x == tile.horizontal+1 then
  143.                         map.image.border:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
  144.                     else
  145.                         map.image.interior:blit(map.x + (x-1)*tile.w, map.y + (y-1)*tile.h)
  146.                     end    
  147.                 end
  148.             end
  149.         end
  150.    
  151. snake.random_head_position()   
  152.        
  153. while true do
  154. controls.read()
  155.  
  156.  
  157.  
  158. map.draw()
  159. food.draw()
  160. snake.change_direction()
  161.  
  162. snake.move()
  163. snake.check_food(x,y)
  164.  
  165. if controls.press("r") then
  166.     table.insert(snake.body,{x = snake.lastx,y = snake.lasty})
  167. end
  168.  
  169.  
  170. snake.draw()
  171.  
  172.  
  173.  
  174. screen.print(5,5,snake.direction)
  175. screen.print(205,5,snake.score)
  176. screen.print(5,25,snake.steps)
  177. screen.print(5,45,snake.velocity)
  178. screen.print(430,3,"@"..screen.fps())
  179. screen.print(5,65,#snake.body)
  180.  
  181. if controls.select() then a() end
  182. screen.flip()
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement