Advertisement
xerpi

Snake PSP alpha1

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