Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------xerpi - (c) 2011 ----------------- PSP
- math.randomseed(os.time()/math.pi*os.clock()/os.time())
- sw = 480
- sh = 272
- map = {ver=10,hor=10,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.food = image.create(map.w, map.h, color.new(150,75,0))
- 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, score = 0}
- food = {}
- function food.change(n)
- local x,y
- while true do
- local taken = false
- x,y = math.random(1,map.hor),math.random(1,map.ver)
- for i = 1, snake.length do
- if x == snake.body[i].x and y == snake.body[i].y then taken = true end
- end
- if not taken then break end
- end
- food[n].x = x
- food[n].y = y
- end
- function food.insert()
- local x,y
- while true do
- local taken = false
- x,y = math.random(1,map.hor),math.random(1,map.ver)
- for i = 1, snake.length do
- if x == snake.body[i].x and y == snake.body[i].y then taken = true end
- end
- if not taken then break end
- end
- table.insert(food, {x = x, y = y})
- end
- function food.blit()
- for i = 1, # food do
- tile.food:blit(map.x+(food[i].x-1)*map.w,map.y+(food[i].y-1)*map.h)
- end
- end
- 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
- function snake.check()
- for i = 1, #food do
- if food[i].x == snake.body[1].x and food[i].y == snake.body[1].y then
- snake.insert()
- snake.score = snake.score + 1
- food.change(i)
- end
- end
- end
- food.insert()
- 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()
- snake.check()
- food.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