Question

In: Computer Science

I need to create a code in C++ that first has a menu animation of game...

I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.

Solutions

Expert Solution

Please Upvote..!! Thank you .. ;)

CODE :

#include <iostream>
#include <stdio.h>
#include<windows.h>
#include<string>
#include<vector>
using namespace std;
char tmp_map[18][32];

char map[18][32] = {
        "+___________ YAZDAN __________+",
        "|    #                        |",
        "|         /|         |        |",
        "|### ## ###|   ####  |# ## ###|",
        "|                             |",
        "|  ###  ##|         #     |   |",
        "|         |           #####   |",
        "| |  # |  | #####     |       |",
        "| |    |  |     #     |  # #  |",
        "| ######  |     #     |       |",
        "|           #####     |       |",
        "|  #### ##        #####       |",
        "|                             |",                             
        "| ###  ##     ##       ### ## |",
        "|      |         T   T  |     |",
        "|# # ##|# ## ## #|   |  |     |",
        "|      |         #####     # #|",
        "+_____________________________+"
        };

void ShowMap()
{
        for(int i = 0; i < 18; i++) {
                printf("%s\n",map[i] );
        }
}

void gotoxy( short x, short y )
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
    COORD position = { x, y } ;
    SetConsoleCursorPosition( hStdout, position ) ;
}

class entity {
public:
        entity( int x, int y ){
                this ->x = x;
                this ->y = y;
        }

        void move_x( int p ){
                if( map[y][x+p] == ' ' ) x += p;
        }

        void move_y( int p ){
                if( map[y+p][x] == ' ' ) y += p;
        }

        void move( int p, int q ){
                x += p;
                y += q;
        }

        int get_x(){ return x; }
        int get_y(){ return y; }

        void draw( char p ){
                map[x][y] = p;
                gotoxy( x, y ); printf( "%c", p );
        }

private:
        int x;
        int y;
};

struct walk {
        short walk_count;
        short x;
        short y;
        short back;
};

struct target {
        short x;
        short y;
};

vector<target> walk_queue;

vector<walk> BFSArray;

void AddArray( int x, int y, int wc , int back ){
        if( tmp_map[y][x] == ' ' || tmp_map[y][x] == '.' ){
                tmp_map[y][x] = '#';
                walk tmp;
                tmp.x = x;
                tmp.y = y;
                tmp.walk_count = wc;
                tmp.back = back;
                BFSArray.push_back( tmp );
        }
}

void FindPath( int sx, int sy, int x, int y ){
        memcpy( tmp_map, map, sizeof(map) );
        BFSArray.clear();
        walk tmp;
        tmp.x = sx;
        tmp.y = sy;
        tmp.walk_count = 0;
        tmp.back = -1;
        BFSArray.push_back( tmp );

        int i = 0;
        while( i < BFSArray.size() ){
                if( BFSArray[i].x == x && BFSArray[i].y == y ){
                        walk_queue.clear();
                        target tmp2;
                        while( BFSArray[i].walk_count != 0 ){
                                tmp2.x = BFSArray[i].x;
                                tmp2.y = BFSArray[i].y;
                                walk_queue.push_back( tmp2 );

                                i = BFSArray[i].back;
                        }

                        break;
                }

                AddArray( BFSArray[i].x+1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
                AddArray( BFSArray[i].x-1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
                AddArray( BFSArray[i].x, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
                AddArray( BFSArray[i].x, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

                
                i++;
        }

        BFSArray.clear();
}


int main()
{
        system("color F2");
        cout<<"Welcome to Pacman Game \n";
    bool running = true;
        int x = 15; // hero x
        int y = 16; // hero y
        int old_x;
        int old_y;

        int ex = 1;
        int ey = 1;


        int pts = 0;

        printf("Instruction:\n1. Arrow Keys to move pacman\n2. Eat the dots produced by the Eater to gain poins\n3. Don't get caught by the Eater\n\n");
        printf("H -> Hard\nN -> Normal\nE -> Easy\n\nInput : ");

        char diffi;
        int speedmod = 3;

        cin >> diffi;

        if( diffi == 'N' ){
                speedmod = 2;
        }else if( diffi == 'H' ){
                speedmod = 1;
        }

        system("cls");
    ShowMap();

        gotoxy( x, y ); cout << "H";

        int frame = 0;

        FindPath( ex,ey,x,y );

        while( running ){
                gotoxy( x, y ); cout << " ";

                old_x = x;
                old_y = y;

                if ( GetAsyncKeyState( VK_UP ) ){
                        if( map[y-1][x] == '.' ){ y--; pts++; } else
                        if( map[y-1][x] == ' ' ) y--;
                }
                if ( GetAsyncKeyState( VK_DOWN ) ){
                        if( map[y+1][x] == '.' ){ y++; pts++; } else
                        if( map[y+1][x] == ' ' ) y++;
                }
                if ( GetAsyncKeyState( VK_LEFT ) ){
                        if( map[y][x-1] == '.' ){ x--; pts++; } else
                        if( map[y][x-1] == ' ' ) x--;
                }
                if ( GetAsyncKeyState( VK_RIGHT ) ){
                        if( map[y][x+1] == '.' ){ x++; pts++; } else
                        if( map[y][x+1] == ' ' ) x++;
                }

                if( old_x != x || old_y != y ){
                        FindPath( ex,ey,x,y );
                }

                gotoxy( x,y ); cout << "H";

                map[ey][ex] = '.';
                gotoxy( ex, ey ); cout << ".";

                if( frame%speedmod == 0 && walk_queue.size() != 0 ){
                        ex = walk_queue.back().x;
                        ey = walk_queue.back().y;
                        walk_queue.pop_back();
                }

                gotoxy( ex, ey ); cout << "E";

                if( ex == x && ey == y ){
                        break;
                }


                gotoxy( 32, 18 );
                gotoxy( 32, 1 ); cout << pts;
                Sleep( 100 );
                frame++;
        }

        system("cls");
        printf("You Lose and your score is : %i", pts );
        cin.get();
        cin.get();
        cin.get();
        cin.get();
        cin.get();
        cin.get();
        cin.get();
        cin.get();

        return 0;
}

Hope this was helpful.

Please Upvote..!! Thank you .. ;)


Related Solutions

i need a javafx code 1. first create menu bar with option open and save 2....
i need a javafx code 1. first create menu bar with option open and save 2. when user click on open it opens the file only image file 3. when user click on save it saves as new file.
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
I need this code in C++ form using visual studios please: Create a class that simulates...
I need this code in C++ form using visual studios please: Create a class that simulates an alarm clock. In this class you should: •       Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have separate private members for the alarm and the clock. Do not forget to have a character variable representing AM or PM.) •       Initialize the clock to a specified time. •       Allow the clock to increment to the...
.Code for a game of Tetris in C# .Short explanation for a first time user, showing...
.Code for a game of Tetris in C# .Short explanation for a first time user, showing them how to use it and pointing out all of the pertinent features. (1 paragraph) .Provide a few screen shots if possible
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method. This application needs to hold the user input's numbers as an array. Thanks for your time. It is a great help!
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER TEMPERATURE – JAMES SMITH Convert Fahrenheit temperature to Celsius Convert Celsius temperature to Fahrenheit Exit CASE 1: Convert Fahrenheit temperature to Celsius -Display the message to ask to enter Fahrenheit degree from the keyboard -Use the following formula to convert to Celsius degree         Celsius Temperature = (Fahrenheit Temperature – 32) * 5/9 ; -Display the output as below: TemperatureConverter_Smith.cpp TEMPERATURE CONVERTER – JAMES...
Simple code for a game on C coding.
Simple code for a game on C coding.
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT