Discord
Login
Community
DARK THEME

how to make objects chase after mouse

I'm trying to make a zombie game but when trying to make the zombie chase after the player (right now its the mouse) but it seems it doesn't want to move, is there any problem with the code. :)

init = function() zombie1 = new zombie zom = 7 player_x = 0 player_y = 0 end

update = function() player_x = mouse.x player_y = mouse.y zombie1.move(1)

end

draw = function() screen.clear zombie1.display() end

zombie = class enemy_x = 0 enemy_y = 0 constructor = function(x,y) this.x = 1 this.y = 0 end move = function(speed) for i=1 to 10 if player_x > this.x then this.x += speed end if player_x < this.x then this.x -= speed end end end display = function() screen.fillRect(enenmy_x,enemy_y,10,10,"rgb(255,255,255)") end end

//end

Fixed a bit of the code but the problem is still there

;init = function() zombie1 = new zombie zom = 7 player_x = 0 player_y = 0 end

update = function() player_x = mouse.x player_y = mouse.y zombie1.move()

end

draw = function() screen.clear zombie1.display() end

zombie = class enemy_x = 0 enemy_y = 0 constructor = function(x,y) this.x = x this.y = y end move = function() for i=1 to 10 if player_x > this.x then this.x = this.x + 1 elsif player_x < this.x then this.x = this.x + 1 end end end display = function() screen.fillRect(enenmy_x,enemy_y,10,10,"rgb(255,255,255)") end end

3 times the character next to key 1 - then the string of characters will not be modified.

init = function() 
  zombie1 = new zombie()
  zom = 7 
  player_x = 0 
  player_y = 0 
end

update = function() 
  player_x = mouse.x 
  player_y = mouse.y 
  zombie1.move()

end

draw = function()
  screen.clear()
  zombie1.display()
end

zombie = class
  enemy_x = 0
  enemy_y = 0
  constructor = function(x,y) 
    this.x = x 
    this.y = y 
  end 
  move = function() 
    for i=1 to 10 
      if player_x > this.x then 
        this.x = this.x + 1 
      elsif player_x < this.x then 
          this.x = this.x - 1 
        end 
    end 
  end 
  display = function() 
    screen.fillRect( this.x,this.y,10,10,"rgb(255,255,255)") 
  end 
end

Thank you, I'm kinda new to this.

Post a reply

Progress

Status

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