Need help finding an algorithm to see of click is on a block
Posted: Wed May 15, 2013 9:46 pm
So I've been building this small game for fun, 3 bricks fall at randomly selected coordinates that you then have to click with your mouse to "save" them from falling
so i need some code to put in here:
so that when i click on my three blocks:
they will be saved and the player will be awarded a point.
I began coding in C# so i thought i could just borrow and convert some code i did in a different project that did the same thing i need done here:
for those who don't code C# this basically finds the distance between the mouse click and the image that moves around on screen, and if the click is within the image you get one point.
but sadly i couldn't get it to work.
And for good measure ill include the main.lua code so you can get the whole view:(i know the codes a little rough, i plan on cleaning it up later though)
Any help or advice would be helpful, thank you.
so i need some code to put in here:
Code: Select all
function love.mousepressed()
end
Code: Select all
objects.enemy.body
objects.enemy2.body
objects.enemy3.body
I began coding in C# so i thought i could just borrow and convert some code i did in a different project that did the same thing i need done here:
Code: Select all
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int x = 40;
int y = 40;
int diffX = e.X - buttonRec.X;
int diffY = e.Y - buttonRec.Y - 10;
double diff = Math.Sqrt((diffX * diffX) - (diffY * diffY));
if (diff <= x && diff <= y)
{
score++;
labelscore.Text = score.ToString();
}
}
but sadly i couldn't get it to work.
And for good measure ill include the main.lua code so you can get the whole view:(i know the codes a little rough, i plan on cleaning it up later though)
Code: Select all
function love.load()
love.physics.setMeter(64)
world = love.physics.newWorld(0, 9.81 * 64, true)
objects = {}
objects.enemy = {}
objects.enemy.body = love.physics.newBody(world, 650/2, 650/5, "dynamic")
objects.enemy.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.enemy.fixture = love.physics.newFixture(objects.enemy.body, objects.enemy.shape, 2)
objects.enemy2 = {}
objects.enemy2.body = love.physics.newBody(world, 650/3, 650/5, "dynamic")
objects.enemy2.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.enemy2.fixture = love.physics.newFixture(objects.enemy2.body, objects.enemy2.shape, 2)
objects.enemy3 = {}
objects.enemy3.body = love.physics.newBody(world, 600, 650/5, "dynamic")
objects.enemy3.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.enemy3.fixture = love.physics.newFixture(objects.enemy3.body, objects.enemy3.shape, 2)
font = love.graphics.newFont(20)
recSaved = 0
thetime = 0
--graphic setup
love.graphics.setBackgroundColor(104, 136, 248)-- set background color to blue
love.graphics.setMode(650, 650, false, true, 0)-- window dimensions to 650 by 650, no full screen, vsync on, no antialiasing
end
function love.update(dt)
world:update(dt)
if objects.enemy.body:getY() > 770 then
newSpawnX = math.random(50, 600)
if newSpawnX > 100 then
newSpawnX2 = math.random(50 + 100, 600)
elseif newSpawnX < 550 then
newSpawnX2 = math.random(50, 450)
else
newSpawnX2 = math.random(50, 600)
end
newSpawnX3 = math.random(50, 600)
objects.enemy.body:setPosition(newSpawnX, 650/5)
objects.enemy.body:setLinearVelocity(0,0)
objects.enemy2.body:setPosition(newSpawnX2, 650/5)
objects.enemy2.body:setLinearVelocity(0,0)
objects.enemy3.body:setPosition(newSpawnX3, 650/5)
objects.enemy3.body:setLinearVelocity(0,0)
end
end
function love.keypressed(key)
if key == "down" then
recSaved = recSaved + 1
newSpawnX = math.random(50, 600)
newSpawnX2 = math.random(50, 600)
newSpawnX3 = math.random(50, 600)
objects.enemy.body:setPosition(newSpawnX, 650/5)
objects.enemy.body:setLinearVelocity(0,0)
objects.enemy2.body:setPosition(newSpawnX2, 650/5)
objects.enemy2.body:setLinearVelocity(0,0)
objects.enemy3.body:setPosition(newSpawnX3, 650/5)
objects.enemy3.body:setLinearVelocity(0,0)
end
end
function love.mousepressed()
end
function love.draw()
love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
love.graphics.polygon("fill", objects.enemy.body:getWorldPoints(objects.enemy.shape:getPoints()))
love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
love.graphics.polygon("fill", objects.enemy2.body:getWorldPoints(objects.enemy2.shape:getPoints()))
love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
love.graphics.polygon("fill", objects.enemy3.body:getWorldPoints(objects.enemy3.shape:getPoints()))
love.graphics.setColor(0, 255, 0, 255)
love.graphics.setFont(font)
love.graphics.print("Rectangles Save: " .. recSaved, 25, 25)
love.graphics.setColor(72, 260, 14)
love.graphics.setFont(font)
love.graphics.print("Timer: " .. thetime, 500, 25)
end