Small retro ship

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Small retro ship

Post by Bigfoot71 »

Just a little delirium that I did quickly, I thought it could possibly be useful for someone to learn or do something about it ^^

It's a ship displayed with lines, it can be filled with a color, you can also define its size and its rotation, it follows the direction of the mouse if you click.

Code: Select all

local lg = love.graphics
local lk = love.keyboard
local lm = love.mouse

local function drawSpaceship(x, y, width, height, r, fill)

	local x1, y1 = x-width/2, y-height/2
	local x2, y2 = x, y+height/2
	local x3, y3 = x+width/2, y-height/2

	x1, y1 = x1 - x, y1 - y
	x2, y2 = x2 - x, y2 - y
	x3, y3 = x3 - x, y3 - y

	x1, y1 = x1 * math.cos(r) - y1 * math.sin(r), x1 * math.sin(r) + y1 * math.cos(r)
	x2, y2 = x2 * math.cos(r) - y2 * math.sin(r), x2 * math.sin(r) + y2 * math.cos(r)
	x3, y3 = x3 * math.cos(r) - y3 * math.sin(r), x3 * math.sin(r) + y3 * math.cos(r)

	x1, y1 = x1 + x, y1 + y
	x2, y2 = x2 + x, y2 + y
	x3, y3 = x3 + x, y3 + y

	lg.setLineWidth(4)
	lg.line(x,y, x1,y1, x2,y2, x3,y3, x,y)
	lg.setLineWidth(1)

	if fill then
		local r,g,b,a = lg.getColor()
		lg.setColor(fill~=true and fill or 0,0,0,1)
		lg.polygon("fill", x,y, x1,y1, x2,y2, x3,y3, x,y)
		lg.setColor(r,g,b,a)
	end

end


local x = 300
local y = 300
local r = 0

function love.update(dt)

	local vx = 0
	local vy = 0

	if lk.isDown("z") then vy = vy - 1 end
	if lk.isDown("s") then vy = vy + 1 end
	if lk.isDown("q") then vx = vx - 1 end
	if lk.isDown("d") then vx = vx + 1 end

	if lm.isDown(1) then
		local mx, my = lm.getPosition()
		r = math.atan2(my-y,mx-x)-1.5708
	end

	local len = math.sqrt(vx*vx+vy*vy)

	if len > 0 then
		vx = vx / len
		vy = vy / len
	end

	x = x + vx * 400 * dt
	y = y + vy * 400 * dt

end

lg.setBackgroundColor(0,0,.1)

function love.draw()
	drawSpaceship(x,y,128,128,r,true)
end

Edit: I made a shorter and slightly more optimized version of the `drawSpaceship` function:

Code: Select all

local function drawSpaceship(x, y, width, height, r, fill, lineWidth)

    local hw = width * 0.5
    local hh = height * 0.5

    local c = math.cos(r)
    local s = math.sin(r)

    local x1, y1 = -hw, hh
    local x2, y2 = 0, -hh
    local x3, y3 = hw, hh

    x1, y1 = x1 * c - y1 * s + x, x1 * s + y1 * c + y
    x2, y2 = x2 * c - y2 * s + x, x2 * s + y2 * c + y
    x3, y3 = x3 * c - y3 * s + x, x3 * s + y3 * c + y

    lg.setLineWidth(lineWidth or 1)
    lg.line(x,y, x1,y1, x2,y2, x3,y3, x,y)
    lg.setLineWidth(1)

    if fill then
        local r,g,b,a = lg.getColor()
        lg.setColor(fill~=true and fill or 0,0,0,1)
        lg.polygon("fill", x,y, x1,y1, x2,y2, x3,y3, x,y)
        lg.setColor(r,g,b,a)
    end

end
Attachments
space_ship.love
(731 Bytes) Downloaded 80 times
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: ShoesForClues and 12 guests