一些代码

Tetris:

#include<iostream>
#include<stdlib.h> 
#include<time.h>
#include<conio.h>
#include<bitset>
#include<windows.h>
#include<string>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int randint(int a, int b)
{
    return a + rand() % (b - a + 1);
}
int main()
{

    for (;;)
    {
        string    m[26][30] = { "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  " };
        double score=0;
        int aaa = 250;
        for (;;)
        {
            int a[4][2] = { 0 }, b = 0, c = 0, e = 0, f = 0, g = 0, h[2] = { 0,5 },bb=0,cc=0,o,p,q=0,r=0,u[4][2],v=0,un=0;
            double s = 1000, t = 1000;
            string    n[26][30] = { "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ", "  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  "};
            for (int j = 0; j != 30; j++)
            {
                if (m[25][j] == "")
                    q = 1;
            }
            if (q == 1) 
            {
                cout << "                                                    Score: " << score << endl;
                for (int j = 0; j != 28; j++)cout << "=";
                cout << "Tetris";
                for (int j = 0; j != 28; j++)cout << "=";
                cout << endl;
                for (int i = 25; i != 0; i--) { cout << "|"; for (int j = 0; j != 30; j++) { cout << m[i][j]; }cout << "|" << endl; }
                for (int j = 0; j != 28; j++)cout << "=";
                cout << "Tetris";
                for (int j = 0; j != 28; j++)cout << "=";
                break; 
            }
            for (int i = 0; i != 4; i++)
            {
                if (c < e)e = c;
                else if (c > f)f = c;
                if (g < b)g = b;
                if (r> b)r = b;
                a[i][0] = b;
                a[i][1] = c;
                    if (b == 0 && c == 0)
                    {
                        if (randint(0, 1) == 0)b = (randint(0, 1) == 0 )? 1 : -1;
                        else c = (randint(0, 1) == 0) ? 1 : -1;
                        bb = b;
                        cc = c;
                    }
                    else
                    {
                        if (randint(0, 2) != 0)
                        {
                            if (bb == 0)
                            {
                                bb = (randint(0, 1) == 0) ? 1 : -1;
                                cc = 0;
                            }
                            else
                            {
                                cc = (randint(0, 1) == 0) ? 1 : -1;
                                bb = 0;
                            }
                            b += bb;
                            c += cc;
                            
                        }
                        else
                        {
                            b += bb;
                            c += cc;
                            
                        }
                }
                h[0] = 25-g;
                h[1] = randint(-e, 29 - f);
            }
            for(;;)
            {
            c = clock();
            for (int i = 25; i != -1; i--) { for (int j = 0; j != 30; j++) { n[i][j] = m[i][j]; } }
            for (int i = 0; i != 4; i++)
            {
                n[a[i][0] + h[0]][a[i][1] + h[1]] = "";

            }
            cout << "                                                    Score: "<<score<<endl;
            for (int j = 0; j != 28; j++)cout << "=";
            cout << "Tetris";
            for (int j = 0; j != 28; j++)cout << "=";
            cout << endl;
            for (int i = 25; i != 0; i--) { cout << "|"; for (int j = 0; j != 30; j++) { cout << n[i][j]; }cout << "|"<<endl; }
            for (int j = 0; j != 28; j++)cout << "=";
            cout << "Tetris";
            for (int j = 0; j != 28; j++)cout << "=";
            o = 0;
            for (int i = 0; i != 4; i++)
            {
                if (m[a[i][0] + h[0] - 1][a[i][1] + h[1]] == "")o = 1;

            }
            if (o != 1)
            {
                h[0]--;
            }
            else 
            {
                for (int i = 0; i != 4; i++)
                {
                    m[a[i][0] + h[0]][a[i][1] + h[1]] = "";

                }
                for (int i = 25; i != 0; i--)
                { 
                    o = 0;
                    for (int j = 0; j != 30; j++) 
                    { 
                        if (m[i][j] == "")
                        o++; 
                    }
                    if (o == 30)
                    {
                        for (int j = 0; j != 30; j++)
                        {
                            m[i][j] = "  ";
                        }
                        score += 30;
                    }
                }
                system("cls");
                if(aaa>=50)
                aaa--;
                break;
            }
            if (un != 0)
            {
                cout << endl << "Unable to rotate!" << endl;
                un--;
            }
            p = 0;
            while (clock() - c < aaa)
            {
                if ((KEY_DOWN(VK_LEFT)) && p == 0)
                {
                    o = 0;
                    if (h[1] + e > 0)
                    {
                    for (int i = 0; i != 4; i++)
                    {
                        if (m[a[i][0] + h[0]][a[i][1] + h[1] - 1] == "")o = 1;

                    }
                    if (o == 0)
                        h[1]--;
                    }
                    p = 1;
                }
                else if ((KEY_DOWN(VK_RIGHT)) && p == 0)
                {
                    o = 0;
                    if (h[1] + f < 29)
                    { 
                    for (int i = 0; i != 4; i++)
                    {
                        if (m[a[i][0] + h[0]][a[i][1] + h[1] + 1] == "")o = 1;

                    }
                    if (o == 0)
                        h[1]++;
                    }
                    p = 1;
                }
                else if ((KEY_DOWN(VK_UP)) && p == 0)
                {
                    if (s == 1000 && t == 1000)
                    {
                        s = (e + f) / 2;
                        t = (g + r) / 2;
                    }
                
                    for (int i = 0; i != 4; i++)
                    {
                        u[i][1] = double(a[i][0]) - t + s;
                        u[i][0] = -double(a[i][1]) - s + t;
                        if (u[i][0] + h[0]<1 || u[i][0] + h[0] >25 || u[i][1] + h[1]<0|| u[i][1] + h[1] >29)
                        {
                            v = 1;
                        }
                        else if (m[u[i][0] + h[0]][u[i][1] + h[1]] == "")
                        {
                            v = 1;
                        }
                    }
                    if (v == 1)
                    {
                        cout << endl << "Unable to rotate!" << endl;
                    }
                    else
                    {
                        e = 0;
                        f = 0;
                        g = 0;
                        r = 0;
                        for (int i = 0; i != 4; i++)
                        {
                            a[i][1] = u[i][1];
                            a[i][0] = u[i][0];
                            if (a[i][1] < e)e = a[i][1];
                            else if (a[i][1] > f)f = a[i][1];
                            if (g < a[i][0])g = a[i][0];
                            if (r > a[i][0])r = a[i][0];
                        }
                    }
                    p = 1;

                }
            }
            system("cls");
            }
        }
        cout << endl << endl << "Game Over!";
        cout << endl << "Enter space to start over...";
        for (;;)
        {
            if (KEY_DOWN(' '))
            {
                system("cls");
                break;
            }
            else
            {
            }
        }
    }
}

Dino:

#include<iostream>
#include<stdlib.h> 
#include<time.h>
#include<conio.h>
#include<bitset>
#include<windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int randint(int a,int b)
{
    return a+rand()%(b-a+1);
}
int main()
{
    for(;;)
    {
    double now=0;
    int a,e,f=0;
    clock_t c;
    int d=0;
    char g;
    char b[79]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    for(;;)
    {
        c=clock();
        cout<<now;
        cout<<endl;
        cout<<endl;
        cout<<endl;
        if(d==3)
        {
            cout<<"   d";
        }
        cout<<endl;
        if(d==2)
        {
            cout<<"   d";
        }
        cout<<endl;
        if(d==1)
        {
            cout<<"   d";
        }
        cout<<endl;
        if(KEY_DOWN(' ') && d==0)
        {
            e=1;
        }
        else
        {
        }
        a=randint(0,20);
        for(int i=0;i!=78;i++)
        {
            b[i]=b[i+1];
        }
        if(a==6)
        {
            b[78]='i';
        }
        else
        {
            b[78]=' ';
        }
        for(int i=0;i!=79;i++)
        {
            if (d==0 && i==3)
            {
                if (b[i]=='i')
                {
                    f=1;
                }
                cout<<'d';
            }
            else
            {
            cout<<b[i];
            }
        }
        cout<<endl;
        for(int i=0;i!=79;i++)
        {
            
                cout<<"N";    
            
        }
        if(d==3)
        {
            e=-1;
        }
        if(e==1)
        {
            d++;
        }
        if(e==-1)
        {
            d--;
        }
        if(d==0)
        {
            e=0;
        }
        if(f==1)
        {
            break;
        }
        while(clock()-c<100)
        {
            if(KEY_DOWN(' ') && d==0)
        {
            e=1;
        }
        else
        {
        }
        }
        now+=0.1;
        system("cls");
    }
    cout<<endl<<endl<<"Game Over!";
    cout<<endl<<"Enter space to start over...";
    for(;;)
    {
        if(KEY_DOWN(' '))
        {
            system("cls");
            break;
        }
        else
        {
        }
    }
    }
}

 

posted @ 2022-11-13 10:02  GodForever  阅读(39)  评论(0)    收藏  举报