Code: Select all
tilesize = 24
playerImage = Image.createEmpty(32, 32)
tileimg=Image.createEmpty(30, 30)
tileimg1=Image.createEmpty(30, 30)
white = Color.new(255,255,255)
red=Color.new(255,0,0)
blue=Color.new(0,255,0)
tileimg:clear(red)
tileimg1:clear(blue)
playerImage:clear(white)
tile = {tileimg, tileimg1}
player = {
x = 40, y = 184, j=200, speedY = -2, speedX = 0, falling = 0,}
player.img = playerImage
screenwidth = 480-24
screenheight = 272-player.img:width()
world = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
}
function drawTiles()
for Y = 1, #world do
for X = 1, #world[Y] do
local x = (X - 1) * tilesize
local y = (Y - 1) * tilesize
for tileNum = 1,#tile do
if world[Y][X] == tileNum then
screen:blit(x, y, tile[tileNum])
end
end
end
end
end
function movePlayer()
--Walk left--
if pad:left() and player.x>0 then
stand = 2
z=1
blit = 2
player.y=184
player.x = player.x-2
player.img = playerImage
end
--Walk right--
if pad:right() and player.x<screenwidth then
stand = 2
z=2
blit = 2
player.y=184
player.x = player.x+2
player.img = playerImage
end
end
--Jump--
function jump(object,Floor, grav)
object = player or object
Floor = Floor or 184
grav = grav or 1
jumping = false
if pad:cross() and z==1 and object.falling == 0 then
jumping = true
player.img=playerImage
stand=2
elseif not pad:cross()and z==1 and object.y < Floor or object.falling == 1 then
jumping = true
player.img=playerImage
stand=2
end
if pad:cross() and z==2 and object.falling == 0 then
jumping = true
player.img = playerImage
stand=2
elseif not pad:cross() and z==2 and object.y < Floor or object.falling == 1 then
jumping = true
player.img = playerImage
stand=2
end
if jumping then
object.speedY = object.speedY - grav
object.y = object.y - object.speedY
end
if object.y >= Floor and object.falling == 0 then
object.y = Floor
object.speedY = 10
jumping = false
end
end
--Blit image--
function blitimg()
if blit == 2 then
screen:blit(player.x, player.y, player.img)
else
screen:blit(player.x, player.y, player.img)
end
end
while true do
pad = Controls.read()
screen:clear()
drawTiles()
blitimg()
movePlayer()
jump()
screen.flip()
screen.waitVblankStart()
end
Advertising