Advertisement
xerpi

PSP Snake valpha1 xerpi (c)

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