Advertisement
a3f

Snake - Part I

a3f
Mar 8th, 2014
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <windows.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. #define LINES 25
  8. #define COLS 80
  9.  
  10. #define fps 10
  11. int score;
  12. char canvas[LINES][COLS];
  13. char *buffer = (char *) &canvas;
  14.  
  15. void drawCanvas()
  16. {
  17.     system("cls");
  18.     puts(buffer);
  19.     printf("SCORE: %d", score);
  20. }
  21. bool isValid(int _sa6r, int _3amood)
  22. {
  23.     if (_sa6r > LINES || _sa6r < 0 || _3amood > COLS || _sa6r < 0)
  24.         return false;
  25.     return true;
  26. }
  27. bool setElem(int _sa6r, int _3amood, char _7arf)
  28. {
  29.     if (isValid(_sa6r, _3amood))
  30.     {
  31.         canvas[_sa6r][_3amood] = _7arf;
  32.         return true;
  33.     }
  34.     return false;
  35.  
  36. }
  37. char getElem(int _sa6r, int _3amood)
  38. {
  39.     if (isValid(_sa6r, _3amood))
  40.         return canvas[_sa6r][_3amood];
  41.     return '\0';
  42. }
  43.  
  44. enum {snake = '$', apple = 'o', box = 'X'};
  45. int main (void)
  46. {
  47.     memset(buffer, ' ', sizeof canvas - 1);
  48.     for (int i = 0; i < 100; i++)
  49.     {
  50.         if (!setElem(i, i, 'X')) break;
  51.         drawCanvas();
  52.         score += 10;
  53.         Sleep(1000 / fps);
  54.         setElem(i, i, ' ');
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement