Hi guys, here is a simple 2-player game I made. Tap to flap and shoot, don’t get hit by a bullet. WARNING: bullets wrap around the screen (if they go off on left they will come back on right) and gravity switches every now and then. Feedback?
-- Flap and shoot
ellipseMode(RADIUS)
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
size=50
shotspeed=5
shotspeedincrease=5
shotsize=20
shotrate=0.5
switchtime=5
shotrepeats=2
reset()
end
function reset()
y1=HEIGHT/2
y2=HEIGHT/2
fall1=0
fall2=0
lastfire1=ElapsedTime-shotrate
lastfire2=ElapsedTime-shotrate
fall=0.2
jump=-5
shots1={}
shots2={}
startleft=false
startright=false
winner=0
switches = 1
wintime=ElapsedTime
starttime=ElapsedTime
end
function draw()
pushStyle()
pushMatrix()
handleShots()
background(255, 255, 255, 255)
fill(0)
stroke(0)
fontSize(50)
if startleft then
rect(0,0,WIDTH/2,HEIGHT)
else
text("Tap to flap and shoot",WIDTH/4,HEIGHT/2+200)
end
if startright then
rect(WIDTH/2,0,WIDTH/2,HEIGHT)
else
text("Tap to flap and shoot",3*WIDTH/4,HEIGHT/2+200)
end
fill(255)
stroke(255)
strokeWidth(5)
line(WIDTH/2,0,WIDTH/2,HEIGHT)
drawChar(100,y1,size,color(0,0,255,255))
drawShot(shots1)
translate(WIDTH,0)
scale(-1,1)
drawChar(100,y2,size,color(255,0,0,255))
drawShot(shots2)
if startleft and startright then
y1 = y1 - fall1
y2 = y2 - fall2
fall1 = fall1 + fall
fall2 = fall2 + fall
if y1<size then y1 = size end
if y1>HEIGHT-size then y1=HEIGHT-size;fall1=0 end
if y2<size then y2 = size end
if y2>HEIGHT-size then y2=HEIGHT-size;fall2=0 end
end
popMatrix()
popStyle()
if winner ~= 0 then
drawwin(winner)
end
if math.abs(ElapsedTime-(switchtime*switches+starttime))<DeltaTime then
fall=-fall
jump=-jump
fall1=0
fall2=0
switches=switches+1
end
end
function touched(t)
if winner ~= 0 and ElapsedTime> wintime+2 then
reset()
end
if t.x < WIDTH/2 and t.state == BEGAN then
if startleft and startright then
fall1 = jump
if ElapsedTime>lastfire1+shotrate then
table.insert(shots1,{y1,2*size+shotsize+100,shotspeed,0})
lastfire1=ElapsedTime
end
end
startleft=true
end
if t.x > WIDTH/2 and t.state == BEGAN then
if startleft and startright then
fall2 = jump
if ElapsedTime>lastfire2+shotrate then
table.insert(shots2,{y2,2*size+shotsize+100,shotspeed,0})
lastfire2=ElapsedTime
end
end
startright=true
end
end
function drawChar(x,y,r,c)
pushStyle()
fill(c)
ellipse(x+r,y,2*r)
popStyle()
end
function drawShot(shots)
for k,shot in pairs(shots) do
pushStyle()
fill(255)
ellipse(shot[2],shot[1],shotsize)
shot[2]=shot[2]+shot[3]
popStyle()
end
end
function handleShots()
for i,shot1 in pairs(shots1) do
for j,shot2 in pairs(shots2) do
if vec2(shot1[2]+shot2[2]-WIDTH,shot1[1]-shot2[1]):len()<shotsize then
table.remove(shots1,i)
table.remove(shots2,j)
end
end
end
for i,shot1 in pairs(shots1) do
if vec2(shot1[2]-size-100,shot1[1]-y1):len()<size+shotsize/2 and winner==0then
winner = 2
wintime = ElapsedTime
table.remove(shots1,i)
end
if vec2(shot1[2]+size+100-WIDTH,shot1[1]-y2):len()<size+shotsize/2 and winner==0 then
winner = 1
wintime = ElapsedTime
table.remove(shots1,i)
end
if shot1[2]>WIDTH then
if shot1[4]<shotrepeats then
shot1[2]=0
shot1[3]=shot1[3]+shotspeedincrease
shot1[4]=shot1[4]+1
else
table.remove(shots1,i)
end
end
end
for j,shot2 in pairs(shots2) do
if vec2(shot2[2]-size-100,shot2[1]-y2):len()<size+shotsize/2 and winner==0 then
winner = 1
wintime = ElapsedTime
table.remove(shots2,j)
end
if vec2(shot2[2]+size+100-WIDTH,shot2[1]-y1):len()<size+shotsize/2 and winner==0 then
winner = 2
wintime = ElapsedTime
table.remove(shots2,j)
end
if shot2[2]>WIDTH then
if shot2[4]<shotrepeats then
shot2[2]=0
shot2[3]=shot2[3]+shotspeedincrease
shot2[4]=shot2[4]+1
else
table.remove(shots2,j)
end
end
end
end
function drawwin(i)
pushStyle()
fontSize(100)
local c
local message
if i==1 then
c=color(0,0,255,255)
message = "BLUE WINS"
end
if i==2 then
c=color(255,0,0,255)
message = "RED WINS"
end
fill(c)
text(message,WIDTH/2,HEIGHT/2)
if ElapsedTime>wintime+2 then
text("Tap to reset",WIDTH/2,HEIGHT/2-200)
end
end