Code Doodles!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Sheepolution »

I learned something new with math.

Image

Right mouse button to move the ball, left mouse button to pause.

You can add graphs by adding

Code: Select all

f[number](x) return --[[do stuff with x here]] end
You can add a differentiation with

Code: Select all

f[number]_ (x) etc.

Code: Select all

function love.load()
	love.window.setMode( 800, 800)
	colors = {
	{255,0,0},
	{0,255,0},
	{0,0,255},
	{255,0,255},
	{255,255,0},
	{0,255,255}
	}
	width = 400
	widthN = 10*(width/400)
	mod = (width/widthN)
	z = -6
	speed = 4
	love.graphics.setBackgroundColor(255, 255, 255)
end


function love.update(dt)
	if not love.mouse.isDown("l") and not love.mouse.isDown("r") then
		
	
		z = z + dt * speed
	end
	if love.mouse.isDown("r") then
		z = (love.mouse.getX()-400)/mod
	end

end


function love.draw()
	love.graphics.translate(width,width)
	love.graphics.setColor(0,0,0)
	love.graphics.setLineWidth(1)

	for i=-widthN,widthN do
		love.graphics.setColor(200,200,200)
		love.graphics.line(-width,i*mod,width+25,i*mod)
		love.graphics.line(i*mod,-width,i*mod,width+25)

		love.graphics.setColor(0,0,0)
		love.graphics.print(i,i*mod,-16)
		love.graphics.circle("fill",i*mod,0,3,10)

		love.graphics.print(-i,4,i*mod)
		love.graphics.circle("fill",0,i*mod,3,10)
	end

	love.graphics.line(-width,0,width+25,0)
	love.graphics.line(0,-width,0,width+25)

	love.graphics.setLineWidth(2)

	local F = 1

	while(true) do
		local f = _G["f" .. F]
		local f_ = _G["f" .. F .. "_"]
		if not f then break end

		--RED
		local col = 1 + ((F-1) % #colors)
		love.graphics.setColor(unpack(colors[col]))
		local line = {};
		for i=-widthN*20,widthN*20 do
			local x = i/20
			local y = _G["f" .. F](x);
			table.insert(line,x*mod)
			table.insert(line,-y*mod)
		end
		love.graphics.line(line)

		if f_ then
			local x = z*mod
			local y = -f(z)*mod
			local dirY = -f_(z)*mod

			love.graphics.setColor(0,0,0)
			love.graphics.line(x,y,x+mod,y+dirY)
			love.graphics.setColor(0,0,0);
			love.graphics.circle("fill",x,y,4,10)

			love.graphics.setColor(0,0,255)
			local line = {};
			for i=-widthN*20,widthN*20 do
				local x = i/20
				local y = f_(x)
				table.insert(line,x*mod)
				table.insert(line,-y*mod)
			end
			love.graphics.line(line)

			x = z*mod
			y = -f_(z)*mod

			love.graphics.setColor(0,0,0)
			love.graphics.circle("fill",x,y,4,10)
		end

		F = F + 1
	end

end


function f1(x)
	return math.sin(x)
end

function f1_(x)
	return math.cos(x)
end

-- function f2(x)
-- 	return x^2
-- end

-- function f2_(x)
-- 	return 2*x
-- end

-- function f3(x)
-- 	return math.sqrt(x)
-- end

-- function f3_(x)
-- 	return 1/(2*math.sqrt(x))
-- end

function love.mousepressed(x,y,key)
	if key == "wd" then
		speed = math.max(0,speed - 0.10)
	elseif key == "wu" then
		speed = math.min(10,speed + 0.10)
	end
end
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post by HugoBDesigner »

Random minigame I wrote just for the sake of passing time. So far, it works flawlessly and pretty optimized (and "prettified") for a 2-3 hours game:

Image

In this game, you just have to move to the next arc. Pretty easy, right? Not so much. You don't control the ship or the arcs. At all. This is a one-button game (you can use basically any key on keyboard or mouse button to fire), in which the arcs rotate around you at random speeds and random orientations, and you have to pass the ship through the hole in them. As levels increase, so does difficulty. Wheels get faster and holes get smaller. If you make it to level 10 without missing a single hole (it's possible, I did it myself), you can continue playing level 10 for as long as you possibly can, just increasing the high score. But if you fail (or failed) at least once, you start over after level 10.
Attachments
RotatingShip.love
(2.1 KiB) Downloaded 208 times
Last edited by HugoBDesigner on Mon Jun 08, 2015 9:07 pm, edited 1 time in total.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Code Doodles!

Post by Robin »

It's so freaking annoying if two consecutive rings move in the same direction at roughly the same speed and you have to wait for a really long time!
Help us help you: attach a .love.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Code Doodles!

Post by Nixola »

Robin wrote:It's so freaking annoying if two consecutive rings move in the same direction at roughly the same speed and you have to wait for a really long time!
I literally just thought "what if this happens? Nah, it's too rare, no one will face this"
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Code Doodles!

Post by HugoBDesigner »

I actually had some special code to make sure that there will always be a minimum time interval between rings so that they don't move along or make you wait two minutes :P
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Garmelon
Prole
Posts: 19
Joined: Tue Jun 02, 2015 5:25 pm

Re: Code Doodles!

Post by Garmelon »

Here are a few doodles, cba to take screenshots now :D
Nothing special because I'm not very creative.

E02.love: Just press any key
E03.love: Hover above the circles and click on them/hold the mouse button down
E04.love: Toggle balls with 1, 2 and 3, toggle canvas/trails with 4 (also clears canvas), clear canvas with C

EDIT: I posted this after looking at the first page. I've looked through all pages now and the more I see the more I like this thread.
Most of my cöde doodles are lost because I didn't save them, I just tried something out while creating something else.
But now that I know of this thread... :P
Attachments
E03.love
(4.43 KiB) Downloaded 192 times
E04.love
(4.5 KiB) Downloaded 184 times
E02.love
(4.21 KiB) Downloaded 183 times
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Code Doodles!

Post by Nixola »

What is a "normal canvas", and what OpenGL version does it require?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Garmelon
Prole
Posts: 19
Joined: Tue Jun 02, 2015 5:25 pm

Re: Code Doodles!

Post by Garmelon »

The "normal" is the [wiki]CanvasFormat[/wiki] (color).

Here's another doodle (totally not inspired by a doodle from this thread):
(Press any key to toggle the random "snow". Use the mouse (lmb) to create "snow" wherever you click.)

Code: Select all

function love.load()
	lgr = love.graphics
	lmo = love.mouse
	lgr.setBackgroundColor(255, 255, 255)
	
	s = {} -- settings
	s.cellWidth = 5 -- pixels
	s.updateTime = 0.02 -- updates after 0.02 seconds
	
	s.width = math.floor(lgr.getWidth()/s.cellWidth)
	s.height = math.floor(lgr.getHeight()/s.cellWidth)
	constantSnow = true
	
	t = 0 -- time
	world = {}
	for x = 1, s.width do
		world[x] = {}
		for y = 1, s.height do
			world[x][y] = false
		end
	end
end

function love.update(dt)
	t = t + dt
	if t < s.updateTime then return else t = 0 end
	
	-- calculate falling of - umm - things
	for x, t in ipairs(world) do
		for y, cell in ipairs(t) do
			if cell and not cell.moved then
				local c1, c2, c3 = getCell(x-1, y+1), getCell(x, y+1), getCell(x+1, y+1)
				if c2 == false then -- move down
					cell.moved = true
					world[x][y+1], world[x][y] = world[x][y], world[x][y+1] -- swap the values
				else
					if math.random(0, 1) == 0 then
						if c1 == false then
							cell.moved = true
							world[x-1][y+1], world[x][y] = world[x][y], world[x-1][y+1] -- swap the values
						elseif c3 == false then
							cell.moved = true
							world[x+1][y+1], world[x][y] = world[x][y], world[x+1][y+1] -- swap the values
						end
					else
						if c3 == false then
							cell.moved = true
							world[x+1][y+1], world[x][y] = world[x][y], world[x+1][y+1] -- swap the values
						elseif c1 == false then
							cell.moved = true
							world[x-1][y+1], world[x][y] = world[x][y], world[x-1][y+1] -- swap the values
						end
					end
				end
			end
		end
	end
	
	for x, t in ipairs(world) do
		for y, cell in ipairs(t) do
			if cell then
				cell.moved = false
			end
		end
	end
	
	spawn = not spawn
	if spawn then
		if lmo.isDown("l") then
			world[math.floor(lmo.getX()/s.cellWidth) + 1][math.floor(lmo.getY()/s.cellWidth) + 1] = {r = math.random(0, 255), g = math.random(0, 255), b = math.random(0, 255)}
		end
		if constantSnow then
			world[math.random(1, s.width)][1] = {r = math.random(0, 255), g = math.random(0, 255), b = math.random(0, 255)}
		end
	end
end

function love.draw()
	for x, t in ipairs(world) do
		for y, cell in ipairs(t) do
			local w = s.cellWidth
			if cell then
				lgr.setColor(cell.r, cell.g, cell.b)
				--lgr.setColor(255, 127, 0)
				lgr.rectangle("fill", (x-1)*w, (y-1)*w, w, w)
			end
			--lgr.setColor(127, 127, 127)
			--lgr.rectangle("line", (x-1)*w, (y-1)*w, w, w)
		end
	end
end

function love.keypressed()
	if key == "escape" then love.event.quit() end
	constantSnow = not constantSnow
end

function getCell(x, y)
	if world[x] then
		return world[x][y]
	else
		return
	end
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Code Doodles!

Post by Nixola »

Weird. I was really really sure this PC supported canvases.
EDIT: This indeed sounds like a bug. First of all, the error is misleading, as this pc supports normal canvases just fine.
Then, "fsaa" is specified in conf.lua. As far as I know, if the pc doesn't support it, it should fall back to zero, and nothing else should change; it instead throws that error (normal canvas format is not supported by your OpenGL implementation).
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Garmelon
Prole
Posts: 19
Joined: Tue Jun 02, 2015 5:25 pm

Re: Code Doodles!

Post by Garmelon »

The same thing happens on my school's computers (kinda old :P) too - When I reduce the samples to 0 it works.

Another doodle! :D The pattern moves btw. Use the arrow keys to change pattern
Image
Image
Attachments
stuffdoodle.love
(2.87 KiB) Downloaded 286 times
Last edited by Garmelon on Sun Jul 19, 2015 2:46 pm, edited 3 times in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests