Looking at the respond function, right now, I want to make the enemy move randomly around my 2D screen, but currently the problem is that the enemy is placing at my screen randomly instead of moving randomly around openGL screen. Help me what is wrong with my codes, how should I do it to make the enemy move randomly. All my codes worked perfectly expect this.Code:#include <windows.h> #include <iostream> #include "gl/freeglut.h" #include "myenigne.h" #include "time.h" #include "stdlib.h" #include "math.h" //#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) using namespace std; enum STATE { SLEEPING, SEARCHING, EATING, ESCAPING, DIE, }; bool sleeping ;//= true; bool searching ;//= false; bool eating ;//= false; bool startgame = true; bool full_screen; bool blend; bool dead; bool myKeys[255]; int translate = 0; int move = 0; int dogMove2 = 0; int dogMove = 0; int move2 = 0; float random(void) { return (float) (rand() % 1000 - 500) /500; //int n = rand() % 200; //return n; }; void *font_style; void printw (float x, float y, float z, char* format, ...) { va_list args; // Variable argument list int len; // String length int i; // Iterator char * text; // Text // Initialize a variable argument list va_start(args, format); // Return the number of characters in the string referenced the list of arguments. // _vscprintf doesn't count terminating '\0' (that's why +1) len = _vscprintf(format, args) + 1; // Allocate memory for a string of the specified size text = (char *)malloc(len * sizeof(char)); // Write formatted output using a pointer to the list of arguments vsprintf_s(text, len, format, args); // End using variable argument list va_end(args); // Specify the raster position for pixel operations. glRasterPos3f (x, y, z); // Draw the characters one by one for (i = 0; text[i] != '\0'; i++) glutBitmapCharacter(font_style, text[i]); // Free the allocated memory for the string free(text); } void drawBar() { glColor3f(1, 0, 1); glPushMatrix(); glTranslatef(200.0,0.0,0.0); glBegin(GL_QUADS); glVertex2f(0, 15); glVertex2f(0, 35); glVertex2f(400, 35); glVertex2f(400, 15); glEnd(); glPopMatrix(); } void drawGround() { glColor3f(1, 1, 1); glEnable(GL_TEXTURE_2D); // Draw Background image //glPushMatrix(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, textures[0].id ); //glPushMatrix(); glBegin(GL_QUADS); int height = 100 * 1.333/1.5; glTexCoord2f(0,0); glVertex2f(0,600); glTexCoord2f(1,0); glVertex2f(800,600); glTexCoord2f(1,1); glVertex2f(800,0); glTexCoord2f(0,1); glVertex2f(0,0); glEnd(); //glPopMatrix(); glDisable(GL_BLEND); //glPopMatrix(); glDisable(GL_TEXTURE_2D); } void drawBone() { glColor3f(1, 1, 1); glEnable(GL_TEXTURE_2D); // Draw Background image //glPushMatrix(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, textures[1].id ); //glPushMatrix(); glBegin(GL_QUADS); int height = 100 * 1.333/1.5; glTexCoord2f(0,0); glVertex2f(0,25); glTexCoord2f(1,0); glVertex2f(50,25); glTexCoord2f(1,1); glVertex2f(50,0); glTexCoord2f(0,1); glVertex2f(0,0); glEnd(); //glPopMatrix(); glDisable(GL_BLEND); //glPopMatrix(); glDisable(GL_TEXTURE_2D); } void drawPoints() { glColor3f(1, 1, 1); glPushMatrix(); glTranslatef(200.0,0.0,0.0); glBegin(GL_QUADS); glVertex2f(0, 15); glVertex2f(0, 35); glVertex2f(20, 35); glVertex2f(20, 15); glEnd(); glPopMatrix(); } void drawDog() { glColor3f(1, 1, 1); glEnable(GL_TEXTURE_2D); //glPushMatrix(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, textures[2].id ); //glPushMatrix(); glBegin(GL_QUADS); //int height = 100 * 1.333/1.5; glTexCoord2f(0,0); glVertex2f(0 + dogMove,50 + dogMove2 ); glTexCoord2f(1,0); glVertex2f(50 + dogMove,50 + dogMove2 ); glTexCoord2f(1,1); glVertex2f(50+ dogMove,0 + dogMove2 ); glTexCoord2f(0,1); glVertex2f(0+ dogMove,0 + dogMove2 ); glEnd(); //glPopMatrix(); glDisable(GL_BLEND); //glPopMatrix(); glDisable(GL_TEXTURE_2D); } void drawSleepingDog() { glColor3f(1, 1, 1); glEnable(GL_TEXTURE_2D); //glPushMatrix(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, textures[4].id ); //glPushMatrix(); glBegin(GL_QUADS); //int height = 100 * 1.333/1.5; glTexCoord2f(0,0); glVertex2f(0 ,50); glTexCoord2f(1,0); glVertex2f(50,50 ); glTexCoord2f(1,1); glVertex2f(50,0 ); glTexCoord2f(0,1); glVertex2f(0,0 ); glEnd(); //glPopMatrix(); glDisable(GL_BLEND); //glPopMatrix(); glDisable(GL_TEXTURE_2D); } void drawEnemy() { glColor3f(1, 1, 1); glEnable(GL_TEXTURE_2D); //glPushMatrix(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, textures[3].id ); //glPushMatrix(); glBegin(GL_QUADS); //int height = 100 * 1.333/1.5; glTexCoord2f(0,0); glVertex2f(100,150); glTexCoord2f(1,0); glVertex2f(200,150); glTexCoord2f(1,1); glVertex2f(200,100); glTexCoord2f(0,1); glVertex2f(100,100); //glTexCoord2f(0,0); glVertex2f(rand(),rand()); //glTexCoord2f(1,0); glVertex2f(rand(),rand()); //glTexCoord2f(1,1); glVertex2f(rand(),rand()); //glTexCoord2f(0,1); glVertex2f(rand(),rand()); glEnd(); //glPopMatrix(); glDisable(GL_BLEND); //glPopMatrix(); glDisable(GL_TEXTURE_2D); } void ChangeState() { if(startgame == true) { searching = false; eating = false; sleeping = false; } if(sleeping == true) { searching = false; eating = false; } if(searching == true) { sleeping = false; eating = false; } if(eating == true) { sleeping = true; searching = false; } } void Respond() { if(startgame == true) { glColor3f(0.5, 0, 1); glPushMatrix(); glTranslatef(200.0,0.0,0.0); glBegin(GL_QUADS); glVertex2f(100, 225); glVertex2f(100, 275); glVertex2f(250, 275); glVertex2f(250, 225); glEnd(); glPopMatrix(); glColor3f(0, 1, 1); printw(325.0, 250.0, 0,"Start Game"); } static int bonex = rand() % 750; static int boney = rand() % 550; static int enemyx = rand() % 750; static int enemyy = rand() % 550; if(sleeping == true) { drawBar(); glColor3f( 1.0f, 1.0f, 1.0f); printw (325.0, 33.0, 0, "Dog is sleeping"); glPushMatrix(); glTranslatef(move, 0, 0); drawPoints(); glPopMatrix(); //sleeping = false; //searching = true; drawSleepingDog(); //cout << searching << endl; //cout << sleeping << endl; } if(searching == true) { glColor3f( 1.0f, 1.0f, 1.0f); printw (325.0, 33.0, 0, "Dog is moving"); printw(600.0, 35.0, 0, "Be careful of the enemy"); glPushMatrix(); glTranslatef(bonex, boney, 0); drawBone(); glPopMatrix(); glPushMatrix(); glTranslatef(rand() % 750 , rand() % 550, 0); drawEnemy(); glPopMatrix(); drawDog(); //searching = false; //sleeping = true; //cout << searching << endl; //cout << sleeping << endl; } } void RunFSM() { } void Update() { } static void Timer(int value){ if(translate < 360) { move += 5.0; translate += 5; } else { //translate = 0.0; move = 0.0; } // 100 milliseconds glutTimerFunc(100, Timer, 0); } void RenderScene() { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0, 800 , 600, 0, -1, 1 ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW ) drawGround(); //drawDog(); Respond(); ChangeState(); glutSwapBuffers (); glutPostRedisplay (); } void Resize( int width, int height ) { glViewport ( 0, 0, width, height ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45, (float)(width)/(float)(height), 0.1f, 1000 ); glMatrixMode ( GL_MODELVIEW ); glLoadIdentity (); } void NormalKeys( unsigned char key, int x, int y ) { switch ( key ) { case 27 : glutLeaveMainLoop(); break; case 'd' : case 'D' : if(searching) { if(dogMove < 750) { glPushMatrix(); //glLoadIdentity(); //glTranslatef(dogMove, 0, 0); dogMove+= 5; drawDog(); glPopMatrix(); cout << dogMove << endl; } else { dogMove += 0; } } break; case 'a': case 'A': if(searching) { if(dogMove > 0) { glPushMatrix(); //glTranslatef(dogMove--, 0, 0); dogMove-= 5; drawDog(); glPopMatrix(); cout << dogMove << endl; } else { dogMove += 0; } } break; case 'w': case 'W': if(searching) { if(dogMove2 > 0) { glPushMatrix(); //glTranslatef(0, dogMove2--, 0); dogMove2 -= 5; drawDog(); glPopMatrix(); cout << dogMove2 << endl; } else { dogMove += 0; } } break; case 'S': case 's': if(searching) { if(dogMove2 < 550) { glPushMatrix(); //glTranslatef(0, dogMove2++, 0); dogMove2 += 5; drawDog(); glPopMatrix(); cout << dogMove2 << endl; } else { dogMove2 += 0; } } break; case'p': sleeping = true; searching = false; startgame = false; break; case'o': searching = true; sleeping = false; startgame = false; break; //} default : break; } } void SpecialKeys( int key, int x, int y ) { switch ( key ) { case GLUT_KEY_UP : break ; case GLUT_KEY_DOWN : break ; case GLUT_KEY_LEFT : break ; case GLUT_KEY_RIGHT : break ; case GLUT_KEY_F1 : blend = !blend; break ; case GLUT_KEY_F2 : break ; default: break; } //glutPostRedisplay(); } void ProcessMouseClick( int button, int state, int x, int y ) { switch( button ) { case GLUT_LEFT_BUTTON : break; case GLUT_RIGHT_BUTTON : break; case GLUT_MIDDLE_BUTTON : break; } } void ProcessMouseMove( int x, int y ) {} void InitApp() { full_screen = false; } void DeinitApp(){} void InitGL() { glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glShadeModel( GL_SMOOTH ); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); LoadTGA( &textures[ 0 ], "http://cboard.cprogramming.com/images/grass.tga", false ); LoadTGA( &textures[ 1 ], "http://cboard.cprogramming.com/images/bone.tga", false ); LoadTGA( &textures[ 2 ], "http://cboard.cprogramming.com/images/dog.tga", false ); LoadTGA( &textures[ 3 ], "http://cboard.cprogramming.com/images/enemy.tga", false ); LoadTGA( &textures[ 4 ], "http://cboard.cprogramming.com/images/sleep.tga", false); font_style = GLUT_BITMAP_8_BY_13; } int main( int argc, char *argv[] ) { //glutSwapBuffers(); glutInit( &argc, argv ); InitApp(); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); glutInitWindowSize( 800, 600 ); if (full_screen) { glutGameModeString("800x600:16@60"); glutEnterGameMode(); } else glutCreateWindow( "Framework" ); InitGL(); glutIdleFunc( RenderScene ); glutReshapeFunc( Resize ); glutDisplayFunc( RenderScene ); glutKeyboardFunc( NormalKeys ); glutSpecialFunc( SpecialKeys ); glutMouseFunc( ProcessMouseClick ); glutPassiveMotionFunc( ProcessMouseMove ); srand( (unsigned)time( NULL ) ); Timer(0); //Update(); glutMainLoop(); return 0; }