我最近开始用Pico-8编程游戏来学习Lua。这是很有趣的,但现在是时候学习一些更强大的实用程序,让事情更智能,更少的冗余。
我是一个绝对的初学者,试图正确地形成这个嵌套的for循环。任何帮助都将是惊人的,并帮助我转移到新的想法和概念。
如果需要/允许,我可以共享完整代码的链接。
下面是代码:
function _init()
mom = {}
dad = {}
-- variable to adjust x and y offset for checking solid tiles when
-- changing the level (default is 0, which aims at level 1)
levelpixeloffset_x = 0
levelpixeloffset_y = 0
--set which scene stage to display / where actors are placed
scene = {}
scene.counter = 1
-- player variables
player = {}
player.sprite = 1
player.speed = 0.8
player.movingup = false
player.spritecounterup = 0
player.movingdown = false
player.spritecounterdown = 0
player.movingleft = false
player.spritecounterleft = 0
player.movingright = false
player.spritecounterright = 0
player.x = 60
player.y = 74
player.w = 8
player.h = 8
-- island scene campfire variables
campfire = {}
campfire.counter = 1
campfire.sprite = 13
-- island scene wave variables
wave = {}
wave.counter = 1
wave.sprite = 86
-- island scene garden variables
garden = {}
garden.counter = 1
garden.sprite = 32
-- establishes standard tile width and height
tile_w = 8
tile_h = 8
end
-- constantly running loop during gameplay
function _update()
if scene.counter == 1 then
islandscene()
end
if scene.counter == 2 then
desertscene()
end
playermovement()
--mom_repel()
--dad_repel()
--campfire_repel()
player_repel()
end
-- draws screen
function _draw()
cls()
draw_background()
end
-- checks for solid map tiles, the flag1 state of which are referenced
-- in the btn press check in playermovement(). if flag1 is set, sprite
-- map tiles are solid, and the player collides with them by affecting
-- the players response to btn presses. levelpixeloffset_x and
-- levelpixeloffset_y need to be adjusted as appropriate when changing
-- the "level" or where the screen is drawing and the actors are placed
function solid_tile(x,y)
local tile_x = ((x - (x % 8)) / 8) + levelpixeloffset_x
local tile_y = ((y - (y % 8)) / 8) + levelpixeloffset_y
if(fget(mget(tile_x, tile_y), 0)) then
return true else
return false
end
end
-- draws backround of current scene
function draw_background()
cls()
if scene.counter == 1 then
-- island scene sprites and map
map(0,0,0,0)
spr(player.sprite, player.x, player.y)
spr(mom.sprite, mom.x, mom.y)
spr(dad.sprite, dad.x, dad.y)
spr(garden.sprite, 28, 85)
spr(garden.sprite, 28, 75)
spr(campfire.sprite, campfire.x, campfire.y)
spr(wave.sprite, 8, 8)
spr(wave.sprite, 16, 16)
spr(wave.sprite, 40, 8)
spr(wave.sprite, 80, 0)
spr(wave.sprite, 96, 8)
spr(wave.sprite, 112, 16)
spr(wave.sprite, 0, 112)
spr(wave.sprite, 8, 120)
spr(wave.sprite, 24, 120)
spr(wave.sprite, 104, 120)
spr(wave.sprite, 112, 112)
spr(wave.sprite, 120, 64)
spr(wave.sprite, 0, 72)
spr(wave.sprite, 0, 48)
end
if scene.counter == 2 then
-- desert scene sprites and map
map(16,0,0,0)
spr(player.sprite, player.x, player.y)
spr(mom.sprite, mom.x, mom.y)
spr(dad.sprite, dad.x, dad.y)
end
end
----------------------------------------------------------------
----------------------------------------------------------------
-- player animation and control section-------------------------
----------------------------------------------------------------
----------------------------------------------------------------
--manages all player controlled input and responses
function playermovement()
-- push button test - currently used to change camera / scene (use z)
if btnp(4) then
scene.counter += 1
end
--move left
if btn(0) then
--map collision check
if (solid_tile(player.x - 1, player.y)) == false
and (solid_tile(player.x - 1, player.y + 1)) == false
and (solid_tile(player.x - 1, player.y + 2)) == false
and (solid_tile(player.x - 1, player.y + 3)) == false
and (solid_tile(player.x - 1, player.y + 4)) == false
and (solid_tile(player.x - 1, player.y + 5)) == false
and (solid_tile(player.x - 1, player.y + 6)) == false
and (solid_tile(player.x - 1, player.y + 7)) == false
then
player.x -= player.speed
end
moveleft()
end
--move right
if btn(1) then
--map collision check
if(solid_tile(player.x + player.w, player.y)) == false
and(solid_tile(player.x + player.w, player.y + 1)) == false
and(solid_tile(player.x + player.w, player.y + 2)) == false
and(solid_tile(player.x + player.w, player.y + 3)) == false
and(solid_tile(player.x + player.w, player.y + 4)) == false
and(solid_tile(player.x + player.w, player.y + 5)) == false
and(solid_tile(player.x + player.w, player.y + 6)) == false
and(solid_tile(player.x + player.w, player.y + 7)) == false
then
player.x += player.speed
end
moveright()
end
--move up
if btn(2) then
--map collision check
if(solid_tile(player.x, player.y - 1)) == false
and(solid_tile(player.x + 1, player.y - 1)) == false
and(solid_tile(player.x + 2, player.y - 1)) == false
and(solid_tile(player.x + 3, player.y - 1)) == false
and(solid_tile(player.x + 4, player.y - 1)) == false
and(solid_tile(player.x + 5, player.y - 1)) == false
and(solid_tile(player.x + 6, player.y - 1)) == false
and(solid_tile(player.x + 7, player.y - 1)) == false
then
player.y -= player.speed
end
moveup()
end
--move down
if btn(3) then
--map collision check
if(solid_tile(player.x, player.y + player.h + 1)) == false
and(solid_tile(player.x + 1, player.y + player.h + 1)) == false
and(solid_tile(player.x + 2, player.y + player.h + 1)) == false
and(solid_tile(player.x + 3, player.y + player.h + 1)) == false
and(solid_tile(player.x + 4, player.y + player.h + 1)) == false
and(solid_tile(player.x + 5, player.y + player.h + 1)) == false
and(solid_tile(player.x + 6, player.y + player.h + 1)) == false
and(solid_tile(player.x + 7, player.y + player.h + 1)) == false
then
player.y += player.speed
end
movedown()
end
end
-- animates player while moving up
function moveup()
player.movingup = true
player.spritecounterup += 1
if player.spritecounterup > 0 then
player.sprite = 4
end
if player.spritecounterup > 4 then
player.sprite = 5
end
if player.spritecounterup > 8 then
player.sprite = 4
end
if player.spritecounterup > 12 then
player.sprite = 6
end
if player.spritecounterup > 16 then
player.spritecounterup = 0
end
end
-- animates player while moving down
function movedown()
player.movingdown = true
player.spritecounterdown += 1
if player.spritecounterdown > 0 then
player.sprite = 1
end
if player.spritecounterdown > 4 then
player.sprite = 2
end
if player.spritecounterdown > 8 then
player.sprite = 1
end
if player.spritecounterdown > 12 then
player.sprite = 3
end
if player.spritecounterdown > 16 then
player.spritecounterdown = 0
end
end
-- animates player while moving left
function moveleft()
player.movingleft = true
player.spritecounterleft += 1
if player.spritecounterleft > 0 then
player.sprite = 10
end
if player.spritecounterleft > 4 then
player.sprite = 11
end
if player.spritecounterleft > 8 then
player.sprite = 10
end
if player.spritecounterleft > 12 then
player.sprite = 12
end
if player.spritecounterleft > 16 then
player.spritecounterleft = 0
end
end
-- animates player while moving right
function moveright()
player.movingright = true
player.spritecounterright += 1
if player.spritecounterright > 0 then
player.sprite = 7
end
if player.spritecounterright > 4 then
player.sprite = 8
end
if player.spritecounterright > 8 then
player.sprite = 7
end
if player.spritecounterright > 12 then
player.sprite = 9
end
if player.spritecounterright > 16 then
player.spritecounterright = 0
end
end
---------------------------------------------------------------------
--Level information functions----------------------------------------
---------------------------------------------------------------------
function islandscene()
if scene.counter == 1 then
waveshift()
gardenshift()
campfireshift()
-- island scene mom variables
mom.sprite = 29
mom.x = 74
mom.y = 64
-- island scene dad variables
dad.sprite = 45
dad.x = 94
dad.y = 64
-- island scene campfire variables
campfire.x = 100
campfire.y = 45
end
end
function desertscene()
if scene.counter == 2 then
levelpixeloffset_x = 16
levelpixeloffset_y = 0
-- desert scene mom variables
mom.sprite = 29
mom.x = 30
mom.y = 64
mom.w = 8
mom.h = 8
-- desert scene dad variables
dad.sprite = 45
dad.x = 39
dad.y = 64
dad.w = 8
dad.h = 8
end
end
---------------------------------------------------------------------
---------------------------------------------------------------------
--utility functions--------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
-- player-repelling force anchored at island scene mom
function mom_repel()
if (player.x-1) > (mom.x) and (player.x-1) < (mom.x + tile_w)
and (player.y+1) > (mom.y) and (player.y+1) < (mom.y + tile_h)
or (player.x + 2) > (mom.x) and (player.x + 2) < (mom.x + tile_w)
and (player.y + 2) > (mom.y) and (player.y + 2) < (mom.y + tile_h)
or (player.x - 3) > (mom.x) and (player.x - 3) < (mom.x + tile_w)
and (player.y - 3) > (mom.y) and (player.y - 3) < (mom.y + tile_h)
or (player.x + 4) > (mom.x) and (player.x + 4) < (mom.x + tile_w)
and (player.y + 4) > (mom.y) and (player.y + 4) < (mom.y + tile_h)
or (player.x + 5) > (mom.x) and (player.x + 5) < (mom.x + tile_w)
and (player.y + 5) > (mom.y) and (player.y + 5) < (mom.y + tile_h)
or (player.x + 6) > (mom.x) and (player.x + 6) < (mom.x + tile_w)
and (player.y + 6) > (mom.y) and (player.y + 6) < (mom.y + tile_h) then
if player.x < mom.x then
player.x -= 0.55
end
if player.x > mom.x then
player.x += 0.55
end
if player.y < mom.y then
player.y -= 0.55
end
if player.y > mom.y then
player.y += 0.55
end
end
end
-- player-repelling force anchored at island scene dad
function dad_repel()
if (player.x-1) > (dad.x) and (player.x-1) < (dad.x + tile_w)
and (player.y+1) > (dad.y) and (player.y+1) < (dad.y + tile_h)
or (player.x + 2) > (dad.x) and (player.x + 2) < (dad.x + tile_w)
and (player.y + 2) > (dad.y) and (player.y + 2) < (dad.y + tile_h)
or (player.x - 3) > (dad.x) and (player.x - 3) < (dad.x + tile_w)
and (player.y - 3) > (dad.y) and (player.y - 3) < (dad.y + tile_h)
or (player.x + 4) > (dad.x) and (player.x + 4) < (dad.x + tile_w)
and (player.y + 4) > (dad.y) and (player.y + 4) < (dad.y + tile_h)
or (player.x + 5) > (dad.x) and (player.x + 5) < (dad.x + tile_w)
and (player.y + 5) > (dad.y) and (player.y + 5) < (dad.y + tile_h)
or (player.x + 6) > (dad.x) and (player.x + 6) < (dad.x + tile_w)
and (player.y + 6) > (dad.y) and (player.y + 6) < (dad.y + tile_h) then
if player.x < dad.x then
player.x -= 0.55
end
if player.x > dad.x then
player.x += 0.55
end
if player.y < dad.y then
player.y -= 0.55
end
if player.y > dad.y then
player.y += 0.55
end
end
end
-- player-repelling force anchored at island scene campfire
function campfire_repel()
if (player.x-1) > (campfire.x) and (player.x-1) < (campfire.x + tile_w)
and (player.y+1) > (campfire.y) and (player.y+1) < (campfire.y + tile_h)
or (player.x + 2) > (campfire.x) and (player.x + 2) < (campfire.x + tile_w)
and (player.y + 2) > (campfire.y) and (player.y + 2) < (campfire.y + tile_h)
or (player.x - 3) > (campfire.x) and (player.x - 3) < (campfire.x + tile_w)
and (player.y - 3) > (campfire.y) and (player.y - 3) < (campfire.y + tile_h)
or (player.x + 4) > (campfire.x) and (player.x + 4) < (campfire.x + tile_w)
and (player.y + 4) > (campfire.y) and (player.y + 4) < (campfire.y + tile_h)
or (player.x + 5) > (campfire.x) and (player.x + 5) < (campfire.x + tile_w)
and (player.y + 5) > (campfire.y) and (player.y + 5) < (campfire.y + tile_h)
or (player.x + 6) > (campfire.x) and (player.x + 6) < (campfire.x + tile_w)
and (player.y + 6) > (campfire.y) and (player.y + 6) < (campfire.y + tile_h) then
if player.x < campfire.x then
player.x -= 0.55
end
if player.x > campfire.x then
player.x += 0.55
end
if player.y < campfire.y then
player.y -= 0.55
end
if player.y > campfire.y then
player.y += 0.55
end
end
end
-- attempt at transforming existing "repel" forces into single function
-- with "for" loop
-- o = origin of repel force, a = area around origin's x/y coordinates
function player_repel()
for o in all(mom, dad, campfire) do
for a = -1,7 do
if player.x - a > o.x and player.y - a < o.x + o.w
and player.y - a > o.y and player.y - a < o.y + o.h then
if player.x < o.x then player.x -= 0.55 end
if player.x > o.x then player.x += 0.55 end
if player.y < o.y then player.y -= 0.55 end
if player.y > o.y then player.y += 0.55 end
end
end
end
end
-- makes island scene campfire shift
function campfireshift()
campfire.counter += 1
if campfire.counter > 0 then
campfire.sprite = 13
end
if campfire.counter > 4 then
campfire.sprite = 14
end
if campfire.counter > 6 then
campfire.sprite = 15
end
if campfire.counter > 9 then
campfire.counter = 0
end
end
-- makes island scene garden shift
function gardenshift()
garden.counter += 1
if garden.counter > 32 then
garden.sprite = 33
end
if garden.counter > 64 then
garden.counter = 0
end
if garden.counter < 1 then
garden.sprite = 32
end
end
-- makes island scene waves shift
function waveshift()
wave.counter += 1
if wave.counter > 24 then
wave.sprite = 102
end
if wave.counter > 48 then
wave.counter = 0
end
if wave.counter < 1 then
wave.sprite = 86
end
end发布于 2017-05-10 18:38:19
您可以简化一些嵌套的"if语句“:
if (solid_tile(player.x, player.y)) == false
and (solid_tile(player.x - 1, player.y + 1)) == false
and (solid_tile(player.x - 1, player.y + 2)) == false
and (solid_tile(player.x - 1, player.y + 3)) == false
and (solid_tile(player.x - 1, player.y + 4)) == false
and (solid_tile(player.x - 1, player.y + 5)) == false
and (solid_tile(player.x - 1, player.y + 6)) == false
and (solid_tile(player.x - 1, player.y + 7)) == false
then
player.x -= player.speed
end你可以这样写:
flag = true
for i=0,7,1 do
if solid_tile(player.x + player.w, player.y + i)) ~= false then
flag = false
end
end
if flag then
player.x += player.speed
endhttps://codereview.stackexchange.com/questions/162967
复制相似问题