Page 1 of 1

Need help with my code (Read please)

Posted: Tue Oct 28, 2014 11:20 pm
by Pollo Rostizado
Hello guys.
I need help with my code, I just have 3 questions by now.
I want that the "Defeat" sound, just plays once after "haValidoQueso". But I dont know how to do it.
I want that when "haValidoQueso", the player can't move anymore. But I dont know how, because when "haValidoQueso", the image "teemo" changes for the image "explosion", and thats ok, but I can still moving teemo.

This maybe is the most difficult thing in my script, but I want that "obstaculoUno" and "obstaculoDos" move constantly to the left, and dissapear when reaching the left part, and also that new "obstaculos" appear constantly, just like in "flappy bird", and if its not a hassle, I want that when "teemo" reach a "obstaculo", the game stop.

I leave the code that I have of all the game, thanks.

Code: Select all

-- main.lua
-- The Teemo's Fly
local posX  	 = nil
local posY  	 = nil
local speed 	 = nil
local gravedad   = nil
local avance     = nil
local obstaculos = {}
local haValidoQueso = false
local haValidoChorizo = false

--Imagenes
local background = nil
local explosion  = nil
local teemo 	 = nil
local suelo      = nil
local obstaculoUno = nil
local obstaculoDos = nil

--Sonidos
local defeat     = nil
local Song  = nil


-- El archivo de entrada a nuestra app.
function love.load()

	--Imagenes
	background 	= love.graphics.newImage("images/background.png")
	explosion   = love.graphics.newImage("images/explosion.png")
	teemo 		= love.graphics.newImage("images/teemo.png")
	suelo       = love.graphics.newImage("images/groundDirt.png")
	obstaculoUno = love.graphics.newImage("images/rockDown.png")
	obstaculoDos = love.graphics.newImage("images/rock.png")
	--Sonidos
	defeat      = love.audio.newSource("sounds/Defeat LOL.mp3")
	Song   = love.audio.newSource("sounds/teemo song.mp3")
	--Otras cosas
	posX 		= 100
	posY 		= 240
	speed 		= 300
	gravedad 	= 150

end

function love.update(dt)

	love.audio.play(Song)  

	if (haValidoQueso) then
		love.audio.pause (Song)

	end




	
	--Avance! 
	--local avance = 480 - teemo:getHeight()
	--posX = posX + (gravedad * dt)

	-- Gravedad!
	local piso = 480 - teemo:getHeight()
	posY = posY + (gravedad * dt)
   
    --Hacer que teemo se estrelle   
	if (posY > piso) then
		posY = piso



		--Avisar que ya perdimos
		haValidoQueso = true

	end

	
     --Movimiento de Teemo 
   	if love.keyboard.isDown("down") then
      	posY = posY + (speed * dt)
   	end
   	if love.keyboard.isDown("up") then
    	posY = posY - (speed * dt)
   	end

  
   	--Cerrar el juego
   	if love.keyboard.isDown("escape") then
    	love.event.quit()
   	end
    
    

   	--Sonido de defeat
   	if (haValidoQueso) then
    	
    	love.audio.play(defeat)

   	end


   end






function love.draw()
	love.graphics.draw(background, 0, 0)
	love.graphics.draw(obstaculoDos, 500, 480 - obstaculoDos:getHeight())
	love.graphics.draw(obstaculoUno, 700, 0)
	love.graphics.draw(suelo, 0, 480 - 70)
	


	if (haValidoQueso) then
		love.graphics.draw(explosion, posX, posY - 40)
	else
		love.graphics.draw(teemo, posX, posY)
	end

end

Re: Need help with my code (Read please)

Posted: Tue Oct 28, 2014 11:32 pm
by HugoBDesigner
Makes it this way:

In the place where you set "haValidoQueso" to true, add the "love.audio.play" call with your sound. If you're setting "haValidoQueso" as true in several places, make a function to set it true and play the sound.

Next, to make teemo stop moving if "haValidoQueso" is true, go to the part where you control it. Add "if not haValidoQueso then [Movement code] end".

As for the last question, make them move constantly to the left by constantly subtracting from it's x position. Once it reaches it's target, and since you want it to create a new obstacle, simply set the "x" value of your obstacles to the end of the screen again, and it'll keep moving left.

To make it stop the game if teemo touches the obstacle, you'll have to detect collision. It seems like you already have it (based on the thing you did for the explosion on fall). Just call a function once this happens, and you have two options: reset the game (by calling love.load() ) or quit the game (by calling love.event.quit() )...

Re: Need help with my code (Read please)

Posted: Tue Oct 28, 2014 11:34 pm
by undef
A .love file might make this easier.
You probably have to just set haValidoQueso (dat variable name) back to nil, if not please upload a .love.

Re: Need help with my code (Read please)

Posted: Tue Oct 28, 2014 11:51 pm
by Pollo Rostizado
HugoBDesigner wrote:In the place where you set "haValidoQueso" to true, add the "love.audio.play" call with your sound. If you're setting "haValidoQueso" as true in several places, make a function to set it true and play the sound.
Do I'm suposed to do it like this?

Code: Select all

--Avisar que ya perdimos
		haValidoQueso = true love.audio.play(defeat)

	end
If yes, it doesnt works, the sounds keeps playing more than once.
HugoBDesigner wrote:Next, to make teemo stop moving if "haValidoQueso" is true, go to the part where you control it. Add "if not haValidoQueso then [Movement code] end".
This works perfectly, thanks.
HugoBDesigner wrote:As for the last question, make them move constantly to the left by constantly subtracting from it's x position. Once it reaches it's target, and since you want it to create a new obstacle, simply set the "x" value of your obstacles to the end of the screen again, and it'll keep moving left.

To make it stop the game if teemo touches the obstacle, you'll have to detect collision. It seems like you already have it (based on the thing you did for the explosion on fall). Just call a function once this happens, and you have two options: reset the game (by calling love.load() ) or quit the game (by calling love.event.quit() )...
I dont have idea how to do this, can you give me the functions (example: "love.graphic.draw") ? With that I will try to do it by myself (:

Re: Need help with my code (Read please)

Posted: Wed Oct 29, 2014 11:51 am
by HugoBDesigner
Pollo Rostizado wrote:
HugoBDesigner wrote:In the place where you set "haValidoQueso" to true, add the "love.audio.play" call with your sound. If you're setting "haValidoQueso" as true in several places, make a function to set it true and play the sound.
Do I'm suposed to do it like this?

Code: Select all

--Avisar que ya perdimos
		haValidoQueso = true love.audio.play(defeat)

	end
If yes, it doesnt works, the sounds keeps playing more than once.
I'm supposing you're setting it to true every time, and not just once. It works, but it's not very efficient after you want to make something with it. So I'd check first if "haValidoQueso" is false and THEN set it to true. This way your sound will just play once:

Code: Select all

if haValidoQueso == false then
    haValidoQueso = true
    love.audio.play(defeat)
end
Pollo Rostizado wrote:
HugoBDesigner wrote:As for the last question, make them move constantly to the left by constantly subtracting from it's x position. Once it reaches it's target, and since you want it to create a new obstacle, simply set the "x" value of your obstacles to the end of the screen again, and it'll keep moving left.

To make it stop the game if teemo touches the obstacle, you'll have to detect collision. It seems like you already have it (based on the thing you did for the explosion on fall). Just call a function once this happens, and you have two options: reset the game (by calling love.load() ) or quit the game (by calling love.event.quit() )...
I dont have idea how to do this, can you give me the functions (example: "love.graphic.draw") ? With that I will try to do it by myself (:
You must set an "x" variable to your obstacles. Then, while drawing it, you do:

Code: Select all

love.graphics.draw(obstacleImage, x, y)
Where the "x" is the same "x" variable you're subtracting from, and "y" is the same value you had before (a number value, I suppose).

Also, just a little advice: since the coding languages are, by themselves, in English, it'd be better to write everything in your code in English. Things like variables and comments work better in English because if someone wants to check out your code, they won't have to translate things. I know it's more comfortable and easier to write things in your own language (I'm Brazilian), but making your code in English is much better, and the payoff too. Also, it helps us practice English ;)