Hi, I’m trying to make a game where enemy sprites chase me around the screen. They spawn whenever I pick up an object at a random position. This is my code so far:
function setup()
displayMode(FULLSCREEN)
-- insert title sceen that says the name, the instruction and controls
x = 300
y = 400
speed = 4
direction = 1
ghostSpeed = 4
score = 0
heartx = 50 + math.random(WIDTH - 1000, WIDTH - 125)
hearty = 50 + math.random(HEIGHT - 600, HEIGHT - 125)
Ghostx = 50 + math.random(WIDTH - 1000, WIDTH - 125)
Ghosty = 50 + math.random(HEIGHT - 600, HEIGHT - 125)
startX = 0
endX = 0
startY = 0
endY = 0
E = {}
spawnEnemy()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- create background by inserting shapes
sprite("Documents:Square",0,0,150,150)
sprite("Documents:Square",0,100,150,150)
sprite("Documents:Square",0,200,150,150)
sprite("Documents:Square",0,300,150,150)
sprite("Documents:Square",0,400,150,150)
sprite("Documents:Square",0,500,150,150)
sprite("Documents:Square",0,600,150,150)
sprite("Documents:Square",0,700,150,150)
sprite("Documents:Square",105,-5,150,150)
sprite("Documents:Square",210,-5,150,150)
sprite("Documents:Square",315,-5,150,150)
sprite("Documents:Square",420,-5,150,150)
sprite("Documents:Square",525,-5,150,150)
sprite("Documents:Square",630,-5,150,150)
sprite("Documents:Square",735,-5,150,150)
sprite("Documents:Square",840,-5,150,150)
sprite("Documents:Square",945,-5,150,150)
sprite("Documents:Square",105,775,150,150)
sprite("Documents:Square",210,775,150,150)
sprite("Documents:Square",315,775,150,150)
sprite("Documents:Square",420,775,150,150)
sprite("Documents:Square",525,775,150,150)
sprite("Documents:Square",630,775,150,150)
sprite("Documents:Square",735,775,150,150)
sprite("Documents:Square",840,775,150,150)
sprite("Documents:Square",945,775,150,150)
sprite("Documents:Square",WIDTH,700,150,150)
sprite("Documents:Square",WIDTH,600,150,150)
sprite("Documents:Square",WIDTH,500,150,150)
sprite("Documents:Square",WIDTH,400,150,150)
sprite("Documents:Square",WIDTH,300,150,150)
sprite("Documents:Square",WIDTH,200,150,150)
sprite("Documents:Square",WIDTH,100,150,150)
-- make it impossible to go outside the screen
if x<90 then
x = 90
end
if x>WIDTH-90 then
x = WIDTH-90
end
if y<90 then
y = 90
end
if y>HEIGHT-90 then
y = HEIGHT-90
end
-- add ghosts
sprite("Documents:Ghost",Ghostx,Ghosty,50,50)
-- make them move in random directions
e.speedX = math.random()*2
e.speedY = math.random()*2
for i,e in pairs (E) do
e.x = e.x + e.speedX
e.y = e.y + e.speedY
sprite("Documents:Ghost",e.x,e.y,50,50)
end
-- insert character and implament controls
pushMatrix()
translate(x,y)
sprite("Documents:Transparent Pacman - Right",0,0,50,50)
popMatrix()
if direction == 1 then
x = x + speed
elseif direction == 2 then
y = y - speed
elseif direction == 3 then
x = x - speed
else
y = y + speed
end
if CurrentTouch.state == BEGAN then
startX = CurrentTouch.x
startY = CurrentTouch.y
end
if CurrentTouch.state == ENDED then
endX = CurrentTouch.x
endY = CurrentTouch.y
end
if math.abs(startX-endX) > math.abs(startY-endY) then
if startX > endX then
direction = 3
else
direction = 1
end
else
if startY > endY then
direction = 2
else
direction = 4
end
end
-- insert items
sprite("Small World:Heart Gold",heartx,hearty)
-- make it possible to collect items
if math.abs(x-heartx)<40 and math.abs(y-hearty)<40 then
score = score + 1
heartx = 50 + math.random(WIDTH-100)
hearty = 50 + math.random(HEIGHT-100)
spawnEnemy()
end
-- make it possible for player to be killed by ghosts if they are touched
if math.abs(x-Ghostx)<40 and math.abs(y-Ghosty)<40 then
restart()
end
-- if player is touched game over
end
function spawnEnemy()
e = {}
e.x = math.random(50,WIDTH-50)
e.y = math.random(50,HEIGHT-50)
e.speedX = math.random()*2
e.speedY = math.random()*2
table.insert(E,e)
end
Can somebody please help?