Advertisement
xerpi

Sneik alpha 1PS3

Aug 29th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.40 KB | None | 0 0
  1. ----------------- xerpi (c) 2011 ------------------
  2.  
  3. InitGFX(720,480)
  4. initPads(7)
  5. setsixaxis(0,1)
  6.  
  7. textures = { }
  8. for i = 1, 9 do
  9.     table.insert(textures, 1, surface())
  10. end
  11. textures[1]:LoadIMG("gfx/head.bmp")
  12. textures[2]:LoadIMG("gfx/body.bmp")
  13. textures[3]:LoadIMG("gfx/tail.bmp")
  14. textures[4]:LoadIMG("gfx/food.bmp")
  15. textures[5]:LoadIMG("gfx/bg.bmp")
  16. textures[6]:LoadIMG("gfx/splashps32011.bmp")
  17. textures[7]:LoadIMG("gfx/sneikps3.bmp")
  18. textures[8]:LoadIMG("gfx/hasperdido.bmp")
  19. textures[9]:LoadIMG("gfx/rock.bmp")
  20.  
  21. number = {}
  22. for i = 1 , 10 do
  23.     table.insert(number, 1,surface())
  24. end
  25. number[1]:LoadIMG("gfx/numbers/1.bmp")
  26. number[2]:LoadIMG("gfx/numbers/2.bmp")
  27. number[3]:LoadIMG("gfx/numbers/3.bmp")
  28. number[4]:LoadIMG("gfx/numbers/4.bmp")
  29. number[5]:LoadIMG("gfx/numbers/5.bmp")
  30. number[6]:LoadIMG("gfx/numbers/6.bmp")
  31. number[7]:LoadIMG("gfx/numbers/7.bmp")
  32. number[8]:LoadIMG("gfx/numbers/8.bmp")
  33. number[9]:LoadIMG("gfx/numbers/9.bmp")
  34. number[10]:LoadIMG("gfx/numbers/0.bmp")
  35.  
  36. function sbsplash2011()
  37.     for i = 1, 500 do
  38.         beginGFX()
  39.         textures[6]:setRectPos(0,0)
  40.         BlitToScreen(textures[6])
  41.         endGFX()
  42.     end
  43. end
  44. function mainmenu()
  45.     while true do
  46.         beginGFX()
  47.         textures[7]:setRectPos(0,0)
  48.         BlitToScreen(textures[7])      
  49.         if cross(0) >= 1 then play_game() end
  50.         endGFX()
  51.     end
  52. end
  53.  
  54. function losemenu()
  55.     while true do
  56.         beginGFX()
  57.         textures[8]:setRectPos(0,0)
  58.         BlitToScreen(textures[8])      
  59.         if cross(0) >= 1 then pressed = false end
  60.         DrawText(350,440, string.format("%i", snake.score))
  61.         endGFX()
  62.     end
  63. end
  64.  
  65. --[[tile.head = image.create(map.w, map.h, color.new(255,0,0))
  66.     tile.body = image.create(map.w, map.h, color.new(0,255,0))
  67.     tile.tail = image.create(map.w, map.h, color.new(0,0,255))
  68.     tile.food = image.create(map.w, map.h, color.new(150,75,0))
  69.     tile.bg   = image.create(map.w, map.h, color.new(255,0,255))
  70.     ]]
  71.  
  72. sw, sh = screenRes()
  73. map = {ver=22,hor=25,w=20,h=20}
  74.     map.x = sw/2 - (map.hor*map.w)/2
  75.     map.y = sh/2 - (map.ver*map.h)/2
  76.  
  77.  
  78. snake = { body ={{x=math.floor(map.hor/2),y=math.floor(map.ver/2)},{x=math.floor(map.hor/2),y=math.floor(map.ver/2)-1}} , vel = 1, direction = "down",length = 2,counter=0,changed = false,score=0, selfcollision = true}
  79. food = {}
  80. level = {current = 2}
  81.  
  82. level[1]= {
  83. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  84. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  85. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  86. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  87. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  88. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  89. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  90. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  91. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  92. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  93. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  94. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  95. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  96. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  97. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  98. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  99. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  100. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  101. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  102. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  103. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  104. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  105. }
  106. level[2]= {
  107. {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"},
  108. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  109. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  110. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  111. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  112. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  113. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  114. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  115. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  116. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  117. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  118. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  119. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  120. {"e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e"},
  121. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  122. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  123. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  124. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  125. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  126. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  127. {"r","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","e","r"},
  128. {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"},
  129. }
  130.  
  131. function food.change(n)
  132.     local x,y
  133.     while true do
  134.         local taken = false
  135.         x,y = math.random(1,map.hor),math.random(1,map.ver)
  136.         for i = 1, snake.length do
  137.             if x == snake.body[i].x and y == snake.body[i].y then taken = true end
  138.         end
  139.         if not taken then break end
  140.     end
  141.     food[n].x = x
  142.     food[n].y = y
  143. end
  144.  
  145. function food.insert()
  146.     local x,y
  147.     while true do
  148.         local taken = false
  149.         x,y = math.random(1,map.hor),math.random(1,map.ver)
  150.         for i = 1, snake.length do
  151.             if x == snake.body[i].x and y == snake.body[i].y then taken = true end
  152.         end
  153.         if not taken then break end
  154.     end
  155.     table.insert(food, {x = x, y = y})
  156. end
  157.  
  158. function food.blit()
  159.     for i = 1, # food do
  160.         textures[4]:setRectPos(map.x+(food[i].x-1)*map.w,map.y+(food[i].y-1)*map.h)
  161.         BlitToScreen(textures[4])
  162.     end
  163. end
  164.  
  165. function snake.insert()
  166.     table.insert(snake.body,{x = snake.body[snake.length].x,y=snake.body[snake.length].y})
  167.     snake.length = snake.length +1
  168. end
  169.  
  170. function snake.kill()
  171.     body ={{x=math.floor(map.hor/2),y=math.floor(map.ver/2)},{x=math.floor(map.hor/2),y=math.floor(map.ver/2)-1}}
  172. end
  173.  
  174. function snake.blit()
  175.     --Blit the body
  176.         for i = 2, snake.length-1 do
  177.             textures[2]:setRectPos(map.x+(snake.body[i].x-1)*map.w,map.y+(snake.body[i].y-1)*map.h)
  178.             BlitToScreen(textures[2])
  179.         end
  180.     --Blit the tail
  181.         textures[3]:setRectPos(map.x+(snake.body[snake.length].x-1)*map.w,map.y+(snake.body[snake.length].y-1)*map.h)  
  182.         BlitToScreen(textures[3])
  183.     --Blit the head
  184.         textures[1]:setRectPos(map.x+(snake.body[1].x-1)*map.w,map.y+(snake.body[1].y-1)*map.h)
  185.         BlitToScreen(textures[1])      
  186. end
  187.  
  188. function snake.move()
  189.     --Change snake direction
  190.         if left(0)  >= 1  and snake.direction ~= "right" and not snake.changed then snake.direction = "left"  snake.changed = true end
  191.         if right(0) >= 1  and snake.direction ~= "left"  and not snake.changed then snake.direction = "right" snake.changed = true end
  192.         if up(0)    >= 1  and snake.direction ~= "down"  and not snake.changed then snake.direction = "up"    snake.changed = true end
  193.         if down(0)  >= 1  and snake.direction ~= "up"    and not snake.changed then snake.direction = "down"  snake.changed = true end
  194.     --Increase the snake vel variable
  195.         snake.vel = snake.vel + 0.0001 
  196.     --Increase the snake counter
  197.         snake.counter = snake.counter + snake.vel
  198.     --Move the snake?
  199.         if snake.counter >= 100 then
  200.             --Move all the body
  201.                 for i = snake.length, 2, -1 do
  202.                     snake.body[i].x = snake.body[i-1].x
  203.                     snake.body[i].y = snake.body[i-1].y
  204.                 end
  205.             --Move the head
  206.                 if snake.direction == "up"    then snake.body[1].y = snake.body[1].y - 1 end
  207.                 if snake.direction == "down"  then snake.body[1].y = snake.body[1].y + 1 end
  208.                 if snake.direction == "left"  then snake.body[1].x = snake.body[1].x - 1 end
  209.                 if snake.direction == "right" then snake.body[1].x = snake.body[1].x + 1 end
  210.             --Check if the snake is at the limit
  211.                 if snake.body[1].y < 1       then snake.body[1].y = map.ver end
  212.                 if snake.body[1].y > map.ver then snake.body[1].y = 1       end
  213.                 if snake.body[1].x < 1       then snake.body[1].x = map.hor end
  214.                 if snake.body[1].x > map.hor then snake.body[1].x = 1       end
  215.             --Reset the counter
  216.                 snake.counter = 0
  217.             --Set the boolean changed as false ( for moving snake again )
  218.                 snake.changed = false
  219.         end
  220. end
  221.  
  222. function snake.checkfood()
  223.     for i = 1, #food do
  224.         if food[i].x == snake.body[1].x and food[i].y == snake.body[1].y then
  225.             snake.insert()
  226.             snake.score = snake.score + 1
  227.             food.change(i)
  228.         end
  229.     end
  230. end
  231.  
  232. function snake.checkcollisions()
  233.     if level[level.current][snake.body[1].y][snake.body[1].x] == "r" then
  234.         return true
  235.     end
  236.     return snake.selfcollision()
  237. end
  238.  
  239. function snake.selfcollision()
  240.     for i = 2, snake.length do
  241.         if snake.body[1].x == snake.body[i].x and snake.body[1].y == snake.body[i].y then return true end
  242.     end
  243.     return false
  244. end
  245.    
  246. function drawmap()
  247.     for y = 1, map.ver do
  248.         for x = 1, map.hor do
  249.             if level[level.current][y][x] == "e" then
  250.                 textures[5]:setRectPos(map.x+(x-1)*map.w,map.y+(y-1)*map.h)    
  251.                 BlitToScreen(textures[5])
  252.             elseif level[level.current][y][x] == "r" then
  253.                 textures[9]:setRectPos(map.x+(x-1)*map.w,map.y+(y-1)*map.h)    
  254.                 BlitToScreen(textures[9])
  255.             end
  256.         end
  257.     end
  258. end
  259.  
  260. function DrawCustom(x,y,text,dist)
  261.     --number={1,2,3,4,5,6,7,8,9,0}
  262.     if type(text) == "number" then text = tostring(text) end
  263.     for i = 1, string.len(text) do
  264.         local n = tonumber(string.sub(text,i,i))
  265.         if n == 0 then
  266.             number[10]:setRectPos(x + (i-1)*dist,y)
  267.             BlitToScreen(number[10])   
  268.         else
  269.             number[n]:setRectPos(x + (i-1)*dist,y) 
  270.             BlitToScreen(number[n])        
  271.         end
  272.     end
  273. end
  274.  
  275.  
  276. function play_game()
  277.     food.insert()
  278.     snake.kill()
  279.     while true do
  280.         beginGFX()
  281.         drawmap()
  282.         snake.move()
  283.         snake.checkfood()
  284.         food.blit()
  285.         snake.blit()   
  286.         snake.checkcollisions()
  287.             if select(0) >= 1 then break end
  288.             if start(0) >= 1 then Screenshot("screenshot.bmp") end
  289.             if cross(0) >= 1 and not pressed then snake.insert() snake.score = snake.score +2 pressed = true end
  290.             if cross(0) < 1 then pressed = false end
  291.             DrawText(10,10, string.format("Score: %i", snake.score))
  292.             DrawCustom(100,100,snake.score,17)
  293.             DrawText(10,30, string.format("Vel: %i", snake.vel))
  294.         if snake.checkcollisions() then DrawText(10,20, "COLLISIONING") end
  295.         endGFX()
  296.     end
  297. end
  298.  --mainmenu()
  299.  
  300.  estado, msg = pcall( mainmenu()) -- llamada a la función main en modo protegido
  301. if not estado then
  302.     while true do
  303.         beginGFX()
  304.         DrawText(10, 10, "Ha ocurrido un error: "..msg)
  305.         DrawText(10, 20, "Presione START para salir o SELECT para reiniciar.")
  306.        
  307.         endGFX()
  308.         if start(0) >= 1 then
  309.             break
  310.         elseif select(0) >= 1 then
  311.             dofile("./app.lua")
  312.         end
  313.     end
  314. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement