这个代码主要运用链表来表示蛇和模拟蛇行动,编译一下就能用。。。。
#include #include #include #include #include #include #include #include #include #include using namespace std; int TILE_WIDTH = 32; int TILE_HEIGHT = 32; const int TILE_COLUMN = 20; const int TILE_ROW = 20; const int dt = 33; #define DIR_UP 0 #define DIR_DOWN 1 #define DIR_LEFT 2 #define DIR_RIGHT 3 #define GL_PI 3.1415f int dir = DIR_RIGHT; int flag = 1; GLint gg = 0; GLint kk = 0; int aa, bb; int x, y, yes = 0; int Dir=0; float snake_x = 0; float snake_y = 0; float snake_v = 3; float food_x = 10; float food_y = 5; int time = 300; struct snake { int head_x; int head_y; struct snake *next; }; snake *h = new snake; map , int> m; map< vector, int>::iterator iter; void ChangeSize(int w, int h) { GLfloat nRange = 100.0f; if (h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, TILE_COLUMN*TILE_WIDTH, TILE_ROW*TILE_HEIGHT, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void SetupRC() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glColor3f(0.0f, 1.0, 0.0f); } void snake_M() { snake *head; snake *node; head = h; head->head_x = 2; head->head_y = 0; for (int i = 1; i >=0; i--) { node = new snake; node->head_x = i; node->head_y = 0; head->next = node; head = node; } node->next = NULL; vector s; s.push_back(2); s.push_back(0); m[s] = 1; s.clear(); s.push_back(1); s.push_back(0); m[s] = 1; s.clear(); s.push_back(0); s.push_back(0); m[s] = 1; return; } void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glPointSize(TILE_HEIGHT); glColor3f(0.0f, 0.0, 1.0f); glBegin(GL_POINTS); if (flag) { glVertex3f((TILE_WIDTH)*(h->head_x) + (TILE_WIDTH / 2), (TILE_HEIGHT)*(h->head_y) + (TILE_HEIGHT / 2), 0.0f); snake *hh; hh = h->next; glColor3f(0.0f, 1.0f, 0.0f); while (1) { glVertex3f((TILE_WIDTH)*(hh->head_x) + (TILE_WIDTH / 2), (TILE_HEIGHT)*(hh->head_y) + (TILE_HEIGHT / 2), 0.0f); if (hh->next == NULL) { if (yes) { int ans = 0; snake *ss = new snake; ss->head_x = x; ss->head_y = y; hh->next = ss; ss->next = NULL; glVertex3f((TILE_WIDTH)*(ss->head_x) + (TILE_WIDTH / 2), (TILE_HEIGHT)*(ss->head_y) + (TILE_HEIGHT / 2), 0.0f); yes = 0; } break; } hh = hh->next; } glColor3f(1.0f, 0.0f, 0.0f); glVertex3f((TILE_WIDTH)*aa + (TILE_WIDTH / 2), (TILE_HEIGHT)*bb + (TILE_HEIGHT / 2), 0.0f); glEnd(); } else { glPointSize(1.0f); glClear(GL_COLOR_BUFFER_BIT); glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glBegin(GL_TRIANGLE_FAN); glColor3f(1.0f, 1.0f, 1.0f); glVertex2f(0.0f, 0.0f); for (int i = 0; i <= 8; i++) { glColor3f(i & 0x04, i & 0x02, i & 0x01); glVertex2f(cos(i*GL_PI / 4), sin(i*GL_PI / 4)); } glEnd(); glFlush(); } glPopMatrix(); glutSwapBuffers(); } void processNormalKeys(int key, int x, int y) { switch (key) { case GLUT_KEY_RIGHT: { Dir = 3; break; } case GLUT_KEY_LEFT: { Dir = 4; break; } case GLUT_KEY_UP: { Dir = 2; break; } case GLUT_KEY_DOWN: { Dir = 1; break; } } return; } void timerFunction(int value) { snake *Z; int xx, yy; if (Dir != 0) { int ans = 1; Z = h->next; x = h->head_x; y = h->head_y; while (1) { xx = Z->head_x; yy = Z->head_y; Z->head_x = x; Z->head_y = y; x = xx; y = yy; Z = Z->next; if (Z == NULL) break; } } vector s2; s2.push_back(x); s2.push_back(y); switch (Dir) { case 0: { break; } case 1: { h->head_y++; if (h->head_y == 20) h->head_y = 0; break; } case 2: { h->head_y--; if (h->head_y == -1) h->head_y = 19; break; } case 3: { h->head_x++; if (h->head_x == 20) h->head_x = 0; break; } case 4: { h->head_x--; if (h->head_x == -1) h->head_x = 19; break; } } if (h->head_x == aa && h->head_y == bb) { if(time!=50) time -= 10; aa = rand() % 19; bb = rand() % 19; vectors5; s5.push_back(aa); s5.push_back(bb); iter = m.find(s5); while (iter != m.end()) { aa = rand() % 19; bb = rand() % 19; s5.clear(); s5.push_back(aa); s5.push_back(bb); iter = m.find(s5); } yes = 1; } else { if (Dir != 0) { iter = m.find(s2); if (iter != m.end()) m.erase(iter); } } vectors1; s1.push_back(h->head_x); s1.push_back(h->head_y); iter = m.find(s1); if (iter != m.end()&&Dir!=0) { flag = 0; } m[s1] = 1; glutPostRedisplay(); if(flag) glutTimerFunc(time, timerFunction, 1); } int main(int argc, char* argv[]) { aa = rand()%19; bb = rand()%19; snake_M(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(TILE_WIDTH*TILE_COLUMN, TILE_HEIGHT*TILE_ROW); glutCreateWindow("Snake"); glutReshapeFunc(ChangeSize); glutDisplayFunc(&RenderScene); glutSpecialFunc(processNormalKeys); glutTimerFunc(time, timerFunction, 1); //SetupRC(); glutMainLoop(); // pair p; }