Math.random help :S

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Puzzelism
Prole
Posts: 16
Joined: Wed Mar 06, 2013 2:00 am

Math.random help :S

Post by Puzzelism »

YO:
I'm having a problem with my code in math.random where it keeps giving me the same integer value every single time, i know it has to be seeded but i don't know how in these circumstances

Code: Select all

enemy = {}
enemyX = math.random(0, 300)
enemyY = math.random(0, 500)
enemyWidth = math.random(32, 50)
enemyHeigth = math.random(32, 50)

function enemy_spawn()
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
end
Also, in the part where i have

Code: Select all

love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
it gives me an error where the enemyWidth/Height isnt being recognized as an integer...

ALL HELP APPRECIATED, THANKS!
Test.love
(672 Bytes) Downloaded 131 times
Last edited by Puzzelism on Sat Dec 14, 2013 6:47 pm, edited 1 time in total.
One cannot simply code love.. Oh wait with LÖVE you can..
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Math.random help :S

Post by veethree »

If you post your .love we you can help us help you.

First the error, Are you sure the enemyX, enemyY etc. variables aren't inside the enemy table? If not you don't really need that table.

As far as the math.random problem,Since 0.8.0 the randomseed is randomized right before love.load() is called, So i'm not sure what the problem is. If you post your code, I can look through it and test it myself to try and figure out what's wrong.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Math.random help :S

Post by Robin »

Okay: here's the deal: you have a function called enemy_spawn(), but you should really call it enemy_draw, because that's what it does: it draws an enemy. Your code as-is creates a single enemy at the start of the game, and that's it. Here's a suggestion that you can start from:

Code: Select all

enemies = {}

function spawn_enemy()
    local e = {}
    e.x = math.random(0, 300)
    e.y = math.random(0, 500)
    e.width = math.random(32, 50)
    e.height = math.random(32, 50)
    table.insert(enemies, e)
end

function draw_enemies()
    love.graphics.setColor(255,0,0)
    for i, enemy in ipairs(enemies) do
        love.graphics.rectangle("fill", enemy.x, enemy.y, enemy.width, enemy.height)
    end
end
Can you take it from here or do you want more explanation?
Help us help you: attach a .love.
User avatar
NightKawata
Party member
Posts: 294
Joined: Tue Jan 01, 2013 9:18 pm
Location: Cyberspace, Room 6502
Contact:

Re: Math.random help :S

Post by NightKawata »

Code: Select all

enemyHeigth = math.random(32, 50)

Code: Select all

love.graphics.rectangle("fill", enemyX, enemyY, enemyWidth, enemyHeight)
Fix the typo. You can criticise his coding methods later.
As to the seeding part, LOVE should do that automatically in love.run. Since 0.9.0 is also out, you can use this for a little bit more consistency.
"I view Python for game usage about the same as going fishing with a stick of dynamite. It will do the job but it's big, noisy, you'll probably get soaking wet and you've still got to get the damn fish out of the water." -taylor
Puzzelism
Prole
Posts: 16
Joined: Wed Mar 06, 2013 2:00 am

Re: Math.random help :S

Post by Puzzelism »

Thanks for the responses, also thank you for finding the typo, i guess i should have looked it over more first... I've also found that I have an older LOVE version, because when installing the newest version and trying to run my games, LOVE just stops responding, i think that's why it didn't automatically seed it. So far though it seems that if i just put

Code: Select all

math.randomseed(os.time())
at the top of my code it temporarily fixes it until i can get the newest version to work...

Anyways, thanks for the help!
One cannot simply code love.. Oh wait with LÖVE you can..
User avatar
NightKawata
Party member
Posts: 294
Joined: Tue Jan 01, 2013 9:18 pm
Location: Cyberspace, Room 6502
Contact:

Re: Math.random help :S

Post by NightKawata »

Wait, stops responding as in it actually crashes, or do you just get some kind of blue error screen?
If it's the latter, you can just update a bunch of functions to fix that up. (I'd assume some love.graphics functions got renamed to love.window functions in that, but eh) If it's the former, uh
"I view Python for game usage about the same as going fishing with a stick of dynamite. It will do the job but it's big, noisy, you'll probably get soaking wet and you've still got to get the damn fish out of the water." -taylor
Puzzelism
Prole
Posts: 16
Joined: Wed Mar 06, 2013 2:00 am

Re: Math.random help :S

Post by Puzzelism »

Crashes as in goes to the stupid white "LOVE.exe is not responding" screen.
One cannot simply code love.. Oh wait with LÖVE you can..
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests