Page 1 of 1

ShapeDrop

Posted: Mon Mar 16, 2009 7:25 am
by Xcmd
So this is something I've been tinkering with for awhile. It basically is a shape-drop type of "game", where you create a bunch of shapes and they drop and you watch the mayhem ensue. Anyway, the license is public domain or whatever. I'm going to be using some of this as the code-base for a game I have in mind. When the game actually has a name, I'll let you know. In the mean time... enjoy.

Code: Select all

-- Shape Drop by Xcmd
-- Released as Public Domain

function initVars()
	math.randomseed( os.time() )
	
	ballImg = love.graphics.newImage("ball.png")
	
	ballX = {}
	ballY = {}
	ballBody = {}
	ballShape = {}
	shapeList = {}
	
	ballCount = -1

	theShape = 1 -- 1: graphic ball, 2: drawn ball, 3: triangle, 4: rectangle, 5: unmoving square
	
	theWorld = love.physics.newWorld(800,600)
	theWorld:setGravity(0, 50)
	
	local f = love.graphics.newFont(love.default_font, 12)
	love.graphics.setFont(f)

	pause = 1
	speed = 10
	
	groundAngle = 0
	ground = love.physics.newBody(theWorld, 400, 300, 50)
	groundRect = love.physics.newRectangleShape(ground,0,0,500,5,groundAngle)
	
	instructions = "Left Click: Place Shape || Right Click: Change Shape || Space: Pause/Unpause || G: Rotate Ground || R: Reset"
end


function load()
	initVars()
end

function update(dt)
	if pause == 0 then
		theWorld:update(dt)
		if love.keyboard.isDown(love.key_g) then
			if groundAngle < 360 then
				groundAngle = groundAngle + (dt * speed)
				ground:setAngle(groundAngle)
			elseif groundAngle >= 360 then
				groundAngle = 0
				ground:setAngle(groundAngle)
			end
		end
		if love.keyboard.isDown(love.key_f) then
			if groundAngle > 0 then
				groundAngle = groundAngle - (dt * speed)
				ground:setAngle(groundAngle)
			elseif groundAngle <= 0 then
				groundAngle = 360
				ground:setAngle(groundAngle)
			end
		end
	end
end

function draw()
	for i = 0, ballCount do
		if shapeList[i] == 1 then
			love.graphics.draw(ballImg, ballBody[i]:getX(), ballBody[i]:getY(), ballBody[i]:getAngle())
		elseif shapeList[i] == 2 then
			love.graphics.circle(1, ballBody[i]:getX(), ballBody[i]:getY(), ballShape[i]:getRadius())
		elseif shapeList[i] == 3 or shapeList[i] == 4 then
			love.graphics.polygon(love.draw_line, ballShape[i]:getPoints())
		elseif shapeList[i] == 5 then
			love.graphics.polygon(0, ballShape[i]:getPoints())
		end
	end
	love.graphics.polygon(love.draw_line, groundRect:getPoints())

	love.graphics.draw(instructions, 5, 15)
end

function mousepressed(x, y, button)
	if button == love.mouse_left then
		ballCount = ballCount + 1
		ballX[ballCount] = x
		ballY[ballCount] = y
		ballBody[ballCount] = love.physics.newBody(theWorld, x, y)
		ballBody[ballCount]:setMass(0, 0, 1, 1)
		ballBody[ballCount]:setSpin(36)
		ballBody[ballCount]:setVelocity(math.random(-50, 50),math.random(-50, 0))
		if theShape == 1 or theShape == 2 then
			ballShape[ballCount] = love.physics.newCircleShape(ballBody[ballCount], 14)
		elseif theShape == 3 then
			ballShape[ballCount] = love.physics.newPolygonShape(ballBody[ballCount], 0,0,28,28,0,28)
		elseif theShape == 4 then
			ballShape[ballCount] = love.physics.newRectangleShape(ballBody[ballCount], 0,0,28,28)
		elseif theShape == 5 then
			ballShape[ballCount] = love.physics.newRectangleShape(ballBody[ballCount], 0,0,28,28)
			ballBody[ballCount]:setMass(0, 0, 0, 0)
			ballBody[ballCount]:setSpin(0)
		end
		shapeList[ballCount] = theShape
	end

	if button == love.mouse_right then
		if theShape < 5 then
			theShape = theShape + 1
		elseif theShape == 5 then
			theShape = 1
		end
	end
end

function keypressed (key)
	if key == love.key_space then
		if pause == 0 then
			pause = 1
		elseif pause == 1 then
			pause = 0
		end
	end
	if key == love.key_r then
		initVars()
	end
	if key == love.key_escape then
		love.system.exit()
	end
	if key == love.key_rightbracket then
		speed = speed + 10
	end
	if key == love.key_leftbracket then
		speed = speed - 10
	end
end
**NOTE: Because I've been having problems getting Love to run my zipped files, I've not included it as dot-love'd file. If you'd like to attach one, feel free! Tested to work under Love 0.5.0 on 64-bit Vista. Should work fine on pretty much everything else. Lemme know!

Re: ShapeDrop

Posted: Tue Mar 17, 2009 2:04 pm
by rude
Is the ground supposed to fall too? :brows:

Re: ShapeDrop

Posted: Tue Mar 17, 2009 3:24 pm
by bartbes
Yeah.. that doesn't make sense

@rude: Thanks for making the love (the file, I meant, I'm serious this time)

Re: ShapeDrop

Posted: Tue Mar 17, 2009 3:54 pm
by whitebear
You just need to make mass 0 to make them static. Also I've noticed that collision between two objects that are static is not counted.

Re: ShapeDrop

Posted: Wed Mar 18, 2009 3:16 am
by Xcmd
rude wrote:Is the ground supposed to fall too? :brows:
Yes, yes it is. Because I added mass to it. Because I can. Thanks for .love-ing it for me. That just sounds dirty.

And I don't count object collisions or anything. I don't care about that. I just want things to go poi-oi-oi-oi-oi-oi-oing