Make sure you have installed ncurses library!
$ make all
$ ./snake
Default size of field is 15x10, can be redefined in 'snake.h' file. You can pass 2 integers as arguments to program, they will be used as new width/height (min is 5x5).
Use arrows or wasd to move, space or 'p' for pause, and 'q' for quit.
Field is a 2-dim array of enum e_cell. Its elements can be: EMPTY, EDGE, BODY, HEAD, FOOD. They used to determine what symbol will be used to represent the field. I used ' ', '#', 'o', '0' and '@'.
Snake is a linked list (data is x and y). Each time we want to move, we push_front a new element with updated coordinates. Then we delete last element of list, and don't do it, if new coordinates point to FOOD.
Game is over when snake's head encounters its BODY part or field's border (EDGE). When food is eaten, speed of game increases.
Main loop content:
- Get user's input.
- If it's quit key, then quit, if it's pause, swap pause flag.
- If game is not over and not paused:
- Check if user wants to move.
- If he does, check if he wants to move opposite direction. Don't move if he does.
- Pass move direction to snake. It can be no-move.
- Snake handles movement command, and returns result, if it's eaten FOOD, or smashed into EDGE or itself, or move was ok.
- Perform auto-move command. Same function as user-move, but using direction from previous moving command. Auto-move proceeds every specific time interval. Default - decreases from 1 sec to 300 ms whenever FOOD is eaten.
- Check if we got some FOOD. Increase length counter, update snake's move result. Place new food to field (get random value from 0 to EMPTY count, and place it there). Game over (win) if we have no space to place FOOD.
- Check if game over (smashed), change flag. Update previous input.
- Display field and snake on the screen.
- Clear field from snake, keep food. We need this to remove tail.
- Update field with snake's coordinates (loop over whole body).
- Show field's array on the screen, by char. May be I can add some colors here.
- Show additional info.
Feel free to email me if you have some questions. This project had been done for educational purposes, I don't mind if you want to use any part of this code.