Advertisement
xerpi

Sneik

Aug 27th, 2011
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. --------------xerpi - (c)   2011   ----------------- PSP
  2. sw = 480
  3. sh = 272
  4.  
  5. map = {ver=20,hor=20,w=20,h=20}
  6.     map.x = sw/2 - (map.hor*map.w)/2
  7.     map.y = sh/2 - (map.ver*map.h)/2
  8. tile = {w=20,h=20}
  9.     tile.head = image.create(map.w, map.h, color.new(255,0,0))
  10.     tile.body = image.create(map.w, map.h, color.new(0,255,0))
  11.     tile.tail = image.create(map.w, map.h, color.new(0,0,255))
  12.     tile.bg = image.create(map.w, map.h, color.new(255,0,255))
  13. snake = { body ={{x=10,y=10},{x=10,y=9}} , vel = 1, direction = "down",length = 2,counter=0,changed = false}
  14.  
  15. function snake.insert()
  16.     table.insert(snake.body,{x = snake.body[snake.length].x,y=snake.body[snake.length].y})
  17.     snake.length = snake.length +1
  18. end
  19.  
  20. function snake.blit()
  21.     --Blit the head
  22.         tile.head:blit(map.x+(snake.body[1].x-1)*map.w,map.y+(snake.body[1].y-1)*map.h)
  23.     --Blit the body
  24.         for i = 2, snake.length-1 do
  25.             tile.body:blit(map.x+(snake.body[i].x-1)*map.w,map.y+(snake.body[i].y-1)*map.h)
  26.         end
  27.     --Blit the tail
  28.         tile.tail:blit(map.x+(snake.body[snake.length].x-1)*map.w,map.y+(snake.body[snake.length].y-1)*map.h)
  29. end
  30.  
  31. function snake.move()
  32.     --Change snake direction
  33.         if controls.left()  and snake.direction != "right" and not snake.changed then snake.direction = "left"  snake.changed = true end
  34.         if controls.right() and snake.direction != "left"  and not snake.changed then snake.direction = "right" snake.changed = true end
  35.         if controls.up()    and snake.direction != "down"  and not snake.changed then snake.direction = "up"    snake.changed = true end
  36.         if controls.down()  and snake.direction != "up"    and not snake.changed then snake.direction = "down"  snake.changed = true end
  37.     --Increase the snake counter
  38.         snake.counter = snake.counter + snake.vel
  39.     --Move the snake?
  40.         if snake.counter >= 100 then
  41.             --Move all the body
  42.                 for i = snake.length, 2, -1 do
  43.                     snake.body[i].x = snake.body[i-1].x
  44.                     snake.body[i].y = snake.body[i-1].y
  45.                 end
  46.             --Move the head
  47.                 if snake.direction == "up"    then snake.body[1].y = snake.body[1].y - 1 end
  48.                 if snake.direction == "down"  then snake.body[1].y = snake.body[1].y + 1 end
  49.                 if snake.direction == "left"  then snake.body[1].x = snake.body[1].x - 1 end
  50.                 if snake.direction == "right" then snake.body[1].x = snake.body[1].x + 1 end
  51.             --Reset the counter
  52.                 snake.counter = 0
  53.             --Set the boolean changed as false ( for moving snake again )
  54.                 snake.changed = false
  55.         end
  56. end
  57.  
  58.  
  59. while true do
  60. controls.read()
  61.  
  62. if controls.square() then snake.vel = snake.vel - 0.1 end
  63. if controls.circle() then snake.vel = snake.vel + 0.1 end
  64.  
  65. snake.move()
  66. snake.blit()
  67. screen.print(5,5,snake.direction.."   "..tostring(snake.changed).."  vel: "..snake.vel.."  c: "..snake.counter)
  68. screen.print(430,5,"@"..screen.fps())
  69. if controls.press("r") then snake.insert() end
  70. if controls.select() then a() end
  71. screen.flip()
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement