Getting the Length and Width of a Random Rectangle
Posted: Sat Jan 17, 2015 3:43 am
Hello! Been a while but I'm finally coding my 2nd 'game' in love and I've gotten a bit stuck.
I have some randomly sized rectangles that I need to get the width and length of to determine if there's a collision to do.
Here is my enemy_load() function
As you can see, there is no enemy.length or enemy.width. I later use an ipairs loop to get the v.width and v.height of the rectangle.
When A timer comes up, this runs:
It makes sense in context, although I'm sure I'm going about it the hard way. Every variable of the rectangle is randomized including x,y,length and width. Length and width should have been inserted with the enemy_spawn() function, right? Well it's not, apparently. Because when I run this:
it gives me this when a bullet comes out:
Which means that it's not creating a width or height variable and I'm quite lost on it. I don't see a way to do like love.graphics.getWidth() for rectangles and I don't know how to do it myself if it won't insert the way I'm doing it.
I'm probably missing something obvious!
I should warn that my code is a warzone of uncommented, unorganized scrap. I'm simply doing a crummy game to learn things for better things. Hopefully that doesn't stop one of you smart people from helping me out!
Cheers!
Edit: I commeted out my bugged collision code. Here is a version of the game that's "playable." You should see what's up. Right now 1,2 and 3 change the RGB colors of the character and the bullets it fires. The goal is that you have to get your RGB colors close to the enemy's RGB colors(which are completely random at spawn) and it will kill them/make them lose health.
Also the border has a threshold like that already, if your RGBs are more than 40 units away, it will make the walls grow, if you're within 40, it'll make them grow. Still have to polish how it works, but right now it's just testing an idea I have.
Doesn't really play well but it's the best I've done so far.
I have some randomly sized rectangles that I need to get the width and length of to determine if there's a collision to do.
Here is my enemy_load() function
Code: Select all
enemy = {}
enemy.x1=0
enemy.x2=love.graphics.getWidth()
enemy.y1=0
enemy.y2=love.graphics.getHeight()
enemy.minSize1=15
enemy.minSize2=enemy.minSize1*2
enemy.maxSize1=30
enemy.maxSize2=enemy.maxSize1*2
enemy.initspeed=20
enemy.timer_init=1
enemy.timer=enemy.timer_init
function enemy_spawn(x,y,width,height,speed,r,g,b)
table.insert(enemy, {x=x,y=y,width=width,height=height,speed=speed,r=r,g=g,b=b})
end
When A timer comes up, this runs:
Code: Select all
enemy_spawn(math.random(e.x1,e.x2),math.random(e.y1,e.y2),math.random(e.minSize1,e.minSize2),math.random( e.maxSize1,e.maxSize2),enemy.initspeed,math.random(0,255),math.random(0,255),math.random(0,255))
Code: Select all
for i,v in ipairs(enemy) do
for bi,bv in ipairs(bullet) do
if bv.x > v.x+v.width or bv.x < bv.width-bv.w or bv.y < v.y+v.height or bv.y > v.height-v.height then
end
end
I'm probably missing something obvious!
I should warn that my code is a warzone of uncommented, unorganized scrap. I'm simply doing a crummy game to learn things for better things. Hopefully that doesn't stop one of you smart people from helping me out!
Cheers!
Edit: I commeted out my bugged collision code. Here is a version of the game that's "playable." You should see what's up. Right now 1,2 and 3 change the RGB colors of the character and the bullets it fires. The goal is that you have to get your RGB colors close to the enemy's RGB colors(which are completely random at spawn) and it will kill them/make them lose health.
Also the border has a threshold like that already, if your RGBs are more than 40 units away, it will make the walls grow, if you're within 40, it'll make them grow. Still have to polish how it works, but right now it's just testing an idea I have.
Doesn't really play well but it's the best I've done so far.