PONG 弹球

· · 科技·工程

"HEY..."

"WHAT'S UP?"

"DO YOU KNOW THE NAME OF THE FIRST VIDEO GAME OF ATARI?"

"DUNNO.DO YOU KNOW?"

"OF COURSE.IT'S CALLED PONG."

"SO WHY ARE YOU TELLING ME THAT?"

"......"

众所周知,ATARI雅达利公司的第一个电子游戏(带图像的那种)叫作PONG,是由诺兰·布什内尔和他的团队研发的。

但知周所众,这篇文章和他没什么多大的关系。

INTRODUCTION

COULD BE SEEN AS THE COPY&UPGRADE VERSION OF THE FIRST PONG.USE 'W' AND 'S' TO CONTROL PADDEL 1,'↑' AND '↓' TO CONTROL PADDLE 2.THE FIRST PLAYER GETS 5 POINTS WINS.

TRANSLATION 翻译

可以看成初版PONG的高清重制版。WS控制玩家一的上下,上下键控制玩家2的上下。谁先拿到5分谁就赢。

CODE

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<bitset>
#include<winuser.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define SHAKE 5
using namespace std;
extern const int
BLACK=0,
DARKBLUE=1,DBLUE=1,
GREEN=2,
BLUE=3,
RED=4,
PURPLE=5,
DIRT=6,YELLOWGREY=6,
LIGHTGREY=7,LGREY=7,
GREY=8,DGREY=8,DARKGREY=8,
SKYBLUE=9,SBLUE=9,
LGREEN=10,LIGHTGREEN=10,
CANYON=11,
LRED=12,LIGHTRED=12,BRED=12,BRIGHTRED=12,
LPURPLE=13,LIGHTPURPLE=13,
LYELLOW=14,LIGHTYELLOW=14,
WHITE=15
;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
void ResizeFont(int a)
{
    HMODULE hKernel32=GetModuleHandle("kernel32");
    SetConsoleFont=(PROCSETCONSOLEFONT)GetProcAddress(hKernel32,"SetConsoleFont");
    SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Color(int text,int background)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),text+background*16);
}
void BColor(int background)
{
    if(background==15)Color(0,background);
    else Color(15,background);
}
void BColor(string background)
{
    int len=background.length();
    for(int i=0;i<len;i++)
    {
        if(background[i]>96)background[i]-=32;
    }
    if(background=="BLACK")Color(15,0);
    if(background=="DARKBLUE"||background=="DBLUE")Color(15,1);
    if(background=="GREEN")Color(15,2);
    if(background=="BLUE")Color(15,3);
    if(background=="RED")Color(15,4);
    if(background=="PURPLE")Color(15,5);
    if(background=="DIRT"||background=="YELLOWGREY")Color(15,6);
    if(background=="LIGHTGREY"||background=="LGREY")Color(15,7);
    if(background=="GREY"||background=="DGREY"||background=="DARKGREY")Color(15,8);
    if(background=="SKYBLUE"||background=="SBLUE")Color(15,9);
    if(background=="LGREEN"||background=="LIGHTGREEN")Color(15,10);
    if(background=="CANYON")Color(15,11);
    if(background=="LRED"||background=="LIGHTRED"||background=="BRED"||background=="BRIGHTRED")Color(15,12);
    if(background=="LPURPLE"||background=="LIGHTPURPLE")Color(15,13);
    if(background=="LYELLOW"||background=="LIGHTYELLOW")Color(15,14);
    if(background=="WHITE")Color(0,15);
}
void cls()
{
    system("cls");
}
struct CTRL
{
    void SetPointerVisual(bool visual)
    {
        //1=show     0=hide
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cci;
        GetConsoleCursorInfo(hOut,&cci);
        cci.bVisible=visual;
        SetConsoleCursorInfo(hOut,&cci);
    }
};
extern CTRL Ctrl;
struct Bas
{
    void Hi()
    {
        cout<<"Hello world!";
    }
};
extern Bas Basic;
void PrintByChar(string str,int t)
{
    int len=str.length();
    for(int i=0;i<len;i++)
    {
        Sleep(double(t/len));
        cout<<str[i];
    }
    cout<<endl;
}
struct BigNum
{
    string num;
    int length()
    {
        return num.length();
    }
    void Print()
    {
        bool m=0;
        for(int i=0;i<length();i++)
        {
            if(num[i]!='0')m=1;
            if(m)cout<<num;
        }
    }
    string operator+(const BigNum& B)
    {
        //string a=this->num;
        string b=B.num;
        return "1";
    };
};
struct TO
{
    template < typename T >
    string String( T& n )
    {
        std::ostringstream stm ;
        stm<<fixed<<setprecision(16)<<n;
        return stm.str() ;
    }
};
extern TO To;
int GetWindowPosX()
{
    RECT rect;
    HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.left;
}
int GetWindowPosY()
{
    RECT rect;
    HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.top;
}
int GetCursorPosX()
{
    POINT pt={0,0};
    GetCursorPos(&pt);
    return pt.x;
}
int GetCursorPosY()
{
    POINT pt={0,0};
    GetCursorPos(&pt);
    return pt.y;
}
int GetSubX()
{
    return GetCursorPosX()-GetWindowPosX();
}
int GetSubY()
{
    return GetCursorPosY()-GetWindowPosY();
}
void gotoxy(int x,int y)
{
    int xx=0x0b;
    HANDLE hOutput;
    COORD loc;
    loc.X=x;
    loc.Y=y;
    hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput,loc);
}
using namespace std;
int balldirX,balldirY,ballX=10,ballY=21; 
int playerX=10,playerY=4;
int player2X=10,player2Y=37;
int p1score=0,p2score=0,len=2;
string mode="null";
int world[50][50];
void gameupdate(){
    int tmpx=balldirX+ballX,tmpy=balldirY+ballY;
    if(tmpx==0 || tmpx==21 || ballY==playerY && (ballX==playerX+len+1 || ballX==playerX-len-1) || ballY==player2Y && (ballX==player2X+len+1 || ballX==player2X-len-1)){
        balldirX=0-balldirX;
    }
    tmpx=balldirX+ballX,tmpy=balldirY+ballY;

    if(tmpy==0 || tmpy==41 || (abs(tmpx-player2X)<=len && tmpy==player2Y) || (abs(tmpx-playerX)<=len && tmpy==playerY)){
        balldirY=0-balldirY;
    }
    ballX+=balldirX;
    ballY+=balldirY;
}
void printscreen(){
    gotoxy(0,0);
    cout << "                                player1 : player2\n";
    cout << "                                       " << p1score << " : " << p2score << "\n";
    for(int i=0;i<=21;i++){
        for(int j=0;j<=41;j++){
            if(i==0 || i==21 || j==0 || j==41){
                BColor("DIRT");
            }else if(i==ballX && j==ballY){
                BColor("RED");
            }else if(abs(playerX-i)<=len && j==playerY || abs(player2X-i)<=len && j==player2Y){
                BColor("WHITE");
            }else{
                BColor("BLACK");
            }
            printf("  ");
        }
        printf("\n");
    }
    BColor("BLACK");
    printf("---------------------------------------------------------------------------------------\n");
    cout << "debug: ballX: " << ballX << " ballY: " << ballY << " balldirX: " << balldirX << " balldirY: " << balldirY << " //\n"; 
}
void trymove(){
    if(KEY_DOWN('W') && playerX>len+1)playerX--;
    if(KEY_DOWN('S') && playerX<20-len)playerX++;
    if(mode=="normal" || mode=="turbo" || mode=="fury"){
        if(KEY_DOWN(VK_UP) && player2X>len+1)player2X--;
        if(KEY_DOWN(VK_DOWN) && player2X<20-len)player2X++;
    }else if(mode=="sai" || mode=="inf"){
        if(ballY>=25 && ballX<player2X && player2X>len+1)player2X--;
         if(ballY>=25 && ballX>player2X && player2X<20-len)player2X++;
    }else if(mode=="DEATH"){
        if(ballX<player2X && player2X>len+1)player2X--;
         if(ballX>player2X && player2X<20-len)player2X++;
    }else if(mode=="ai" || mode=="turboai"){
        if(ballY>=30 && ballX<player2X && player2X>len+1 && rand()%4!=1)player2X--;
        if(ballY>=30 && ballX>player2X && player2X<20-len && rand()%4!=1)player2X++;
    }

}
int main(){
    system("mode con cols=50 lines=25");
    srand(time(0));
    while(1){
        cout << "\n\n\n                     pong!\n\n\n";
        Sleep(100);
        cout << "           normal mode:pvp(press 1)\n\n";Sleep(100);
        cout << "           AI mode:pvc(press 2)\n\n";Sleep(100);
        cout << "           turbo mode:pvp(press 3)\n\n";Sleep(100);
        cout << "           turbo mode:pvc(press 4)\n\n";Sleep(100);
        cout << "           superAI mode:pvc(press 5)\n\n";Sleep(100);
        cout << "           fury mode:pvp(press 6)\n\n";Sleep(100);
        cout << "           FUN mode:pvc(press X if you DARE)\n           (NOT FINISHED YET)\n";Sleep(100);
        cout << "           infinity mode:pvc(press 7)\n\n";Sleep(100);
        cout << "NOTE:this game SOMETIMES occurs some weird bugs :(";
        Sleep(500);    
        while(1){
            if(KEY_DOWN('1')){
                mode="normal";
                break;
            }
            if(KEY_DOWN('2')){
                mode="ai";
                break;
            } 
            if(KEY_DOWN('3')){
                mode="turbo";
                break;
            } 
            if(KEY_DOWN('4')){
                mode="turboai";
                break;
            } 
            if(KEY_DOWN('5')){
                mode="sai";
                break;
            } 
            if(KEY_DOWN('6')){
                mode="fury";
                break;
            } 
            if(KEY_DOWN('X')){
                mode="DEATH";
                break;
            }
            if(KEY_DOWN('7')){
                mode="inf";
                break;
            } 
        }
        system("mode con cols=90 lines=30");
        if(rand()%2==0)balldirX=1;
        else balldirX=-1;
        if(rand()%2==0)balldirY=1;
        else balldirY=-1;
        if(mode=="fury")playerY=7,player2Y=33;
        if(mode=="sai")len=1;
        while(1){
            printscreen();
            gameupdate();
            trymove();
            if(playerY-ballY>2){
                p2score++;
                ballX=10,ballY=21;
                cout << "                               player2 scores 1 point!\n";
                Sleep(2000);
                cls();
                if(rand()%2==0)balldirX=1;
                else balldirX=-1;
                if(rand()%2==0)balldirY=1;
                else balldirY=-1;
            }else if(player2Y-ballY<(0-2)){
                p1score++;
                ballX=10,ballY=21;
                cout << "                               player1 scores 1 point!\n";
                Sleep(2000);
                cls();
                if(rand()%2==0)balldirX=1;
                else balldirX=-1;
                if(rand()%2==0)balldirY=1;
                else balldirY=-1;
            }
            if(mode!="inf"){
                if(p1score==5){
                    cout << "                                 player1 wins!\n";
                    Sleep(2000);
                    cls();
                    ballX=10,ballY=21;
                    if(rand()%2==0)balldirX=1;
                    else balldirX=-1;
                    if(rand()%2==0)balldirY=1;
                    else balldirY=-1;
                    p1score=p2score=0;
                }else if(p2score==5){
                    cout << "                                 player2 wins!\n";
                    Sleep(2000);
                    cls();
                    ballX=10,ballY=21;
                    if(rand()%2==0)balldirX=1;
                    else balldirX=-1;
                    if(rand()%2==0)balldirY=1;
                    else balldirY=-1;
                    p1score=p2score=0;
                    if(mode=="DEATH"){
                        char path[100];
                        GetModuleFileName(NULL, path, 100); 
                        printf("%s\n",path);
                        char *p = strrchr(path,'\\');
                        //printf("%s\n",p);
                        char *name = p + 1; 
                        printf("%s\n",name); 
                        rename(name,"GG.exe");
                        /*while(1){
                            system("start GG.exe");
                        }*/
                    }
                }
            }
            if(mode!="turbo" || mode!="turboai" || mode=="fury")Sleep(20);
            if(mode=="fury" && rand()%10==0){
                if(rand()%2==0)balldirX=1;
                else balldirX=-1;
                if(rand()%2==0)balldirY=1;
                else balldirY=-1;   
                if(rand()%3==0){
                    if(rand()%2==0){
                        playerY+=0-(rand()%2+1);
                        player2Y+=0-(rand()%2+1);
                    }else{
                        playerY+=(rand()%2+1);
                        player2Y+=(rand()%2+1);
                    }
                    if(playerY==0 || playerY==41)playerY=3;
                    if(player2Y==0 || player2Y==41)player2Y=36;
                }else if(rand()%5==0){
                    len=rand()%5+1;
                }else if(rand()%2==0){
                    if(rand()%5==0)system("title IAKIOI!");
                    if(rand()%5==1)system("title pong");
                    if(rand()%5==2)system("title pppppppoooooooonnnnnnnggggg!!!!!!!");
                    if(rand()%5==3)system("title 扣1学校爆炸");
                    if(rand()%5==4)system("title bro,you suck");
                    if(rand()%5==5)system("never gonna give you up,never gonna let you down");
                }
            }
        }
    }
}

提示:FURY模式十分令人红温!