1. Home
  2. /
  3. Love2d
  4. /
  5. Lua Example - Simple Enemy AI

Lua Example - Simple Enemy AI - Love2d Typing CST Test

Loading…

Lua Example - Simple Enemy AI — Love2d Code

Enemy moves towards player automatically.

-- main.lua
local player = {x=400, y=300}
local enemy = {x=100, y=100, speed=100}

function love.update(dt)
	local dx = player.x - enemy.x
	local dy = player.y - enemy.y
	local dist = math.sqrt(dx*dx + dy*dy)
	if dist > 0 then
		enemy.x = enemy.x + dx/dist * enemy.speed * dt
		enemy.y = enemy.y + dy/dist * enemy.speed * dt
	end
end

function love.draw()
	love.graphics.rectangle("fill", player.x, player.y, 50, 50)
	love.graphics.rectangle("fill", enemy.x, enemy.y, 50, 50)
end

Love2d Language Guide

LOVE2D (LÖVE) is a free, open-source framework for 2D game development using Lua, focusing on simplicity, rapid prototyping, and cross-platform desktop and mobile deployment.

Primary Use Cases

  • ▸2D game development with Lua
  • ▸Indie game projects
  • ▸Rapid prototypes for game jams
  • ▸Educational programming projects
  • ▸Cross-platform 2D desktop and mobile games

Notable Features

  • ▸Lua scripting for game logic
  • ▸2D graphics rendering
  • ▸Audio and sound effects support
  • ▸Physics module via Box2D
  • ▸Input handling for keyboard, mouse, and gamepads

Origin & Creator

LOVE2D was created by Rafael 'Hoelzro' Lima in 2006, designed to make 2D game development accessible, fast, and flexible with Lua scripting.

Industrial Note

LOVE2D is widely used in indie game jams, rapid prototyping, educational coding projects, and hobbyist 2D game development due to its simplicity and lightweight framework.

More Love2d Typing Exercises

Love2D Lua Example - Simple CounterLove2D Lua Example - Moving RectangleLove2D Lua Example - Circle BounceLove2D Lua Example - Keyboard Controlled PlayerLove2D Lua Example - Timer CountdownLove2D Lua Example - Player JumpLove2D Lua Example - Moving BackgroundLove2D Lua Example - CollectiblesLove2D Lua Example - Particle System

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher