#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Generating pipes

edited May 2017 Posts: 9

Hi, I am trying to generate pipes (sort of like in flappy bird), but which spawn from the top. I wanted to make a fixed gap of 200, but it turned out to be something very strange. Could someone help?

Here is my code:
function setup()
x=100
y=300
speedY=-3
speedX=-3
gap=200
x1=WIDTH
y1=math.random(10,WIDTH-gap)
pipeH=HEIGHT
print(WIDTH,HEIGHT)

end
function draw()
background(0, 0, 0, 255)
fill(255, 255, 255, 255)
--y=y+speedY
--speedY=speedY-.2
--if speedY>6.5 then
--speedY=6.5
-- end
if speedY<-6.5 then
speedY=-6.5
end
rect(0+gap,pipeH,y1,50)
rect(x1,pipeH,WIDTH,50)
pipeH=pipeH+speedX
if pipeH<=-50 then
pipeH=HEIGHT
y1=math.random(10,WIDTH-gap)
end
end
--function touched(touch)
-- if touch.state==BEGAN then
-- speedY=speedY+13
-- print("touch")
-- end
--end

• Posts: 7,612

@DrSurname I'm not sure what you're referring to in flappy bird. Can you explain in more detail what you're trying to do.

• Posts: 9

@dave1707 So in Flappy Bird, there are pipes on the right part of the screen, which gradually move towards the left part of the screen. Each pipe has a gap in it, that is located at random parts of the pipe, so that it would be harder to play. What I am trying to do, is to generate two "pipes" on the top of the screen, whith a gap between them of size 200. I want the pipes to generate on the top of the screen, gradually go down towards the bottom, and as they pass the bottom of the screen, re-spawn on the top again with the same gap, but located in a more or less random place. So it is basically like flappy bird, but with the pipes rotated 90° and going from top to bottom instead of right to left.

• Posts: 7,612

@DrSurname Sorry for my reply. I wasn't up very long when I read you question and for some reason I was thinking of Angry Birds when you said Flappy Birds. That's what had me confused. I can't give you an example right now, I have to be somewhere. When I get back, about 2 hours, I'll show you unless someone else does first.

• Posts: 9

@dave1707 OK, will be waiting for your response!

• Posts: 7,612

@DrSurname Just got back, will have an example soon.

• Posts: 7,612

@DrSurname Is this what you had in mind. You can change the gap, diff, and speed sizes for what you want.

``````displayMode(FULLSCREEN)

function setup()
tab={}
speed=2
gap=200
diff=250
for z= 1,15 do
tab[z]=vec3(math.random(30,WIDTH-gap-30),HEIGHT-diff+z*diff)
end
end

function draw()
background(0)
fill(255)
for z=1,#tab do
rect(0,tab[z].y,tab[z].x,30)
rect(tab[z].x+gap,tab[z].y,WIDTH,30)
tab[z].y=tab[z].y-speed
if tab[z].y<-100 then
h=z-1
if h<1 then
h=#tab
end
tab[z]=vec3(math.random(WIDTH-gap-30),tab[h].y+diff)
end
end
end
``````
• Posts: 9

@dave1707 Yeah! Thank you so much

• Posts: 237

@DrSurname I made my own little program and it looks pretty terrible because I didn't give it much time. The goal was to make it look as close to Flappy Bird as possible, but this was the best I got.

``````--# Main
-- Generating Pipes
displayMode(FULLSCREEN)
spaceBetweenPipes=225
function setup()
pipes={}

m=mesh()
pipeCol=color(26, 170, 20, 255)

mm=mesh()

speed=5
gap=300
end

function draw()
background(88, 215, 216, 255)
if #pipes==0 or (pipes[#pipes].bottom.boundaries.x<=WIDTH-gap) then
local data={x=WIDTH,length=math.random(30,HEIGHT*3/4),width=100}
data.boundaries=vec4(data.x+data.width/2,data.length/2,data.width,data.length)
local top={x=data.x,length=HEIGHT-spaceBetweenPipes-data.length,width=data.width}
top.boundaries=vec4(top.x+top.width/2,HEIGHT-top.length/2,top.width,top.length)
local p={bottom=data,top=top}
table.insert(pipes,p)
end
for k,v in ipairs(pipes)do
v.bottom.boundaries.x = v.bottom.boundaries.x - speed
m:setRect(v.bottom.index,v.bottom.boundaries:unpack())
v.top.boundaries.x=v.bottom.boundaries.x
m:setRect(v.top.index,v.top.boundaries:unpack())
local sch=vec2(1.2,50)
mm:setRect(v.bottom.top,v.bottom.boundaries.x,v.bottom.boundaries.y+v.bottom.length
/2,v.bottom.width*sch.x,sch.y)
mm:setRect(v.top.top,v.top.boundaries.x,v.top.boundaries.y-v.top.length/2,
v.top.width*sch.x,sch.y)
end
m:draw()
mm:draw()
end

beam={
v=
[[uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying highp vec2 vTexCoord;
void main()
{
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}]],
f=
[[
precision highp float;
uniform lowp vec4 mainCol;
uniform lowp vec4 subCol;
uniform bool vertical;
uniform float alpha;
varying highp vec2 vTexCoord;
void main()
{
lowp float c=vTexCoord.x;
if (vertical) c=vTexCoord.y;
highp vec4 col=mix(mainCol,subCol,abs(c-0.5));
gl_FragColor = vec4(col.rgb*alpha,alpha);
}
]]
}
``````