Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------xerpi - (c) 2011 ----------------- PSP
- sw = 480
- sh = 272
- map = {ver=20,hor=20,w=20,h=20}
- map.x = sw/2 - (map.hor*map.w)/2
- map.y = sh/2 - (map.ver*map.h)/2
- tile = {w=20,h=20}
- tile.head = image.create(map.w, map.h, color.new(255,0,0))
- tile.body = image.create(map.w, map.h, color.new(0,255,0))
- tile.tail = image.create(map.w, map.h, color.new(0,0,255))
- tile.bg = image.create(map.w, map.h, color.new(255,0,255))
- snake = { body ={{x=10,y=10},{x=10,y=9}} , vel = 1, direction = "down",length = 2,counter=0,changed = false}
- function snake.insert()
- table.insert(snake.body,{x = snake.body[snake.length].x,y=snake.body[snake.length].y})
- snake.length = snake.length +1
- end
- function snake.blit()
- --Blit the head
- tile.head:blit(map.x+(snake.body[1].x-1)*map.w,map.y+(snake.body[1].y-1)*map.h)
- --Blit the body
- for i = 2, snake.length-1 do
- tile.body:blit(map.x+(snake.body[i].x-1)*map.w,map.y+(snake.body[i].y-1)*map.h)
- end
- --Blit the tail
- tile.tail:blit(map.x+(snake.body[snake.length].x-1)*map.w,map.y+(snake.body[snake.length].y-1)*map.h)
- end
- function snake.move()
- --Change snake direction
- if controls.left() and snake.direction != "right" and not snake.changed then snake.direction = "left" snake.changed = true end
- if controls.right() and snake.direction != "left" and not snake.changed then snake.direction = "right" snake.changed = true end
- if controls.up() and snake.direction != "down" and not snake.changed then snake.direction = "up" snake.changed = true end
- if controls.down() and snake.direction != "up" and not snake.changed then snake.direction = "down" snake.changed = true end
- --Increase the snake counter
- snake.counter = snake.counter + snake.vel
- --Move the snake?
- if snake.counter >= 100 then
- --Move all the body
- for i = snake.length, 2, -1 do
- snake.body[i].x = snake.body[i-1].x
- snake.body[i].y = snake.body[i-1].y
- end
- --Move the head
- if snake.direction == "up" then snake.body[1].y = snake.body[1].y - 1 end
- if snake.direction == "down" then snake.body[1].y = snake.body[1].y + 1 end
- if snake.direction == "left" then snake.body[1].x = snake.body[1].x - 1 end
- if snake.direction == "right" then snake.body[1].x = snake.body[1].x + 1 end
- --Reset the counter
- snake.counter = 0
- --Set the boolean changed as false ( for moving snake again )
- snake.changed = false
- end
- end
- while true do
- controls.read()
- if controls.square() then snake.vel = snake.vel - 0.1 end
- if controls.circle() then snake.vel = snake.vel + 0.1 end
- snake.move()
- snake.blit()
- screen.print(5,5,snake.direction.." "..tostring(snake.changed).." vel: "..snake.vel.." c: "..snake.counter)
- screen.print(430,5,"@"..screen.fps())
- if controls.press("r") then snake.insert() end
- if controls.select() then a() end
- screen.flip()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement