// Initializes map void initMap() { // Places the initual head location in middle of map headxpos = row / 2; headypos = col / 2; map[headxpos][headypos] = 1; // Places top and bottom walls for (int x = 0; x < row; ++x) { for (auto y = 0; y < col; y++) { map[x][y] = -1; } } // Places left and right walls //for (int y = 0; y < col; y++) { // map[0 + y * row] = -1; // map[(row - 1) + y * row] = -1; //} // Generates first food generateFood(); }
// Moves snake head to new location void move(int dx, int dy) { // determine new head position int newx = headxpos + dx; int newy = headypos + dy; // Check if there is food at location for (auto i = 0; i < row; i++) { for (auto j = 0; j < col; j++) if (map[newx][newy] == -2) food++; } // Generate new food on map generateFood(); // Check location is free if (map[newx][newy] != 0) { running = false; } // Move head to new location headxpos = newx; headypos = newy; map[headxpos][headypos] = food + 1; }
Game::Game() { generateFood(); user= User(); dead = false; paused = false; score = 1; speed = 0; }
Game::Game(User& u) { generateFood(); user = u; dead = false; paused = false; score = 1; speed = 0; }
int main() { printf("\t\t\t\t****Controls****\n\n"); printf("\tW = Up | A = Left | S = Down | D = Right\n\n"); printf("\t\t\t\t****KNOWN ISSUES****\n\n"); printf("\t As the main functionality has been added already (which was the intent)\n"); printf("\t I didn\'t care to develop it further.\n\n"); printf("\t (1) Game crashes when snake reaches the edge, no boundary \n\t limit has been defined for the snake to crawl.\n"); printf("\n\t (2) No score/level system.\n"); printf("\n\t (3) Food generates around a specific region only, Tweak the\n \t rand() values to get more random locations.\n"); printf("\n\t\t\t\t\t\tGaurav Butola"); printf("\n\t\t\t\t\t\[email protected]"); printf("\n\n\t\t\t\tPress any key to play"); getch(); printf("%c[2J",0x1B); /*Erase the screen*/ /*Show the snake when game starts*/ gotoxy(headPositionX, headPositionY); putchar(snakeHead); int i; for(i=1; i<=bodyLength; i++) { bodyPositionX[i] = headPositionX-i; bodyPositionY[i] = headPositionY; /*Not moving in Y axis*/ gotoxy(bodyPositionX[i], bodyPositionY[i]); putchar(snakeBody); } /*********Start game loop here*******/ while(1) { changemode(1); if(!foodEaten) { generateFood(); foodEaten = 1; } if(kbhit()) { direction = getch(); moveSnake(); } else moveSnake(); } return 0; }
void Game::playGame() { resizeConsole(x_max+1,y_max+1);//game size screen.clear(); drawBoundary(); while(!dead) { displayScore(); if(!paused) { snake.clear(screen); snake.move(); snake.draw(screen); food.draw(screen); Sleep(100-speed); if(snake.hit(x_min,x_max,y_min,y_max)) { dead = true; } else if(snake.getHead().equals(food)) { snake.grow(); generateFood(); harder(); } } if(kbhit()) { getKeys(); } } //endgame(); updateUser(); }
void loadGame(void) { int snakeXY[2][SNAKE_ARRAY_SIZE]; /*two dimensional array*/ int snakeLength = 4; /*initial lenght*/ int direction = LEFT_ARROW; /*initial direction*/ int foodXY[] = {5,5}; int score = 0; int consoleWidth = 80; int consoleHeight = 25; int speed = getGameSpeed(); snakeXY[0][0] = 40; /*starting position of the snake*/ snakeXY[1][0] = 10; loadEnviroment(consoleWidth, consoleHeight); /*borders*/ prepairSnakeArray(snakeXY, snakeLength); loadSnake(snakeXY, snakeLength); generateFood( foodXY, consoleWidth, consoleHeight, snakeXY, snakeLength); refreshInfoBar(score, speed); /*Bottom info bar. Score, Level etc*/ startGame(snakeXY, foodXY, consoleWidth, consoleHeight, snakeLength, direction, score, speed); return; }
/*** MAIN ***/ int main(int argc, const char *argv[]) { unsigned int i, j; unsigned int end = 0; unsigned int foodFlag = 0; unsigned int length = 1; int score = -1, record = 0, delay = 1000; char direction, prevDirection = 'd'; long duration = 0; static struct termios oldt, newt; // Structs to get all the keystrokes directly to stdin struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI }; // Struct to timeout getchar FILE *recordFile = fopen("snake_record.bin", "r+b"); if (recordFile != NULL) { fread(&record, sizeof(int), 1, recordFile); fclose(recordFile); } recordFile = NULL; srand((unsigned int)time(NULL)); // Initialize grid for_i for_j grid[i][j] = EMPTY; for (i = 21; i >= 18; i--, length++) grid[10][i] = length; splashScreen(); // Instructions to get all the keystrokes directly to stdin tcgetattr( STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON); tcsetattr( STDIN_FILENO, TCSANOW, &newt); // Game cycle while (end == 0) { if (foodFlag == 0) { generateFood(); foodFlag = 1; score++; } clearScreen(); printGrid(score, record); do { if (poll(&mypoll, 1, delay)) direction = getchar(); else direction = prevDirection; } while (direction != 'w' && direction != 'a' && direction != 's' && direction != 'd' && direction != 'q'); if (direction == 'q') { puts("\n\nEXIT\n"); exit(EXIT_SUCCESS); } end = turnSnake(direction, end, &foodFlag); prevDirection = direction; duration++; if ((duration % 30) == 0 && delay > 200) delay -= 50; } puts("\n\nGAME OVER!\n"); if (score > record) { recordFile = fopen("snake_record.bin", "wb"); if (recordFile != NULL) { fwrite(&score, sizeof(int), 1, recordFile); fclose(recordFile); } } // Restore normal mode tcsetattr( STDIN_FILENO, TCSANOW, &oldt); }
void SFML::init(size_t width, size_t height) { width *= 10; height *= 10; x = width; y = height; sf::RenderWindow window(sf::VideoMode(width, height), "Nibbler!"); sf::RectangleShape shape(sf::Vector2f((width - 100), (height - 100))); sf::Text text; sf::Text atext; sf::Text food; sf::Text snake; sf::Font font; sf::SoundBuffer soundBuffer; sf::Sound sound; sf::Event Event; std::ostringstream ss; std::list<Snake> snakes; std::list<Snake>::iterator it; snakes.push_front(Snake((x / 2) + 1, (y / 2))); int xSnake = 2; int ySnake = 2; // if(key == 1) // ySnake--; // else if(key == 2) // xSnake++; // else if(key == 3) // ySnake++; // else if(key == 4) // xSnake--; snakes.push_front(Snake(xSnake, ySnake)); shape.setFillColor(sf::Color(0, 0, 0)); shape.setOutlineThickness(5); shape.setOutlineColor(sf::Color(250, 150, 100)); shape.move(50, 50); if (!font.loadFromFile("libs/fonts/arial.ttf")) { throw std::exception(); } text.setFont(font); text.setString("Score: "); text.setCharacterSize(15); text.setColor(sf::Color::Green); text.setStyle(sf::Text::Bold); text.move(50, (height - 30)); if (!soundBuffer.loadFromFile("libs/sounds/sound.wav")) { throw std::exception(); } sound.setBuffer(soundBuffer); // sound.pause(); sound.play(); generateFood(); food.setFont(font); food.setString("X"); food.setCharacterSize(15); food.setColor(sf::Color::Green); food.setStyle(sf::Text::Bold); food.move(food_x, food_y); snake.setFont(font); snake.setString("o"); snake.setCharacterSize(15); snake.setColor(sf::Color::Green); snake.setStyle(sf::Text::Bold); snake.move(food_x, food_y); while (window.isOpen()) { while (window.pollEvent(Event)) { if (Event.type == sf::Event::Closed) window.close(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close(); counter++; ss << counter; atext.setFont(font); atext.setCharacterSize(15); atext.setColor(sf::Color::Green); atext.setStyle(sf::Text::Bold); atext.setString(ss.str()); atext.move(110, (height - 30)); window.clear(); window.draw(shape); window.draw(text); window.draw(atext); window.draw(food); for(it = snakes.begin(); it != snakes.end(); it++) { snake.move((*it).getY(),(*it).getX()); window.draw(snake); } window.display(); } } }