how to move the enemy in microstudio
how to move the enemy in microstudio
HOW to move the enemy in microstudio
Hi and thank you very much for your prompt reply!
Now, where I must place these strings of code? In the main?
And, it's possible to create a sort of "attack" and "demage" for my hero and/or for enemies?
Thanks again!
Actor = class
constructor = function(x, y, size, color, frameColor = "orange", liveCount = 100)
this.x = x
this.y = y
this.size = size
this.color = color
this.frameColor = frameColor
this.liveCount = liveCount
this.drawLiveCount = true
end
draw = function()
screen.fillRect(this.x, this.y, this.size, this.size, this.color)
screen.drawRect(this.x, this.y, this.size, this.size, this.frameColor)
if this.drawLiveCount then
screen.drawText(this.liveCount, this.x, this.y+this.size/2+this.size*0.2, 10, this.color)
end
end
end
Enemy = class extends Actor
constructor = function(x, y)
super(x, y, 20, "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, "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, 5, "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()
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.size + obj2.size) / 2 and
abs(obj1.y - obj2.y) < (obj1.size + obj2.size) / 2
end
edit 2024.07.20
Thanks again for your reply :)
Sorry for bother you again, but since I'm starting from some days ago with the creation of my first game, how could I bring this function into my game? I mean that I want to use the function that you have suggested me, so add some enemies (with my sprites) in my levels that run to the hero for example and he must to jump or combat them.
Thanks in advance Tom
You can make your project public. You check the option in the project and enter the address here.
It is easier to suggest a solution when we see the source code of the program.
You can also look at the libraries and how problems were solved in the Explore section, e.g. at: https://microstudio.dev/i/gilles/quickengine/
Thanks for the reply
this is the link to my ongoing beta game: https://microstudio.io/i/TOMdesignorg/meowys_adventure/
You add new functionality to the existing code. To do this correctly, you need to understand how the creator of this code thought. You need to analyze all the code.
It will be easier for you if you start by understanding how to detect collisions with another object (enemy) or with the environment (map). Here are some examples.
https://microstudio.io/i/Loginus/collisiondemoquick/
https://microstudio.io/i/Loginus/mapcollision/
Start by trying QuickEngine. It has everything you need, collision detection and drawing. Moreover, it is very easy - perfect for small projects.