Question

In: Computer Science

Create a Snake game project in C++ using all these which are given below. 1. Classes...

Create a Snake game project in C++ using all these which are given below.

1. Classes

2. structures

3. files ( MULTFILLING AND FILE HANDLING )

4. loops

5. conditions

6. switch

7. functions

8. array

Note: Also upload the zip file.

Solutions

Expert Solution

//This program has successfully run on
//operating system - windows 10 64 bit
//IDE - Code::Blocks 16.01
#include <iostream>
#include <windows.h>
#include <winuser.h>

using namespace std;


struct po_si_tion
{
    int x_axis,y_axis;
};

//Defining field class
class fld_cls
{
    static const int ht; //ht = height
    static const int wth; //wth = width
    char ** fld;
    fld_cls(const fld_cls &);
    fld_cls operator=(const fld_cls &);
public:
    fld_cls()
    {
        fld = new char*[fld_cls::ht]; //fld = field
        for(int i = 0; i < fld_cls::ht; ++i)
            {
            fld[i] = new char[fld_cls::wth];
            }
    }
    ~fld_cls()
    {
        for(int i = 0; i < fld_cls::ht; ++i)
            {
               delete[] fld[i];
            }
        delete[] fld;
    }

//to clear snake
    void to_clear()
    {
        for(int i = 0; i < ht; ++i)
         {
               for(int r = 0; r < wth; ++r)
               {
                 fld[i][r] = ' ';
               }
         }
    }
//printing snake
    void to_print()
    {
        for(int i = 0; i < ht; ++i)
            {
               for(int r = 0; r < wth; ++r)
               {
                 cout << fld[i][r];
               }
            cout << endl;
            }
    }


    int get_wth() const {return wth;}
    int get_ht() const {return ht;}

//to draw sanke
    void to_draw(int y_axis, int x_axis, char what)
    {
        //y_axis = (y_axis < 0) ? 0 : (y_axis >= ht ? ht - 1 : y_axis);
        //x_axis = (x_axis < 0) ? 0 : (x_axis >= wth ? wth - 1 : x_axis);

        fld[y_axis][x_axis] = what;
    }

} fld;

//food represented by letter 'X'
class food_cls
{
    po_si_tion pos;
    char symbol;
public:
    food_cls(): symbol('X'), pos()
    {
        pos.x_axis = pos.y_axis = -1;
    }
//setting position of snake
    void set_pos(int x_axis, int y_axis)
    {
        pos.x_axis = x_axis;
        pos.y_axis = y_axis;
    }
//resetting position of snake
    void re_po_si_tion(const fld_cls & fld)
    {
        pos.x_axis = rand() % fld.get_wth();
        pos.y_axis = rand() % fld.get_ht();
    }

    int get_x_axis() const {return pos.x_axis;}
    int get_y_axis() const {return pos.y_axis;}
    char get_symbol() const {return symbol;}
} food;
//class for giving direction to snake
class snake_cls
{
    enum {UP, DOWN, LEFT, RIGHT} dir;
    char symbol, head_symbol;
    po_si_tion pos[100];
    po_si_tion & head;
    int speed;
    int size;
    bool can_turn;
public:
    snake_cls(int x_axis, int y_axis):
        symbol('#'), head_symbol('@'), pos(),//snake head is '@' and tail is '#'
        speed(1), size(1), dir(RIGHT),
        head(pos[0]), can_turn(true)
    {
        pos[0].x_axis = x_axis;
        pos[0].y_axis = y_axis;
    }
//Display random food by symbol 'X'
    bool check_food(const food_cls & food)
    {
        if(food.get_x_axis() == head.x_axis && food.get_y_axis() == head.y_axis)
            {
              size += 1;
              return true;
             }
        return false;
    }
//Function to take direction from user/navigation for snake
    void get_input(const fld_cls & fld)
    {
        if(GetAsyncKeyState(VK_UP) && dir != DOWN)
            {
              dir = UP;
            }
        if(GetAsyncKeyState(VK_DOWN) && dir != UP)
            {
            dir = DOWN;
            }
        if(GetAsyncKeyState(VK_LEFT) && dir != RIGHT)
            {
            dir = LEFT;
            }
        if(GetAsyncKeyState(VK_RIGHT) && dir != LEFT)
            {
            dir = RIGHT;
            }
    }

    void to_move(const fld_cls & fld)
    {
        po_si_tion nex_axist = {0, 0};
        switch(dir)
        {
        case UP:
            nex_axist.y_axis = -speed;
            break;
        case DOWN:
            nex_axist.y_axis = speed;
            break;
        case LEFT:
            nex_axist.x_axis = -speed;
            break;
        case RIGHT:
            nex_axist.x_axis = speed;
        }
        for(int i = size - 1; i > 0; --i)
            {
            pos[i] = pos[i-1];
            }
        head.x_axis += nex_axist.x_axis;
        head.y_axis += nex_axist.y_axis;

        if(head.x_axis < 0 || head.y_axis < 0 || head.x_axis >= fld.get_wth() || head.y_axis >= fld.get_ht())
            {
            throw "DEADD!!!!";
            }
    }

    void to_draw(fld_cls & fld)
    {
        for(int i = 0; i < size; ++i)
            {
            if(i == 0)
                {
                fld.to_draw(pos[i].y_axis, pos[i].x_axis, head_symbol);
                }
              else
               {
                fld.to_draw(pos[i].y_axis, pos[i].x_axis, symbol);
               }
             }
    }

    int get_x_axis() const { return head.x_axis; }
    int get_y_axis() const { return head.y_axis; }
    char get_symbol() const { return symbol; }
} snake(1, 1);


const int fld_cls::ht = 24;
const int fld_cls::wth = 79;


int main()
{
    fld.to_clear();

    food.set_pos(5, 5); //setting food position

    while(1)
        {
        fld.to_clear();

        snake.get_input(fld);
        try
        {
            snake.to_move(fld);
        }

          catch (const char * er)
          {
            fld.to_clear();
            cerr << er << endl;
            system("pause");
            return -1;
          }
        snake.to_draw(fld);
        fld.to_draw(food.get_y_axis(), food.get_x_axis(), food.get_symbol());


        if(snake.check_food(food))
            {
              food.re_po_si_tion(fld);
            }

        fld.to_print();

        Sleep(1000/30);
        system("cls");
    }

    return 0;
}


Related Solutions

Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
C++ Project 1 For this project, we are going to create our own classes and use...
C++ Project 1 For this project, we are going to create our own classes and use them to build objects. For each class you will create an *.h and *.cpp file that contains the class definition and the member functions. There will also be one file called 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. You will create 2 new classes for this project, one will be from the list below and one...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
For the sample Data given create a frequency table using 6 classes. Find all relative and...
For the sample Data given create a frequency table using 6 classes. Find all relative and cumulative frequencies Data:94.7, 106.1, 117.5, 128.9, 135.5, 139.2, 142.3, 143.1, 144.5, 152.5, 156.4, 158.5, 159.6, 165.7, 166.5, 173.2, 175.5, 188.0, 199.0, 216.1 Use the frequencies found in the table above to sketch the histogram for the distribution. Use the class boundaries for the horizontal axis.
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
Using C++, create a program that will input the 3 game scores of a player and...
Using C++, create a program that will input the 3 game scores of a player and then output the level of the player with a message.   Specifications Prompt the user to input 3 game scores (double values). Compute the weighted average (DO NOT JUST ADD AND DIVIDE BY 3!) score as follows: 20% for first game, 30% for second game, 50% for last game Earlier games are worth less in the weighted average; later games are worth more. Print...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT