# Space ship shooting?

How do i get my space ship to shoot?
This is my code.

``````
function setup()
displayMode(FULLSCREEN)

print("Hello World!")

x = 200 -- starting x position
y = 200 -- starting y position
w = 100 -- space ship width
h = 54 -- space ship height
FPS = 0
watch("FPS")
watch()
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(75, 75, 75, 250)

-- This sets the line thickness
strokeWidth(5)

sprite("SpaceCute:Background",374,374,WIDTH/2*3,HEIGHT/2*3)
y = y + 1
sprite("Space Art:Green Ship",x,y,w,h)

FPS = math.round(1/DeltaTime)

end

function touched(touch)

x = math.max(w, math.min(WIDTH - (w), touch.x))
y = math.max(h, math.min(HEIGHT - (h), touch.y))

end

function watch()

parameter.watch("FPS")
parameter.watch("x")
parameter.watch("y")

end

function math.round(num)

return math.floor(num+0.5)

end
``````

@Jessevanderheide Here’s something I wrote that might help you. Move your thumb along the right side of the screen to rotate the ship, tap on the left side to shoot.

``````
function setup()
displayMode(FULLSCREEN)
tab1={}
m=0
b1={}
x=1
ang=0
new =0
old=0
xa=0
ya=1
yy=20
targets()
end

function draw()
local x,y,xx
background(40, 40, 50)
fill(255,0,0)

-- this section displays the rotated ship
pushMatrix()
translate(WIDTH/2,HEIGHT/2)
rotate(180-ang)
sprite("Tyrian Remastered:Plane Boss Destroyed",0,0)
popMatrix()

-- this section draws the red targets
xx=0
for z=1,5 do
x=tab1[z].x
y=tab1[z].y
ellipse(x,y,20,20)
if x>0 and x<WIDTH and y>0 and y<HEIGHT then
xx=1 -- a target is still on the screen
end
end
if xx==0 then    -- no targets on the screen
targets()    -- create 5 random targets
end

-- this section draws the missiles
xx=0
for z=1,m do
x=b1[z].x
y=b1[z].y
fill(255,0,0)
ellipse(x,y,5,5)
if x>0 and x<WIDTH and y>0 and y<HEIGHT then
xx=1
end
end
if xx==0 then    -- no targets, set missiles count to 0
m=0
end
end

function shoot(x)   -- add a missile to the table at offset x
b1[x] = physics.body(CIRCLE,5)
b1[x].x=WIDTH/2
b1[x].y=HEIGHT/2
b1[x].gravityScale=0
b1[x].linearVelocity=vec2(xa*400,ya*400) -- x,y velocity from angle
end

function targets() -- create 5 random targets
local x
for x=1,5 do
tab1[x]=physics.body(CIRCLE,10)
tab1[x].x=math.random(WIDTH)
tab1[x].y=math.random(HEIGHT)
tab1[x].gravityScale=0
end
end

function touched(t)
if t.state == BEGAN then
if CurrentTouch.x < WIDTH/2 then -- left screen touched
m = m + 1
shoot(m)    -- create a missile when left screen touched
end
end
if CurrentTouch.x>WIDTH/2 then    -- right screen touched
ang=ang+t.deltaY    -- change angle as screen is touched
xa=math.sin(math.rad(ang))    -- calc x direction for missile
ya=math.cos(math.rad(ang))    -- calc y direction for missile
end
end

``````

@dave1707 thanksss! You are on of the best!!

@dave1707 one more question…

I dont get anything in this code…
So can you explain it a bit or make it simpler?

@Jessevanderheide I added comments to the above code. If you have questions about anything specific, let me know.

@dave1707 yess! Thank you so much, this is explains everything.
The only thing i want to know, is there a other way? I dont mind if there isnt.

@Jessevanderhelde There’s always other ways to do something. That way will depend on what you have in mind for your program to do. Since only you know what that is, that other way will depend on you. I just gave you an example to show you how to do some of the things involved. If you can follow what’s happening in my code, you should be able to expand on it for your program.

@dave1707 yes you are right, i am currently learning how to use parameters.
But i tink this is gonna be a bit to hard for me, in a week ill will go back to this, and try to understand this.