ShapeDrop

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

ShapeDrop

Post 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!
Attachments
ball.png
ball.png (459 Bytes) Viewed 3222 times
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: ShapeDrop

Post by rude »

Is the ground supposed to fall too? :brows:
Attachments
shapedrop.love
(1.83 KiB) Downloaded 165 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: ShapeDrop

Post by bartbes »

Yeah.. that doesn't make sense

@rude: Thanks for making the love (the file, I meant, I'm serious this time)
User avatar
whitebear
Citizen
Posts: 86
Joined: Sun Mar 15, 2009 1:50 am

Re: ShapeDrop

Post 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.
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

Re: ShapeDrop

Post 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
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests