让我们看一下上一讲我们编写的程序。代码应该与下面的类似:
代码:
grass = Image.load("grass.png")
player = Image.load("player.png")
flower = Image.load("flower.png")
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
Player = { }
Player[1] = { x = 200, y = 50 }
while true do
pad = Controls.read()
screen:clear()
for a = 0, 14 do
for b = 0,8 do
screen:blit(32 * a, 32 * b, grass)
end
end
screen:blit(100,100,flower)
screen:blit(300,220,flower)
screen:blit(Player[1].x,Player[1].y,player)
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end
if pad:right() and Player[1].x < screenwidth then
Player[1].x = Player[1].x + 2
end
if pad:up() and Player[1].y > 0 then
Player[1].y = Player[1].y - 2
end
if pad:down() and Player[1].y < screenheight then
Player[1].y = Player[1].y + 2
end
screen.waitVblankStart()
screen.flip()
end
我们可以创建一些函数,让主循环看上去更干净整齐一些。我们可以将所有的方向键检查放入一个函数,还可以将屏幕绘制的代码放入一个函数。这两个都不需要返回值。看看下面的代码,了解函数的使用:
代码:
grass = Image.load("grass.png")
player = Image.load("player.png")
flower = Image.load("flower.png")
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
Player = { }
Player[1] = { x = 200, y = 50 }
-- Function to check player movements
function playerMovement()
pad = Controls.read()
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end
if pad:right() and Player[1].x < screenwidth then
Player[1].x = Player[1].x + 2
end
if pad:up() and Player[1].y > 0 then
Player[1].y = Player[1].y - 2
end
if pad:down() and Player[1].y < screenheight then