Coding Snake in Functional JavaScript (Part 1)

preview_player
Показать описание

Source code:

Let me know if you want me to do other games! :)
Рекомендации по теме
Комментарии
Автор

For the draw function, you can pass in canvas by using an IIFE.
const draw = ((ctx) => () => { ctx.fill() })(canvas.getContext('2d'))
This avoids creating a global ctx variable, while still only getting the context once.

Glinkis
Автор

You are at another level, over the top!!!!

eugeniogonzato
Автор

Stupid question here. what's
const step = t1 => t2 => {
}

I didn't quite get that? It doesn't seem like a function with two parameters like: const step = (t1, t2) => {}, It seems like a function step() that accepts t1 but then returns another function t2 which returns {...}. What's that for? What sort of topics should I look into to understand this?

yhevrah
Автор

12:05 and that's why you don't have magic numbers

programmingloop
Автор

I think only Left and Right should be available. These moves are relative to the current direction, not absolute.

christernilsson
Автор

Hey.. I noticed you have a setting on vim that expands the current split window.. could you post a link to your vimrc when you have time?

bobbydrake
Автор

I'm surely missing something, but much of this makes the code far less readable for me, and it looks like art for art's sake. For example, I don't get why having functions to calculate position ('position helpers') is better than having a constant like TILE_SIZE = canvas.width / cols and calculating the actual position by simple multiplication, like p.x * TILE_SIZE. It still only deals with the graphics, but is much, much simpler to read - you immediately know what it is doing, because it's made for an average human to comprehend, and you don't have to spend time to de-obfuscate this whole x = r => blah contraption.

matzieq
Автор

Functional programming is good in this case but what about large games or apps which are 99% of the apps in reality.
Imagine you are creating counter-strike with simple graphics.
Each human, each room, each tree and guns structures will be created on every frame even if they dont need to be recalculated which will be very slow if you have many item, which is often true.
How to handle games in this case.

NM-qwbm
Автор

maybe off topic but i did not get how you "faked" the require/module.Export thing, do you have a resource about it? Or is it just useless, like, js loads all the files whatsoever so everything is global and if so, i don't get the point

lutaseb
Автор

I cannot understand the code.... Still a lot to learn...

zhiyizhu
Автор

Part 2 will be released in a week, if not earlier :)

ChristopherOkhravi