Don't wanna be here? Send us removal request.
Link
1 note
·
View note
Link
0 notes
Text
Snakes
segmentation
I did not invent Snake. Neither did Google, or Neave, or Nokia. I’m not even sure who it was. But it’s such a simple game that I recently coded my own.
If you’re somehow not familiar with Snake, basically, you control a uh… snake, slithering around eating apples while avoiding the walls (and yourself!). That means each grid cell is one of four things: snake, apple, collision, or empty.
In programming, there’s a concept I’m probably getting wrong called “control flow,” which essentially describes the path your code follows as it executes. I’ve found that separating stuff into standalone functions with descriptive names makes it easy to follow that control flow.
For example, every time the snake advances into the next grid cell, the code executes a function called isCollision(x, y) that checks if the cell at those coordinates is outside the grid… or already contains the snake.
Once I defined that function, I realized it could be put to work in another spot. After the snake eats an apple, a new one is generated, but this also can’t be placed outside the grid or in a snake cell. So isCollision(x, y) becomes the condition of a do{} while() loop that randomly generates the new apple’s coordinates.
So even without comments, it’s clear what both of these functions do. Just like the snake itself, this segmentation is great for scaling.
tl;dr:Â split Javascript code into short functions with descriptive names and variables;Â be clear and consistent with CSS classes
project:Â snakeSnacker
131 notes
·
View notes
Text
The moment I intend to break up with her but still need to rehearse
0 notes
Photo
A Revolutionary Platform of IT SERVICES Is Releasing Soon!
268 notes
·
View notes