2018年01月27日

LÖVE でゲームプログラミング

 スクリプト言語で簡便にプログラミングできる環境をいろいろ検討していたのだが、最近 LÖVE というのを見つけた。

 これ、ファイナルアンサーじゃないかなと思いつつある。こういうのが欲しかったんだよ。

  • Windows/Mac/Linux のクロスプラットフォーム。Raspbian でも動く。iOS/Android 版もある?
  • 完全にフリーでオープンソース。
  • プログラム言語は Luajit。
  • GPU によるハードウェア描画をサポート。Raspbian 上では、X がなくてもフレームバッファ直書きで動く。
  • コミュニティが活発。フォーラムには毎日数件の投稿がある(英語だけど)。

 惜しむらくは、入門者向けのチュートリアルが少ない。例えば Scratch で遊んでいた人が移行できる感じではない。もともと Lua の知識がある人なら、スムーズに入っていけるんだと思うけど。また、日本語の情報はまだまだ少ない。このへんを参考に。

 とりあえず Mac で動かしてみた。LÖVE のホームページから Mac 版をダウンロードして展開、/Applications に置いておく。適当な場所に fish というディレクトリを作って、その中に main.lua を以下の内容で作成する。PNG ファイルはこれをダウンロードして、fish/images ディレクトリとして展開する。

player     = {}
fish       = {}
images = {}
cwide      = 320
chigh      = 240

love.window.setTitle('Les Poissons Danse')
swide = love.graphics.getWidth()
shigh = love.graphics.getHeight()

function love.load()
  images.fish0 = love.graphics.newImage('images/fish0.png')
  images.man0 = love.graphics.newImage('images/man0.png')
  fish.x    = 0
  fish.y    = 0
  fish.img  = images.fish0
  fish.width = fish.img:getWidth()
  fish.height = fish.img:getHeight()
  fish.speed = 150
  player.x     = 150
  player.y     = 150
  player.img   = images.man0
  player.width = player.img:getWidth()
  player.height = player.img:getHeight()
  player.speed = 200
end
    
function love.update(dt)
  dx = player.speed * dt
  if love.keyboard.isDown("right") then
    if player.x <= (cwide - player.width - dx) then
      player.x = player.x + dx
    end
  elseif love.keyboard.isDown("left") then
    if player.x >= dx then
      player.x = player.x - dx
    end
  elseif love.keyboard.isDown("down")  then
    if player.y <= (chigh - player.height - dx) then
      player.y = player.y + dx
    end
  elseif love.keyboard.isDown("up") then
    if player.y >= dx then
      player.y = player.y - dx
    end
  end
  dx = fish.speed * dt
  fish.x = fish.x + dx
  if fish.x >= cwide - fish.width then
    fish.speed = -math.abs(fish.speed)
    fish.x = fish.x - dx
  elseif fish.x <= 0 then
    fish.speed = math.abs(fish.speed)
    fish.x = fish.x - dx
  end
end
    
function love.draw()
  love.graphics.scale(swide / cwide, shigh / chigh)
  love.graphics.draw(player.img, player.x, player.y, 0, 1, 1, 0, 0)
  love.graphics.draw(fish.img, fish.x, fish.y, 0, 1, 1, 0, 0)
end

 ターミナルから /Applications/love.app/Contents/MacOS/love fish で実行。まだキャラクタを動かしているだけで、ゲームにはなってませんけどね。

20180127-1.png

 ラズパイでも動くことは確認した。ちょっとインストールが面倒なのと、挙動不審なところもあるので、まだ調査中。そのうちまとめます。

Posted at 2018年01月27日 11:34:18
email.png