Hello all,
Recently I have been trying to code a sort of space game where both players have a steering wheel they can use to control their spaceship. First, I programmed the first player’s spaceship and steering wheel, and then I added the second players.
-- Driving
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
carOne=vec2(WIDTH/2,HEIGHT/2)
carTwo=vec2(WIDTH/2,HEIGHT/2)
velOne,accOne=vec2(0,0)
rotatOne=0
rotatTwo=0
touches={}
velTwo,accTwo=vec2(0,0)
PlayerOne=0
PlayerTwo=0
end
function touched(touch)
-- If the touch is in any other state
-- (such as BEGAN) we add it to our
--
touches[touch.id]=touch
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
for i,v in pairs(touches) do
if i~=PlayerTwo and v.state~=ENDED and v.x>WIDTH/2 and v.y<HEIGHT/2 then PlayerOne=i end
if i~=PlayerOne and v.state~=ENDED and v.x<WIDTH/2 and v.y>HEIGHT/2 then PlayerTwo=i end
end
print(PlayerOne..", "..PlayerTwo)
if touches[PlayerOne]~=nil and touches[PlayerOne].state~=ENDED then
difOne=vec2(CurrentTouch.x,CurrentTouch.y)-vec2(3*WIDTH/4,200)
rotatOne=-math.deg(math.atan2(difOne.x,difOne.y))
--accOne=carOne:dist(vec2(CurrentTouch.x,CurrentTouch.y))
--accOne=accOne:normalize()
end
if touches[PlayerTwo]~=nil and touches[PlayerTwo].state~=ENDED then
difTwo=vec2(CurrentTouch.x,CurrentTouch.y)-vec2(WIDTH/4,HEIGHT-200)
rotatTwo=-math.deg(math.atan2(difTwo.x,difTwo.y))
--accOne=carOne:dist(vec2(CurrentTouch.x,CurrentTouch.y))
--accOne=accOne:normalize()
end
-- Do your drawing here
if touches[PlayerOne]~=nil and touches[PlayerOne].state~=ENDED then
accOne=vec2(CurrentTouch.x,CurrentTouch.y)-vec2(3*WIDTH/4,200)
accOne=accOne:normalize()
else accOne=vec2(0,0)
end
if touches[PlayerTwo]~=nil and touches[PlayerTwo].state~=ENDED then
accTwo=vec2(CurrentTouch.x,CurrentTouch.y)-vec2(WIDTH/4,HEIGHT-200)
accTwo=accTwo:normalize()
else accTwo=vec2(0,0)
end
velOne=velOne+accOne
velTwo=velTwo+accTwo
if velOne.x>6 then velOne.x=6 end
if velOne.x<-6 then velOne.x=-6 end
if velOne.y>6 then velOne.y=6 end
if velOne.y<-6 then velOne.y=-6 end
if touches[PlayerOne]~=nil and touches[PlayerOne].state==ENDED then
if velOne.x>0 then velOne.x = velOne.x - 0.1 end
if velOne.x<0 then velOne.x = velOne.x + 0.1 end
if velOne.y>0 then velOne.y = velOne.y - 0.1 end
if velOne.y<0 then velOne.y = velOne.y + 0.1 end
end
if velTwo.x>6 then velTwo.x=6 end
if velTwo.x<-6 then velTwo.x=-6 end
if velTwo.y>6 then velTwo.y=6 end
if velTwo.y<-6 then velTwo.y=-6 end
if touches[PlayerTwo]~=nil and touches[PlayerTwo].state==ENDED then
if velTwo.x>0 then velTwo.x = velTwo.x - 0.1 end
if velTwo.x<0 then velTwo.x = velTwo.x + 0.1 end
if velTwo.y>0 then velTwo.y = velTwo.y - 0.1 end
if velTwo.y<0 then velTwo.y = velTwo.y + 0.1 end
end
carOne=carOne+velOne
carTwo=carTwo+velTwo
pushMatrix()
translate(carOne.x,carOne.y)
rotate(rotatOne)
sprite("Space Art:Part Gray Hull 4", 0,0,50,50)
popMatrix()
pushMatrix()
translate(3*WIDTH/4,200)
rotate(rotatOne)
fill(0,0,0,0)
stroke(255)
strokeWidth(5)
ellipse(0,0,100,100)
line(0,0,45,0)
line(0,0,-45,0)
line(0,0,0,45)
popMatrix()
pushMatrix()
translate(carTwo.x,carTwo.y)
rotate(rotatTwo)
sprite("Space Art:Part Red Hull 4", 0,0,50,50)
popMatrix()
pushMatrix()
translate(WIDTH/4,HEIGHT-200)
rotate(rotatTwo)
fill(0,0,0,0)
stroke(255)
strokeWidth(5)
ellipse(0,0,100,100)
line(0,0,-45,0)
line(0,0,45,0)
line(0,0,0,45)
popMatrix()
end