Advertisement
xerpi

sneik lala

Aug 27th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. --------------xerpi - (c)   2011   ----------------- PSP
  2. math.randomseed(os.time()/math.pi*os.clock()/os.time())
  3.  
  4. sw = 480
  5. sh = 272
  6.  
  7. map = {ver=10,hor=10,w=20,h=20}
  8.     map.x = sw/2 - (map.hor*map.w)/2
  9.     map.y = sh/2 - (map.ver*map.h)/2
  10. tile = {w=20,h=20}
  11.     tile.head = image.create(map.w, map.h, color.new(255,0,0))
  12.     tile.body = image.create(map.w, map.h, color.new(0,255,0))
  13.     tile.tail = image.create(map.w, map.h, color.new(0,0,255))
  14.     tile.food = image.create(map.w, map.h, color.new(150,75,0))
  15.     tile.bg = image.create(map.w, map.h, color.new(255,0,255))
  16. snake = { body ={{x=10,y=10},{x=10,y=9}} , vel = 1, direction = "down",length = 2,counter=0,changed = false, score = 0}
  17. food = {}
  18.  
  19. function food.change(n)
  20.     local x,y
  21.     while true do
  22.         local taken = false
  23.         x,y = math.random(1,map.hor),math.random(1,map.ver)
  24.         for i = 1, snake.length do
  25.             if x == snake.body[i].x and y == snake.body[i].y then taken = true end
  26.         end
  27.         if not taken then break end
  28.     end
  29.     food[n].x = x
  30.     food[n].y = y
  31. end
  32. function food.insert()
  33.     local x,y
  34.     while true do
  35.         local taken = false
  36.         x,y = math.random(1,map.hor),math.random(1,map.ver)
  37.         for i = 1, snake.length do
  38.             if x == snake.body[i].x and y == snake.body[i].y then taken = true end
  39.         end
  40.         if not taken then break end
  41.     end
  42.     table.insert(food, {x = x, y = y})
  43. end
  44.  
  45. function food.blit()
  46.     for i = 1, # food do
  47.         tile.food:blit(map.x+(food[i].x-1)*map.w,map.y+(food[i].y-1)*map.h)
  48.     end
  49. end
  50. function snake.insert()
  51.     table.insert(snake.body,{x = snake.body[snake.length].x,y=snake.body[snake.length].y})
  52.     snake.length = snake.length +1
  53. end
  54.  
  55. function snake.blit()
  56.     --Blit the head
  57.         tile.head:blit(map.x+(snake.body[1].x-1)*map.w,map.y+(snake.body[1].y-1)*map.h)
  58.     --Blit the body
  59.         for i = 2, snake.length-1 do
  60.             tile.body:blit(map.x+(snake.body[i].x-1)*map.w,map.y+(snake.body[i].y-1)*map.h)
  61.         end
  62.     --Blit the tail
  63.         tile.tail:blit(map.x+(snake.body[snake.length].x-1)*map.w,map.y+(snake.body[snake.length].y-1)*map.h)
  64. end
  65.  
  66. function snake.move()
  67.     --Change snake direction
  68.         if controls.left()  and snake.direction != "right" and not snake.changed then snake.direction = "left"  snake.changed = true end
  69.         if controls.right() and snake.direction != "left"  and not snake.changed then snake.direction = "right" snake.changed = true end
  70.         if controls.up()    and snake.direction != "down"  and not snake.changed then snake.direction = "up"    snake.changed = true end
  71.         if controls.down()  and snake.direction != "up"    and not snake.changed then snake.direction = "down"  snake.changed = true end
  72.     --Increase the snake counter
  73.         snake.counter = snake.counter + snake.vel
  74.     --Move the snake?
  75.         if snake.counter >= 100 then
  76.             --Move all the body
  77.                 for i = snake.length, 2, -1 do
  78.                     snake.body[i].x = snake.body[i-1].x
  79.                     snake.body[i].y = snake.body[i-1].y
  80.                 end
  81.             --Move the head
  82.                 if snake.direction == "up"    then snake.body[1].y = snake.body[1].y - 1 end
  83.                 if snake.direction == "down"  then snake.body[1].y = snake.body[1].y + 1 end
  84.                 if snake.direction == "left"  then snake.body[1].x = snake.body[1].x - 1 end
  85.                 if snake.direction == "right" then snake.body[1].x = snake.body[1].x + 1 end
  86.             --Reset the counter
  87.                 snake.counter = 0
  88.             --Set the boolean changed as false ( for moving snake again )
  89.                 snake.changed = false
  90.         end
  91. end
  92.  
  93. function snake.check()
  94.     for i = 1, #food do
  95.         if food[i].x == snake.body[1].x and food[i].y == snake.body[1].y then
  96.             snake.insert()
  97.             snake.score = snake.score + 1
  98.             food.change(i)
  99.         end
  100.     end
  101. end
  102.  
  103.  
  104. food.insert()
  105. while true do
  106. controls.read()
  107.  
  108. if controls.square() then snake.vel = snake.vel - 0.1 end
  109. if controls.circle() then snake.vel = snake.vel + 0.1 end
  110.  
  111. snake.move()
  112. snake.blit()
  113. snake.check()
  114. food.blit()
  115. screen.print(5,5,snake.direction.."   "..tostring(snake.changed).."  vel: "..snake.vel.."  c: "..snake.counter)
  116. screen.print(430,5,"@"..screen.fps())
  117. if controls.press("r") then snake.insert() end
  118. if controls.select() then a() end
  119. screen.flip()
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement