Discord
Login
Community
DARK THEME

Random loop

I wanna know how can i do for, when the player win for example: 40 of score, spawn more enemies, for example: 10

link of my game: https://microstudio.io/rianstar/ghosthunt/

thanks for the help.

answer question 2 - about spawning enemies and changing the code to OOP. I changed the ghost file so that there was a class there.


enemy file >>

Enemy = class
  constructor = function()
    this.x = random.intInRange(-152,152)
    this.y = random.intInRange(-80,80)
    this.speed = 1.8
    this.facing = 0
    this.width = 16
    this.height = 16
    this.hp = 4
  end
  
  Movement = function()
    if ghost.x > this.x then
      this.x += 1
    end
    if ghost.x < this.x then
      this.x -= 1
    end
    if ghost.y > this.y then
      this.y += 1
    end
    if ghost.y < this.y then
      this.y -= 1
   end
  end
    
  update = function()
    Movement()
  end
    
  draw = function()
    screen.drawSprite("red",this.x,this.y,this.width,this.height)
    screen.drawText(this.hp, this.x, this.y + this.height, 10, "orange")
  end
end

ghost file >>

Ghost = class
  constructor = function()
    this.speed = 2.2
    this.facing = 1
    this.x = 0
    this.y = 0
    this.width = 16
    this.height = 16
    this.dash = 0
    this.shot_cooldown = 0
    this.dash_active = true
    this.bullets = []
    this.dash_cooldown = 0
  end
  
  Cooldown = function()
    if (this.dash == 0) and (this.shot_cooldown > 0) then
      this.shot_cooldown -= 1
    end
    if this.dash_cooldown > 0 then
      this.dash_cooldown -= 1
    end
  end
  
  Dash = function(isMoving)
    if this.dash_active and this.dash_cooldown == 0 then
      if isMoving then
        if this.dash == 0 then
          this.dash = 10
        end
      end
      if this.dash > 0 then
        this.dash -= 1
        if this.dash == 0 then
          this.dash_cooldown = 50
        end
      end
    end
    return this.dash > 0
  end
  
  update = function()
    this.Cooldown()
    local isMoving = false 
    if keyboard.release.SPACE then
      this.dash_active = not this.dash_active
    end
    if keyboard.RIGHT then
      this.facing = 1
      this.x += this.speed + this.Dash(true) * this.speed
      isMoving = true
    end
    if keyboard.LEFT then
      this.facing = -1
      this.x -= this.speed + this.Dash(true) * this.speed
      isMoving = true
    end
    if keyboard.UP then
      this.y += this.speed + this.Dash(true) * this.speed
      isMoving = true
    end
    if keyboard.DOWN then
      this.y -= this.speed + this.Dash(true) * this.speed
      isMoving = true
    end
    if not isMoving then
      this.Dash(false)
    end
    if (keyboard.X) and (this.shot_cooldown == 0) then
      this.bullets.push(new Shot(this))
      this.shot_cooldown = 20
    end
    for i = this.bullets.length - 1 to 0 by -1
      local b = this.bullets[i]
      b.update()
      if b.active == false then
        this.bullets.removeElement(b)
      end
    end
    print("shot: "+this.shot_cooldown + " dash:" + this.dash_cooldown)
  end
  
  draw = function()
    this.auraDraw()
    if keyboard.RIGHT then
      screen.drawSprite("ghost_walking", this.x, this.y, 16, 16)
    elsif keyboard.LEFT then
      screen.drawSprite("ghost_walking", this.x, this.y, 16, 16)
    elsif keyboard.DOWN then
      screen.drawSprite("ghost_going_down", this.x, this.y, 16, 16)
    elsif keyboard.UP then
      screen.drawSprite("ghost_going_up", this.x, this.y, 16, 16)
    end
  end
  
  auraDraw = function()
    if keyboard.X then
      screen.drawSprite("aura", this.x, this.y, 32, 32)
    end
  end
end


Shot = class
  constructor = function( source )
  this.x = source.x
  this.y = source.y
  this.facing = source.facing
  this.width = 12
  this.height = 12
  this.step = 0
  this.active = true
  end


  update = function()
    if this.active then
      this.step += 1
      if this.step > 1000 then
        this.active = false
      end
      this.x += 10 * this.facing
    end
  end

  draw = function()
    if this.active then
      screen.drawSprite("gshot",this.x,this.y,this.width,this.height)
    end
  end
end

main file >>

init = function()
  
  //maneira porca de fzr coordenadas no mapakkkk
  x = 0
  y = 0
  
  //isso e pra chamar a funcao do player
  //uma tecnica pra nn deixar o codigo mt longo
  ghost = new Ghost()
 
  touchKeys.arrows = not system.input.keyboard

  touchKeys.keys = ["X"]
  
  menuINIT()
  
  enemies = []
  for i=1 to 4 by 1
    enemies.push( new Enemy())
  end
 
  
  score = 0
  
  level = 1
  pointsThreshold = 20
  points = 0
end

update = function()
  menuUPDATE()
  
  //isso e pra chamar a funcao de update
  ghost.update()

  for e in enemies
    e.update()
  end

  //maneira tenebrosa q eu fiz o limite do mapa
  //se o codigo de um segundo atras me da medo
  //imagine o de 1hr atras kskskskssk
  ghost.x = clamp(ghost.x, -152, 152)
  ghost.y = clamp(ghost.y, -80, 80)
  
  
  touchKeys.update()
  
  for i = enemies.length -1 to 0 by -1
    local e = enemies[i]
    for j = ghost.bullets.length -1 to 0 by -1
      local b = ghost.bullets[j]
      if AABB( e, b ) then
        e.hp -= 1
        ghost.bullets.removeAt( j )
      end
      if e.hp <= 0 then
        score += 10
        points += 10
        enemies.removeElement(e)
        break
      end
    end
  end
  if points > pointsThreshold then 
    enemies.push( new Enemy() )
    enemies.push( new Enemy() )
    points -= pointsThreshold
  end 
end


draw = function()
  
  screen.fillRect(x,y, 450, 200, "rgb(0,0,0)")
  screen.drawMap("level1_red",x,y, 350, 200)
 

  screen.drawText("Score: " + score, -126, 94, 10, "#FFF")
  

  //isso de setDrawScale pra poder desenhar as escalas
  //dependendo de pra onde o personagem esteja virado 
  screen.setDrawScale(ghost.facing, 1)
  screen.drawSprite("ghost_idle", ghost.x, ghost.y, 16, 16)
  ghost.draw()
  for e in enemies
    e.draw()
  end

  for b in ghost.bullets
    b.draw()
  end
  screen.setDrawScale(1,1)
  touchKeys.draw()
  
  menuDRAW()
  
end

Edit 2024.08.08 - ghost file fixes

@Loginus thank you too, too much<3

Post a reply

Progress

Status

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