Jump to content
Sign in to follow this  
themax90

C++ Shooting Game

Recommended Posts

themax90

App:Shooting Game

This is a fun little shooting game.

Player one(right): Use Q to go up, A to go down, and S to shoot.

Player two(left): Use O to go up, L to go down, and K to shoot.

I made this over the last 2 days so that C++ Intermediates and Noobs can learn some more.

ScreenShot:

EDIT:Screenshot Removed

http://www.autoitscript.com/fileman/users/public/Agent%20Smith/sg.zip

http://www.autoitscript.com/fileman/users/public/Agent%20Smith/sg.exe

This compiles under Visual Studio 6.0 and the wookspace files are included in the Zip

Source code:

/*App: SGame ~ Shooting Game
 *Version: 1.0.102
 *Author: Max Gardner
 */


#include <stdlib.h>
#include <windows.h>
#include <iostream>
using namespace std;

#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_N 0x4E
#define VK_O 0x4F
#define VK_P 0x50
#define VK_Q 0x51
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54
#define VK_U 0x55
#define VK_V 0x56
#define VK_W 0x57
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A

struct GAMEINFO {
    COORD PlayerOnePosition;
    COORD PlayerTwoPosition;
    COORD PlayerOneBullet;
    COORD PlayerTwoBullet;
    COORD PlayerOneBullet2;
    COORD PlayerTwoBullet2;
    COORD ZeroZero;
};

HANDLE hInput, hOutput;
GAMEINFO GameInfo;

void Movement(GAMEINFO &GameInfo);
void Draw(GAMEINFO);
void Erase(GAMEINFO);
int LaunchBullet(GAMEINFO &GameInfo, int);
void LaunchBullet2(GAMEINFO &GameInfo, int);
int Wait();


int main()
{
    GAMEINFO GameInfo;
    
    

    hInput = GetStdHandle(STD_INPUT_HANDLE);
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
    
    SetConsoleMode(hOutput, ENABLE_PROCESSED_INPUT);
    
    GameInfo.PlayerOnePosition.X = 19;
    GameInfo.PlayerOnePosition.Y = 12;
    GameInfo.PlayerTwoPosition.X = 61;
    GameInfo.PlayerTwoPosition.Y = 12;
    GameInfo.PlayerOneBullet.X = 0;
    GameInfo.PlayerOneBullet.Y = 0;
    GameInfo.PlayerTwoBullet.X = 79;
    GameInfo.PlayerTwoBullet.Y = 0;
    GameInfo.PlayerOneBullet2.X = 1;
    GameInfo.PlayerOneBullet2.Y = 0;
    GameInfo.PlayerTwoBullet2.X = 78;
    GameInfo.PlayerTwoBullet2.Y = 0;
    GameInfo.ZeroZero.X = 0;
    GameInfo.ZeroZero.Y = 0;

    int i;
    GameInfo.ZeroZero.Y = 24;
    for(i = 0; i < 79; i++){
        SetConsoleCursorPosition(hOutput, GameInfo.ZeroZero);
        cout << ".";
        GameInfo.ZeroZero.X++;
    }

    Draw(GameInfo);

    while(1){
        Movement(GameInfo);
    }
    
    return 0;
}

void Movement(GAMEINFO &GameInfo)
{    
    INPUT_RECORD InputRecord;
    DWORD Events = 0;

    ReadConsoleInput(hInput, &InputRecord, 1, &Events);
    
    if(InputRecord.EventType == KEY_EVENT){
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_Q && InputRecord.Event.KeyEvent.bKeyDown == 1){
            Erase(GameInfo);
            GameInfo.PlayerOnePosition.Y--;
            if(GameInfo.PlayerOnePosition.Y < 0)
                GameInfo.PlayerOnePosition.Y++;
            Draw(GameInfo);
        }
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_A && InputRecord.Event.KeyEvent.bKeyDown == 1){
            Erase(GameInfo);
            GameInfo.PlayerOnePosition.Y++;
            if(GameInfo.PlayerOnePosition.Y > 24)
                GameInfo.PlayerOnePosition.Y--;
            Draw(GameInfo);
        }

        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_S && InputRecord.Event.KeyEvent.bKeyDown == 1){
            LaunchBullet(GameInfo, 1);
        }
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_O && InputRecord.Event.KeyEvent.bKeyDown == 1){
            Erase(GameInfo);
            GameInfo.PlayerTwoPosition.Y--;
            if(GameInfo.PlayerTwoPosition.Y < 0)
                GameInfo.PlayerTwoPosition.Y++;
            Draw(GameInfo);
        }

        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_L && InputRecord.Event.KeyEvent.bKeyDown == 1){
            Erase(GameInfo);
            GameInfo.PlayerTwoPosition.Y++;
            if(GameInfo.PlayerTwoPosition.Y > 24)
                GameInfo.PlayerTwoPosition.Y--;
            Draw(GameInfo);
        }

        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_K && InputRecord.Event.KeyEvent.bKeyDown == 1){
            LaunchBullet(GameInfo, 2);
        }

        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
            exit(0);

    }
    FlushConsoleInputBuffer(hInput);
}

void Draw(GAMEINFO GameInfo)
{
    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOnePosition);
    cout << "|";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoPosition);
    cout << "|";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOneBullet);
    cout << ".";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoBullet);
    cout << ".";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOneBullet2);
    cout << ".";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoBullet2);
    cout << ".";
    
    GameInfo.ZeroZero.X = 0;
    GameInfo.ZeroZero.Y = 0;

    int i;
    for(i = 0; i < 79; i++){
        SetConsoleCursorPosition(hOutput, GameInfo.ZeroZero);
        cout << ".";
        GameInfo.ZeroZero.X++;
    }

}

void Erase(GAMEINFO GameInfo)
{
    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOnePosition);
    cout << " ";
    
    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoPosition);
    cout << " ";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOneBullet);
    cout << " ";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoBullet);
    cout << " ";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerOneBullet2);
    cout << " ";

    SetConsoleCursorPosition(hOutput, GameInfo.PlayerTwoBullet2);
    cout << " ";
}

int LaunchBullet(GAMEINFO &GameInfo, int PlayerNumber)
{    
    int i;
    if(PlayerNumber == 1){
        GameInfo.PlayerOneBullet.Y = GameInfo.PlayerOnePosition.Y;
        GameInfo.PlayerOneBullet.X = GameInfo.PlayerOnePosition.X + 1;
        Draw(GameInfo);
        Erase(GameInfo);
        for(i = 0; i < 77; i++){
            GameInfo.PlayerOneBullet.X += 1;
            Draw(GameInfo);
    
            int move;
            move =    Wait();
            switch(move){
            case 1:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y--;
                if(GameInfo.PlayerOnePosition.Y < 0)
                    GameInfo.PlayerOnePosition.Y++;
                break;
            case 2:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y++;
                if(GameInfo.PlayerOnePosition.Y > 24)
                    GameInfo.PlayerOnePosition.Y--;
                break;
            case 3:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y--;
                if(GameInfo.PlayerTwoPosition.Y < 0)
                    GameInfo.PlayerTwoPosition.Y++;
                break;
            case 4:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y++;
                if(GameInfo.PlayerTwoPosition.Y > 24)
                    GameInfo.PlayerTwoPosition.Y--;
                break;
            case 5:
                LaunchBullet2(GameInfo, 1);
                return 0;
                break;
            case 6:
                LaunchBullet2(GameInfo, 2);
                return 0;
                break;
            }

            Draw(GameInfo);
            Erase(GameInfo);
            if(GameInfo.PlayerOneBullet.X == GameInfo.PlayerTwoPosition.X)
                if(GameInfo.PlayerOneBullet.Y == GameInfo.PlayerTwoPosition.Y){
                    system("cls");
                    cout << "\aPlayer 1 Wins" << endl;
                    system("pause");
                    exit(0);
                }
        }
        GameInfo.PlayerOneBullet.Y = 0;
        GameInfo.PlayerOneBullet.X = 0;
        Draw(GameInfo);
    }
    if(PlayerNumber == 2){
        GameInfo.PlayerTwoBullet.Y = GameInfo.PlayerTwoPosition.Y;
        GameInfo.PlayerTwoBullet.X = GameInfo.PlayerTwoPosition.X - 1;
        Draw(GameInfo);
        Erase(GameInfo);
        for(i = 0; i < 77; i++){
            GameInfo.PlayerTwoBullet.X -= 1;
            Draw(GameInfo);
        
            int move;
            move =    Wait();
            switch(move){
            case 1:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y--;
                if(GameInfo.PlayerOnePosition.Y < 0)
                    GameInfo.PlayerOnePosition.Y++;
                break;
            case 2:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y++;
                if(GameInfo.PlayerOnePosition.Y > 24)
                    GameInfo.PlayerOnePosition.Y--;
                break;
            case 3:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y--;
                if(GameInfo.PlayerTwoPosition.Y < 0)
                    GameInfo.PlayerTwoPosition.Y++;
                break;
            case 4:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y++;
                if(GameInfo.PlayerTwoPosition.Y > 24)
                    GameInfo.PlayerTwoPosition.Y--;
                break;
            case 5:
                LaunchBullet2(GameInfo, 1);
                return 0;
                break;
            case 6:
                LaunchBullet2(GameInfo, 2);
                return 0;
                break;
            }
            
            Draw(GameInfo);
            Erase(GameInfo);
            if(GameInfo.PlayerTwoBullet.X == GameInfo.PlayerOnePosition.X)
                if(GameInfo.PlayerTwoBullet.Y == GameInfo.PlayerOnePosition.Y){
                    system("cls");
                    cout << "\aPlayer 2 Wins" << endl;
                    system("pause");
                    exit(0);
            }
        }
        GameInfo.PlayerTwoBullet.Y = 0;
        GameInfo.PlayerTwoBullet.X = 79;
        Draw(GameInfo);
    }
    return 0;
}

int Wait()
{    
    INPUT_RECORD InputRecord;
    DWORD Events = 0;
    
    if(WAIT_TIMEOUT == WaitForSingleObject(hInput,1))
                return 0;
    ReadConsoleInput(hInput, &InputRecord, 1, &Events);

    if(InputRecord.EventType == KEY_EVENT){
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_Q && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 1;
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_A && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 2;
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_O && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 3;
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_L && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 4;
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_S && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 5;
        
        if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_K && InputRecord.Event.KeyEvent.bKeyDown == 1)
            return 6;
    }
    FlushConsoleInputBuffer(hInput);
    return 0;
}

void LaunchBullet2(GAMEINFO &GameInfo, int PlayerNumber)
{
    if(PlayerNumber == 1){
        GameInfo.PlayerOneBullet2.X = GameInfo.PlayerOnePosition.X + 1;
        GameInfo.PlayerOneBullet2.Y = GameInfo.PlayerOnePosition.Y;

        Draw(GameInfo);
        Erase(GameInfo);
        int i;
        for(i = 0; i < 77; i++){
            GameInfo.PlayerOneBullet.X += 1;
            GameInfo.PlayerOneBullet2.X += 1;
            GameInfo.PlayerTwoBullet.X -= 1;
            GameInfo.PlayerTwoBullet2.X -= 1;
            Draw(GameInfo);
    
            int move;
            move =    Wait();
            switch(move){
            case 1:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y--;
                if(GameInfo.PlayerOnePosition.Y < 0)
                    GameInfo.PlayerOnePosition.Y++;
                break;
            case 2:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y++;
                if(GameInfo.PlayerOnePosition.Y > 24)
                    GameInfo.PlayerOnePosition.Y--;
                break;
            case 3:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y--;
                if(GameInfo.PlayerTwoPosition.Y < 0)
                    GameInfo.PlayerTwoPosition.Y++;
                break;
            case 4:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y++;
                if(GameInfo.PlayerTwoPosition.Y > 24)
                    GameInfo.PlayerTwoPosition.Y--;
                break;
            }

            Draw(GameInfo);
            Erase(GameInfo);
            if(GameInfo.PlayerOneBullet.X == GameInfo.PlayerTwoPosition.X)
                if(GameInfo.PlayerOneBullet.Y == GameInfo.PlayerTwoPosition.Y){
                    system("cls");
                    cout << "\aPlayer 1 Wins" << endl;
                    system("pause");
                    exit(0);
                }
            if(GameInfo.PlayerOneBullet2.X == GameInfo.PlayerTwoPosition.X)
                if(GameInfo.PlayerOneBullet2.Y == GameInfo.PlayerTwoPosition.Y){
                    system("cls");
                    cout << "\aPlayer 1 Wins" << endl;
                    system("pause");
                    exit(0);
                }
        }
        GameInfo.PlayerOneBullet.Y = 0;
        GameInfo.PlayerOneBullet.X = 0;
        GameInfo.PlayerOneBullet2.Y = 0;
        GameInfo.PlayerOneBullet2.X = 1;
        Draw(GameInfo);
    }

    if(PlayerNumber == 2){
        GameInfo.PlayerTwoBullet2.Y = GameInfo.PlayerTwoPosition.Y;
        GameInfo.PlayerTwoBullet2.X = GameInfo.PlayerTwoPosition.X - 1;
        Draw(GameInfo);
        Erase(GameInfo);
        int i;
        for(i = 0; i < 77; i++){
            GameInfo.PlayerTwoBullet.X -= 1;
            GameInfo.PlayerTwoBullet2.X -= 1;
            GameInfo.PlayerOneBullet.X += 1;
            GameInfo.PlayerOneBullet2.X += 1;
            Draw(GameInfo);
        
            int move;
            move =    Wait();
            switch(move){
            case 1:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y--;
                if(GameInfo.PlayerOnePosition.Y < 0)
                    GameInfo.PlayerOnePosition.Y++;
                break;
            case 2:
                Erase(GameInfo);
                GameInfo.PlayerOnePosition.Y++;
                if(GameInfo.PlayerOnePosition.Y > 24)
                    GameInfo.PlayerOnePosition.Y--;
                break;
            case 3:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y--;
                if(GameInfo.PlayerTwoPosition.Y < 0)
                    GameInfo.PlayerTwoPosition.Y++;
                break;
            case 4:
                Erase(GameInfo);
                GameInfo.PlayerTwoPosition.Y++;
                if(GameInfo.PlayerTwoPosition.Y > 24)
                    GameInfo.PlayerTwoPosition.Y--;
                break;
            }

            Draw(GameInfo);
            Erase(GameInfo);
            if(GameInfo.PlayerTwoBullet.X == GameInfo.PlayerOnePosition.X)
                if(GameInfo.PlayerTwoBullet.Y == GameInfo.PlayerOnePosition.Y){
                    system("cls");
                    cout << "\aPlayer 2 Wins" << endl;
                    system("pause");
                    exit(0);
            }
            if(GameInfo.PlayerTwoBullet2.X == GameInfo.PlayerOnePosition.X)
                if(GameInfo.PlayerTwoBullet2.Y == GameInfo.PlayerOnePosition.Y){
                    system("cls");
                    cout << "\aPlayer 2 Wins" << endl;
                    system("pause");
                    exit(0);
            }
        }
        GameInfo.PlayerOneBullet.Y = 0;
        GameInfo.PlayerOneBullet.X = 0;
        GameInfo.PlayerOneBullet2.Y = 0;
        GameInfo.PlayerOneBullet2.X = 1;
        Draw(GameInfo);
    }
}
Edited by Agent Smith

Share this post


Link to post
Share on other sites
b14ck

Hrm... Both you and Darlan have the name "Max". Interesting.


-I am the giver of life and the bringer of death.

Share this post


Link to post
Share on other sites
themax90

No, black, appartenly you didn't read the ENTIRe thing. He was trying to Be ME. He stole my QuickE script and used my name in the forums. He is not me and in no way associated with me. Now can we put this behind us? i am tired of this comming up,

Thank you

Agent Smith

Share this post


Link to post
Share on other sites
Jos

Hrm... Both you and Darlan have the name "Max". Interesting.

<{POST_SNAPBACK}>

He black.... its chistmas time... so maybe thats a good enough reason for you to stop?

Whatever the truth is will come out ... just give it time and let it rest.

Edit spelling

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Nova

Why do you care so much b14ck, can you just let it die ???

by the way isnt this section reserved for c++ autoit stuff ?

Edited by nova

Share this post


Link to post
Share on other sites
this-is-me

That's exactly what I thought. No offense Smith, but don't go posting your personal programs just to show off. If it's not realted to autoit then don't post it. Your ftp program was an example of what is acceptable. This definately is not.

There is no need to show anyone else how to create a program in C++. If they want to learn, let them buy a good book on the subject. Adding posts like this is not going to be helpful.

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites
layer

not to get on anyones bad side but i thought this section was for any c++ or autoit developing stuff... personally, i just learned some new C++ syntax YAY! :idiot: because im a begginer, even though i am still going to read some books or something...


FootbaG

Share this post


Link to post
Share on other sites
this-is-me

Well, I will admit that I might be wrong, but I just don't really see this helping the cause of autoit. If you learned something, does that mean that someday you will grow out of the teenage idiosyncrasies and become an autoit developer? Is that the reason Smith posted this otherwise useless code?


Who else would I be?

Share this post


Link to post
Share on other sites
themax90

Well I must abide by the rules, ok, I wont post like this again unless it's connected to an autoit project.

Look This-is-me stop being such a stuck up about it, I just wanted to help people learn. You have said what you need to say now move along. Please.

Edited by Agent Smith

Share this post


Link to post
Share on other sites
Guest Guidosoft

Hrm... Both you and Darlan have the name "Max". Interesting.

WTF!!! HE IS NOT DARLARN. HE IS AGENT SMITH!!!!

OMG.

Edited by Guidosoft

Share this post


Link to post
Share on other sites
SlimShady

WTF!!! HE IS NOT DARLARN. HE IS AGENT SMITH!!!!

OMG.

<{POST_SNAPBACK}>

You don't have to respond like that.

Share this post


Link to post
Share on other sites
themax90

That is a little overboard Guido, no matter how strong you feel show some restraint.

:idiot:

Share this post


Link to post
Share on other sites
this-is-me

Hey, Smith. I was not being stuck up about it. I just wanted your explanation. No offense, no harm, no foul. I have nothing against you. I just didn't see any relation with this and autoit. Now that you have explained, I am fine with that.


Who else would I be?

Share this post


Link to post
Share on other sites
Valik

Its a judgement call, in my opinion. I've posted small little examples and snippets of C++ concepts that didn't relate to AutoIt at all (The last of which being the fun test I did calling functions via the vtable). Little proof-of-concept examples and what-not would be okay, as it could teach other developers or inspire them to make something in AutoIt better. However, I don't think posting anything of any significance is acceptable unless it is directly related to AutoIt.

Share this post


Link to post
Share on other sites
layer

yea i agree, although Valik, your text below your homer avatar scares me :D:idiot::lol:


FootbaG

Share this post


Link to post
Share on other sites
themax90

Ok, kool....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×