Discord
Login
Community
DARK THEME

How do I create multiple enemies using classes?

I can't figure out how to do it. Can I have some help?

Try adding an empty list and IDs to identify your enemy. To create an enemy, use ```your list name.push("a.x, a.y a.width, a.height")

init = function()
  Enemi = class
    constructor = function(width, height, id, number)
      this.width = width
      this.height = height
      this.id = id
      this.x = random.next() * 200 - 100
      this.y = random.next() * 200 - 100
    end
  end
  
  list_enemi = [] 
  for i = 0 to 8 
    list_enemi.push(new Enemi(20, 20, "zombi", 1))
    list_enemi.push(new Enemi(20 ,20, "spider", 2))
  end
end

update = function()
  for e in list_enemi
    if e.id == "zombi" then
      if keyboard.LEFT then
        e.x -=3
      end
    elsif e.id == "spider" then
      if keyboard.RIGHT then
        e.x += 3
        end
      end
  end
end

draw = function()
  screen.clear("rgb(0,170,255)")
  for e in list_enemi
    if e.id == "zombi" then
    screen.fillRect(e.x, e.y, e.width, e.height, "green")
    screen.fillRect(e.x,e.y - 5,e.width,e.height - 10,"blue")
    elsif e.id == "spider" then
      screen.fillRect(e.x,e.y,e.width,e.height,"grey")
      end
  end
end

If you want to delve deeper into the movements and other aspects, here's one of my projects that can help you. https://microstudio.io/i/Rylotox/pixelsurvival/

Thanks

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community