Page 1 of 1

Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 1:48 am
by rockstar0419
Basically, the game I am making is one in which you must rebuild a town after a natural disaster. When you press the "z" key and you have at least 50 lumber, a house is drawn. However, whenever I press the "z" key multiple houses are drawn at the same time because you can make multiple houses. My question is: How can you make a time buffer between drawing each house so that all the houses are not drawn at the same time?
The part that needs work:

Code: Select all

function love.update(dt)
if lumber >= 50 then
if love.keyboard.isDown("z") then

if smallshackCounter == 1 then
smallshack2State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
if smallshackCounter == 2 then
smallshack3State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
if smallshackCounter == 3 then
smallshack4State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
Thank you in advance.

Re: Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 6:11 am
by Wojak
The easy way:

Code: Select all

function love.keypressed(key)
if key == "z" then
if lumber >= 50 then
if smallshackCounter == 1 then
smallshack2State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
if smallshackCounter == 2 then
smallshack3State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
if smallshackCounter == 3 then
smallshack4State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
end
end

Re: Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 7:51 am
by jjmafiae
use tabs, it's much easier to look your code if you use tabs.

Re: Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 8:53 am
by Plu
Using love.keypressed is the easiest way; that triggers only once per tap of the button which means that the second house won't appear until you tap it again.

If you want the ability to keep the button pressed and then spray houses onto the map, you'll either need a timed delay or some sort of measurement of how far you have to move before you can paint again. The first one is fairly easy:

Code: Select all

function love.load()
  cooldown = 0
end

function love.update( dt )
  cooldown = cooldown - dt
  if love.keyboard.isDown("z") and cooldown < 0 then
    -- all your code goes here
    cooldown = 5 -- this will install a 5 second wait because cooldown will now be larger than 0 for 5 seconds
  end
end
The movement one is a little harder because it relies on the kind of map you use, so I can't give you an easy example for it.

Re: Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 10:58 pm
by rockstar0419
Alright, thanks.

Re: Beginner Coding Problem 2

Posted: Thu Feb 13, 2014 11:38 pm
by rockstar0419
Plu wrote:function love.update( dt )
  cooldown = cooldown - dt
  if love.keyboard.isDown("z") and cooldown < 0 then
    -- all your code goes here
    cooldown = 5 -- this will install a 5 second wait because cooldown will now be larger than 0 for 5 seconds
  end
end
After trying to use Plu's way, I am still confused. Where does the "cooldown = 5" go?

Code: Select all

function love.load()
cooldown = 0
smallshack = love.graphics.newImage("smallshack.png")
	-- lumber left
	lumber = 200
	-- cost  of a house
	-- amount of population
	pop = 3
	houseCost = 50
	-- houses - hold all generated houses
	houses = {}

	smallshackCounter = 1

end

-- and lumber >= 50
function love.update( dt )
cooldown = cooldown - dt
if love.keyboard.isDown("z") and cooldown < 0 then
if smallshackCounter == 1 then
smallshack2State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
cooldown = 5 -- this will install a 5 second wait because cooldown will now be larger than 0 for 5 seconds
end
if smallshackCounter == 2 then
smallshack3State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end
if smallshackCounter == 3 then
smallshack4State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
end






		



function love.draw() 
charitybutton = love.graphics.newImage("charitybutton.png")--displays the charity button
x = 530
y = 530
love.graphics.draw(charitybutton,x, y)
bottommenu = love.graphics.newImage("bottommenu.png")
x = 10
y = 410
love.graphics.draw(bottommenu,x, y) --displays the bottom menu
divider = love.graphics.newImage("divider.png")
x = 0
y = 405
love.graphics.draw(divider,x, y) --displays the divider
tree1 = love.graphics.newImage("tree1.png")
x = 700
y = 100
love.graphics.draw(tree1,x, y)

x = 750
y = 233
love.graphics.draw(tree1,x, y)

x = 668
y = 292
love.graphics.draw(tree1,x, y)
tree2 = love.graphics.newImage("tree2.png")
x = 690
y = 260
love.graphics.draw(tree2,x, y)

x = 623
y = 220
love.graphics.draw(tree2,x, y)
x = 780
y = 100
love.graphics.draw(tree2,x, y)
tree3 = love.graphics.newImage("tree3.png")
x = 700
y = 150
love.graphics.draw(tree3,x, y)

x = 600
y = 100
love.graphics.draw(tree3,x, y)

x = 620
y = 300
love.graphics.draw(tree3,x, y)
lumbercamp = love.graphics.newImage("lumbercamp.png")
x = 500
y = 200
love.graphics.draw(lumbercamp,x, y)
x = 50
y = 50
love.graphics.draw(smallshack,x, y)
guy = love.graphics.newImage("guy.png")
x = 600
y = 200
love.graphics.draw(guy,x, y)

x = 590
y = 300
love.graphics.draw(guy,x, y)

x = 685
y = 340
love.graphics.draw(guy,x, y)
topmenu = love.graphics.newImage("topmenu.png")
x = 530
y = 460
love.graphics.draw(topmenu,x, y)
love.graphics.print("Created by Ryan Martin", 3, 575)
love.graphics.print("V1.0", 210, 575)
love.graphics.print("x3", 100, 490)
love.graphics.print("x10", 270, 490)
love.graphics.print("x5", 415, 490)
love.graphics.print("50", 8, 443)
love.graphics.print("100", 165, 443)
love.graphics.print("150", 310, 443)
love.graphics.print("z", 50, 530)
love.graphics.print("x", 220, 530)
love.graphics.print("c", 360, 585)
love.graphics.print(pop, 672, 470)
love.graphics.print(lumber, 535, 470)

if smallshack2State == "drawn" then 
love.graphics.draw (smallshack, 300, 300)
end

if smallshack3State == "drawn" then
love.graphics.draw (smallshack, 250, 250)
end

if smallshack4State == "drawn" then
love.graphics.draw (smallshack, 100, 100)
end
end

Re: Beginner Coding Problem 2

Posted: Fri Feb 14, 2014 8:36 am
by Plu
At the end of this if statement, just before you hits its end:

Code: Select all

if love.keyboard.isDown("z") and cooldown < 0 then
So in the program you posted, that would be here:

Code: Select all

if smallshackCounter == 3 then
smallshack4State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
cooldown = 5
end
(Also you really should try to indent your code more, it makes it much more readable.)

Re: Beginner Coding Problem 2

Posted: Fri Feb 14, 2014 10:47 am
by Jeeper
Just another small nitpick, post questions and "help me"-posts under "Support and Development" and not in General :)

Re: Beginner Coding Problem 2

Posted: Fri Feb 21, 2014 2:26 am
by rockstar0419
Plu wrote:At the end of this if statement, just before you hits its end:

Code: Select all

if love.keyboard.isDown("z") and cooldown < 0 then
So in the program you posted, that would be here:

Code: Select all

if smallshackCounter == 3 then
smallshack4State = "drawn"
lumber = lumber - 50
smallshackCounter = smallshackCounter + 1
pop = pop + 3
end
cooldown = 5
end
(Also you really should try to indent your code more, it makes it much more readable.)
Sorry to bother you again, but after trying to place "cooldown = 5", there is still not a cooldown between each build. Is there more needed besides a cooldown?

Re: Beginner Coding Problem 2

Posted: Fri Feb 21, 2014 9:13 am
by Wojak
oryginal code:

Code: Select all

function love.update( dt )
	cooldown = cooldown - dt
	if love.keyboard.isDown("z") and lumber > 49 and cooldown < 0 then
		if smallshackCounter == 1 then
			lumber = lumber - 50
			smallshackCounter = smallshackCounter + 1 --  smallshackCounter = 2
			pop = pop + 3
			smallshack2State = "drawn"
		end
		if smallshackCounter == 2 then -- condition is true...
			lumber = lumber - 50
			smallshackCounter = smallshackCounter + 1 --  smallshackCounter = 3
			pop = pop + 3
			smallshack3State = "drawn"
		end
		if smallshackCounter == 3 then -- condition is true...
			lumber = lumber - 50
			smallshackCounter = smallshackCounter + 1
			pop = pop + 3
			smallshack4State = "drawn"
		end
		cooldown = 5
	end
end
fix:

Code: Select all

function love.update( dt )
	cooldown = cooldown - dt
	if love.keyboard.isDown("z") and lumber > 49 and cooldown < 0 and smallshackCounter < 4 then
		if smallshackCounter == 1 then
			smallshack2State = "drawn"
		elseif smallshackCounter == 2 then
			smallshack3State = "drawn"
		elseif smallshackCounter == 3 then
			smallshack4State = "drawn"
		end
		lumber = lumber - 50
		smallshackCounter = smallshackCounter + 1
		pop = pop + 3
		cooldown = 5
	end
end