Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:21:04+00:00 2026-06-17T11:21:04+00:00

My problem is simple, i used to do this in order to check if

  • 0

My problem is simple, i used to do this in order to check if any of my lines (see photo) bump into any other line (or trail of any other line). But the way i do this now is by doing a lot of if statements, which is not good. So here is the code i used:

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

Here is my init:

    -- 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 = {}

Now i tried doing the following, but failed because i dont now how to use variable variable names:

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

— EDIT: i use is 4 times, for 4 players!

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

end

— EDIT

How should i do this correctly? Thanks in advance!

Image: enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:21:07+00:00Added an answer on June 17, 2026 at 11:21 am

    You probably don’t really want variable variable names like you said, but you can do it…

    All globals are accessible by table named _G, and you can access your global variables by doing this:

    _G['killp1']
    _G['killp2']
    -- etc
    

    You can then use string manipulation as usual.

    local i = 1
    _G['killp' .. tostring(i)] -- same as _G['killp1']
    i = i + 1
    _G['killp' .. tostring(i)] -- same as _G['killp2']
    

    But this is going to get really messy, fast. What you probably want to do instead is start using objects. You object might look something like this:

    Linething = { x = 0, y = 0, dead = false, speed = 2.5 }
    
    function Linething:new(o)
      o = o or {}
      setmetatable(o, self)
      self.__index = self
      return o
    end
    
    function Linething:checkCollision(other)
      if self.x - other.x < 5 and self.y - other.y < 5 then
        self.dead = true
        -- other stuff
      end
    end
    
    function Linething:update(delta_t)
       -- update the game logic
       self.x = self.x + self.speed * cos(self.angle) * delta_t
       self.y = self.y + self.speed * sin(self.angle) * delta_t
    end
    
    function Linething:draw()
      -- do line drawing here
      tint(255, 0, 0, 162)
      ellipse(self.x, self.y, 10, 10)
      noTint()
      -- etc
    end
    
    -- in main:
    linething1 = Linething:new()
    linething2 = Linething:new()
    linething1.checkCollision(linething2)
    

    EDIT: Typically you want to seperate the game logic from the rendering. Most games have a loop that looks like this:

    function main_loop()
      while true do
        delta_t = get_change_in_time()
        update_all(delta_t)
        draw_all()
      end
    end
    
    -- where update_all is something like this:
    function update_all(delta_t)
      player1:update(delta_t)
      player2:update(delta_t)
      -- things in a table:
      for _, x in pairs(table_of_x) do
        x:update(delta_t)
      end
    end
    
    -- and draw all is basically the same
    function draw_all()
      player1:draw()
      player2:draw()
      -- things in a table:
      for _, x in pairs(table_of_x) do
        x:draw()
      end
    end
    

    It looks like your Player object has-a Linething object. So you might want to add a Linething object to the Player object and change Player:draw() to call players_linething:draw(). Then you can have 4 Player objects, which each have a Linething object. Does that make sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really simple problem: I want to split a connection string into its keyword /
This simple problem is kiling me. I posted something earlier about trying to clean
This is a simple problem, but I would like suggestions on how to solve
I am rather newbie in SQL and before this moment I only used simple
This should be quite a simple problem, I've come across it enough that there
Simple problem that I can't figure out... How can I print a '%' character
Simple problem (I think): I want to be able to invoke a click method
simple problem with a simple question. I have a string, in C# i can
My problem is simple, I have the font set in the body style to
The problem seems simple, but I can't seem to find a way to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.