Hey. I’m making a space shooter game, the below script works fully, except for when it goes to draw the “Play Again?” text near the end of this code. Some times it appears in the right position, other times it goes offset. It seriously works and doesn’t work at times! Why?
ship_pos = vec2(WIDTH * .5,40)
enemies = {}
bullets = {}
asteroids = {}
framebullet = 0
frameenemy = 0
current_rate = 30
asteroidframe = 0
score = 0
lives = 3
txt = nil
txtframe = 0
roundtime = 0
reduce = false
vel = 3
stop = false
canmove = false
canclr = true
endtext = nil
paused = false
button_coordinates = vec2(WIDTH * .38,HEIGHT * .3)
ship_img = 1
function touched(t)
if t.state == BEGAN then
canmove = true
if vec2(CurrentTouch.x,CurrentTouch.y):dist(vec2(WIDTH * .95,HEIGHT * .95)) < 24 and not stop then
paused = not paused
end
elseif t.state == ENDED then
canmove = false
ship_img = 1
end
end
function draw()
if canmove then
if CurrentTouch.x < WIDTH * .5 and not stop and not paused and ship_pos.x > WIDTH * .01 then
ship_pos.x = ship_pos.x - 4
ship_img = 2
elseif not stop and not paused and ship_pos.x < WIDTH * .95 then
ship_pos.x = ship_pos.x + 4
ship_img = 3
else
ship_img = 1
end
end
if not paused then
roundtime = roundtime + 1
framebullet = framebullet + 1
frameenemy = frameenemy + 1
asteroidframe = asteroidframe + 1
end
background(0,0,0)
smooth()
sprite("Project:space",WIDTH * .5,HEIGHT * .5,WIDTH,HEIGHT)
if not stop and not paused and asteroidframe > 15 then
table.insert(asteroids,{vec2(math.random(math.floor(WIDTH)),HEIGHT * 1.3),math.random(3,math.floor(vel) + 3)})
asteroidframe = 0
end
if not stop then
for i,v in pairs(asteroids) do
if not paused then
v[1].y = v[1].y - v[2]
end
if v[1].y < HEIGHT * .003 then
table.remove(asteroids,i)
else
sprite("Project:asteroid",v[1].x,v[1].y,43,43)
end
end
end
if not stop then
sprite("Project:ship"..ship_img,ship_pos.x,ship_pos.y,60,60)
end
if framebullet > 45 and not stop and not paused then
framebullet = 0
sound(SOUND_HIT, 49062)
local projectile = vec2(ship_pos.x,ship_pos.y + 5)
table.insert(bullets,projectile)
end
if frameenemy > current_rate and not stop and not paused then
frameenemy = 0
local newenemy = vec2(math.random(math.floor(WIDTH * .9)),HEIGHT * 1.3)
table.insert(enemies,newenemy)
end
for i,v in pairs(bullets) do
if v.y + 4 > HEIGHT and not stop then
table.remove(bullets,i)
end
if not stop and not paused then
v.y = v.y + 4
end
if not stop then
fill(255, 255, 255, 255)
ellipse(v.x,v.y,16,16)
end
end
for i,v in pairs(enemies) do
if v.y < HEIGHT * .004 and not stop then
table.remove(enemies,i)
end
if not stop and not paused then
v.y = v.y - vel
end
if not stop then
sprite("Project:enemy",v.x,v.y,45,45)
end
if ship_pos:dist(v) < 36 and not stop then
table.remove(enemies,i)
txt = "You lost a life! You have "..lives-tonumber("1").." more."
end
for ii,v2 in pairs(bullets) do
if v2:dist(v) < 22 and not stop then
sound(SOUND_EXPLODE, 49037)
score = score + 50
table.remove(enemies,i)
table.remove(bullets,ii)
end
end
end
if not stop then
font("Optima-ExtraBlack")
fontSize(30)
fill(255, 255, 255, 255)
text("Score: "..score,WIDTH * .8,HEIGHT * .95)
text("Lives: "..lives,WIDTH * .8,HEIGHT * .9)
end
if txt and txtframe <= 70 and not stop then
if reduce == false then
reduce = true
lives = lives - 1
end
txtframe = txtframe + 1
fill(255, 0, 0, 255)
text(txt,WIDTH * .5,HEIGHT * .5)
end
if txtframe >= 70 and not stop then
txtframe = 0
reduce = false
txt = nil
end
if lives <= 0 then
stop = true
fill(255, 255, 255, 255)
text("Game over! You scored "..score.." points.",WIDTH * .5,HEIGHT * .5)
fill(127, 127, 127, 255)
strokeWidth(4)
stroke(186, 186, 186, 255)
rect(button_coordinates.x,button_coordinates.y,250,48)
fill(255, 255, 255, 255)
text("Play Again?",WIDTH * .54,HEIGHT * .32)
if vec2(CurrentTouch.x,CurrentTouch.y):dist(vec2(WIDTH * .54,HEIGHT * .32)) < 80 then
restart()
end
end
if roundtime > 89 and not stop then
roundtime = 0
if current_rate > 2 then
vel = vel + .12
current_rate = current_rate - 1
end
end
if paused then
fill(255, 255, 255, 255)
text("Paused",WIDTH * .5,HEIGHT * .5)
end
if not stop then
sprite("Project:pause",WIDTH * .95,HEIGHT * .95,50,50)
end
end