Discord
Login
Community
DARK THEME

What's the best way to make and reuse random numbers?

here's my current code

...

range=120

random1=((random.nextInt(range))-(range/2))

random2=((random.nextInt(range))-(range/2))

random3=((random.nextInt(range))-(range/2))

random4=((random.nextInt(range))-(range/2))

random5=((random.nextInt(range))-(range/2))

random6=((random.nextInt(range))-(range/2))

...

pipeSize=20

pipeGap=120

drawPipe(-200, random1)

drawPipe(0, random2)

drawPipe(200, random3)

drawPipe(400, random4)

drawPipe(600, random5)

drawPipe(800, random6)

...

I'd like a way to do these in loops so they can generate infinitely, but I can't find out how

Pipes? Sounds like Flappy Bird style game?

As usual, it is hard to give any advice without knowing any details.
If it is a Flappy Bird style game check the existing projects.

For a quick overview, normally each Pipe is created as an object with its own start values (random start position).
Once the Pipe leaves the screen it destroys itself and triggers the creation of a new Pipe. For sure the details are a little bit more complex than that, check existing examples.

Live Long and Tinker

P.S. if my guess was wrong it is always a good idea to add a link to your game to the question. Publish it ( unlisted) and post the link from the Publish page

This is written without checking, but it's more or less what you are asking about:

local range=120
local rndNumbers=[]
for i = 0 to 6
  rndNumbers.push(((random.nextInt(range))-(range/2)))
end

and then

local pipeSize=20
local pipeGap=120
local startX=-200
for i=0 to 6
  drawPipe(-200 + i*200,rndNumbers[i])
end

create a class that has the x and y rechanges by a function called set_Last, but you need a camera left side point for this.

init = function()

    pipe = class
        constructor = function(_x, _y)
            this.set_Last(_x, _y)
        end

        set_Last = function(_x, _y) // this function is generally called by the pipes manager
            this.x = _x
            this.y = _y
        end

    end

    pipeSpacing = 100 // any number
    maxValueY = 120 // same ^^
    maxValueX = 0

    pipeManager = object
        pipes = []
        init = function(_numberofPipes)
            for _index = 0 to _numberofPipes
                this.pipes.push(new pipe(_index * pipeSpacing, this.getRandomY())
                maxValueX = max(maxValueX, this.pipes[_index].x)
            end
        end

        getRandomY = function() return random.nextInt(maxValueY) - maxValueY / 2 end

        update = function()
            for _pipe in this.pipes
                if _pipe.x > camera.leftSidePoint then // where the camera here must be defined and you have the left side point
                    _pipe.set_Last(maxValueX + pipeSpacing, getRandomY())
                    maxValueX = _pipe.x
                end
            end
        end
        // add your draw function as for loop
    end
end

You just have to init the pipeManager, update it, and draw it. Don't forget to add random.seed(system.time())

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community