我的问题很简单,我曾经这样做是为了检查我的任何线路(见图)是否撞到任何其他线路(或任何其他线路的轨迹)。但是我现在这样做的方式是做很多 if 语句,这是不好的。所以这是我使用的代码:
function Bumper:draw()
for id,posx1 in pairs(tableposx1) do
posy1 = tableposy1[id]
posx2 = tableposx2[id]
posy2 = tableposy2[id]
posx3 = tableposx3[id]
posy3 = tableposy3[id]
posx4 = tableposx4[id]
posy4 = tableposy4[id]
if (posx1 ~= nil) and (posy1 ~= nil) and
((math.abs(xposplayer1 - posx1) < 5) and (math.abs(yposplayer1 - posy1) < 5))
and (id < (count - 5) and killp1 == "false") then
killp1 = "true"
tint(255, 0, 0, 162)
sprite("Dropbox:kill",xposplayer1,yposplayer1)
noTint()
end
if (posx2 ~= nil) and (posy2 ~= nil) and
((math.abs(xposplayer1 - posx2) < 5) and (math.abs(yposplayer1 - posy2) < 5))
and killp1 == "false" then
killp1 = "true"
tint(255, 0, 0, 162)
sprite("Dropbox:kill",xposplayer1,yposplayer1)
noTint()
end
if (posx3 ~= nil) and (posy3 ~= nil) and
((math.abs(xposplayer1 - posx3) < 5) and (math.abs(yposplayer1 - posy3) < 5))
and killp1 == "false" then
killp1 = "true"
tint(255, 0, 0, 162)
sprite("Dropbox:kill",xposplayer1,yposplayer1)
noTint()
end
if (posx4 ~= nil) and (posy4 ~= nil) and
((math.abs(xposplayer1 - posx4) < 5) and (math.abs(yposplayer1 - posy4) < 5))
and killp1 == "false" then
killp1 = "true"
tint(255, 0, 0, 162)
sprite("Dropbox:kill",xposplayer1,yposplayer1)
noTint()
end
if (posx1 ~= nil) and (posy1 ~= nil) and
((math.abs(xposplayer2 - posx1) < 5) and (math.abs(yposplayer2 - posy1) < 5))
and killp2 == "false" then
killp2 = "true"
tint(0, 29, 255, 162)
sprite("Dropbox:kill",xposplayer2,yposplayer2)
noTint()
end
if (posx2 ~= nil) and (posy2 ~= nil) and
((math.abs(xposplayer2 - posx2) < 5) and (math.abs(yposplayer2 - posy2) < 5))
and (id < (count - 5) and killp2 == "false")then
killp2 = "true"
tint(0, 29, 255, 162)
sprite("Dropbox:kill",xposplayer2,yposplayer2)
noTint()
end
if (posx3 ~= nil) and (posy3 ~= nil) and
((math.abs(xposplayer2 - posx3) < 5) and (math.abs(yposplayer2 - posy3) < 5))
and killp2 == "false" then
killp2 = "true"
tint(0, 29, 255, 162)
sprite("Dropbox:kill",xposplayer2,yposplayer2)
noTint()
end
if (posx4 ~= nil) and (posy4 ~= nil) and
((math.abs(xposplayer2 - posx4) < 5) and (math.abs(yposplayer2 - posy4) < 5))
and killp2 == "false" then
killp2 = "true"
tint(0, 29, 255, 162)
sprite("Dropbox:kill",xposplayer2,yposplayer2)
noTint()
end
if (posx1 ~= nil) and (posy1 ~= nil) and
((math.abs(xposplayer3 - posx1) < 5) and (math.abs(yposplayer3 - posy1) < 5))
and killp3 == "false" then
killp3 = "true"
tint(1, 255, 0, 162)
sprite("Dropbox:kill",xposplayer3,yposplayer3)
noTint()
end
if (posx2 ~= nil) and (posy2 ~= nil) and
((math.abs(xposplayer3 - posx2) < 5) and (math.abs(yposplayer3 - posy2) < 5))
and killp3 == "false" then
killp3 = "true"
tint(1, 255, 0, 162)
sprite("Dropbox:kill",xposplayer3,yposplayer3)
noTint()
end
if (posx3 ~= nil) and (posy3 ~= nil) and
((math.abs(xposplayer3 - posx3) < 5) and (math.abs(yposplayer3 - posy3) < 5))
and (id < (count - 5) and killp3 == "false") then
killp3 = "true"
tint(1, 255, 0, 162)
sprite("Dropbox:kill",xposplayer3,yposplayer3)
noTint()
end
if (posx4 ~= nil) and (posy4 ~= nil) and
((math.abs(xposplayer3 - posx4) < 5) and (math.abs(yposplayer3 - posy4) < 5))
and killp3 == "false" then
killp3 = "true"
tint(1, 255, 0, 162)
sprite("Dropbox:kill",xposplayer3,yposplayer3)
noTint()
end
if (posx1 ~= nil) and (posy1 ~= nil) and
((math.abs(xposplayer4 - posx1) < 5) and (math.abs(yposplayer4 - posy1) < 5))
and killp4 == "false" then
killp4 = "true"
tint(255, 0, 229, 162)
sprite("Dropbox:kill",xposplayer4,yposplayer4)
noTint()
end
if (posx2 ~= nil) and (posy2 ~= nil) and
((math.abs(xposplayer4 - posx2) < 5) and (math.abs(yposplayer4 - posy2) < 5))
and killp4 == "false" then
killp4 = "true"
tint(255, 0, 229, 162)
sprite("Dropbox:kill",xposplayer4,yposplayer4)
noTint()
end
if (posx3 ~= nil) and (posy3 ~= nil) and
((math.abs(xposplayer4 - posx3) < 5) and (math.abs(yposplayer4 - posy3) < 5))
and killp4 == "false" then
killp4 = "true"
tint(255, 0, 229, 162)
sprite("Dropbox:kill",xposplayer4,yposplayer4)
noTint()
end
if (posx4 ~= nil) and (posy4 ~= nil) and
((math.abs(xposplayer4 - posx4) < 5) and (math.abs(yposplayer4 - posy4) < 5))
and id < (count - 5) and killp4 == "false" then
killp4 = "true"
tint(255, 0, 229, 162)
sprite("Dropbox:kill",xposplayer4,yposplayer4)
noTint()
end
end
end
这是我的初始化:
-- Main
a = 1
gameover = 0
tx = 0
paused = 1
count = 0
stagecount = 0
player1count = 0
player2count = 0
player3count = 0
player4count = 0
-- Random hole times
thistime1 = 0
thistime2 = 0
thistime3 = 0
thistime4 = 0
-- Dead players
killp1 = "false"
killp2 = "false"
killp3 = "false"
killp4 = "false"
kills = 0
-- Touch control
touches = {}
-- Player direction
pi = math.pi
o5pi = 50 * pi
mo5pi = - (50 * pi)
minpi = - (100*pi)
-- Random start direction
r1t = math.random(0,o5pi)
r2t = math.random(mo5pi,0)
r3t = math.random(minpi,mo5pi)
r4t = math.random(o5pi,100*pi)
r1 = r1t / 100
r2 = r2t / 100
r3 = r3t / 100
r4 = r4t / 100
deltar1 = pi / 55
deltar2 = pi / 55
deltar3 = pi / 55
deltar4 = pi / 55
-- Player speed
speed1 = 2.5
speed2 = 2.5
speed3 = 2.5
speed4 = 2.5
-- Player random start position
xposplayer1 = math.random(123,300)
yposplayer1 = math.random(123,300)
xposplayer2 = math.random(123,300)
yposplayer2 = math.random(468,645)
xposplayer3 = math.random(724,901)
yposplayer3 = math.random(468,645)
xposplayer4 = math.random(724,901)
yposplayer4 = math.random(123,300)
-- Player coordinate tables
tableposx1 = {}
tableposy1 = {}
tableposx2 = {}
tableposy2 = {}
tableposx3 = {}
tableposy3 = {}
tableposx4 = {}
tableposy4 = {}
现在我尝试执行以下操作,但失败了,因为我现在不知道如何使用变量变量名:
for id,posx[a] in pairs(tableposx[a]) do
posy[a] = tableposy[a][id]
if ((posx[a] ~= nil) and (posy[a] ~= nil))
and (math.abs(xposplayer[a] - posx[a]) < 5)
and (math.abs(yposplayer[a] - posy[a]) < 5)
and (id < (count - 5))
and (killp[a] == "false")
then
killp[a] = "true"
tintp[a]
sprite("Dropbox:kill",xposplayer[a],yposplayer[a])
noTint()
end
end
a = a + 1
if a = 4 then
a = 1
end
-- 编辑:我使用的是 4 次,4 名玩家!
function Player:draw1()
fill(255, 0, 11, 255)
xposplayer1 = xposplayer1 + speed1 * math.cos(r1)
yposplayer1 = yposplayer1 + speed1 * math.sin(r1)
if player1count == 0 then
thistime1 = math.random(200,500)
end
player1count = player1count + 1
if player1count < thistime1 then
table.insert(tableposx1,count,xposplayer1)
table.insert(tableposy1,count,yposplayer1)
tint(255, 0, 0, 162)
ellipse(xposplayer1,yposplayer1,10,10)
noTint()
elseif player1count > thistime1 then
tint(255, 0, 0, 162)
ellipse(xposplayer1,yposplayer1,5,5)
noTint()
if player1count > thistime1 + 20 then
player1count = 0
end
end
结尾
- 编辑
我应该如何正确地做到这一点?提前致谢!
图片: