Advertisement
joric

snake.c

Feb 16th, 2024
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. //omg teh rule snake by joric, 2008
  2. //win: cl snake.cpp /link glut32.lib | penguin: gcc snake.cpp -lglut
  3.  
  4. #include <stdlib.h>
  5. #include <GL/glut.h>
  6.  
  7. float zoom = 35.0f;
  8. float rotx = 45.0f;
  9. float roty = 0.001f;
  10. float tx = 0;
  11. float ty = 0;
  12. int lastx = 0;
  13. int lasty = 0;
  14. unsigned char Buttons[3] = { 0 };
  15.  
  16. const int w = 20;
  17. const int h = 20;
  18.  
  19. int data[h][w];
  20.  
  21. #define W 640
  22. #define H 480
  23. #define FOOD -1
  24.  
  25. int cx, cy, dx, dy, timerrate, items, level, length;
  26.  
  27. void display()
  28. {
  29.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30.  
  31.     glLoadIdentity();
  32.  
  33.     glTranslatef(0, 0, -zoom);
  34.     glTranslatef(tx, ty, 0);
  35.     glRotatef(rotx, 1, 0, 0);
  36.     glRotatef(roty, 0, 1, 0);
  37.  
  38.     float c1[] = { 1, 1, 0 };
  39.     float c2[] = { 1, 0, 0 };
  40.  
  41.     glDisable(GL_LIGHTING);
  42.  
  43.     // draw grid
  44.     {
  45.         glColor3f(0.2f, 0.2f, 0.2f);
  46.         glBegin(GL_LINES);
  47.         for (int i = -w / 2; i <= w / 2; ++i)
  48.         {
  49.             glVertex3f(i, -0.5f, -w / 2);
  50.             glVertex3f(i, -0.5f, w / 2);
  51.         }
  52.  
  53.         for (int j = -h / 2; j <= h / 2; ++j)
  54.         {
  55.             glVertex3f(h / 2, -0.5f, j);
  56.             glVertex3f(-h / 2, -0.5f, j);
  57.         }
  58.         glEnd();
  59.     }
  60.  
  61.     glEnable(GL_LIGHTING);
  62.  
  63.     //draw level
  64.     for (int i = 0; i < h; i++)
  65.     {
  66.         for (int j = 0; j < w; j++)
  67.         {
  68.             float x = i - w / 2 + 0.5f;
  69.             float y = j - h / 2 + 0.5f;
  70.  
  71.             glPushMatrix();
  72.             glTranslatef(y, 0, x);
  73.  
  74.             int n = data[i][j];
  75.  
  76.             if (n > 0)
  77.             {
  78.                 glLightfv(GL_LIGHT0, GL_AMBIENT, c1);
  79.                 glLightfv(GL_LIGHT0, GL_DIFFUSE, c1);
  80.             }
  81.  
  82.             if (n == FOOD)
  83.             {
  84.                 glLightfv(GL_LIGHT0, GL_AMBIENT, c2);
  85.                 glLightfv(GL_LIGHT0, GL_DIFFUSE, c2);
  86.             }
  87.  
  88.             if (n != 0)
  89.                 //glutSolidCube(1);
  90.                 glutSolidSphere(0.5, 20, 10);
  91.             //glutSolidTeapot(0.5);
  92.  
  93.             glPopMatrix();
  94.         }
  95.     }
  96.  
  97.     glutSwapBuffers();
  98. }
  99.  
  100. void loadlevel(int level)
  101. {
  102.     for (int i = 0; i < h; i++)
  103.     {
  104.         for (int j = 0; j < w; j++)
  105.         {
  106.             data[i][j] = 0;
  107.         }
  108.     }
  109. }
  110.  
  111. void reset()
  112. {
  113.     level = 1;
  114.     loadlevel(level);
  115.     length = 1;
  116.     items = 0;
  117.     cx = w / 2;
  118.     cy = h / 2;
  119.     dx = 0;
  120.     dy = 0;
  121.     timerrate = 125;
  122. }
  123.  
  124. void tick(int value)
  125. {
  126.     cx += dx;
  127.     cy += dy;
  128.  
  129.     if (cx >= w)
  130.         cx = 0;
  131.     if (cx < 0)
  132.         cx = w - 1;
  133.  
  134.     if (cy >= h)
  135.         cy = 0;
  136.     if (cy < 0)
  137.         cy = h - 1;
  138.  
  139.     for (int i = 0; i < h; i++)
  140.     {
  141.         for (int j = 0; j < w; j++)
  142.         {
  143.             if (data[i][j] > 0)
  144.                 data[i][j]--;
  145.         }
  146.     }
  147.  
  148.     if (data[cy][cx] == FOOD)
  149.     {
  150.         length++;
  151.         items--;
  152.     }
  153.     else if (data[cy][cx] != 0 && (dx != 0 || dy != 0))
  154.     {
  155.         reset();
  156.     }
  157.  
  158.     data[cy][cx] = length;
  159.  
  160.     if (items == 0)
  161.     {
  162.         int x, y;
  163.         int i = 0;
  164.         do
  165.         {
  166.             x = rand() % w;
  167.             y = rand() % h;
  168.             i++;
  169.         }
  170.         while (data[y][x] != 0 && i < 5);
  171.  
  172.         data[y][x] = FOOD;
  173.  
  174.         items++;
  175.     }
  176.  
  177.     glutTimerFunc(timerrate, tick, 1);
  178.     glutPostRedisplay();
  179. }
  180.  
  181. void reshape(int w, int h)
  182. {
  183.     if (w == 0)
  184.         h = 1;
  185.  
  186.     glViewport(0, 0, w, h);
  187.     glMatrixMode(GL_PROJECTION);
  188.     glLoadIdentity();
  189.     gluPerspective(45, (float) w / h, 0.1, 100);
  190.     glMatrixMode(GL_MODELVIEW);
  191.     glLoadIdentity();
  192. }
  193.  
  194. void motion(int x, int y)
  195. {
  196.     int diffx = x - lastx;
  197.     int diffy = y - lasty;
  198.     lastx = x;
  199.     lasty = y;
  200.  
  201.     if (Buttons[2])
  202.     {
  203.         zoom -= (float) 0.05f * (diffx - diffy);
  204.     }
  205.     else if (Buttons[0])
  206.     {
  207.         rotx += (float) 0.5f * diffy;
  208.         roty += (float) 0.5f * diffx;
  209.     }
  210.     else if (Buttons[1])
  211.     {
  212.         tx += (float) 0.05f * diffx;
  213.         ty -= (float) 0.05f * diffy;
  214.     }
  215.     glutPostRedisplay();
  216. }
  217.  
  218. void keyboard(unsigned char key, int x, int y)
  219. {
  220.     switch (key)
  221.     {
  222.         case 27:
  223.             exit(0);
  224.             break;
  225.     }
  226. }
  227.  
  228. void special(int key, int x, int y) // Create Special Function (required for arrow keys)
  229. {
  230.     switch (key)
  231.     {
  232.         case GLUT_KEY_UP:
  233.             dx = 0;
  234.             dy = -1;
  235.             break;
  236.         case GLUT_KEY_DOWN:
  237.             dx = 0;
  238.             dy = 1;
  239.             break;
  240.         case GLUT_KEY_LEFT:
  241.             dx = -1;
  242.             dy = 0;
  243.             break;
  244.         case GLUT_KEY_RIGHT:
  245.             dx = 1;
  246.             dy = 0;
  247.             break;
  248.     }
  249. }
  250.  
  251. void mouse(int b, int s, int x, int y)
  252. {
  253.     lastx = x;
  254.     lasty = y;
  255.     switch (b)
  256.     {
  257.         case GLUT_LEFT_BUTTON:
  258.             Buttons[0] = ((GLUT_DOWN == s) ? 1 : 0);
  259.             break;
  260.         case GLUT_MIDDLE_BUTTON:
  261.             Buttons[1] = ((GLUT_DOWN == s) ? 1 : 0);
  262.             break;
  263.         case GLUT_RIGHT_BUTTON:
  264.  
  265.             Buttons[2] = ((GLUT_DOWN == s) ? 1 : 0);
  266.             break;
  267.     }
  268.     glutPostRedisplay();
  269. }
  270.  
  271. int main(int argc, char **argv)
  272. {
  273.     glutInit(&argc, argv);
  274.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  275.     glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - W) / 2, (glutGet(GLUT_SCREEN_HEIGHT) - H) / 2);
  276.     glutInitWindowSize(W, H);
  277.     glutCreateWindow(argv[0]);
  278.     glutDisplayFunc(display);
  279.     glutReshapeFunc(reshape);
  280.     glutKeyboardFunc(keyboard);
  281.     glutSpecialFunc(special);
  282.     glutMouseFunc(mouse);
  283.     glutMotionFunc(motion);
  284.     reset();
  285.     glEnable(GL_DEPTH_TEST);
  286.     glShadeModel(GL_SMOOTH);
  287.     glEnable(GL_LIGHTING);
  288.     glEnable(GL_LIGHT0);
  289.     glutTimerFunc(timerrate, tick, 1);
  290.     glutMainLoop();
  291.     return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement