2 Replies - 10886 Views - Last Post: 31 December 2010 - 12:39 PM

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,650
  • Joined: 27-December 08

Week #49- Love2D

Post icon  Posted 28 December 2010 - 09:57 AM

This week's challenge is love2d. Thanks to ishkabible for submitting this challenge!

Getting started with love2d.

Posted Image

Love2d is a 2D game engine that utilizes 3D acceleration. it uses Lua scripting along side, SDL, OpenGL, DevIL, OpenAL, Box2D and other library's to give you a truly awesome engine. it allows for robust 2D games without the fuss. it features physics, a particle engine, a vast array of supported file types among other things.

IDEAS:
*make a hello world application, prints "hello world" to the screen
*make a shape or charter that moves around the screen with key presses (e.g. arrow keys/WASD)
*make a charter shoot something
*play with physics, make a stack of objects that you can knock down
*make a simple pong game

RESOURCES:
http://love2d.org/wiki/Main_Page
http://love2d.org/forums/
http://www.headchant...aders-must-die/


you can get the latest release (0.7) here and also you can get the LuaJIT version here (note: the last link is a download link)

Is This A Good Question/Topic? 1
  • +

Replies To: Week #49- Love2D

#2 Huzi94  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Re: Week #49- Love2D

Posted 29 December 2010 - 01:10 AM

Hello,
Love2D seems to be an interesting language. Below is the traditional hello world for the language.

function love.draw()
    love.graphics.print('Hello World!', 400, 300)
end


Was This Post Helpful? 0
  • +
  • -

#3 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Week #49- Love2D

Posted 31 December 2010 - 12:39 PM

This is a neat little framework. I've got a ship moving around with a star field backdrop, but I'm planning on expanding it into a little game where you fly into the love logo to get more fuel in your ship. Here's the code so far:

gfx = love.graphics

-- Ship
Ship = {}

function Ship:new( i, x, y)
	local object = {
		image = i,
		x_pos = x,
		y_pos = y,
		angle = 0,
		x_vel = 0,
		y_vel = 0,
		turn_vel = 1.5,
		thrust = false,
		thrust_vel = 50,
		max_vel = 500,
		turning_left = false,
		turning_right = false }
	
	setmetatable(object, { __index = Ship })
	return object
end

function Ship:update_vel( dt )
	if self.thrust then
		local x_vel = self.x_vel
		local y_vel = self.y_vel
		
		self.x_vel = x_vel + (dt * self.thrust_vel * math.sin(self.angle))
		self.y_vel = y_vel - (dt * self.thrust_vel * math.cos(self.angle))
		
		if self:past_max( ) then
			self.x_vel = x_vel
			self.y_vel = y_vel
		end
	end
end

function Ship:update_angle( dt )
	if self.turning_right then
		self.angle = self.angle + (dt * self.turn_vel)
	elseif player.turning_left then
		self.angle = self.angle - (dt * self.turn_vel)
	end
end

function Ship:update_pos( dt, width, height )
	self.x_pos = self.x_pos + (dt * self.x_vel)
	self.y_pos = self.y_pos + (dt * self.y_vel)
	
	-- Wrap ship at window edges
	if self.x_pos <= 0 then
		self.x_pos = self.x_pos + width
	elseif self.x_pos >= width then
		self.x_pos = self.x_pos - width
	end
	
	if self.y_pos <= 0 then
		self.y_pos = self.y_pos + height
	elseif self.y_pos >= height then
		self.y_pos = self.y_pos - height
	end
end

function Ship:past_max( )
	local total_velocity = math.sqrt(self.x_vel^2 + self.y_vel^2)
	return total_velocity > self.max_vel
end

function Ship:render( )
	gfx.draw(self.image, self.x_pos, self.y_pos, self.angle, 0.5, 0.5, 16, 16)
end

-- Starfield
StarField = {}

function StarField:new( density, depth )
	local object = { stars = {}, move_scale = 100 }
	
	for i=1, density do
		local x = math.random(love.graphics.getWidth())
		local y = math.random(love.graphics.getHeight())
		local s = math.random(depth, 3 * depth) / depth
		object.stars[i] = { x_pos = x, y_pos = y, size = s }
	end
	
	setmetatable(object, { __index = StarField })
	return object
end

function StarField:move_stars( x_vel, y_vel, width, height )
	for i,v in ipairs(self.stars) do
		v.x_pos = v.x_pos - ((x_vel * v.size) / self.move_scale)
		v.y_pos = v.y_pos - ((y_vel * v.size) / self.move_scale)
		
		-- Wrap stars at window edges
		if v.x_pos <= 0 then
			v.x_pos = v.x_pos + width
		elseif v.x_pos >= width then
			v.x_pos = v.x_pos - width
		end

		if v.y_pos <= 0 then
			v.y_pos = v.y_pos + height
		elseif v.y_pos >= height then
			v.y_pos = v.y_pos - height
		end
	end
end

function StarField:render( )
	gfx.setColor(255, 255, 255)
	for i,v in ipairs(self.stars) do
		gfx.circle("fill", v.x_pos, v.y_pos, v.size)
	end
end

-- Love callbacks
function love.load( )
	local sprite = gfx.newImage("images/ship.png")
	local x_pos = gfx.getWidth()/2
	local y_pos = gfx.getHeight()/2
	player = Ship:new(sprite, x_pos, y_pos)
	
	stars = StarField:new(200, 50)
end

function love.update( dt )
	local w = gfx.getWidth()
	local h = gfx.getHeight()
	
	player:update_angle(dt)
	player:update_vel(dt)
	player:update_pos(dt, w, h)
	
	stars:move_stars(player.x_vel, player.y_vel, w, h)
end

function love.draw( )
	stars:render()
	player:render()
end

function love.keypressed( key, unicode )
	if key == "up" then
		player.thrust = true
	end
	
	if key == "right" then
		player.turning_right = true
	end
	
	if key == "left" then
		player.turning_left = true
	end
end

function love.keyreleased( key, unicode )
	if key == "up" then 
		player.thrust = false
	end
	
	if key == "right" then
		player.turning_right = false
	end
	
	if key == "left" then
		player.turning_left = false
	end
end


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1