3 doesn't equal 3?

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.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Re: 3 doesn't equal 3?

Post by YoungNeer »

MrGreen wrote: Fri Aug 02, 2019 10:13 am To be honset I was not able to get this going. I saw some issues which I wanted to try out, but I think the version is too old.
Hello there! To be honest there are millions of ways to write snake game. The repo that I provided is very simple and easy to understand. And about the old version - well you can run it quick using the appImages for linux or binaries for windows. But if you want to program in the latest version then here's a youtube video which might help you. The author is an instructor at Harvard Extension School - so you shouldn't have much problem understanding what's going on



And if you don't like watching youtube tutorials then head on to this site. The last time I saw the source-code it was like 150 lines long - so this shouldn't be a head-ache plus everything is modular.
MrGreen wrote: Fri Aug 02, 2019 10:13 am I was not able to understand what it is really doing; If you could explain me that again for an idiot like me, that would be awesome.
Just as an extra comment- please do not disrespect yourself (even in jokes) :brows: - or people won't respect you out of their will

But anyways. I downloaded your files. And just a side-comment you don't really need conf.lua in this context. It's useful when publishing game but in this context it's completely unnecessary I'd say. And about your main code- it's humongous and that definitely is not a compliment to a developer. Plus it's broken as well. And I'm no-one to give you advice on programming but - Richard Buckland said this - "It's not the algorithm but the data-structure that is the key to any problem". So rather than working on how to move the snake you should consider "how should I store the snake" and that would be the key to your problem

And finally back to your code. I have seen atleast 5 snake games written in LOVE but not even in one of those did i see anything such as 'alive=true'. Really you should minimize the amount of variables you use - plus the spelling of snake is incorrect (snack :rofl: - it's no big deal but it can create problems later especially in context of Lua programming). Plus I really can't understand the 'aim in life' of other variables such as place1,etc. And generally speaking there's a rule in programming that if you are using variables that are used only few times or not used at all then you are doing something wrong. In your case many variables are unused like 'success' (even if setMode does return a boolean - you don't need anything to store it to some variable for it to work) 'way' (what's the use of 'way'?) and complementary1,...,complementary3.

Also I strongly discourage on using isDown function for this purpose- i remember when I was programming snake (it's super ugly so I didn't upload it on my github. plus it's not even polished :3 ) it was causing a lot of buggy behaviour since you will only change direction of snake on one frame - but isDown will be true for as long as the key is pressed - which we don't want in this case.

There are lot more errors which I won't point out- since i don't want to discourage you. Well figuring out things on your own is a good idea but you should also look at how other people solved the same problem. In programming is all about - "finding the best possible solution to a problem?" - so how would you know if your solution is the best unless you compare it with that of your peers.

Regards :)
Last edited by YoungNeer on Fri Aug 02, 2019 1:58 pm, edited 1 time in total.
My Github- your contribution is highly appreciated
MrGreen
Prole
Posts: 12
Joined: Thu Aug 01, 2019 2:07 pm

Re: 3 doesn't equal 3?

Post by MrGreen »

Okay, thank you for your advise. I already worked on the structure this afternoon and deleted all unnecessary stuff. And by snack I mean the circle the snake can eat.

Latest version:
SnakeShort.lua
(4.84 KiB) Downloaded 225 times
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Re: 3 doesn't equal 3?

Post by YoungNeer »

MrGreen wrote: Fri Aug 02, 2019 1:53 pm Okay, thank you for your advise. I already worked on the structure this afternoon and deleted all unnecessary stuff. And by snack I mean the circle the snake can eat.
I'm sorry I misjudged that. And regarding the latest file - it's already infinitely better than the previous one - from 365 lines to 228 - that's a great improvement (ofcourse most of the part was comments in the previous file but still I'd see that as an improvement)

And just wanted to remind you that the latest version of love doesn't support 8bit rgb triplets. So you will have to bring that down to decimal format [i.e. (255,255,255) -> (1,1,1)]. But that's only if you are using the latest version

Plus you don't need to have a background image. You could basically just have a for loop and render rectangles of unit size (let's say 32x32 in this context) :- { and I recommend you change the window size to 992 from 1005 just for this to work well)

Code: Select all

function love.draw()
    for j=1,31 do
        for i=1,31 do
        	love.graphics.rectangle('line',(i-1)*32,(j-1)*32,32,32)
	end
    end
end
And to match the line width you could possibly do this:-

Code: Select all

love.graphics.setWidth(2)
And i've edited the previous post a bit (which talks about unused variables) - so please read that one as well
My Github- your contribution is highly appreciated
MrGreen
Prole
Posts: 12
Joined: Thu Aug 01, 2019 2:07 pm

Re: 3 doesn't equal 3?

Post by MrGreen »

YoungNeer wrote: Fri Aug 02, 2019 11:52 am In your case many variables are unused like 'success' (even if setMode does return a boolean - you don't need anything to store it to some variable for it to work) 'way' (what's the use of 'way'?) and complementary1,...,complementary3.
Thank you, I improved and will try to improve this.
YoungNeer wrote: Fri Aug 02, 2019 11:52 am Also I strongly discourage on using isDown function for this purpose- i remember when I was programming snake (it's super ugly so I didn't upload it on my github. plus it's not even polished :3 ) it was causing a lot of buggy behaviour since you will only change direction of snake on one frame - but isDown will be true for as long as the key is pressed - which we don't want in this case.
Okay, that is bothering me, too. Could you give me another tip for this problem?
YoungNeer wrote: Fri Aug 02, 2019 11:52 am There are lot more errors which I won't point out- since i don't want to discourage you. Well figuring out things on your own is a good idea but you should also look at how other people solved the same problem. In programming is all about - "finding the best possible solution to a problem?" - so how would you know if your solution is the best unless you compare it with that of your peers.
Yes, you are absolutly right. It is very important to learn from others. This is why I am very thankful for everyone who offers help.
YoungNeer wrote: Fri Aug 02, 2019 2:03 pm And just wanted to remind you that the latest version of love doesn't support 8bit rgb triplets. So you will have to bring that down to decimal format [i.e. (255,255,255) -> (1,1,1)]. But that's only if you are using the latest version
In the beginning I had problems with that, but now I just devide my color by 255.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Re: 3 doesn't equal 3?

Post by YoungNeer »

MrGreen wrote: Fri Aug 02, 2019 2:19 pm Okay, that is bothering me, too. Could you give me another tip for this problem?
I'm sure other people do it differently. But here's how I do it:-

Code: Select all

	function love.update(dt)
		if love.keyboard.lastKeyPressed=='up' then
			-- your logic here
		end
		love.keyboard.lastKeyPressed=nil
	end

	function love.keypressed(key)
		love.keyboard.lastKeyPressed=key
	end
Please note that love.keyboard.lastKeyPressed is a custom variable that we created (just now). As the name says- it will give you the last key pressed i.e. the key pressed in the last frame - if no key was pressed then this will be nil.
MrGreen wrote: Fri Aug 02, 2019 2:19 pm In the beginning I had problems with that, but now I just devide my color by 255.
Maybe you don't have to do even that - you can check loveCC and ColorPicker

Working around colors can be a waste of time sometimes - so most of the time you want some-thing to make it easy for you. Like you want to set the color crimson and you don't know it's code. No worries. If you are using loveCC then you can simply say

Code: Select all

lovecc:setColor('red')
For full documentation you can visit the post link
My Github- your contribution is highly appreciated
MrGreen
Prole
Posts: 12
Joined: Thu Aug 01, 2019 2:07 pm

Re: 3 doesn't equal 3?

Post by MrGreen »

YoungNeer wrote: Fri Aug 02, 2019 2:03 pm Plus you don't need to have a background image. You could basically just have a for loop and render rectangles of unit size (let's say 32x32 in this context) :- { and I recommend you change the window size to 992 from 1005 just for this to work well)

Code: Select all

function love.draw()
    for j=1,31 do
        for i=1,31 do
        	love.graphics.rectangle('line',(i-1)*32,(j-1)*32,32,32)
	end
    end
end
That is a thing which I was unsure about. So it is faster than taking an image? I decided to leave the background away, but it would be interseting when it is faster to draw an image or using the graphics-function (I think it is a lot faster to load an image where all pixels are different than to make only 1x1 rectangles)

Another question: Did the file worked on your computer? When I download the file it does not. But the file I uploaded works. Strange.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Re: 3 doesn't equal 3?

Post by YoungNeer »

MrGreen wrote: Fri Aug 02, 2019 2:32 pm That is a thing which I was unsure about. So it is faster than taking an image? I decided to leave the background away, but it would be interseting when it is faster to draw an image or using the graphics-function (I think it is a lot faster to load an image where all pixels are different than to make only 1x1 rectangles)
Every algorithm is a trade-off between speed complexity, time complexity, scalability and other stuff like ease to use. I'm not sure about the speed thing cause the image has more pixels. But I'd say for development environment it's better to draw rectangles cause it's easy to change the number and size of rectangles. So if at some point of the program you want the snake to be bigger then you don't have to use gimp or something.
MrGreen wrote: Fri Aug 02, 2019 2:32 pm Another question: Did the file worked on your computer? When I download the file it does not. But the file I uploaded works. Strange.
Well it does work! (atleast for me I'd say)
My Github- your contribution is highly appreciated
MrGreen
Prole
Posts: 12
Joined: Thu Aug 01, 2019 2:07 pm

Re: 3 doesn't equal 3?

Post by MrGreen »

YoungNeer wrote: Fri Aug 02, 2019 2:31 pm

Code: Select all

	function love.update(dt)
		if love.keyboard.lastKeyPressed=='up' then
			-- your logic here
		end
		love.keyboard.lastKeyPressed=nil
	end

	function love.keypressed(key)
		love.keyboard.lastKeyPressed=key
	end
Thanks a lot I will try this.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Re: 3 doesn't equal 3?

Post by YoungNeer »

MrGreen wrote: Fri Aug 02, 2019 2:38 pm Thanks a lot I will try this.
Oh no it's nothing! Anyways will see u in the Games and Creation forum with your Snake Game.
Good Luck :nyu:
My Github- your contribution is highly appreciated
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: 3 doesn't equal 3?

Post by zorg »

MrGreen wrote: Fri Aug 02, 2019 10:13 am I was not able to understand what it is really doing; If you could explain me that again, that would be awesome.
If you check the wiki, you'll find that löve provides functions called callbacks (love#Callbacks); what that means is that löve itself will call those functions when it needs to (for example, if you press a key, it will try to call love.keypressed) if you have written those functions yourself.

In my code, i wrote the callback for exactly that functionality; when you press any button, it will be called once with the corresponding key (and scancode).

Each of the 4 parts of the function checks whether a direction is already being held down or not for only the opposite directions, meaning that, if you're holding down the right arrow already, it will not allow you to modify the direction of the snake by pressing the left arrow. The same is true for the other 3 directions, and their opposites. (like up/down, down/up, left/right)
These lines in turn set the movement speed on the correct axis (horizontal - left/right, or vertical - up/down) to a number that represents movement in that direction, the positive or negative ones. (that you can multiply with another number, say velocity, if you want a faster snake)

The other line within the if/else blocks sets the movement on the other axis to be zero, so that the snake will never move diagonally, even if you didn't release any previous keys.

Don't be confused by me not writing the function as "function love.keypressed(k,s)" though, that's the same thing in lua.

Actually, a bit more code and it can actually work as a basic (but incomplete) snake game already:

Code: Select all

local position = {0,0} -- where the head of the snake is
local direction = {0,0} -- what direction it's going
local speed = 10 -- it moves this many pixels each time it moves

love.keypressed = function(k,s)
  if s == 'up' then
    direction[1] = love.keyboard.isScancodeDown('down') and direction[1] or 1.0
    direction[2] = 0.0 -- orthogonal movement
  elseif s == 'down' then
    direction[1] = love.keyboard.isScancodeDown('up') and direction[1] or -1.0
    direction[2] = 0.0 -- orthogonal movement
  elseif s == 'left' then
    direction[1] = 0.0 -- orthogonal movement
    direction[2] = love.keyboard.isScancodeDown('right') and direction[2] or -1.0
  elseif s == 'right' then
    direction[1] = 0.0 -- orthogonal movement
    direction[2] = love.keyboard.isScancodeDown('left') and direction[2] or 1.0
  end
end

local time = 0.0
love.update = function(dt)
  time = time + dt -- time is passing, dt is the time passed since the last love.update call.
  if time > 0.5 then -- move twice every second
    position[1] = position[1] + direction[1] * speed -- set the new horizontal position
    position[1] = position[1] + direction[1] * speed -- set the new vertical position
    time = time - 0.5 -- reset the timer so it moves continuously
  end
end

love.draw = function()
  love.graphics.rectangle('fill', position[1], position[2], 10, 10) -- draw the head of the snake as a 10x10 square.
end
The only thing i didn't include is collision detection (including with itself), food, and for the snake to grow if it eats the food, so it's currently a 1-block snake.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 162 guests