Enemy Sprite

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?

What is the problem?

btw, you can make your code more compact and efficient by creating an image in memory with all the boxes on it, in setup, then just spriting that one image in draw, eg (unchecked code)

--put this in setup
imgBack=image(WIDTH,HEIGHT)
setContext(imgBack)
local b=readImage("Documents:Square")
for i=0,700,100 do 
    sprite("Documents:Square",0,i,150,150)
end
for i=105,945,105 do 
    sprite("Documents:Square",i,-5,150,150)
    sprite("Documents:Square",i,775,150,150)
end
setContext()

--then in draw
sprite(imgBack,WIDTH/2,HEIGHT/2)

I want the ghost sprites to follow the Pacman sprite. At the moment the first ghost that appears won’t move and all the ghosts after that just fly off the screen.

I’m not surprised the ghosts fly off the screen, because you have them going in random directions and don’t stop them leaving the screen

To do following behaviour, implement something like this. You should find it automatically stops ghosts from leaving the screen.

--in spawnEnemy, set a random speed for the sprite
e.speed=1+math.random() --between 1 and 2 pixels per frame (alter as necessary)

--in draw, do this (get rid of all the ghost movements you have there)
for i,e in pairs (E) do
    local d=vec2(e.x,e.y):dist(vec2(x,y)) --get distance to player
    if d>0 then
        local m=math.min(d,e.speed) --limit actual move to speed of sprite
        e.x=e.x+(x-e.x)*m/d --move along straight line toward player
        e.y=e.y+(y-e.y)*m/d --pro rating x,y differences
    end
end

Now the ghosts aren’t spawning at all

you’d better post your code again

Thanks this helps a lot :slight_smile: