# starter game 18

I had part of this for awhile and thought I’d turn it into a starter game for someone. The object is to run the space ship into each star. If you hit a star, it disappears. To steer the ship, touch the screen anywhere and move your finger around. There is a red dot where you first touched the screen and the ship will go in the direction that your finger is from the red dot. The speed of the ship is dependent on the distance your finger is from the red dot. There is no scoring or end game code, so it’s up to you to add whatever you want to turn this into a useful game.

EDIT: Game modified. See post below.

``````displayMode(FULLSCREEN)

function setup()
font("Courier-BoldOblique")
scores={}
start()
end

function start()
cx=400;cy=600
jx=0;jy=0
tx=0;ty=0
tab={}
for z=1,20 do
table.insert(tab,{p=vec2(math.random(20,WIDTH-20),math.random(20,HEIGHT-20)),
s=math.random(5),d=vec2(math.random(-2,2),math.random(-2,2)),
h=false,n=z})
end
z=0
next=1
time=0
et=ElapsedTime
update=true
end

function touched(t)
if t.state==BEGAN then
if t.tapCount==2 and #tab==0 then
start()
return
end
jx=t.x;jy=t.y;tx=jx;ty=jy
end
if t.state==MOVING then
tx=t.x;ty=t.y
z=90+math.deg(math.atan2(jy-t.y,jx-t.x))
end
if t.state==ENDED then
jx=0;jy=0;tx=0;ty=0
end
end

function checkHit()
for a,b in pairs(tab) do
if vec2(cx,cy):dist(vec2(b.p.x,b.p.y))<10 then
if next==b.n then
b.h=true
next=next+1
sprite("Tyrian Remastered:Explosion Huge",b.p.x,b.p.y)
end
end
end
end

function draw()
background(40, 40, 50)
fill(255)
if #tab==0 then
text("        Game over\
\
Double tap to restart",WIDTH/2,HEIGHT/2)
if update then
table.insert(scores,time)
update=false
end
for a,b in pairs(scores) do
text(string.format("Game %2d   %4.1f  seconds",a,b),WIDTH/2,HEIGHT-20*a)
end
else
time=ElapsedTime-et
text("Find  "..next,WIDTH/2,HEIGHT-25)
cx=cx-(jx-tx)*.1
cy=cy-(jy-ty)*.1
if cx>WIDTH then cx=0 elseif cx<0 then cx=WIDTH end
if cy>HEIGHT then cy=0 elseif cy<0 then cy=HEIGHT end
text(string.format("%.1f  seconds",time),WIDTH/2,HEIGHT-50)
end
for a,b in pairs(tab) do
sprite("Space Art:Green Explosion",b.p.x,b.p.y,30)
b.p=b.p+b.d
if b.p.x<0 then b.p.x=WIDTH end
if b.p.x>WIDTH then b.p.x=0 end
if b.p.y<0 then b.p.y=HEIGHT end
if b.p.y>HEIGHT then b.p.y=0 end
text(b.n,b.p.x,b.p.y-20)
end
if #tab~=0 then
if jx+jy>0 then
stroke(255)
fill(255,0,0)
ellipse(jx,jy,16)
end
pushMatrix()
translate(cx,cy)
rotate(z)
sprite(ship,0,0,40)
popMatrix()
checkHit()
for a,b in pairs(tab) do
if b.h then
table.remove(tab,a)
return
end
end
end
end
``````

I modified the code above. Each star has a number. You must hit the stars in order starting with 1. There is also a timer that keeps track of how long it takes you to hit all the stars… Once a game is over, double tap to start another. I show the times for each game, but I clear the times once you exit the code.

@dave1707 , I know it is just a starter game, but my sugestion is put a virtual joystick when you touch the screen

@erickyamato I didn’t see any reason to draw the joystick. After you play the game for awhile, you don’t look at what would have been the joystick. You just move your finger in the direction you want the ship to go.

@erickyamato That’s one good thing about writing starter games. I can add as much as I want or leave out as much as I want. It’s up to whoever wants to continue the game to add all the other stuff.

@dave1707 , I got it! But as I said, it was just a suggestion.

@dave1707 I was looking for and I found your example:

``````displayMode(FULLSCREEN)

function setup()
cx,cy=0,0
v1=vec2(0,0)
end

function draw()
background(40, 40, 50)
noFill()
if cx+cy>0 then
stroke(255)
strokeWidth(2)
ellipse(cx,cy,200)  -- large circle 200 pixels in diameter
ellipse(v1.x+cx,v1.y+cy,25) -- small circle
end
end

function touched(t)
if t.state==BEGAN then -- center of circle
cx=t.x
cy=t.y
end
if t.state==MOVING then -- direction to move and limit direction
v1=vec2(cx,cy)
d=v1:dist(vec2(t.x,t.y))    -- distance, center to touch point
v1=vec2(t.x-cx,t.y-cy)
v1=v1:normalize()*math.min(d,100)   -- limit distance to 100 pixels
end
if t.state==ENDED then  -- clear values
cx,cy=0,0
end
end
``````

I totally forgot about that one.