Discord
Login
Community
DARK THEME

How do you make an boss an object and do some cool things to attack in microscript code?

I have so tried so many things and I need to see an example in microscript so that I can make this cool game and put it in microstudio's explore section. :)

Actor = class
  constructor = function(x, y, width, height, color, frameColor = "orange", liveCount = 100)
    this.x = x
    this.y = y
    this.width = width
    this.height = height
    this.color = color
    this.frameColor = frameColor
    this.liveCount = liveCount
    this.drawLiveCount = true
  end
  
  draw = function()
    screen.fillRect(this.x, this.y, this.width, this.height, this.color)
    screen.drawRect(this.x, this.y, this.width, this.height, this.frameColor)
    if this.drawLiveCount then 
      screen.drawText( ""+this.liveCount, this.x, this.y+this.width/2+this.height*0.3, 10, this.color)
    end
  end
  
  top = function()
    return this.y + this.height / 2
  end
  
  bottom = function()
    return this.y - this.height / 2
  end
  
  right = function()
    return this.x + this.width / 2
  end
  
  left = function()
    return this.x - this.width / 2
  end
end

Enemy = class extends Actor
  constructor = function(x, y)
    super(x, y, 20, 30, "red")
    this.damage = 1  
  end
  
  hit = function(bullet)
    this.liveCount -= bullet.power
  end
  
  update = function(target)
    local direct_x = target.x - this.x
    local direct_y = target.y - this.y
    if direct_x < 0 then
      this.x -= 0.1
    else
      this.x += 0.1
    end
    if direct_y < 0 then
      this.y -= 0.1
    else
      this.y += 0.1
    end
  end
end

Player = class extends Actor
  constructor = function(x, y)
    super(x, y, 30, 40, "green")
    this.bullets = []
    this.invincibilityFrames = 0
    this.shootCooldown = 0  
  end
  
  update = function()
    if keyboard.UP then
      this.y += 1
    elsif keyboard.DOWN then
      this.y -= 1
    elsif keyboard.LEFT then
      this.x -= 1
    elsif keyboard.RIGHT then
      this.x += 1
    end
    
    if this.invincibilityFrames > 0 then
      this.invincibilityFrames -= 1
    end

    if this.shootCooldown > 0 then
      this.shootCooldown -= 1
    end
    
    if keyboard.SPACE and this.bullets.length < 5 and this.shootCooldown == 0 then
      local direction = object x= 0 y= 0 end
      if keyboard.UP then direction.y = 1
      elsif keyboard.DOWN then direction.y = -1
      elsif keyboard.LEFT then direction.x = -1
      elsif keyboard.RIGHT then direction.x = 1
      end
      if direction.x != 0 or direction.y != 0 then
        this.bullets.push(new Bullet(this.x, this.y, direction))
        this.shootCooldown = 6  
      end
    end    
    
    for bullet in this.bullets
      bullet.update()
    end
    
    this.bullets = this.bullets.filter(function(bullet)
      return bullet.x > -screen.width/2 and bullet.x < screen.width/2 and
             bullet.y > -screen.height/2 and bullet.y < screen.height/2
    end)

  end
  
  draw = function()
    super()
    for bullet in this.bullets
      bullet.draw()
    end
  end

  takeDamage = function(damage)
    if this.invincibilityFrames == 0 then
      this.liveCount -= damage
      this.invincibilityFrames = 60  
    end
  end
end

Bullet = class extends Actor
  constructor = function(x, y, direction)
    super(x, y, 10, 10, "yellow")
    this.direction = direction
    this.speed = 5
    this.drawLiveCount = false
    this.power = 5
  end
  
  update = function()
    this.x += this.direction.x * this.speed
    this.y += this.direction.y * this.speed
  end
end

init = function()
  map = maps["demo"]
  player = new Player(0, 0)
  enemies = []
  for index = 1 to 20 by 1
    enemies.push(new Enemy(random.nextInt(screen.width) - screen.width / 2,
                           random.nextInt(screen.height) - screen.height / 2))
  end
end

update = function()
  player.update()
  enemies.forEach(function(enemy) 
    enemy.update(player)
    if checkCollision(enemy, player) then
      player.takeDamage(enemy.damage)
    end
  end)
  
  for bullet in player.bullets
    for enemy in enemies
      if checkCollision(bullet, enemy) then
        enemy.hit(bullet)
        if enemy.liveCount < 1 then
          enemies = enemies.filter(function(e) return e != enemy end)
        end
        player.bullets = player.bullets.filter(function(b) return b != bullet end)
      end
    end
  end
end

draw = function()
  screen.clear()
  screen.drawText("WASD or Arrow to Move, SPACE to Shoot", 0, 90, 20, "orange")
  player.draw()
  enemies.forEach(function(enemy) enemy.draw() end)
end

checkCollision = function(obj1, obj2)
  return abs(obj1.x - obj2.x) < (obj1.width + obj2.width) / 2 and
         abs(obj1.y - obj2.y) < (obj1.height + obj2.height) / 2
end

I am sorry to say but that did not work. :(

Is there a reason, Loginus? Because I also wanted it to be a function and the boss an object and make him do moves that kills the player when you face him in level 10. Can you do that?

I hope you can help.

Post a reply

Progress

Status

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