I have a simple code here that stops when the square collides with another square. If I wanted to make lots more squares of random sizes (clones of the original square) come through , like an “avoid the square” game , how could I do that without duplicating several lines of code? So each level spawns increasing amounts of squares to try and dodge.
displayMode(FULLSCREEN)
function setup()
print("w"..WIDTH.." h"..HEIGHT.."")
level=1
distance=0
w=50
w1=math.random(10,200)
x1=WIDTH+w1
y1=math.random(0,HEIGHT)
speed=3
hit="not hit"
t=ElapsedTime
tt=ElapsedTime
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
rectMode(CENTER)
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
rect(300,CurrentTouch.y,w)
--1
rect(x1,y1,w1)
text("w "..w.."",500,600)
text(hit,400,600)
text(distance,100,600)
-- line(300+w/2,0,300+w/2,HEIGHT)
-- line(x1-w1/2,0,x1-w1/2,HEIGHT)
if ElapsedTime-t>0.01 and hit=="not hit" then
w=w-0.0025
x1 = x1 - speed
t=ElapsedTime
end
if ElapsedTime-tt>(speed)^-1 and hit=="not hit"then
distance=distance+1
tt=ElapsedTime
end
if x1<300+(w/2)+(w1/2) and x1>300-(w/2)-(w1/2) and y1<CurrentTouch.y+(w1/2) and y1>CurrentTouch.y-(w/2)-(w1/2) then
hit="hit"
end
if x1<0-w1 then
w1=math.random(10,200)
x1 = WIDTH+w1
y1=math.random(0,HEIGHT)
speed = speed + 1
end ,
end