Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- //Binary logic
- #define TRUE 1
- #define FALSE 0
- //Snake directions
- #define STOP 0 //snake is not moving (in the beginning or when game is paused)
- #define LEFT 1 //snake is moving left
- #define RIGHT 2 //snake is moving right
- #define UP 3 //snake is moving up
- #define DOWN 4 //snake is moving down
- //Snake, Fruit, Boundary
- #define SNAKE_HEAD "O" //printf(SNAKE_HEAD) will draw the snake head
- #define SNAKE_TAIL "x" //printf(SNAKE_TAIL) will draw a segment of the snake tail
- #define FRUIT "F" //printf(FRUIT) will draw the fruit
- #define MOUSE "M" //printf(FRUIT) will draw the fruit
- #define BOUNDARY "#" //printf(BOUNDARY) will draw the boundary
- #define SPACE " " //printf(SPACE) will draw an empty space
- //Escape key
- #define ESC_KEY 27 //ASCII code for escape ESC key
- // global variables
- int gameOver; //to indicate game over
- int mapWidth = 20; //width of map
- int mapHeight = 20; //height of map
- int snakeHeadX, snakeHeadY; //X, Y coordinates of snake head
- int fruitX, fruitY; //X, Y coordinates of fruit
- int mouseX, mouseY; //X, Y coordinates of fruit
- int dir; //direction of snake
- int tailX[400], tailY[400]; //snake tail X Y coordinates arrays ; maximum length of snake is 400
- int tailLength; //length of snake tail
- int lives = 0;
- bool gameclose;
- int score;
- int scoremod;
- bool pause;
- // function prototypes
- void displayWelcome(); //display welcome screen with welcome message and instructions
- void initializeGame(); //initialize the snake game
- void drawScreen(); //draw display screen with map boundary, snake and fruit
- void readInput(); //read input from keyboard
- void updateSnake(); //update snake head and tail positions to move by one step
- void checkConditions(); //check if snake hit boundary, bites its own tail or eats the fruit
- void displayGameOver(); //display game over screen
- void main()
- {
- gameclose = false;
- pause = false;
- while (!gameclose)
- {
- system("cls");
- fseek(stdin, 0, SEEK_END);
- displayWelcome(); //display welcome screen with welcome message and instructions
- initializeGame(); //initialize the snake game
- while (!gameOver) { //if not yet game over
- if (pause)
- {
- if (getch() == 'p')
- pause = false;
- continue;
- }
- drawScreen(); //draw display screen with map boundary, snake and fruit
- readInput(); //read input from keyboard
- updateSnake();//update snake head and tail positions to move by one step
- checkConditions();//check if snake hit boundary, bites its own tail or eats the fruit
- Sleep(100); //delay by 50ms to slow down the snake's movement
- }
- displayGameOver(); //display game over screen
- }
- }
- void displayWelcome()
- {
- // Part 1 : Complete the welcome screen
- printf("\n\t\t\t\t\t Welcome to snake game\n\n\n");
- printf("\t\t Control the snake to eat the fruits that appear at random locations\n\n");
- printf("\n\n\t\t\t The snake will grow longer when it has eaten a fruit");
- printf("\n\n\n\n\t\t\t\t\t GOOD LUCK!!!\n\n\n");
- printf("Directions:\n");
- printf("Left- Press'a'\n");
- printf("Right- Press'd'\n");
- printf("Up- Press'w'\n");
- printf("Down- Press's'\n");
- printf("Quit- Press'Esc'\n\n");
- printf("Press any key to start.");
- fseek(stdin, 0, SEEK_END);
- while (true)
- {
- if(getch())
- {
- break;
- }
- }
- Sleep(1000);
- fseek(stdin, 0, SEEK_END);
- }
- void initializeGame() {
- gameOver = FALSE; // gameOver is false at beginning of the game
- srand(NULL); // initialize seed for random number generator
- dir = STOP; // snake has not yet started moving
- // Part 2: initialize the X and Y coordinates of snake head and fruit
- // place the head of the snake at the centre of the map at the beginning of the game
- // use variables : snakeX, snakeY
- snakeHeadX = mapWidth / 2;
- snakeHeadY = mapHeight / 2;
- score = 0;
- scoremod = 5;
- lives = 3;
- // place the fruit at random location inside the map
- // use variables : fruitX, fruitY and rand() function
- fruitX = rand() % (mapWidth - 1);
- fruitY = rand() % (mapHeight - 1);
- while (fruitX == 0) {
- fruitX = rand() % (mapWidth - 1);
- }
- while (fruitY == 0) {
- fruitY = rand() % (mapHeight - 1);
- }
- mouseX = rand() % (mapWidth - 1);
- mouseY = rand() % (mapHeight - 1);
- while (mouseX == 0) {
- mouseX = rand() % (mapWidth - 1);
- }
- while (mouseY == 0) {
- mouseY = rand() % (mapHeight - 1);
- }
- }
- void drawScreen() {
- //This function draws display screen with map boundary, snake and fruit
- int x; // loop counter for x axis
- int y; // loop counter for y axis
- int k; // loop counter to count length of snake tail
- int drawTail = FALSE; //draw is set to TRUE when a tail is drawn at the coordinates
- system("cls"); // clears the entire screen
- printf("\n");
- //draw left and right boundary, top and bottom boundary, snake and fruit
- // Part 3: Complete the nested for loops
- for (y = 0; y < mapHeight; y++) { //loop from top to bottom, y=0 to y=mapHeight-1
- for (x = 0; x < mapWidth; x++) { //loop from left to right, x=0 to x=mapWidth-1
- // Insert your code below
- if (y == 0 || y == mapHeight - 1)
- {
- printf(BOUNDARY);
- }
- else if (x == 0 || x == mapWidth - 1)
- {
- printf(BOUNDARY);
- }
- else if (x == snakeHeadX && y == snakeHeadY)
- {
- switch(dir)
- {
- case RIGHT:
- printf(">");
- break;
- case LEFT:
- printf("<");
- break;
- case UP:
- printf("^");
- break;
- case DOWN:
- printf("v");
- break;
- default:
- printf(SNAKE_HEAD);
- break;
- }
- }
- else if (x == fruitX && y == fruitY)
- {
- printf(FRUIT);
- }
- else if (x == mouseX && y == mouseY)
- {
- printf(MOUSE);
- }
- else
- {
- drawTail = FALSE;
- for (k = tailLength; k >= 0; k--)
- {
- if (x == tailX[k] && y == tailY[k])
- {
- printf(SNAKE_TAIL);
- drawTail = TRUE;
- }
- }
- if (drawTail == FALSE)
- {
- printf(SPACE);
- }
- }
- }//end of inner for loop
- printf("\n");
- }//end of outer for loop
- printf("Score: %d\n", score);
- printf("Lives: %d\n", lives);
- printf("Press 'p' to pause/unpause\n");
- }
- void readInput() {
- // Part 4: Complete the switch-case structure
- // This function reads input from keyboard and determine direction of snake's movement
- if (kbhit()) { //detect whether a key is pressed
- switch (getch()) { //read in a character and determine the direction of the snake
- case 'a':
- {
- if (dir == RIGHT)
- break;
- dir = LEFT;
- break;
- }
- case 'd':
- {
- if (dir == LEFT)
- break;
- dir = RIGHT;
- break;
- }
- case 'w':
- {
- if (dir == DOWN)
- break;
- dir = UP;
- break;
- }
- case 's':
- {
- if (dir == UP)
- break;
- dir = DOWN;
- break;
- }
- case 'p':
- pause = true;
- break;
- }
- }
- }
- void updateSnake() {
- //This function update the coordinates of the snake head, and snake tail segments
- //Update from the final segment of the snake to the snake head
- int i; //loop counter
- //loop to the move all the segments of the snake tail
- //The final segment of the tail will move to the 2nd last segment of the tail
- //The 2nd last segment of the tail will move to the 3rd last segment of the tail
- //...and so on
- //The second segment of the tail will move to the first segment of the tail
- for (i = tailLength - 1; i >0; i--) {
- tailX[i] = tailX[i - 1];
- tailY[i] = tailY[i - 1];
- }
- // move the first segment of tail to the snake head's position
- tailX[0] = snakeHeadX;
- tailY[0] = snakeHeadY;
- //check snake direction and update coordinates of snake head
- switch (dir) {
- case LEFT: //if snake moves to the left
- snakeHeadX--; //x coordinate decreases by 1; no change in y coordinates
- break;
- case RIGHT: //if snake moves to the right
- snakeHeadX++; //x coordinate increases by 1; no change in y coordinates
- break;
- case UP: //if snake moves up
- snakeHeadY--; //y coordinate decreases by 1; no change in x coordinates
- break;
- case DOWN: //if snake moves down
- snakeHeadY++; //y coordinate increases by 1; no change in x coordinates
- break;
- default:
- break;
- }
- static int counter = 0;
- ++counter;
- if (counter > 5)
- {
- //check snake direction and update coordinates of snake head
- switch (rand() % 5) {
- case LEFT: //if snake moves to the left
- if (mouseX <= 1)
- break;
- mouseX--; //x coordinate decreases by 1; no change in y coordinates
- break;
- case RIGHT: //if snake moves to the right
- if (mouseX >= 17)
- break;
- mouseX++; //x coordinate increases by 1; no change in y coordinates
- break;
- case UP: //if snake moves up
- if (mouseY <= 1)
- break;
- mouseY--; //y coordinate decreases by 1; no change in x coordinates
- break;
- case DOWN: //if snake moves down
- if (mouseY >= 17)
- break;
- mouseY++; //y coordinate increases by 1; no change in x coordinates
- break;
- default:
- break;
- }
- counter = 0;
- }
- }
- void checkConditions()
- {
- // Part 5 : Complete the if statements
- //This function checks whether the snake hits the boundary, bites its own tail or eats the fruit
- // if the snake head hits or crosses the boundary
- // game over !
- bool hit = false;
- int i;
- if (snakeHeadX <= 0 || snakeHeadX > 18 || snakeHeadY <= 0 || snakeHeadY >= 18)
- hit = true;
- // if the snake head bites any part of its tail
- // game over !
- for (i = tailLength; i>0; i--) {
- if (snakeHeadX == tailX[i] && snakeHeadY == tailY[i])
- hit = false;
- }
- // if the snake eats the fruit
- // then regenerates a new fruit at random locations
- if (snakeHeadX == fruitX && snakeHeadY == fruitY) {
- score += scoremod;
- tailLength++;
- fruitX = rand() % (mapWidth - 1);
- fruitX = rand() % (mapHeight - 1);
- while (fruitX == 0) {
- fruitX = rand() % (mapWidth - 1);
- }
- while (fruitY == 0) {
- fruitY = rand() % (mapHeight - 1);
- }
- }
- if (snakeHeadX == mouseX && snakeHeadY == mouseY) {
- score += scoremod;
- tailLength++;
- mouseX = rand() % (mapWidth - 1);
- mouseY = rand() % (mapHeight - 1);
- while (mouseX == 0) {
- mouseX = rand() % (mapWidth - 1);
- }
- while (mouseY == 0) {
- mouseY = rand() % (mapHeight - 1);
- }
- }
- if (hit)
- {
- tailLength = 0;
- lives--;
- snakeHeadX = mapWidth / 2;
- snakeHeadY = mapHeight / 2;
- for (int i = 0; i < 400; ++i)
- {
- tailX[i] = tailY[i] = 0;
- }
- }
- if (lives < 0)
- {
- gameOver = true;
- }
- }
- void displayGameOver()
- {
- fseek(stdin,0,SEEK_END);
- //This function discplays the game over screen
- system("cls"); //clear screen
- printf("\n\n\t\t\t Game Over! \n\n\n\n"); //display Game Over message
- printf("\n \n \n \n Press any key to return to main menu.");// prompt user to press a key
- printf("\n \n \n \n Press any 'X' to End.");// prompt user to press a key
- while (!gameclose)
- {
- if (kbhit())
- {
- char key = getch();
- if (key == 'x')
- gameclose = true;
- else
- break;
- }
- }
- fseek(stdin,0,SEEK_END);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement