Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------- xerpi (c) 2011 ------------------
- InitGFX(720,480)
- initPads(7)
- setsixaxis(0,1)
- textures = { }
- for i = 1, 9 do
- table.insert(textures, 1, surface())
- end
- textures[1]:LoadIMG("gfx/head.bmp")
- textures[2]:LoadIMG("gfx/body.bmp")
- textures[3]:LoadIMG("gfx/tail.bmp")
- textures[4]:LoadIMG("gfx/food.bmp")
- textures[5]:LoadIMG("gfx/bg.bmp")
- textures[6]:LoadIMG("gfx/splashps32011.bmp")
- textures[7]:LoadIMG("gfx/sneikps3.bmp")
- textures[8]:LoadIMG("gfx/hasperdido.bmp")
- textures[9]:LoadIMG("gfx/rock.bmp")
- number = {}
- for i = 1 , 10 do
- table.insert(number, 1,surface())
- end
- number[1]:LoadIMG("gfx/numbers/1.bmp")
- number[2]:LoadIMG("gfx/numbers/2.bmp")
- number[3]:LoadIMG("gfx/numbers/3.bmp")
- number[4]:LoadIMG("gfx/numbers/4.bmp")
- number[5]:LoadIMG("gfx/numbers/5.bmp")
- number[6]:LoadIMG("gfx/numbers/6.bmp")
- number[7]:LoadIMG("gfx/numbers/7.bmp")
- number[8]:LoadIMG("gfx/numbers/8.bmp")
- number[9]:LoadIMG("gfx/numbers/9.bmp")
- number[10]:LoadIMG("gfx/numbers/0.bmp")
- function sbsplash2011()
- for i = 1, 500 do
- beginGFX()
- textures[6]:setRectPos(0,0)
- BlitToScreen(textures[6])
- endGFX()
- end
- end
- function mainmenu()
- while true do
- beginGFX()
- textures[7]:setRectPos(0,0)
- BlitToScreen(textures[7])
- if cross(0) >= 1 then play_game() end
- endGFX()
- end
- end
- function losemenu()
- while true do
- beginGFX()
- textures[8]:setRectPos(0,0)
- BlitToScreen(textures[8])
- if cross(0) >= 1 then pressed = false end
- DrawText(350,440, string.format("%i", snake.score))
- endGFX()
- end
- end
- --[[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))
- ]]
- sw, sh = screenRes()
- map = {ver=22,hor=25,w=20,h=20}
- map.x = sw/2 - (map.hor*map.w)/2
- map.y = sh/2 - (map.ver*map.h)/2
- 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}
- food = {}
- level = {current = 2}
- level[1]= {
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- }
- level[2]= {
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- {"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"},
- }
- 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
- textures[4]:setRectPos(map.x+(food[i].x-1)*map.w,map.y+(food[i].y-1)*map.h)
- BlitToScreen(textures[4])
- 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.kill()
- 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}}
- end
- function snake.blit()
- --Blit the body
- for i = 2, snake.length-1 do
- textures[2]:setRectPos(map.x+(snake.body[i].x-1)*map.w,map.y+(snake.body[i].y-1)*map.h)
- BlitToScreen(textures[2])
- end
- --Blit the tail
- textures[3]:setRectPos(map.x+(snake.body[snake.length].x-1)*map.w,map.y+(snake.body[snake.length].y-1)*map.h)
- BlitToScreen(textures[3])
- --Blit the head
- textures[1]:setRectPos(map.x+(snake.body[1].x-1)*map.w,map.y+(snake.body[1].y-1)*map.h)
- BlitToScreen(textures[1])
- end
- function snake.move()
- --Change snake direction
- if left(0) >= 1 and snake.direction ~= "right" and not snake.changed then snake.direction = "left" snake.changed = true end
- if right(0) >= 1 and snake.direction ~= "left" and not snake.changed then snake.direction = "right" snake.changed = true end
- if up(0) >= 1 and snake.direction ~= "down" and not snake.changed then snake.direction = "up" snake.changed = true end
- if down(0) >= 1 and snake.direction ~= "up" and not snake.changed then snake.direction = "down" snake.changed = true end
- --Increase the snake vel variable
- snake.vel = snake.vel + 0.0001
- --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
- --Check if the snake is at the limit
- if snake.body[1].y < 1 then snake.body[1].y = map.ver end
- if snake.body[1].y > map.ver then snake.body[1].y = 1 end
- if snake.body[1].x < 1 then snake.body[1].x = map.hor end
- if snake.body[1].x > map.hor then 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.checkfood()
- 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
- function snake.checkcollisions()
- if level[level.current][snake.body[1].y][snake.body[1].x] == "r" then
- return true
- end
- return snake.selfcollision()
- end
- function snake.selfcollision()
- for i = 2, snake.length do
- if snake.body[1].x == snake.body[i].x and snake.body[1].y == snake.body[i].y then return true end
- end
- return false
- end
- function drawmap()
- for y = 1, map.ver do
- for x = 1, map.hor do
- if level[level.current][y][x] == "e" then
- textures[5]:setRectPos(map.x+(x-1)*map.w,map.y+(y-1)*map.h)
- BlitToScreen(textures[5])
- elseif level[level.current][y][x] == "r" then
- textures[9]:setRectPos(map.x+(x-1)*map.w,map.y+(y-1)*map.h)
- BlitToScreen(textures[9])
- end
- end
- end
- end
- function DrawCustom(x,y,text,dist)
- --number={1,2,3,4,5,6,7,8,9,0}
- if type(text) == "number" then text = tostring(text) end
- for i = 1, string.len(text) do
- local n = tonumber(string.sub(text,i,i))
- if n == 0 then
- number[10]:setRectPos(x + (i-1)*dist,y)
- BlitToScreen(number[10])
- else
- number[n]:setRectPos(x + (i-1)*dist,y)
- BlitToScreen(number[n])
- end
- end
- end
- function play_game()
- food.insert()
- snake.kill()
- while true do
- beginGFX()
- drawmap()
- snake.move()
- snake.checkfood()
- food.blit()
- snake.blit()
- snake.checkcollisions()
- if select(0) >= 1 then break end
- if start(0) >= 1 then Screenshot("screenshot.bmp") end
- if cross(0) >= 1 and not pressed then snake.insert() snake.score = snake.score +2 pressed = true end
- if cross(0) < 1 then pressed = false end
- DrawText(10,10, string.format("Score: %i", snake.score))
- DrawCustom(100,100,snake.score,17)
- DrawText(10,30, string.format("Vel: %i", snake.vel))
- if snake.checkcollisions() then DrawText(10,20, "COLLISIONING") end
- endGFX()
- end
- end
- --mainmenu()
- estado, msg = pcall( mainmenu()) -- llamada a la función main en modo protegido
- if not estado then
- while true do
- beginGFX()
- DrawText(10, 10, "Ha ocurrido un error: "..msg)
- DrawText(10, 20, "Presione START para salir o SELECT para reiniciar.")
- endGFX()
- if start(0) >= 1 then
- break
- elseif select(0) >= 1 then
- dofile("./app.lua")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement