//returns a pointer to an array of Vertices. Takes the number of Vertices to be created, //the max dimensions of the board and the margin on the sides of the board as arguments Vertex* createVertices(int num, int max_X, int max_Y, int margin){ Vertex *tmp = NULL; if (num < 2){ return NULL; } tmp = (Vertex *)malloc(sizeof(Vertex)*num); if (tmp == NULL){ return tmp; } time_t t; int dis = 1; srand((unsigned)time(&t)); tmp[0].locked = 2; tmp[0].x = rand() % 50 + margin; tmp[0].y = rand() % 50 + 2 * margin; tmp[0].edgeInd = 0; while (dis){ tmp[1].locked = 2; tmp[1].x = max_X - rand() % 50 - margin; tmp[1].y = max_Y - rand() % 50 - margin; tmp[1].edgeInd = num - 1; dis = tooClose(0, 1, tmp); } for (int i = 2; i < num; i++) { dis = 1; while (dis){ tmp[i].locked = 0; tmp[i].x = (max_X - margin) - (rand() % (max_X - 2 * margin)); tmp[i].y = (max_Y - margin) - (rand() % (max_Y - 2 * margin)); tmp[i].edgeInd = i - 1; for (int j = 0; j < i; j++){ dis = tooClose(j, i, tmp); if (dis){ j=i; } } } } return tmp; }
int Game::gameLoop() { Object ScoreDisplay(0,0); int displayScore = 0; int moveEnemy = 0; int movePlayer = 0; int oatpoints = oatWorth; int moveScoreUp = 0; int nextRandomMove = 0; int lastScore = 0; int nextLevel = 0; int rMove = 0; bool done = false; while(!done) { ticks = GetTickCount(); render(); if (nextRandomMove==100) { srand(ticks); for(int i = 2; i < objects; object[i++].setDirection(0)); nextRandomMove=0; } if (nextLevel > 1000) { levelUp(); nextLevel -= 1000; } // input and output of user and enemies if (movePlayer >= playerSlowD) { object[0].getInput(); movePlayer = 0; } if (moveEnemy >= enemySlowD) { for (int i = 2; i < objects; i++) { if(!tooClose(object[0], object[i])) object[i].randMove(rand()%5); } moveEnemy = 0; } for (int i = 0; i < objects; testCollision(object[i++])); for (int i = 2; i < objects && !done; done = contact(object[0],object[i++])); // check if player ate oat if (contact(object[0],object[1])) { ScoreDisplay.x() = object[1].x(); ScoreDisplay.y() = object[1].y(); randLocation(object[1]); score+=oatpoints; nextLevel += oatpoints; if (oatWorth > MAXOATWORTH) console.alarm(); lastScore = oatpoints; oatpoints = oatWorth; } // display score of oat just eaten if (lastScore > 0 && moveScoreUp < showScorexln) { if (displayScore < showScore1ln) { console.setPos(ScoreDisplay.y(), ScoreDisplay.x()); std::cout << lastScore; displayScore++; } else { ScoreDisplay.x()++; ScoreDisplay.y()--; displayScore = 0; moveScoreUp++; } } else { moveScoreUp = 0; lastScore = 0; } // change counter values if (oatpoints > 0) oatpoints--; else { randLocation(object[1]); oatpoints = oatWorth; } moveEnemy++; movePlayer++; nextRandomMove++; // regulate fps if (1000/FPS > GetTickCount()-ticks) { Sleep(1000/FPS-(GetTickCount()-ticks)); } } return score; }