вторник, 22 апреля 2014 г.

Getting Started with LÖVE

Эта же запись в русском переводе.
LÖVE (also known as love2d) is a Free & OpenSource framework for 2D game development with Lua.
Licensed under zlib/libpng license, that mean you can freely use it for commercial purposes with no limitations.

You can download LÖVE from official web-site. Ubuntu users can download deb packages, other distro users need to search packages in repositories of yours distros or compile LÖVE from source code.

As IDE you can use ZeroBrane Studio or Sublime Text 2 with SublimeLove or LuaLove (I prefer to use Sublime Text 2 + SublimeLove or ZeroBrane Studio).

This tutorial oriented for GNU/Linux users. Source code is similar on any platform, but about distribution on Windows you need to read in LÖVE wiki.

Distribution

LÖVE games distributes in *.love files (renamed zip archives).
For making love-archive you need
cd /path/to/game/project
zip -9 -q -r SuperGame.love .

Result will be SuperGame.love in /path/to/game/project directory.
SuperGame.love can be executed with
love /path/to/game/project/SuperGame.love

If you need standalone binary
cat /usr/bin/love SuperGame.love > SuperGame

Set SuperGame binary file as executable and run
chmod +x SuperGame
./SuperGame

Linux users also can build standalone exe files for windows
cat love.exe SuperGame.love > SuperGame.exe

Main game file - main.lua. Configuration file - conf.lua. When you execute love2d game always executes main.lua and games executes with options from conf.lua.

Hello LÖVE!

Create main.lua
function love.draw()
  love.graphics.print("Hello LOVE", 50, 50)
end
and execute.

If you are using ZeroBrane Studio
  • set love2d as interpreter: Project - Interpreter - Love2D; 
  • set project directory: Project - Project directory - Set from current file;
  • run game: Project - Run. 

Basic game structure on LÖVE

Any love2d game include at least three callbacks.
function love.load()
  --
end

function love.update(dt)
  --
end

function love.draw()
  --
end
  • love.load() - data from this callback with be processed (executed) on game load.
  • love.update(dt) - data from this callback will be processed in non-stop loop. While game is running, data from this callback processing per a frame. See to dt (delta timing). That need for processing task with similar speed on different hardware. Read about it on wikipedia
  • love.draw() - data from this callback will be drawn on screen. Note that data drawn in order as it placed in callback. So, draw background BEFORE player :)

Example

Draw red rectangle on screen.
function love.load()
  --
end

function love.update(dt)
  --
end

function love.draw()
  
  -- Set red color
  love.graphics.setColor(255, 0, 0)
  -- Draw rectangle
  love.graphics.rectangle("fill", 300, 200, 30, 60)
  
end

See to love.graphics.rectangle
  • "fill" mean that rectangle will be filled
  • 300 - X position (X coordinates calculates from left (0 to right (screen width))
  • 200 - Y position (Y coordinates calculates from top (0) to bottom (screen height))
  • 30 - rectangle width
  • 60 - rectangle height

Now create new table (for example name it "block") for our rectangle properties.
function love.load()
  
  block = {} -- table
  block.x = 300 -- x position
  block.y = 200 -- y position
  block.w = 30 -- width
  block.h = 60 -- height
  
end

function love.update(dt)
  --
end

function love.draw()
  
  -- Set red color
  love.graphics.setColor(255, 0, 0)
  -- Draw rectangle
  love.graphics.rectangle("fill", block.x, block.y, block.w, block.h)
  
end

Add movement.
function love.load()
  
  block = {} -- table
  block.x = 300 -- x position
  block.y = 200 -- y position
  block.w = 30 -- width
  block.h = 60 -- height
  
end

function love.update(dt)
  
  -- Move up - down 
  if (love.keyboard.isDown("up")) then
    block.y = block.y - 100*dt
  elseif (love.keyboard.isDown("down")) then
    block.y = block.y + 100*dt
  end
  
  -- Move left - right
  if (love.keyboard.isDown("left")) then
    block.x = block.x - 100*dt
  elseif (love.keyboard.isDown("right")) then
    block.x = block.x + 100*dt
  end
  
end

function love.draw()
  
  -- Set red color
  love.graphics.setColor(255, 0, 0)
  -- Draw rectangle
  love.graphics.rectangle("fill", block.x, block.y, block.w, block.h)
  
end
Now on arrows keys pressing rectangle moves to up/down, left/right. Note that we use two if - else constructions for movement. That's because Lua interpreter in multi if - (elseif - elseif - ... - elseif) - else check if-s item by item. If first "if" is true than nexts will be ignored. So, if you want to be able to move rectangle, for example, to up and right in same time you need to use two if - else constructions. 

That's end.

I recommend to you look to
Enjoy!

понедельник, 21 апреля 2014 г.

Введение в LÖVE

LÖVE (LOVE2D) - свободный кроссплатформенный фреймворк для создания 2D игр на Lua. Распространяется по лицензии zlib/libpng, а значит может свободно использоваться для создания игр, в том числе и коммерческих.

Скачать фреймворк можно с официального сайта: https://love2d.org/ Пользователи Ubuntu могут загрузить готовые пакеты, а пользователям других дистрибутивов придется скачать исходный код и самостоятельно собрать. Инструкция по сборке есть в вики фреймворка. При наличии установленных необходимых инструментов и зависимостей сборка делается буквально в две команды. Я использую самостоятельно собранный LÖVE версии 0.9.1 в Mageia 4, 32-bit. В качестве IDE рекомендую использовать ZeroBrane Studio.