GHOSTLY (Help for a Beginner)

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
GHOSTLY
Prole
Posts: 3
Joined: Wed Jul 29, 2009 8:15 pm

GHOSTLY (Help for a Beginner)

Post by GHOSTLY »

Hello.

First of, I am new to programming, obviously new to Lua and Lo[e]ve :brows:
I am dreaming, as I read on the board as some of you, of a zelda-esque game. You know, some dungeons, a worldmap, some events, bosses and some weapons, a save function and so on.

I know, pretty high aim for a beginner, nevertheless not unreachable. So I decided to create a little game. Learning by doing. After completing some starting tutorials for basic Lo[e]ve functions, I thought it might be a good start, using the first tutorial (moving an image) to go to a "higher" level. Still small breads...

So I ended up with GHOSTLY.

First of all I describe what I intended, then I post you the code and last but not least, the problem that occured :brows: .

GHOSTLY

You have a friendly ghost that's soaring through the night. You can move, left,right and up. The controls are left key for left, right key for right and up key for... well .. up. However by pressing down, the ghost constantly soars down and the game will restart, after the ghost reached the bottom (so it's your duty to keep that little ghost flying ).

That's all it should do by now. Later on I want to add collission, enemies and a sidescrolling level, but well, as I said, I am a beginner and I want to expand my knowledge by creating and expanding this game step by step.

Problems that occured so far

Well, I don't get the ghost to soar down. I used true / false and repeat - until loop, however nothing happens at all, but I think the game is getting stuck in the loop (and I see no problem there) and well doesn't react at all.

Here is the code.

Code: Select all

-- GHOSTLY Demo by F. Bingart aka GHOSTLY
-- Want to learn more about Lua and Lo[e]ve
-- I would be grateful if you want to help me out
-- Thanks in advance for all suggestions, tipps and helpful comments
---------------------------------------------------------------------------------------------------------------------------------------------------
-- I'm a absolute beginner to coding, lua and Lo[e]ve, however I am eager to learn
-- I want to start small, so I can really learn things, then just asking for code, code, code. I really want to understand where I messed up

function load()
	

-- Variables

	ghostly_r = 							-- loading right looking ghost, isn't he cute, done by me, using gimp
	love.graphics.newImage("images/ghost_right.png")

	ghostly_l = 							-- loding left looking ghost, isn't he cute, done by me, using gimp
	love.graphics.newImage("images/ghost_left.png")
	
	ghostlypicture =						-- to alter the picture, according to the buttons pressed, I guess it's easier using another variable
	ghostly_r		

	ghostlyx = 							-- start x variable, centers the ghost
	400
	ghostlyy = 							-- start y variable, centers the ghost
	300
	ghostlyspeed = 							-- high velocity, baby
	5
	ghostlygravity =						-- if the gravity is true, the ghost shall fall permanently, look further down the code. thanks. 
	false

-- Backgroundcolor

	love.graphics.setBackgroundColor(50, 5 , 185)			-- I lo[e]ve me some blue, also I was able test the transparacy of the .png files
	
end


function update(dt)

-- Setting end of game

	if ghostlyy > 600 then
		restart()						-- later on the ghost shall hit things and causes a game over screen,	
	end								-- but since I am in the "beginning class" this shall do it for now

-- Movement controls and triggering gravity

	if love.keyboard.isDown(love.key_right) then
		ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
		ghostlyx = ghostlyx + (ghostlyspeed * dt)		-- movement right
		ghostlypicture = ghostly_r				-- sets "ghostlypicture" to ghost_right.png
	elseif love.keyboard.isDown(love.key_left) then
		ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
		ghostlyx = ghostlyx - (ghostlyspeed * dt)		-- movement left
		ghostlypicture = ghostly_l				-- Sets "ghostlypicture" to ghost_left.png
	end
	
	if love.keyboard.isDown(love.key_up) then
		ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
		ghostlyy = ghostlyy - (ghostlyspeed * dt * 1.5)		-- movement up, * 1.5 for giving you the opportunity to raise again, slowly, but you'll raise
	end

-- Gravitycheck and what to do

	if ghostlygravity == true then					-- basicly, the Ghost shall drop down, until "ghostlygravity" is false again 
		repeat							
			ghostlyy = ghostlyy + (ghostlyspeed * dt)		
		until ghostlygravity == false				
	end
end

function draw()
			
	love.graphics.draw(ghostlypicture, ghostlyx, ghostlyy)
end
So where is my problem :/. I really can't help it. Thank you for your effort.

- GHOSTLY / F. Bingart

Oh.. and before I forget, here is the Love :nyu:
Attachments
GHOSTLY.love
The Lo[e]ve, from me, to you, to everyone.
(2.5 KiB) Downloaded 270 times
I'm still learning, so show me some Lo[e]ve!
User avatar
Person
Prole
Posts: 39
Joined: Thu Jun 18, 2009 2:35 am

Re: GHOSTLY (Help for a Beginner)

Post by Person »

You have an infinite loop, which causes the game to crash (or at least freeze the Löve window). It is the repeat..until ghostlygravity == false at around line 59. You have no way of making the ghostlygravity = false and so the game continues the loop forever.

A more exact look

Code: Select all

	if ghostlygravity == true then					-- basicly, the Ghost shall drop down, until "ghostlygravity" is false again 
		repeat							
			ghostlyy = ghostlyy + (ghostlyspeed * dt)		
		until ghostlygravity = false				-- No way of making ghostlygravity false, so the loop continues forever!
	end
"Here's another curse for you, may all your bacon burn."
User avatar
GHOSTLY
Prole
Posts: 3
Joined: Wed Jul 29, 2009 8:15 pm

Re: GHOSTLY (Help for a Beginner)

Post by GHOSTLY »

Thank you, well I changed the code a bit.

Now you can only control the GHOST if it's not on the bottom of the screen, by a simple line of code.

Also instead of checking if the "ghostlygravity" is true or false, I let it check if "ghostlyy" is equal or bigger than 600 to stop the repeat.

However it's way too fast down the bottom screen. If I should take I guess, I'd say it is the repeat loop.

I don't know how to code that it shall repeat with the delta time or by a variable or something else.

Also after reaching the bottom in an instant it starts soaring down as I want it, too.

Before I forced the programm to check if the ghostlyy < 600, I could move it left and right (locked it now) and while putting arrow key up all the time it went never higher than the place it "warped" the ghost to in the first time (You can see what I am talking by removing three times "if ghostlyy < 600 then ... end". I am really stuck here, because

1.) I seem to understand why it is warping down, but
2.) not why it would still move down more and
3.) why I can't go further up (maybe the ghost would go up, but the repeat loop sets it back in an instant, so I won't notice the difference and it's the same as it wouldn't go up)

As I said before, I am a absolute beginner, and I want to learn on this. As you see, I wrote the code myself (yeah, how exciting, a lot of ifs... :cry: ),but now I encounter problems I can grasp (1.), 3.) if my guesses are right) or even can't grasp (2.) why is it still moving downwards), but can't come up with a proper solution, or a solution at all, because I simply don't know how to set that up.

Sorry for being the n00b I am :brows:

Anyway... here is the code

Code: Select all

-- GHOSTLY Demo by F. Bingart aka GHOSTLY
-- Want to learn more about Lua and Lo[e]ve
-- I would be grateful if you want to help me out
-- Thanks in advance for all suggestions, tipps and helpful comments
---------------------------------------------------------------------------------------------------------------------------------------------------
-- I'm a absolute beginner to coding, lua and Lo[e]ve, however I am eager to learn
-- I want to start small, so I can really learn things, then just asking for code, code, code. I really want to understand where I messed up

function load()
	

-- Variables

	ghostly_r = 								-- loading right looking ghost, isn't he cute, done by me, using gimp
	love.graphics.newImage("images/ghost_right.png")

	ghostly_l = 								-- loding left looking ghost, isn't he cute, done by me, using gimp
	love.graphics.newImage("images/ghost_left.png")
	
	ghostlypicture =							-- to alter the picture, according to the buttons pressed
	ghostly_r		

	ghostlyx = 								-- start x variable, centers the ghost
	400
	ghostlyy = 								-- start y variable, centers the ghost
	300
	ghostlyspeed = 								-- high velocity, baby
	5
	ghostlygravity =							-- if the gravity is true, the ghost shall fall permanently, look further down the code. thanks. 
	false

-- Backgroundcolor

	love.graphics.setBackgroundColor(50, 5 , 185)				-- I lo[e]ve me some blue, also I was able test the transparacy of the .png files
	
end


function update(dt)


-- Movement controls and triggering gravity


	
	if love.keyboard.isDown(love.key_right) then
		if ghostlyy < 600 then						-- you are only allowed to move, if you haven't lost
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyx = ghostlyx + (ghostlyspeed * dt)		-- movement right
			ghostlypicture = ghostly_r				-- sets "ghostlypicture" to ghost_right.png
		end
	elseif love.keyboard.isDown(love.key_left) then
		if ghostlyy < 600 then						-- you are only allowed to move, if you haven't lost
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyx = ghostlyx - (ghostlyspeed * dt)		-- movement left
			ghostlypicture = ghostly_l				-- Sets "ghostlypicture" to ghost_left.png
		end
	end

	if love.keyboard.isDown(love.key_up) then
		if ghostlyy < 600 then						-- you are only allowed to move, if you haven't lost
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyy = ghostlyy - (ghostlyspeed * dt * 2)		-- movement up, * 2 for giving you the opportunity to raise again
		end
	end

-- Gravitycheck and what to do
	
	if ghostlygravity == true then						-- basicly, the Ghost shall drop down, until "ghostlygravity" is false again 
		repeat							
			ghostlyy = ghostlyy + (ghostlyspeed * dt)		
		until ghostlyy >= 600				
	end
end

function draw()
			
	love.graphics.draw(ghostlypicture, ghostlyx, ghostlyy)			-- left-right exchange works perfectly
end
And here is my love

-- GHOSTLY aka F. Bingart


EDIT:

Okay, I learned now the difference between while and repeat, while first checks if something is true and repeat will do it over and over again... :/ so with using

Code: Select all

		while ghostlyy < 600 do							
			ghostlyy = ghostlyy + (ghostlyspeed * dt)
		end						
instead of

Code: Select all

		repeat							
			ghostlyy = ghostlyy + (ghostlyspeed * dt)		
		until ghostlyy >= 600	
the ghost stops... learned a new thing, so much more to learn ... sigh
Attachments
GHOSTLY.love
The altered GHOSTLY file, now the GHOST doesn't crash, but ...
(2.45 KiB) Downloaded 250 times
I'm still learning, so show me some Lo[e]ve!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: GHOSTLY (Help for a Beginner)

Post by Robin »

I changed two things, both in the update() function.

I'm not going to tell what they are, because I think it's a good exercise for you to find out what I've changed and why. ^^

If you really don't know, I'll explain it, ok?

Code: Select all

	if ghostlyy < 600 then						-- you are only allowed to move, if you haven't lost
		if love.keyboard.isDown(love.key_right) then
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyx = ghostlyx + (ghostlyspeed * dt)		-- movement right
			ghostlypicture = ghostly_r				-- sets "ghostlypicture" to ghost_right.png
		elseif love.keyboard.isDown(love.key_left) then
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyx = ghostlyx - (ghostlyspeed * dt)		-- movement left
			ghostlypicture = ghostly_l				-- Sets "ghostlypicture" to ghost_left.png
		end
		if love.keyboard.isDown(love.key_up) then
			ghostlygravity = true					-- triggers "ghostlygravity", if that's the fist button you press
			ghostlyy = ghostlyy - (ghostlyspeed * dt * 2)		-- movement up, * 2 for giving you the opportunity to raise again
		end
	end

	-- Gravitycheck and what to do
	
	if ghostlygravity == true then						-- basicly, the Ghost shall drop down, until "ghostlygravity" is false again 
		ghostlyy = ghostlyy + (ghostlyspeed * dt)
	end
And welcome, by the way. :P
Help us help you: attach a .love.
User avatar
GHOSTLY
Prole
Posts: 3
Joined: Wed Jul 29, 2009 8:15 pm

Re: GHOSTLY (Help for a Beginner)

Post by GHOSTLY »

Thank you.

So that you don't think I am not trying to use my brain, when help is given I hereby grace you with the answers.

1. You atlered the if ghostlyy < 600 to enclose all three keys, which helps me to reduce code and always writing if ghostlyy < 600 after the key is pressed section. So it first checks if ghostlyy < 600, if it's the case, it checks which key is pressed and uses this code, if ghostlyy is not < 600, well it's the end and I can press as much button as I want to. So instead of writing three times if ghostlyy < 600 then I have to write it only one time, and then I end it.

2. You removed the while / repeat, whatsoever function. I guess I was thinking to hard to not see the obvious. Because I did it with the keys all the time :? . In this case with the ghostlygravity set to true it's as someone would always presses the down arrow key so to speak (and if I would have set it to a key). So as I said, true is shoving the ghost constantly down. If I want to release the virtual down button, I can assign something to set the ghostgravity to false again and it would stop moving down. I even wrote it in the comment
-- basicly, the Ghost shall drop down, until "ghostlygravity" is false again
and still was obsessed that it should repeat. Also the delta time does the rest, since the delta time is the time elapsed (in this case) while the gravtiy = true :ultrashocked::

Am I right? Correct me if I am wrong and yes I feel stupid thinking a while for / repeat until function is the solution... damn I have to think s i m p l e :x
I'm still learning, so show me some Lo[e]ve!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: GHOSTLY (Help for a Beginner)

Post by Robin »

You are absolutely right.

Sometimes, you have to look at your code and see what it does, instead of what you want it to do. I sometimes forget that myself. :death:

The way you answered tells me your logic skills are all right, which is always a pre in programming. ^^
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 9 guests